23
23
)
24
24
from cmd2 .utils import (
25
25
StdSim ,
26
+ align_right ,
26
27
)
27
28
28
29
from .conftest import (
@@ -718,6 +719,53 @@ def test_autocomp_blank_token(ac_app):
718
719
assert sorted (completions ) == sorted (ArgparseCompleterTester .completions_for_pos_2 )
719
720
720
721
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
+
721
769
@pytest .mark .parametrize (
722
770
'num_aliases, show_description' ,
723
771
[
@@ -729,7 +777,7 @@ def test_autocomp_blank_token(ac_app):
729
777
(100 , False ),
730
778
],
731
779
)
732
- def test_completion_items (ac_app , num_aliases , show_description ):
780
+ def test_max_completion_items (ac_app , num_aliases , show_description ):
733
781
# Create aliases
734
782
for i in range (0 , num_aliases ):
735
783
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):
758
806
assert description_displayed
759
807
760
808
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
-
782
809
@pytest .mark .parametrize (
783
810
'args, completions' ,
784
811
[
0 commit comments