Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Kirill's branch #1890

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion src/prompt_toolkit/completion/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
25 changes: 19 additions & 6 deletions src/prompt_toolkit/layout/dimension.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
28 changes: 28 additions & 0 deletions tests/test_commonprefix_coverage.py
Original file line number Diff line number Diff line change
@@ -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()

27 changes: 27 additions & 0 deletions tests/test_is_dimension_coverage.py
Original file line number Diff line number Diff line change
@@ -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()
Loading