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/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_commonprefix_coverage.py b/tests/test_commonprefix_coverage.py new file mode 100644 index 000000000..72a595f92 --- /dev/null +++ b/tests/test_commonprefix_coverage.py @@ -0,0 +1,28 @@ +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]] = [ + (["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}") + result = _commonprefix(strings) + print("Common prefix:", result) + +print_coverage() + diff --git a/tests/test_is_dimension_coverage.py b/tests/test_is_dimension_coverage.py new file mode 100644 index 000000000..4892262b9 --- /dev/null +++ b/tests/test_is_dimension_coverage.py @@ -0,0 +1,27 @@ +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 = [ + (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}") + result = is_dimension(value) + print(f"Result: {result}") + +print_coverage() \ No newline at end of file