Skip to content

Commit e1e7959

Browse files
Merge pull request #2 from knikolaevskii/Rov's-branch
Rov's branch
2 parents 3dbcc15 + 6fadf7d commit e1e7959

File tree

4 files changed

+85
-0
lines changed

4 files changed

+85
-0
lines changed

src/prompt_toolkit/layout/dimension.py

+19
Original file line numberDiff line numberDiff line change
@@ -182,20 +182,39 @@ def max_layout_dimensions(dimensions: list[Dimension]) -> Dimension:
182182
Callable[[], Any],
183183
]
184184

185+
from typing import Any, Callable, Union
186+
187+
to_dimension_coverage = {
188+
"to_dimension_1": False,
189+
"to_dimension_2": False,
190+
"to_dimension_3": False,
191+
"to_dimension_4": False,
192+
"to_dimension_5": False
193+
}
185194

186195
def to_dimension(value: AnyDimension) -> Dimension:
187196
"""
188197
Turn the given object into a `Dimension` object.
189198
"""
190199
if value is None:
200+
to_dimension_coverage["to_dimension_1"] = True
201+
print("Branch 1 was hit")
191202
return Dimension()
192203
if isinstance(value, int):
204+
to_dimension_coverage["to_dimension_2"] = True
205+
print("Branch 2 was hit")
193206
return Dimension.exact(value)
194207
if isinstance(value, Dimension):
208+
to_dimension_coverage["to_dimension_3"] = True
209+
print("Branch 3 was hit")
195210
return value
196211
if callable(value):
212+
to_dimension_coverage["to_dimension_4"] = True
213+
print("Branch 4 was hit")
197214
return to_dimension(value())
198215

216+
to_dimension_coverage["to_dimension_5"] = True
217+
print("Branch 5 was hit")
199218
raise ValueError("Not an integer or Dimension object.")
200219

201220

src/prompt_toolkit/layout/processors.py

+9
Original file line numberDiff line numberDiff line change
@@ -943,16 +943,25 @@ def apply_transformation(self, ti: TransformationInput) -> Transformation:
943943
return processor.apply_transformation(ti)
944944

945945

946+
merge_processors_coverage = {
947+
"merge_processors_1": False, # Branch for empty list of processors
948+
"merge_processors_2": False, # Branch for a single processor
949+
"merge_processors_3": False # Branch for multiple processors
950+
}
951+
946952
def merge_processors(processors: list[Processor]) -> Processor:
947953
"""
948954
Merge multiple `Processor` objects into one.
949955
"""
950956
if len(processors) == 0:
957+
merge_processors_coverage["merge_processors_1"] = True
951958
return DummyProcessor()
952959

953960
if len(processors) == 1:
961+
merge_processors_coverage["merge_processors_2"] = True
954962
return processors[0] # Nothing to merge.
955963

964+
merge_processors_coverage["merge_processors_3"] = True
956965
return _MergedProcessor(processors)
957966

958967

tests/test_merge_processors.py

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
from prompt_toolkit.layout.processors import merge_processors, merge_processors_coverage
2+
from prompt_toolkit.layout.processors import Processor, DummyProcessor, _MergedProcessor
3+
4+
def print_coverage():
5+
print("\nCoverage report:")
6+
hit_branches = sum(merge_processors_coverage.values())
7+
total_branches = len(merge_processors_coverage)
8+
coverage_percentage = (hit_branches / total_branches) * 100
9+
for branch, hit in merge_processors_coverage.items():
10+
print(f"{branch} was {'hit' if hit else 'not hit'}")
11+
print(f"Coverage: {hit_branches}/{total_branches} branches hit ({coverage_percentage:.2f}%)\n")
12+
13+
class MockProcessor(Processor):
14+
def apply_transformation(self, *args, **kwargs):
15+
pass
16+
17+
test_cases = [
18+
([], "Empty list of processors (should hit Branch 1)"),
19+
([MockProcessor()], "Single processor (should hit Branch 2)"),
20+
([MockProcessor(), MockProcessor()], "Multiple processors (should hit Branch 3)")
21+
]
22+
23+
for processors, description in test_cases:
24+
print(f"\nTesting case: {description}")
25+
result = merge_processors(processors)
26+
print(f"Result: {result}")
27+
28+
print_coverage()

tests/test_to_dimension.py

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
from prompt_toolkit.layout.dimension import Dimension, to_dimension, to_dimension_coverage
2+
3+
def print_coverage():
4+
print("\nCoverage report:")
5+
hit_branches = sum(to_dimension_coverage.values())
6+
total_branches = len(to_dimension_coverage)
7+
coverage_percentage = (hit_branches / total_branches) * 100
8+
for branch, hit in to_dimension_coverage.items():
9+
print(f"{branch} was {'hit' if hit else 'not hit'}")
10+
print(f"Coverage: {hit_branches}/{total_branches} branches hit ({coverage_percentage:.2f}%)\n")
11+
12+
13+
test_cases = [
14+
(None, "None value (should hit Branch 1)"),
15+
(69, "Integer value (should hit Branch 2)"),
16+
(Dimension(), "Dimension instance (should hit Branch 3)"),
17+
(lambda: 42, "Callable returning an integer (should hit Branch 4)"),
18+
("Unsupported type", "Unsupported type (should hit Branch 5)")
19+
]
20+
21+
for value, description in test_cases:
22+
print(f"\nTesting case: {description}")
23+
try:
24+
result = to_dimension(value)
25+
print(f"Result: {result}")
26+
except ValueError as e:
27+
print(f"Exception: {e}")
28+
29+
print_coverage()

0 commit comments

Comments
 (0)