Skip to content

Commit

Permalink
Added unit tests for right-aligned numbers in completion hint tables
Browse files Browse the repository at this point in the history
  • Loading branch information
kmvanbrunt committed Nov 19, 2021
1 parent f2c4fd1 commit 3243e9b
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 24 deletions.
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
## 2.3.1 (TBD, 2021)
## 2.3.1 (November 18, 2021)
* Bug Fixes
* Fixed issue introduced in 2.3.0 with `AlternatingTable`, `BorderedTable`, and `SimpleTable` that caused
header alignment settings to be overridden by data alignment settings.
* Enhancements
* `CompletionItems` now saves the original object from which it creates a string.
* Using `CompletionItems` as argparse choices is fully supported. `cmd2` patched `argparse` to compare input to
the original value instead of the `CompletionItems` instance.
* `ArgparseCompleter` now does the following if a list of `CompletionItems` was created with numerical types
* `ArgparseCompleter` now does the following if a list of `CompletionItems` was created with numerical types:
* Sorts completion hints numerically
* Right-aligns the left-most column in completion hint table

Expand Down
71 changes: 49 additions & 22 deletions tests/test_argparse_completer.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
)
from cmd2.utils import (
StdSim,
align_right,
)

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


def test_completion_items(ac_app):
# First test CompletionItems created from strings
text = ''
line = 'choices --completion_items {}'.format(text)
endidx = len(line)
begidx = endidx - len(text)

first_match = complete_tester(text, line, begidx, endidx, ac_app)
assert first_match is not None
assert len(ac_app.completion_matches) == len(ac_app.completion_item_choices)
assert len(ac_app.display_matches) == len(ac_app.completion_item_choices)

# Look for both the value and description in the hint table
line_found = False
for line in ac_app.formatted_completions.splitlines():
# Since the CompletionItems were created from strings, the left-most column is left-aligned.
# Therefore choice_1 will begin the line.
if line.startswith('choice_1') and 'A description' in line:
line_found = True
break

assert line_found

# Now test CompletionItems created from numbers
text = ''
line = 'choices --num_completion_items {}'.format(text)
endidx = len(line)
begidx = endidx - len(text)

first_match = complete_tester(text, line, begidx, endidx, ac_app)
assert first_match is not None
assert len(ac_app.completion_matches) == len(ac_app.num_completion_items)
assert len(ac_app.display_matches) == len(ac_app.num_completion_items)

# Look for both the value and description in the hint table
line_found = False
aligned_val = align_right('1.5', width=cmd2.ansi.style_aware_wcswidth('num_completion_items'))
for line in ac_app.formatted_completions.splitlines():
# Since the CompletionItems were created from numbers, the left-most column is right-aligned.
# Therefore 1.5 will be right-aligned in a field as wide as the arg ("num_completion_items").
if line.startswith(aligned_val) and 'One.Five' in line:
line_found = True
break

assert line_found


@pytest.mark.parametrize(
'num_aliases, show_description',
[
Expand All @@ -729,7 +777,7 @@ def test_autocomp_blank_token(ac_app):
(100, False),
],
)
def test_completion_items(ac_app, num_aliases, show_description):
def test_max_completion_items(ac_app, num_aliases, show_description):
# Create aliases
for i in range(0, num_aliases):
run_cmd(ac_app, 'alias create fake_alias{} help'.format(i))
Expand Down Expand Up @@ -758,27 +806,6 @@ def test_completion_items(ac_app, num_aliases, show_description):
assert description_displayed


def test_completion_item_choices(ac_app):
text = ''
line = 'choices --completion_items {}'.format(text)
endidx = len(line)
begidx = endidx - len(text)

first_match = complete_tester(text, line, begidx, endidx, ac_app)
assert first_match is not None
assert len(ac_app.completion_matches) == len(ac_app.completion_item_choices)
assert len(ac_app.display_matches) == len(ac_app.completion_item_choices)

# The table will show both the choice and description
description_displayed = False
for line in ac_app.formatted_completions.splitlines():
if 'choice_1' in line and 'A description' in line:
description_displayed = True
break

assert description_displayed


@pytest.mark.parametrize(
'args, completions',
[
Expand Down

0 comments on commit 3243e9b

Please sign in to comment.