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 1/4] 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 2/4] 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 3/4] 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 4/4] 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}")