From cdc3e8a32c05bc5a90589350510c7616ba334e8f Mon Sep 17 00:00:00 2001 From: knikolaevskii <90223210+knikolaevskii@users.noreply.github.com> Date: Wed, 19 Jun 2024 22:37:48 +0200 Subject: [PATCH 01/27] The instrumented code for is_dimension() function and a coverage test for it --- src/prompt_toolkit/layout/dimension.py | 25 +++++++++++++++++++------ tests/test_is_dimension_coverage.py | 22 ++++++++++++++++++++++ 2 files changed, 41 insertions(+), 6 deletions(-) create mode 100644 tests/test_is_dimension_coverage.py diff --git a/src/prompt_toolkit/layout/dimension.py b/src/prompt_toolkit/layout/dimension.py index 2e6f5dd4e..497e6b5d3 100644 --- a/src/prompt_toolkit/layout/dimension.py +++ b/src/prompt_toolkit/layout/dimension.py @@ -199,17 +199,30 @@ def to_dimension(value: AnyDimension) -> Dimension: raise ValueError("Not an integer or Dimension object.") -def is_dimension(value: object) -> TypeGuard[AnyDimension]: - """ - Test whether the given value could be a valid dimension. - (For usage in an assertion. It's not guaranteed in case of a callable.) - """ +# Initialize coverage tracking global variable +branch_coverage = { + "is_dimension_1": False, # Branch for checking if value is None + "is_dimension_2": False, # Branch for checking if value is callable + "is_dimension_3": False, # Branch for checking if value is int or Dimension + "is_dimension_4": False # Branch for default case +} + +def is_dimension(value: object) -> bool: if value is None: + branch_coverage["is_dimension_1"] = True + print("Branch 1 was hit") return True if callable(value): - return True # Assume it's a callable that doesn't take arguments. + branch_coverage["is_dimension_2"] = True + print("Branch 2 was hit") + return True if isinstance(value, (int, Dimension)): + branch_coverage["is_dimension_3"] = True + print("Branch 3 was hit") return True + + branch_coverage["is_dimension_4"] = True + print("Branch 4 was hit") return False diff --git a/tests/test_is_dimension_coverage.py b/tests/test_is_dimension_coverage.py new file mode 100644 index 000000000..83fc9621e --- /dev/null +++ b/tests/test_is_dimension_coverage.py @@ -0,0 +1,22 @@ +from prompt_toolkit.layout.dimension import branch_coverage,is_dimension + + +def print_coverage(): + print("\nCoverage report:") + hit_branches = sum(branch_coverage.values()) + total_branches = len(branch_coverage) + coverage_percentage = (hit_branches / total_branches) * 100 + for branch, hit in branch_coverage.items(): + print(f"{branch} was {'hit' if hit else 'not hit'}") + print(f"Coverage: {hit_branches}/{total_branches} branches hit ({coverage_percentage:.2f}%)\n") + + +# Tests +test_cases = [] + +for value, description in test_cases: + print(f"\nTesting case: {description}") + result = is_dimension(value) + print(f"Result: {result}") + +print_coverage() \ No newline at end of file From ab9afba5a15cd60f887627a91f0416683917a441 Mon Sep 17 00:00:00 2001 From: knikolaevskii <90223210+knikolaevskii@users.noreply.github.com> Date: Wed, 19 Jun 2024 22:40:36 +0200 Subject: [PATCH 02/27] Enhanced test for is_dimension() --- tests/test_is_dimension_coverage.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/tests/test_is_dimension_coverage.py b/tests/test_is_dimension_coverage.py index 83fc9621e..4892262b9 100644 --- a/tests/test_is_dimension_coverage.py +++ b/tests/test_is_dimension_coverage.py @@ -12,7 +12,12 @@ def print_coverage(): # Tests -test_cases = [] +test_cases = [ + (None, "Test 1: None value"), + (52, "Test 2: Integer value"), + (print_coverage, "Test 3: Regular function"), + ("not a dimension", "Test 4: String value (invalid dimension)") +] for value, description in test_cases: print(f"\nTesting case: {description}") From bb1d1f27e98855dc4737c18d3147900f45054e1f Mon Sep 17 00:00:00 2001 From: knikolaevskii <90223210+knikolaevskii@users.noreply.github.com> Date: Wed, 19 Jun 2024 23:07:20 +0200 Subject: [PATCH 03/27] The instrumented code for _commonprefix() function and a coverage test for it --- src/prompt_toolkit/completion/base.py | 15 ++++++++++++++- tests/test_commonprefix_coverage.py | 24 ++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 tests/test_commonprefix_coverage.py diff --git a/src/prompt_toolkit/completion/base.py b/src/prompt_toolkit/completion/base.py index 3846ef756..2b59751c8 100644 --- a/src/prompt_toolkit/completion/base.py +++ b/src/prompt_toolkit/completion/base.py @@ -422,17 +422,30 @@ def get_suffix(completion: Completion) -> str: return _commonprefix([get_suffix(c) for c in completions2]) +# Data structures that hold coverage information about the conditional branches +branch_coverage = { + "_commonprefix_1": False, # Branch for checking if not strings + "_commonprefix_2": False, # Branch for comparing characters + "_commonprefix_3": False, # Branch for when the characters do not match +} + def _commonprefix(strings: Iterable[str]) -> str: - # Similar to os.path.commonprefix if not strings: + branch_coverage["_commonprefix_1"] = True + print("Branch 1 was hit") return "" else: + branch_coverage["_commonprefix_2"] = True + print("Branch 2 was hit") s1 = min(strings) s2 = max(strings) for i, c in enumerate(s1): if c != s2[i]: + branch_coverage["_commonprefix_3"] = True + print("Branch 3 was hit") + print("here") return s1[:i] return s1 diff --git a/tests/test_commonprefix_coverage.py b/tests/test_commonprefix_coverage.py new file mode 100644 index 000000000..09ae1effe --- /dev/null +++ b/tests/test_commonprefix_coverage.py @@ -0,0 +1,24 @@ +from __future__ import annotations +from typing import Iterable, List, Tuple +from prompt_toolkit.completion.base import branch_coverage,_commonprefix + +def print_coverage(): + print("\nCoverage report:") + hit_branches = sum(branch_coverage.values()) + total_branches = len(branch_coverage) + coverage_percentage = (hit_branches / total_branches) * 100 + for branch, hit in branch_coverage.items(): + print(f"{branch} was {'hit' if hit else 'not hit'}") + print(f"Coverage: {hit_branches}/{total_branches} branches hit ({coverage_percentage:.2f}%)\n") + + +# Tests +test_cases: List[Tuple[List[str], str]] = [] + +for strings, description in test_cases: + print(f"\nTesting case: {description} - Input: {strings}") + result = _commonprefix(strings) + print("Common prefix:", result) + +print_coverage() + From f7743cfd58a5cb39cf39dd3a08f573fa051cd0bd Mon Sep 17 00:00:00 2001 From: knikolaevskii <90223210+knikolaevskii@users.noreply.github.com> Date: Wed, 19 Jun 2024 23:08:56 +0200 Subject: [PATCH 04/27] Enhanced test for _commonprefix() --- tests/test_commonprefix_coverage.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/tests/test_commonprefix_coverage.py b/tests/test_commonprefix_coverage.py index 09ae1effe..72a595f92 100644 --- a/tests/test_commonprefix_coverage.py +++ b/tests/test_commonprefix_coverage.py @@ -13,7 +13,11 @@ def print_coverage(): # Tests -test_cases: List[Tuple[List[str], str]] = [] +test_cases: List[Tuple[List[str], str]] = [ + (["car", "car"], "Test 1: Same words"), + ([], "Test 2: Empty list"), + (["car", "dog"], "Test 2: Different words") +] for strings, description in test_cases: print(f"\nTesting case: {description} - Input: {strings}") From 15f93db20ddc973af132cbc891fb900d94ff0d73 Mon Sep 17 00:00:00 2001 From: rovshan Date: Fri, 21 Jun 2024 01:10:49 +0200 Subject: [PATCH 05/27] to_dimension unenhanced --- src/prompt_toolkit/layout/dimension.py | 19 +++++++++++++++++++ tests/test_to_dimension.py | 25 +++++++++++++++++++++++++ 2 files changed, 44 insertions(+) create mode 100644 tests/test_to_dimension.py diff --git a/src/prompt_toolkit/layout/dimension.py b/src/prompt_toolkit/layout/dimension.py index 2e6f5dd4e..ef2705775 100644 --- a/src/prompt_toolkit/layout/dimension.py +++ b/src/prompt_toolkit/layout/dimension.py @@ -182,20 +182,39 @@ def max_layout_dimensions(dimensions: list[Dimension]) -> Dimension: Callable[[], Any], ] +from typing import Any, Callable, Union + +to_dimension_coverage = { + "to_dimension_1": False, + "to_dimension_2": False, + "to_dimension_3": False, + "to_dimension_4": False, + "to_dimension_5": False +} def to_dimension(value: AnyDimension) -> Dimension: """ Turn the given object into a `Dimension` object. """ if value is None: + to_dimension_coverage["to_dimension_1"] = True + print("Branch 1 was hit") return Dimension() if isinstance(value, int): + to_dimension_coverage["to_dimension_2"] = True + print("Branch 2 was hit") return Dimension.exact(value) if isinstance(value, Dimension): + to_dimension_coverage["to_dimension_3"] = True + print("Branch 3 was hit") return value if callable(value): + to_dimension_coverage["to_dimension_4"] = True + print("Branch 4 was hit") return to_dimension(value()) + to_dimension_coverage["to_dimension_5"] = True + print("Branch 5 was hit") raise ValueError("Not an integer or Dimension object.") diff --git a/tests/test_to_dimension.py b/tests/test_to_dimension.py new file mode 100644 index 000000000..ea6d9404c --- /dev/null +++ b/tests/test_to_dimension.py @@ -0,0 +1,25 @@ +from prompt_toolkit.layout.dimension import Dimension, to_dimension, to_dimension_coverage + +def print_coverage(): + print("\nCoverage report:") + hit_branches = sum(to_dimension_coverage.values()) + total_branches = len(to_dimension_coverage) + coverage_percentage = (hit_branches / total_branches) * 100 + for branch, hit in to_dimension_coverage.items(): + print(f"{branch} was {'hit' if hit else 'not hit'}") + print(f"Coverage: {hit_branches}/{total_branches} branches hit ({coverage_percentage:.2f}%)\n") + + +test_cases = [ + +] + +for value, description in test_cases: + print(f"\nTesting case: {description}") + try: + result = to_dimension(value) + print(f"Result: {result}") + except ValueError as e: + print(f"Exception: {e}") + +print_coverage() From f8e6d56b56b76ec6d3a9579d7b945db6f17c7d6d Mon Sep 17 00:00:00 2001 From: rovshan Date: Fri, 21 Jun 2024 01:11:26 +0200 Subject: [PATCH 06/27] to_dimension enhanced --- tests/test_to_dimension.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/tests/test_to_dimension.py b/tests/test_to_dimension.py index ea6d9404c..bd208768a 100644 --- a/tests/test_to_dimension.py +++ b/tests/test_to_dimension.py @@ -1,4 +1,6 @@ from prompt_toolkit.layout.dimension import Dimension, to_dimension, to_dimension_coverage +# /home/r0vshan/University/SEP/SEP_python-prompt-toolkit/src/prompt_toolkit/layout/dimension.py This is the correct path +# /home/r0vshan/.local/lib/python3.10/site-packages/prompt_toolkit/layout/dimension.py This is the wrong path def print_coverage(): print("\nCoverage report:") @@ -9,11 +11,16 @@ def print_coverage(): print(f"{branch} was {'hit' if hit else 'not hit'}") print(f"Coverage: {hit_branches}/{total_branches} branches hit ({coverage_percentage:.2f}%)\n") - +# Define test cases test_cases = [ - + (None, "None value (should hit Branch 1)"), + (69, "Integer value (should hit Branch 2)"), + (Dimension(), "Dimension instance (should hit Branch 3)"), + (lambda: 42, "Callable returning an integer (should hit Branch 4)"), + ("Unsupported type", "Unsupported type (should hit Branch 5)") ] +# Run tests for value, description in test_cases: print(f"\nTesting case: {description}") try: From d5af7fdf04a551fbc7eb157b764bb85da57ab5ac Mon Sep 17 00:00:00 2001 From: rovshan Date: Fri, 21 Jun 2024 01:13:58 +0200 Subject: [PATCH 07/27] to_dimension enhanced --- tests/test_to_dimension.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/tests/test_to_dimension.py b/tests/test_to_dimension.py index bd208768a..28fe7d8fa 100644 --- a/tests/test_to_dimension.py +++ b/tests/test_to_dimension.py @@ -1,6 +1,4 @@ from prompt_toolkit.layout.dimension import Dimension, to_dimension, to_dimension_coverage -# /home/r0vshan/University/SEP/SEP_python-prompt-toolkit/src/prompt_toolkit/layout/dimension.py This is the correct path -# /home/r0vshan/.local/lib/python3.10/site-packages/prompt_toolkit/layout/dimension.py This is the wrong path def print_coverage(): print("\nCoverage report:") @@ -11,7 +9,7 @@ def print_coverage(): print(f"{branch} was {'hit' if hit else 'not hit'}") print(f"Coverage: {hit_branches}/{total_branches} branches hit ({coverage_percentage:.2f}%)\n") -# Define test cases + test_cases = [ (None, "None value (should hit Branch 1)"), (69, "Integer value (should hit Branch 2)"), @@ -20,7 +18,6 @@ def print_coverage(): ("Unsupported type", "Unsupported type (should hit Branch 5)") ] -# Run tests for value, description in test_cases: print(f"\nTesting case: {description}") try: From 67343f6e2386ecb1c9977040541fac4a65f0c721 Mon Sep 17 00:00:00 2001 From: rovshan Date: Fri, 21 Jun 2024 01:14:58 +0200 Subject: [PATCH 08/27] merge_processors unenhanced --- src/prompt_toolkit/layout/processors.py | 9 +++++++++ tests/test_merge_processors.py | 26 +++++++++++++++++++++++++ 2 files changed, 35 insertions(+) create mode 100644 tests/test_merge_processors.py diff --git a/src/prompt_toolkit/layout/processors.py b/src/prompt_toolkit/layout/processors.py index b10ecf718..280fc1f53 100644 --- a/src/prompt_toolkit/layout/processors.py +++ b/src/prompt_toolkit/layout/processors.py @@ -943,16 +943,25 @@ def apply_transformation(self, ti: TransformationInput) -> Transformation: return processor.apply_transformation(ti) +merge_processors_coverage = { + "merge_processors_1": False, # Branch for empty list of processors + "merge_processors_2": False, # Branch for a single processor + "merge_processors_3": False # Branch for multiple processors +} + def merge_processors(processors: list[Processor]) -> Processor: """ Merge multiple `Processor` objects into one. """ if len(processors) == 0: + merge_processors_coverage["merge_processors_1"] = True return DummyProcessor() if len(processors) == 1: + merge_processors_coverage["merge_processors_2"] = True return processors[0] # Nothing to merge. + merge_processors_coverage["merge_processors_3"] = True return _MergedProcessor(processors) diff --git a/tests/test_merge_processors.py b/tests/test_merge_processors.py new file mode 100644 index 000000000..23809c583 --- /dev/null +++ b/tests/test_merge_processors.py @@ -0,0 +1,26 @@ +from prompt_toolkit.layout.processors import merge_processors, merge_processors_coverage +from prompt_toolkit.layout.processors import Processor, DummyProcessor, _MergedProcessor + +def print_coverage(): + print("\nCoverage report:") + hit_branches = sum(merge_processors_coverage.values()) + total_branches = len(merge_processors_coverage) + coverage_percentage = (hit_branches / total_branches) * 100 + for branch, hit in merge_processors_coverage.items(): + print(f"{branch} was {'hit' if hit else 'not hit'}") + print(f"Coverage: {hit_branches}/{total_branches} branches hit ({coverage_percentage:.2f}%)\n") + +class MockProcessor(Processor): + def apply_transformation(self, *args, **kwargs): + pass + +test_cases = [ + +] + +for processors, description in test_cases: + print(f"\nTesting case: {description}") + result = merge_processors(processors) + print(f"Result: {result}") + +print_coverage() From 6fadf7d66bdd56c6c7e11e1ceea859f642472050 Mon Sep 17 00:00:00 2001 From: rovshan Date: Fri, 21 Jun 2024 01:15:23 +0200 Subject: [PATCH 09/27] merge_processors enhanced --- tests/test_merge_processors.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/test_merge_processors.py b/tests/test_merge_processors.py index 23809c583..31e6f802b 100644 --- a/tests/test_merge_processors.py +++ b/tests/test_merge_processors.py @@ -15,7 +15,9 @@ def apply_transformation(self, *args, **kwargs): pass test_cases = [ - + ([], "Empty list of processors (should hit Branch 1)"), + ([MockProcessor()], "Single processor (should hit Branch 2)"), + ([MockProcessor(), MockProcessor()], "Multiple processors (should hit Branch 3)") ] for processors, description in test_cases: From e9e7dc6e8c10c1b3851a036daa1db8a119e6cd76 Mon Sep 17 00:00:00 2001 From: Daniil Malygin Date: Sat, 22 Jun 2024 16:19:33 +0200 Subject: [PATCH 10/27] Edited document.py --- src/prompt_toolkit/document.py | 81 ++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) diff --git a/src/prompt_toolkit/document.py b/src/prompt_toolkit/document.py index df5293e62..02cd96914 100644 --- a/src/prompt_toolkit/document.py +++ b/src/prompt_toolkit/document.py @@ -14,6 +14,25 @@ from .filters import vi_mode from .selection import PasteMode, SelectionState, SelectionType + + + + + + +branch_coverage_next = { + "find_next_1": False, + "find_next_2": False, +} + +branch_coverage_prev = { + "find_prev_1": False, + "find_prev_2": False +} + + + + __all__ = [ "Document", ] @@ -652,7 +671,41 @@ def find_previous_word_ending( except StopIteration: pass return None + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + def find_next_matching_line( self, match_func: Callable[[str], bool], count: int = 1 ) -> int | None: @@ -664,10 +717,12 @@ def find_next_matching_line( for index, line in enumerate(self.lines[self.cursor_position_row + 1 :]): if match_func(line): + branch_coverage_next["find_next_1"] = True result = 1 + index count -= 1 if count == 0: + branch_coverage_next["find_next_2"] = True break return result @@ -683,13 +738,38 @@ def find_previous_matching_line( for index, line in enumerate(self.lines[: self.cursor_position_row][::-1]): if match_func(line): + branch_coverage_prev["find_prev_1"] = True result = -1 - index count -= 1 if count == 0: + branch_coverage_prev["find_prev_2"] = True break return result + + + + + + + + + + + + + + + + + + + + + + + def get_cursor_left_position(self, count: int = 1) -> int: """ @@ -1180,3 +1260,4 @@ def insert_before(self, text: str) -> Document: cursor_position=self.cursor_position + len(text), selection=selection_state, ) + From 06947d08e61e2f3828e800e7c1efcb40ddf6fdd4 Mon Sep 17 00:00:00 2001 From: Daniil Malygin Date: Sat, 22 Jun 2024 16:20:09 +0200 Subject: [PATCH 11/27] Test file for "find_next_matching_line" --- tests/test_find_next_matching_line.py | 44 +++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 tests/test_find_next_matching_line.py diff --git a/tests/test_find_next_matching_line.py b/tests/test_find_next_matching_line.py new file mode 100644 index 000000000..18908ad14 --- /dev/null +++ b/tests/test_find_next_matching_line.py @@ -0,0 +1,44 @@ +from prompt_toolkit.document import Document, branch_coverage_next + +def print_coverage(): + print("\nCoverage report:") + hit_branches = sum(branch_coverage_next.values()) + total_branches = len(branch_coverage_next) + coverage_percentage = (hit_branches / total_branches) * 100 + for branch, hit in branch_coverage_next.items(): + print(f"{branch} was {'hit' if hit else 'not hit'}") + print(f"Coverage: {hit_branches}/{total_branches} branches hit ({coverage_percentage:.2f}%)\n") + +test_cases = [ + { + "description": "Find the second next empty line", + "text": "line 1\n\nline 3\n\nline 5", + "cursor_position": 0, + "match_func": lambda line: line.strip() == "", + "count": 2, + "expected_result": 3 + }, + { + "description": "No match found", + "text": "line 1\nline 2\nline 3\nline 4", + "cursor_position": 0, + "match_func": lambda line: line.strip() == "", + "count": 1, + "expected_result": None + }, + { + "description": "Match after cursor position", + "text": "line 1\nline 2\n\nline 4", + "cursor_position": 7, + "match_func": lambda line: line.strip() == "", + "count": 1, + "expected_result": 1 + } +] + +for case in test_cases: + document = Document(text=case["text"], cursor_position=case["cursor_position"]) + result = document.find_next_matching_line(case["match_func"], case["count"]) + assert result == case["expected_result"], f"Test failed for: {case['description']}" + +print_coverage() From 57b6f3e60c1aa2a82b743625a0dfa1988c297c6d Mon Sep 17 00:00:00 2001 From: Daniil Malygin Date: Sat, 22 Jun 2024 16:20:27 +0200 Subject: [PATCH 12/27] Test file for "find_prev_matching_line" --- tests/test_find_prev_matching_line.py | 44 +++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 tests/test_find_prev_matching_line.py diff --git a/tests/test_find_prev_matching_line.py b/tests/test_find_prev_matching_line.py new file mode 100644 index 000000000..0e4ecda1a --- /dev/null +++ b/tests/test_find_prev_matching_line.py @@ -0,0 +1,44 @@ +from prompt_toolkit.document import Document, branch_coverage_prev + +def print_coverage(): + print("\nCoverage report:") + hit_branches = sum(branch_coverage_prev.values()) + total_branches = len(branch_coverage_prev) + coverage_percentage = (hit_branches / total_branches) * 100 + for branch, hit in branch_coverage_prev.items(): + print(f"{branch} was {'hit' if hit else 'not hit'}") + print(f"Coverage: {hit_branches}/{total_branches} branches hit ({coverage_percentage:.2f}%)\n") + +test_cases = [ + { + "description": "Find the second previous empty line", + "text": "line 1\n\nline 3\n\nline 5", + "cursor_position": 18, + "match_func": lambda line: line.strip() == "", + "count": 2, + "expected_result": -3 + }, + { + "description": "No match found", + "text": "line 1\nline 2\nline 3\nline 4", + "cursor_position": 10, + "match_func": lambda line: line.strip() == "", + "count": 1, + "expected_result": None + }, + { + "description": "Match before cursor position", + "text": "line 1\n\nline 3\nline 4", + "cursor_position": 18, + "match_func": lambda line: line.strip() == "", + "count": 1, + "expected_result": -2 + } +] + +for case in test_cases: + document = Document(text=case["text"], cursor_position=case["cursor_position"]) + result = document.find_previous_matching_line(case["match_func"], case["count"]) + assert result == case["expected_result"], f"Test failed for: {case['description']}" + +print_coverage() From d2a478d6fe21ddc380ed6ec1125e9894d4961104 Mon Sep 17 00:00:00 2001 From: Daniil Malygin Date: Sun, 23 Jun 2024 21:44:01 +0200 Subject: [PATCH 13/27] Added extra print statements --- tests/test_find_next_matching_line.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/tests/test_find_next_matching_line.py b/tests/test_find_next_matching_line.py index 18908ad14..c148a8ccb 100644 --- a/tests/test_find_next_matching_line.py +++ b/tests/test_find_next_matching_line.py @@ -40,5 +40,14 @@ def print_coverage(): document = Document(text=case["text"], cursor_position=case["cursor_position"]) result = document.find_next_matching_line(case["match_func"], case["count"]) assert result == case["expected_result"], f"Test failed for: {case['description']}" - + + print("\n") + print(f"Test case: {case['description']}") + print(f"Expected result: {case['expected_result']}") + print(f"Actual result: {result}") + print("Branches hit:") + for branch, hit in branch_coverage_next.items(): + if hit: + print(f" {branch}") + print_coverage() From 3e6b5010ba924efbaea1ae690b474e50697f0ff2 Mon Sep 17 00:00:00 2001 From: Daniil Malygin Date: Sun, 23 Jun 2024 21:44:05 +0200 Subject: [PATCH 14/27] Added extra print statements --- tests/test_find_prev_matching_line.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/tests/test_find_prev_matching_line.py b/tests/test_find_prev_matching_line.py index 0e4ecda1a..ba2f875b6 100644 --- a/tests/test_find_prev_matching_line.py +++ b/tests/test_find_prev_matching_line.py @@ -41,4 +41,13 @@ def print_coverage(): result = document.find_previous_matching_line(case["match_func"], case["count"]) assert result == case["expected_result"], f"Test failed for: {case['description']}" -print_coverage() + print("\n") + print(f"Test case: {case['description']}") + print(f"Expected result: {case['expected_result']}") + print(f"Actual result: {result}") + print("Branches hit:") + for branch, hit in branch_coverage_prev.items(): + if hit: + print(f" {branch}") + +print_coverage() \ No newline at end of file From 6c4e8c8bc79fe0a32421b7513199961c9b532f1e Mon Sep 17 00:00:00 2001 From: Daniil Malygin Date: Sun, 23 Jun 2024 21:53:13 +0200 Subject: [PATCH 15/27] Last minor changes --- src/prompt_toolkit/document.py | 66 ---------------------------------- 1 file changed, 66 deletions(-) diff --git a/src/prompt_toolkit/document.py b/src/prompt_toolkit/document.py index 02cd96914..80df3511f 100644 --- a/src/prompt_toolkit/document.py +++ b/src/prompt_toolkit/document.py @@ -14,12 +14,6 @@ from .filters import vi_mode from .selection import PasteMode, SelectionState, SelectionType - - - - - - branch_coverage_next = { "find_next_1": False, "find_next_2": False, @@ -30,9 +24,6 @@ "find_prev_2": False } - - - __all__ = [ "Document", ] @@ -672,40 +663,6 @@ def find_previous_word_ending( pass return None - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - def find_next_matching_line( self, match_func: Callable[[str], bool], count: int = 1 ) -> int | None: @@ -748,29 +705,6 @@ def find_previous_matching_line( return result - - - - - - - - - - - - - - - - - - - - - - - def get_cursor_left_position(self, count: int = 1) -> int: """ Relative position for cursor left. From e7dc22d2487b4dc8bf216f430e24c2c1837d5eea Mon Sep 17 00:00:00 2001 From: Daniil Malygin Date: Sun, 23 Jun 2024 22:17:42 +0200 Subject: [PATCH 16/27] Reversed stuff so I can do it again in correct order and paste a good link in README --- src/prompt_toolkit/document.py | 15 -------- tests/test_find_next_matching_line.py | 53 --------------------------- tests/test_find_prev_matching_line.py | 53 --------------------------- 3 files changed, 121 deletions(-) diff --git a/src/prompt_toolkit/document.py b/src/prompt_toolkit/document.py index 80df3511f..f922fa228 100644 --- a/src/prompt_toolkit/document.py +++ b/src/prompt_toolkit/document.py @@ -14,21 +14,10 @@ from .filters import vi_mode from .selection import PasteMode, SelectionState, SelectionType -branch_coverage_next = { - "find_next_1": False, - "find_next_2": False, -} - -branch_coverage_prev = { - "find_prev_1": False, - "find_prev_2": False -} - __all__ = [ "Document", ] - # Regex for finding "words" in documents. (We consider a group of alnum # characters a word, but also a group of special characters a word, as long as # it doesn't contain a space.) @@ -674,12 +663,10 @@ def find_next_matching_line( for index, line in enumerate(self.lines[self.cursor_position_row + 1 :]): if match_func(line): - branch_coverage_next["find_next_1"] = True result = 1 + index count -= 1 if count == 0: - branch_coverage_next["find_next_2"] = True break return result @@ -695,12 +682,10 @@ def find_previous_matching_line( for index, line in enumerate(self.lines[: self.cursor_position_row][::-1]): if match_func(line): - branch_coverage_prev["find_prev_1"] = True result = -1 - index count -= 1 if count == 0: - branch_coverage_prev["find_prev_2"] = True break return result diff --git a/tests/test_find_next_matching_line.py b/tests/test_find_next_matching_line.py index c148a8ccb..e69de29bb 100644 --- a/tests/test_find_next_matching_line.py +++ b/tests/test_find_next_matching_line.py @@ -1,53 +0,0 @@ -from prompt_toolkit.document import Document, branch_coverage_next - -def print_coverage(): - print("\nCoverage report:") - hit_branches = sum(branch_coverage_next.values()) - total_branches = len(branch_coverage_next) - coverage_percentage = (hit_branches / total_branches) * 100 - for branch, hit in branch_coverage_next.items(): - print(f"{branch} was {'hit' if hit else 'not hit'}") - print(f"Coverage: {hit_branches}/{total_branches} branches hit ({coverage_percentage:.2f}%)\n") - -test_cases = [ - { - "description": "Find the second next empty line", - "text": "line 1\n\nline 3\n\nline 5", - "cursor_position": 0, - "match_func": lambda line: line.strip() == "", - "count": 2, - "expected_result": 3 - }, - { - "description": "No match found", - "text": "line 1\nline 2\nline 3\nline 4", - "cursor_position": 0, - "match_func": lambda line: line.strip() == "", - "count": 1, - "expected_result": None - }, - { - "description": "Match after cursor position", - "text": "line 1\nline 2\n\nline 4", - "cursor_position": 7, - "match_func": lambda line: line.strip() == "", - "count": 1, - "expected_result": 1 - } -] - -for case in test_cases: - document = Document(text=case["text"], cursor_position=case["cursor_position"]) - result = document.find_next_matching_line(case["match_func"], case["count"]) - assert result == case["expected_result"], f"Test failed for: {case['description']}" - - print("\n") - print(f"Test case: {case['description']}") - print(f"Expected result: {case['expected_result']}") - print(f"Actual result: {result}") - print("Branches hit:") - for branch, hit in branch_coverage_next.items(): - if hit: - print(f" {branch}") - -print_coverage() diff --git a/tests/test_find_prev_matching_line.py b/tests/test_find_prev_matching_line.py index ba2f875b6..e69de29bb 100644 --- a/tests/test_find_prev_matching_line.py +++ b/tests/test_find_prev_matching_line.py @@ -1,53 +0,0 @@ -from prompt_toolkit.document import Document, branch_coverage_prev - -def print_coverage(): - print("\nCoverage report:") - hit_branches = sum(branch_coverage_prev.values()) - total_branches = len(branch_coverage_prev) - coverage_percentage = (hit_branches / total_branches) * 100 - for branch, hit in branch_coverage_prev.items(): - print(f"{branch} was {'hit' if hit else 'not hit'}") - print(f"Coverage: {hit_branches}/{total_branches} branches hit ({coverage_percentage:.2f}%)\n") - -test_cases = [ - { - "description": "Find the second previous empty line", - "text": "line 1\n\nline 3\n\nline 5", - "cursor_position": 18, - "match_func": lambda line: line.strip() == "", - "count": 2, - "expected_result": -3 - }, - { - "description": "No match found", - "text": "line 1\nline 2\nline 3\nline 4", - "cursor_position": 10, - "match_func": lambda line: line.strip() == "", - "count": 1, - "expected_result": None - }, - { - "description": "Match before cursor position", - "text": "line 1\n\nline 3\nline 4", - "cursor_position": 18, - "match_func": lambda line: line.strip() == "", - "count": 1, - "expected_result": -2 - } -] - -for case in test_cases: - document = Document(text=case["text"], cursor_position=case["cursor_position"]) - result = document.find_previous_matching_line(case["match_func"], case["count"]) - assert result == case["expected_result"], f"Test failed for: {case['description']}" - - print("\n") - print(f"Test case: {case['description']}") - print(f"Expected result: {case['expected_result']}") - print(f"Actual result: {result}") - print("Branches hit:") - for branch, hit in branch_coverage_prev.items(): - if hit: - print(f" {branch}") - -print_coverage() \ No newline at end of file From ae26bfdf11366217cac1a3ebb0054402056ab240 Mon Sep 17 00:00:00 2001 From: Daniil Malygin Date: Sun, 23 Jun 2024 22:19:38 +0200 Subject: [PATCH 17/27] find_next_matching_line --- src/prompt_toolkit/document.py | 7 +++++++ tests/test_find_next_matching_line.py | 28 +++++++++++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/src/prompt_toolkit/document.py b/src/prompt_toolkit/document.py index f922fa228..bf2aaf15d 100644 --- a/src/prompt_toolkit/document.py +++ b/src/prompt_toolkit/document.py @@ -14,6 +14,11 @@ from .filters import vi_mode from .selection import PasteMode, SelectionState, SelectionType +branch_coverage_next = { + "find_next_1": False, + "find_next_2": False, +} + __all__ = [ "Document", ] @@ -663,10 +668,12 @@ def find_next_matching_line( for index, line in enumerate(self.lines[self.cursor_position_row + 1 :]): if match_func(line): + branch_coverage_next["find_next_1"] = True result = 1 + index count -= 1 if count == 0: + branch_coverage_next["find_next_2"] = True break return result diff --git a/tests/test_find_next_matching_line.py b/tests/test_find_next_matching_line.py index e69de29bb..2e2989efb 100644 --- a/tests/test_find_next_matching_line.py +++ b/tests/test_find_next_matching_line.py @@ -0,0 +1,28 @@ +from prompt_toolkit.document import Document, branch_coverage_next + +def print_coverage(): + print("\nCoverage report:") + hit_branches = sum(branch_coverage_next.values()) + total_branches = len(branch_coverage_next) + coverage_percentage = (hit_branches / total_branches) * 100 + for branch, hit in branch_coverage_next.items(): + print(f"{branch} was {'hit' if hit else 'not hit'}") + print(f"Coverage: {hit_branches}/{total_branches} branches hit ({coverage_percentage:.2f}%)\n") + +test_cases = [] + +for case in test_cases: + document = Document(text=case["text"], cursor_position=case["cursor_position"]) + result = document.find_next_matching_line(case["match_func"], case["count"]) + assert result == case["expected_result"], f"Test failed for: {case['description']}" + + print("\n") + print(f"Test case: {case['description']}") + print(f"Expected result: {case['expected_result']}") + print(f"Actual result: {result}") + print("Branches hit:") + for branch, hit in branch_coverage_next.items(): + if hit: + print(f" {branch}") + +print_coverage() \ No newline at end of file From dd1c578b57f619cd1bc2320cb07ded85737c225a Mon Sep 17 00:00:00 2001 From: Daniil Malygin Date: Sun, 23 Jun 2024 22:22:36 +0200 Subject: [PATCH 18/27] find_previous_matching_line --- src/prompt_toolkit/document.py | 7 +++++++ tests/test_find_prev_matching_line.py | 28 +++++++++++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/src/prompt_toolkit/document.py b/src/prompt_toolkit/document.py index bf2aaf15d..0100c6913 100644 --- a/src/prompt_toolkit/document.py +++ b/src/prompt_toolkit/document.py @@ -19,6 +19,11 @@ "find_next_2": False, } +branch_coverage_prev = { + "find_prev_1": False, + "find_prev_2": False +} + __all__ = [ "Document", ] @@ -689,10 +694,12 @@ def find_previous_matching_line( for index, line in enumerate(self.lines[: self.cursor_position_row][::-1]): if match_func(line): + branch_coverage_prev["find_prev_1"] = True result = -1 - index count -= 1 if count == 0: + branch_coverage_prev["find_prev_2"] = True break return result diff --git a/tests/test_find_prev_matching_line.py b/tests/test_find_prev_matching_line.py index e69de29bb..ce8774e4a 100644 --- a/tests/test_find_prev_matching_line.py +++ b/tests/test_find_prev_matching_line.py @@ -0,0 +1,28 @@ +from prompt_toolkit.document import Document, branch_coverage_prev + +def print_coverage(): + print("\nCoverage report:") + hit_branches = sum(branch_coverage_prev.values()) + total_branches = len(branch_coverage_prev) + coverage_percentage = (hit_branches / total_branches) * 100 + for branch, hit in branch_coverage_prev.items(): + print(f"{branch} was {'hit' if hit else 'not hit'}") + print(f"Coverage: {hit_branches}/{total_branches} branches hit ({coverage_percentage:.2f}%)\n") + +test_cases = [] + +for case in test_cases: + document = Document(text=case["text"], cursor_position=case["cursor_position"]) + result = document.find_previous_matching_line(case["match_func"], case["count"]) + assert result == case["expected_result"], f"Test failed for: {case['description']}" + + print("\n") + print(f"Test case: {case['description']}") + print(f"Expected result: {case['expected_result']}") + print(f"Actual result: {result}") + print("Branches hit:") + for branch, hit in branch_coverage_prev.items(): + if hit: + print(f" {branch}") + +print_coverage() \ No newline at end of file From b71160dc6db29693ffc83fde9834ab9a4c89d8f5 Mon Sep 17 00:00:00 2001 From: Daniil Malygin Date: Sun, 23 Jun 2024 22:24:36 +0200 Subject: [PATCH 19/27] test cases for find_next_matching_line --- tests/test_find_next_matching_line.py | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/tests/test_find_next_matching_line.py b/tests/test_find_next_matching_line.py index 2e2989efb..7795d2b26 100644 --- a/tests/test_find_next_matching_line.py +++ b/tests/test_find_next_matching_line.py @@ -9,7 +9,32 @@ def print_coverage(): print(f"{branch} was {'hit' if hit else 'not hit'}") print(f"Coverage: {hit_branches}/{total_branches} branches hit ({coverage_percentage:.2f}%)\n") -test_cases = [] +test_cases = [ + { + "description": "Find the second next empty line", + "text": "line 1\n\nline 3\n\nline 5", + "cursor_position": 0, + "match_func": lambda line: line.strip() == "", + "count": 2, + "expected_result": 3 + }, + { + "description": "No match found", + "text": "line 1\nline 2\nline 3\nline 4", + "cursor_position": 0, + "match_func": lambda line: line.strip() == "", + "count": 1, + "expected_result": None + }, + { + "description": "Match after cursor position", + "text": "line 1\nline 2\n\nline 4", + "cursor_position": 7, + "match_func": lambda line: line.strip() == "", + "count": 1, + "expected_result": 1 + } +] for case in test_cases: document = Document(text=case["text"], cursor_position=case["cursor_position"]) From 5bfd91aa0de9dc61a57279218bd904d9fb0ac9f7 Mon Sep 17 00:00:00 2001 From: Daniil Malygin Date: Sun, 23 Jun 2024 22:27:07 +0200 Subject: [PATCH 20/27] test cases for find_previous_matching_line --- tests/test_find_prev_matching_line.py | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/tests/test_find_prev_matching_line.py b/tests/test_find_prev_matching_line.py index ce8774e4a..ba2f875b6 100644 --- a/tests/test_find_prev_matching_line.py +++ b/tests/test_find_prev_matching_line.py @@ -9,7 +9,32 @@ def print_coverage(): print(f"{branch} was {'hit' if hit else 'not hit'}") print(f"Coverage: {hit_branches}/{total_branches} branches hit ({coverage_percentage:.2f}%)\n") -test_cases = [] +test_cases = [ + { + "description": "Find the second previous empty line", + "text": "line 1\n\nline 3\n\nline 5", + "cursor_position": 18, + "match_func": lambda line: line.strip() == "", + "count": 2, + "expected_result": -3 + }, + { + "description": "No match found", + "text": "line 1\nline 2\nline 3\nline 4", + "cursor_position": 10, + "match_func": lambda line: line.strip() == "", + "count": 1, + "expected_result": None + }, + { + "description": "Match before cursor position", + "text": "line 1\n\nline 3\nline 4", + "cursor_position": 18, + "match_func": lambda line: line.strip() == "", + "count": 1, + "expected_result": -2 + } +] for case in test_cases: document = Document(text=case["text"], cursor_position=case["cursor_position"]) From daeccc3cb0855bfd87a9a3e4c1cc4cbbf965c661 Mon Sep 17 00:00:00 2001 From: BMatajsz <90217645+BMatajsz@users.noreply.github.com> Date: Mon, 24 Jun 2024 14:22:26 +0200 Subject: [PATCH 21/27] Added modified src and tests --- src/prompt_toolkit/buffer.py | 15 ++++++-- src/prompt_toolkit/document.py | 17 +++++++++ tests/test_insert_line_above.py | 38 ++++++++++++++++++++ tests/test_translate_row_col_to_index.py | 45 ++++++++++++++++++++++++ 4 files changed, 112 insertions(+), 3 deletions(-) create mode 100644 tests/test_insert_line_above.py create mode 100644 tests/test_translate_row_col_to_index.py diff --git a/src/prompt_toolkit/buffer.py b/src/prompt_toolkit/buffer.py index f5847d4ab..cb41fcc62 100644 --- a/src/prompt_toolkit/buffer.py +++ b/src/prompt_toolkit/buffer.py @@ -39,6 +39,11 @@ from .utils import Event, to_str from .validation import ValidationError, Validator +branch_coverage_insert = { + "insert_1": False, # Branch for copy_margin = True + "insert_2": False, # Branch for copy_margin = False +} + __all__ = [ "EditReadOnlyBuffer", "Buffer", @@ -1179,13 +1184,17 @@ def newline(self, copy_margin: bool = True) -> None: self.insert_text("\n") def insert_line_above(self, copy_margin: bool = True) -> None: - """ - Insert a new line above the current one. - """ + """Insert a new line above the current one.""" + global branch_coverage_insert + if copy_margin: insert = self.document.leading_whitespace_in_current_line + "\n" + branch_coverage_insert["insert_1"] = True + print("Branch 1 was hit!") else: insert = "\n" + branch_coverage_insert["insert_2"] = True + print("Branch 2 was hit!") self.cursor_position += self.document.get_start_of_line_position() self.insert_text(insert) diff --git a/src/prompt_toolkit/document.py b/src/prompt_toolkit/document.py index df5293e62..85fbe28b7 100644 --- a/src/prompt_toolkit/document.py +++ b/src/prompt_toolkit/document.py @@ -14,6 +14,13 @@ from .filters import vi_mode from .selection import PasteMode, SelectionState, SelectionType +branch_coverage_translate = { + "translate_1": False, # Branch for successful try block execution + "translate_2": False, # Branch for IndexError with row < 0 + "translate_3": False, # Branch for IndexError with row >= number of lines + "translate_4": False, # Branch for ensuring col is within valid range +} + __all__ = [ "Document", ] @@ -323,18 +330,28 @@ def translate_row_col_to_index(self, row: int, col: int) -> int: Negative row/col values are turned into zero. """ + global branch_coverage_translate + try: result = self._line_start_indexes[row] line = self.lines[row] + branch_coverage_translate["translate_1"] = True + print("Try branch was hit!") except IndexError: if row < 0: result = self._line_start_indexes[0] line = self.lines[0] + branch_coverage_translate["translate_2"] = True + print("Branch 1 was hit!") else: result = self._line_start_indexes[-1] line = self.lines[-1] + branch_coverage_translate["translate_3"] = True + print("Branch 2 was hit!") result += max(0, min(col, len(line))) + branch_coverage_translate["translate_4"] = True + print("Result was hit!") # Keep in range. (len(self.text) is included, because the cursor can be # right after the end of the text as well.) diff --git a/tests/test_insert_line_above.py b/tests/test_insert_line_above.py new file mode 100644 index 000000000..4c893fc35 --- /dev/null +++ b/tests/test_insert_line_above.py @@ -0,0 +1,38 @@ +import pytest + +from prompt_toolkit.buffer import Buffer, branch_coverage_insert + + +@pytest.fixture +def _buffer(): + buff = Buffer() + return buff + + +test_cases = [ + (True, "Test 1: Insert line above with copy_margin=True"), + (False, "Test 2: Insert line above with copy_margin=False"), +] + + +def print_coverage(): + print("\nCoverage report:") + hit_branches = sum(branch_coverage_insert.values()) + total_branches = len(branch_coverage_insert) + coverage_percentage = (hit_branches / total_branches) * 100 + for branch, hit in branch_coverage_insert.items(): + print(f"{branch} was {'hit' if hit else 'not hit'}") + print( + f"Coverage: {hit_branches}/{total_branches} branches hit ({coverage_percentage:.2f}%)\n" + ) + + +def test_insert_line_above(_buffer): + for copy_margin, description in test_cases: + try: + _buffer.insert_line_above(copy_margin) + except Exception as e: + print(f"{description}: Failed with exception: {e}") + + +print_coverage() diff --git a/tests/test_translate_row_col_to_index.py b/tests/test_translate_row_col_to_index.py new file mode 100644 index 000000000..cd1371fa4 --- /dev/null +++ b/tests/test_translate_row_col_to_index.py @@ -0,0 +1,45 @@ +import pytest + +from prompt_toolkit.document import Document, branch_coverage_translate + + +@pytest.fixture +def document(): + return Document( + "line 1\n" + "line 2\n" + "line 3\n" + "line 4\n", len("line 1\n" + "lin") + ) + + +test_cases = [ + ((0, 0), "Test 1: Basic case with first row and first column"), + ((0, 5), "Test 2: First row with a valid column index within range"), + ((1, 0), "Test 3: Second row, first column"), + ((-1, 0), "Test 4: Negative row index"), + ((0, -1), "Test 5: Negative column index"), + ((10, 0), "Test 6: Row index out of range (greater than number of lines)"), + ((0, 100), "Test 7: Column index out of range (greater than line length)"), + ((10, 100), "Test 8: Both row and column indices out of range"), + ((-1, -1), "Test 9: Both row and column indices negative"), +] + + +def print_coverage(): + print("\nCoverage report:") + hit_branches = sum(branch_coverage_translate.values()) + total_branches = len(branch_coverage_translate) + coverage_percentage = (hit_branches / total_branches) * 100 + for branch, hit in branch_coverage_translate.items(): + print(f"{branch} was {'hit' if hit else 'not hit'}") + print( + f"Coverage: {hit_branches}/{total_branches} branches hit ({coverage_percentage:.2f}%)\n" + ) + + +for (row, col), description in test_cases: + try: + result = document.translate_row_col_to_index(row, col) + print(f"{description}: Success, Result = {result}") + except Exception as e: + print(f"{description}: Failed with exception: {e}") + +print_coverage() From 6110a0f971a011149f948ebb006a5d3f6e49f5b3 Mon Sep 17 00:00:00 2001 From: BMatajsz <90217645+BMatajsz@users.noreply.github.com> Date: Mon, 24 Jun 2024 14:36:31 +0200 Subject: [PATCH 22/27] test_row_col_to_index bug fix --- tests/test_translate_row_col_to_index.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/tests/test_translate_row_col_to_index.py b/tests/test_translate_row_col_to_index.py index cd1371fa4..92eda3354 100644 --- a/tests/test_translate_row_col_to_index.py +++ b/tests/test_translate_row_col_to_index.py @@ -35,11 +35,13 @@ def print_coverage(): ) -for (row, col), description in test_cases: - try: - result = document.translate_row_col_to_index(row, col) - print(f"{description}: Success, Result = {result}") - except Exception as e: - print(f"{description}: Failed with exception: {e}") +def test(document): + for (row, col), description in test_cases: + try: + result = document.translate_row_col_to_index(row, col) + print(f"{description}: Success, Result = {result}") + except Exception as e: + print(f"{description}: Failed with exception: {e}") + print_coverage() From a7b8ea8cba549616414fc262e764481f9123abdf Mon Sep 17 00:00:00 2001 From: BMatajsz <90217645+BMatajsz@users.noreply.github.com> Date: Mon, 24 Jun 2024 18:40:15 +0200 Subject: [PATCH 23/27] Fixed tests --- src/prompt_toolkit/document.py | 15 +++++++++------ tests/test_insert_line_above.py | 9 ++------- tests/test_translate_row_col_to_index.py | 11 ++++------- 3 files changed, 15 insertions(+), 20 deletions(-) diff --git a/src/prompt_toolkit/document.py b/src/prompt_toolkit/document.py index 85fbe28b7..ba34c0c52 100644 --- a/src/prompt_toolkit/document.py +++ b/src/prompt_toolkit/document.py @@ -16,9 +16,10 @@ branch_coverage_translate = { "translate_1": False, # Branch for successful try block execution - "translate_2": False, # Branch for IndexError with row < 0 - "translate_3": False, # Branch for IndexError with row >= number of lines - "translate_4": False, # Branch for ensuring col is within valid range + "translate_2": False, # Branch for entering except block + "translate_3": False, # Branch for IndexError with row < 0 + "translate_4": False, # Branch for IndexError with row >= number of lines + "translate_5": False, # Branch for ensuring col is within valid range } __all__ = [ @@ -338,19 +339,21 @@ def translate_row_col_to_index(self, row: int, col: int) -> int: branch_coverage_translate["translate_1"] = True print("Try branch was hit!") except IndexError: + branch_coverage_translate["translate_2"] = True + print("Except branch was hit!") if row < 0: result = self._line_start_indexes[0] line = self.lines[0] - branch_coverage_translate["translate_2"] = True + branch_coverage_translate["translate_3"] = True print("Branch 1 was hit!") else: result = self._line_start_indexes[-1] line = self.lines[-1] - branch_coverage_translate["translate_3"] = True + branch_coverage_translate["translate_4"] = True print("Branch 2 was hit!") result += max(0, min(col, len(line))) - branch_coverage_translate["translate_4"] = True + branch_coverage_translate["translate_5"] = True print("Result was hit!") # Keep in range. (len(self.text) is included, because the cursor can be diff --git a/tests/test_insert_line_above.py b/tests/test_insert_line_above.py index 4c893fc35..d2abd7c23 100644 --- a/tests/test_insert_line_above.py +++ b/tests/test_insert_line_above.py @@ -1,13 +1,7 @@ -import pytest - from prompt_toolkit.buffer import Buffer, branch_coverage_insert -@pytest.fixture -def _buffer(): - buff = Buffer() - return buff - +BUF = Buffer() test_cases = [ (True, "Test 1: Insert line above with copy_margin=True"), @@ -35,4 +29,5 @@ def test_insert_line_above(_buffer): print(f"{description}: Failed with exception: {e}") +test_insert_line_above(BUF) print_coverage() diff --git a/tests/test_translate_row_col_to_index.py b/tests/test_translate_row_col_to_index.py index 92eda3354..d1e22c904 100644 --- a/tests/test_translate_row_col_to_index.py +++ b/tests/test_translate_row_col_to_index.py @@ -1,13 +1,9 @@ -import pytest - from prompt_toolkit.document import Document, branch_coverage_translate -@pytest.fixture -def document(): - return Document( - "line 1\n" + "line 2\n" + "line 3\n" + "line 4\n", len("line 1\n" + "lin") - ) +DOC = Document( + "line 1\n" + "line 2\n" + "line 3\n" + "line 4\n", len("line 1\n" + "lin") +) test_cases = [ @@ -44,4 +40,5 @@ def test(document): print(f"{description}: Failed with exception: {e}") +test(DOC) print_coverage() From 76ce4910929cc3370af934bea2ca7a5f20380a48 Mon Sep 17 00:00:00 2001 From: Daniil Malygin Date: Wed, 26 Jun 2024 12:51:12 +0200 Subject: [PATCH 24/27] added branch_coverage_translate --- src/prompt_toolkit/document.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/prompt_toolkit/document.py b/src/prompt_toolkit/document.py index 0100c6913..428343785 100644 --- a/src/prompt_toolkit/document.py +++ b/src/prompt_toolkit/document.py @@ -24,6 +24,14 @@ "find_prev_2": False } +branch_coverage_translate = { + "translate_1": False, # Branch for successful try block execution + "translate_2": False, # Branch for entering except block + "translate_3": False, # Branch for IndexError with row < 0 + "translate_4": False, # Branch for IndexError with row >= number of lines + "translate_5": False, # Branch for ensuring col is within valid range +} + __all__ = [ "Document", ] From ff98c4b03a847b11c72b2389c952e3a320c6ee45 Mon Sep 17 00:00:00 2001 From: BMatajsz <90217645+BMatajsz@users.noreply.github.com> Date: Wed, 26 Jun 2024 14:42:40 +0200 Subject: [PATCH 25/27] Minor bug fix --- tests/test_translate_row_col_to_index.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_translate_row_col_to_index.py b/tests/test_translate_row_col_to_index.py index d1e22c904..a55535e99 100644 --- a/tests/test_translate_row_col_to_index.py +++ b/tests/test_translate_row_col_to_index.py @@ -10,7 +10,7 @@ ((0, 0), "Test 1: Basic case with first row and first column"), ((0, 5), "Test 2: First row with a valid column index within range"), ((1, 0), "Test 3: Second row, first column"), - ((-1, 0), "Test 4: Negative row index"), + ((-1000, 0), "Test 4: Negative row index"), ((0, -1), "Test 5: Negative column index"), ((10, 0), "Test 6: Row index out of range (greater than number of lines)"), ((0, 100), "Test 7: Column index out of range (greater than line length)"), From e4909690356ac3747f1e4a0d47d8670000cf37b8 Mon Sep 17 00:00:00 2001 From: knikolaevskii <90223210+knikolaevskii@users.noreply.github.com> Date: Thu, 27 Jun 2024 17:38:32 +0200 Subject: [PATCH 26/27] Invisible else statements are added to is_dimension function --- src/prompt_toolkit/layout/dimension.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/prompt_toolkit/layout/dimension.py b/src/prompt_toolkit/layout/dimension.py index 1055a12cc..9b4bfa3a9 100644 --- a/src/prompt_toolkit/layout/dimension.py +++ b/src/prompt_toolkit/layout/dimension.py @@ -221,8 +221,11 @@ def to_dimension(value: AnyDimension) -> Dimension: # Initialize coverage tracking global variable branch_coverage = { "is_dimension_1": False, # Branch for checking if value is None + "is_dimension_1_else": False, # Branch for else case of checking if value is None "is_dimension_2": False, # Branch for checking if value is callable + "is_dimension_2_else": False, # Branch for else case of checking if value is callable "is_dimension_3": False, # Branch for checking if value is int or Dimension + "is_dimension_3_else": False, # Branch for else case of checking if value is int or Dimension "is_dimension_4": False # Branch for default case } @@ -231,20 +234,32 @@ def is_dimension(value: object) -> bool: branch_coverage["is_dimension_1"] = True print("Branch 1 was hit") return True + else: + branch_coverage["is_dimension_1_else"] = True + print("Branch 1 was not hit") + if callable(value): branch_coverage["is_dimension_2"] = True print("Branch 2 was hit") return True + else: + branch_coverage["is_dimension_2_else"] = True + print("Branch 2 was not hit") + if isinstance(value, (int, Dimension)): branch_coverage["is_dimension_3"] = True print("Branch 3 was hit") return True + else: + branch_coverage["is_dimension_3_else"] = True + print("Branch 3 was not hit") branch_coverage["is_dimension_4"] = True print("Branch 4 was hit") return False + # Common alias. D = Dimension From 3e6985fa33d58842abec25454c0baf5e9c982bb8 Mon Sep 17 00:00:00 2001 From: knikolaevskii <90223210+knikolaevskii@users.noreply.github.com> Date: Thu, 27 Jun 2024 17:55:53 +0200 Subject: [PATCH 27/27] Invisible else statements are added to _commonprefix function --- src/prompt_toolkit/completion/base.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/prompt_toolkit/completion/base.py b/src/prompt_toolkit/completion/base.py index 2b59751c8..2c6c787d2 100644 --- a/src/prompt_toolkit/completion/base.py +++ b/src/prompt_toolkit/completion/base.py @@ -427,6 +427,7 @@ def get_suffix(completion: Completion) -> str: "_commonprefix_1": False, # Branch for checking if not strings "_commonprefix_2": False, # Branch for comparing characters "_commonprefix_3": False, # Branch for when the characters do not match + "_commonprefix_3_else": False, # Branch for else case of when the characters match } def _commonprefix(strings: Iterable[str]) -> str: @@ -445,7 +446,9 @@ def _commonprefix(strings: Iterable[str]) -> str: if c != s2[i]: branch_coverage["_commonprefix_3"] = True print("Branch 3 was hit") - print("here") return s1[:i] + else: + branch_coverage["_commonprefix_3_else"] = True + print("Branch 3 was not hit") return s1