Skip to content

Commit 3243e9b

Browse files
committed
Added unit tests for right-aligned numbers in completion hint tables
1 parent f2c4fd1 commit 3243e9b

File tree

2 files changed

+51
-24
lines changed

2 files changed

+51
-24
lines changed

CHANGELOG.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
## 2.3.1 (TBD, 2021)
1+
## 2.3.1 (November 18, 2021)
22
* Bug Fixes
33
* Fixed issue introduced in 2.3.0 with `AlternatingTable`, `BorderedTable`, and `SimpleTable` that caused
44
header alignment settings to be overridden by data alignment settings.
55
* Enhancements
66
* `CompletionItems` now saves the original object from which it creates a string.
77
* Using `CompletionItems` as argparse choices is fully supported. `cmd2` patched `argparse` to compare input to
88
the original value instead of the `CompletionItems` instance.
9-
* `ArgparseCompleter` now does the following if a list of `CompletionItems` was created with numerical types
9+
* `ArgparseCompleter` now does the following if a list of `CompletionItems` was created with numerical types:
1010
* Sorts completion hints numerically
1111
* Right-aligns the left-most column in completion hint table
1212

tests/test_argparse_completer.py

+49-22
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
)
2424
from cmd2.utils import (
2525
StdSim,
26+
align_right,
2627
)
2728

2829
from .conftest import (
@@ -718,6 +719,53 @@ def test_autocomp_blank_token(ac_app):
718719
assert sorted(completions) == sorted(ArgparseCompleterTester.completions_for_pos_2)
719720

720721

722+
def test_completion_items(ac_app):
723+
# First test CompletionItems created from strings
724+
text = ''
725+
line = 'choices --completion_items {}'.format(text)
726+
endidx = len(line)
727+
begidx = endidx - len(text)
728+
729+
first_match = complete_tester(text, line, begidx, endidx, ac_app)
730+
assert first_match is not None
731+
assert len(ac_app.completion_matches) == len(ac_app.completion_item_choices)
732+
assert len(ac_app.display_matches) == len(ac_app.completion_item_choices)
733+
734+
# Look for both the value and description in the hint table
735+
line_found = False
736+
for line in ac_app.formatted_completions.splitlines():
737+
# Since the CompletionItems were created from strings, the left-most column is left-aligned.
738+
# Therefore choice_1 will begin the line.
739+
if line.startswith('choice_1') and 'A description' in line:
740+
line_found = True
741+
break
742+
743+
assert line_found
744+
745+
# Now test CompletionItems created from numbers
746+
text = ''
747+
line = 'choices --num_completion_items {}'.format(text)
748+
endidx = len(line)
749+
begidx = endidx - len(text)
750+
751+
first_match = complete_tester(text, line, begidx, endidx, ac_app)
752+
assert first_match is not None
753+
assert len(ac_app.completion_matches) == len(ac_app.num_completion_items)
754+
assert len(ac_app.display_matches) == len(ac_app.num_completion_items)
755+
756+
# Look for both the value and description in the hint table
757+
line_found = False
758+
aligned_val = align_right('1.5', width=cmd2.ansi.style_aware_wcswidth('num_completion_items'))
759+
for line in ac_app.formatted_completions.splitlines():
760+
# Since the CompletionItems were created from numbers, the left-most column is right-aligned.
761+
# Therefore 1.5 will be right-aligned in a field as wide as the arg ("num_completion_items").
762+
if line.startswith(aligned_val) and 'One.Five' in line:
763+
line_found = True
764+
break
765+
766+
assert line_found
767+
768+
721769
@pytest.mark.parametrize(
722770
'num_aliases, show_description',
723771
[
@@ -729,7 +777,7 @@ def test_autocomp_blank_token(ac_app):
729777
(100, False),
730778
],
731779
)
732-
def test_completion_items(ac_app, num_aliases, show_description):
780+
def test_max_completion_items(ac_app, num_aliases, show_description):
733781
# Create aliases
734782
for i in range(0, num_aliases):
735783
run_cmd(ac_app, 'alias create fake_alias{} help'.format(i))
@@ -758,27 +806,6 @@ def test_completion_items(ac_app, num_aliases, show_description):
758806
assert description_displayed
759807

760808

761-
def test_completion_item_choices(ac_app):
762-
text = ''
763-
line = 'choices --completion_items {}'.format(text)
764-
endidx = len(line)
765-
begidx = endidx - len(text)
766-
767-
first_match = complete_tester(text, line, begidx, endidx, ac_app)
768-
assert first_match is not None
769-
assert len(ac_app.completion_matches) == len(ac_app.completion_item_choices)
770-
assert len(ac_app.display_matches) == len(ac_app.completion_item_choices)
771-
772-
# The table will show both the choice and description
773-
description_displayed = False
774-
for line in ac_app.formatted_completions.splitlines():
775-
if 'choice_1' in line and 'A description' in line:
776-
description_displayed = True
777-
break
778-
779-
assert description_displayed
780-
781-
782809
@pytest.mark.parametrize(
783810
'args, completions',
784811
[

0 commit comments

Comments
 (0)