Skip to content

Commit

Permalink
The instrumented code for _commonprefix() function and a coverage tes…
Browse files Browse the repository at this point in the history
…t for it
  • Loading branch information
knikolaevskii committed Jun 19, 2024
1 parent ab9afba commit bb1d1f2
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
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
24 changes: 24 additions & 0 deletions tests/test_commonprefix_coverage.py
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit bb1d1f2

Please sign in to comment.