@@ -59,8 +59,9 @@ def get_endidx():
5959 with mock .patch .object (readline , 'get_begidx' , get_begidx ):
6060 with mock .patch .object (readline , 'get_endidx' , get_endidx ):
6161 # Run the readline tab-completion function with readline mocks in place
62- completion = cmd2_app .complete (text , state )
63- assert completion == 'help '
62+ first_match = cmd2_app .complete (text , state )
63+
64+ assert first_match is not None and cmd2_app .completion_matches == ['help ' ]
6465
6566def test_complete_command_invalid_state (cmd2_app ):
6667 text = 'he'
@@ -82,8 +83,9 @@ def get_endidx():
8283 with mock .patch .object (readline , 'get_begidx' , get_begidx ):
8384 with mock .patch .object (readline , 'get_endidx' , get_endidx ):
8485 # Run the readline tab-completion function with readline mocks in place get None
85- completion = cmd2_app .complete (text , state )
86- assert completion is None
86+ first_match = cmd2_app .complete (text , state )
87+
88+ assert first_match is None
8789
8890def test_complete_empty_arg (cmd2_app ):
8991 text = ''
@@ -105,9 +107,10 @@ def get_endidx():
105107 with mock .patch .object (readline , 'get_begidx' , get_begidx ):
106108 with mock .patch .object (readline , 'get_endidx' , get_endidx ):
107109 # Run the readline tab-completion function with readline mocks in place
108- completion = cmd2_app .complete (text , state )
110+ first_match = cmd2_app .complete (text , state )
109111
110- assert completion == cmd2_app .complete_help (text , line , begidx , endidx )[0 ]
112+ assert first_match is not None and \
113+ cmd2_app .completion_matches == cmd2_app .complete_help (text , line , begidx , endidx )
111114
112115def test_complete_bogus_command (cmd2_app ):
113116 text = ''
@@ -129,9 +132,9 @@ def get_endidx():
129132 with mock .patch .object (readline , 'get_begidx' , get_begidx ):
130133 with mock .patch .object (readline , 'get_endidx' , get_endidx ):
131134 # Run the readline tab-completion function with readline mocks in place
132- completion = cmd2_app .complete (text , state )
135+ first_match = cmd2_app .complete (text , state )
133136
134- assert completion is None
137+ assert first_match is None
135138
136139def test_cmd2_command_completion_is_case_insensitive_by_default (cmd2_app ):
137140 text = 'HE'
@@ -465,6 +468,127 @@ def sc_app():
465468 return app
466469
467470
471+ def test_cmd2_subcommand_completion_single_end (sc_app ):
472+ text = 'f'
473+ line = 'base f'
474+ endidx = len (line )
475+ begidx = endidx - len (text )
476+ state = 0
477+
478+ def get_line ():
479+ return line
480+
481+ def get_begidx ():
482+ return begidx
483+
484+ def get_endidx ():
485+ return endidx
486+
487+ with mock .patch .object (readline , 'get_line_buffer' , get_line ):
488+ with mock .patch .object (readline , 'get_begidx' , get_begidx ):
489+ with mock .patch .object (readline , 'get_endidx' , get_endidx ):
490+ # Run the readline tab-completion function with readline mocks in place
491+ first_match = sc_app .complete (text , state )
492+
493+ # It is at end of line, so extra space is present
494+ assert first_match is not None and sc_app .completion_matches == ['foo ' ]
495+
496+ def test_cmd2_subcommand_completion_single_mid (sc_app ):
497+ text = 'f'
498+ line = 'base f'
499+ endidx = len (line ) - 1
500+ begidx = endidx - len (text )
501+ state = 0
502+
503+ def get_line ():
504+ return line
505+
506+ def get_begidx ():
507+ return begidx
508+
509+ def get_endidx ():
510+ return endidx
511+
512+ with mock .patch .object (readline , 'get_line_buffer' , get_line ):
513+ with mock .patch .object (readline , 'get_begidx' , get_begidx ):
514+ with mock .patch .object (readline , 'get_endidx' , get_endidx ):
515+ # Run the readline tab-completion function with readline mocks in place
516+ first_match = sc_app .complete (text , state )
517+
518+ assert first_match is not None and sc_app .completion_matches == ['foo' ]
519+
520+ def test_cmd2_subcommand_completion_multiple (sc_app ):
521+ text = ''
522+ line = 'base '
523+ endidx = len (line )
524+ begidx = endidx - len (text )
525+ state = 0
526+
527+ def get_line ():
528+ return line
529+
530+ def get_begidx ():
531+ return begidx
532+
533+ def get_endidx ():
534+ return endidx
535+
536+ with mock .patch .object (readline , 'get_line_buffer' , get_line ):
537+ with mock .patch .object (readline , 'get_begidx' , get_begidx ):
538+ with mock .patch .object (readline , 'get_endidx' , get_endidx ):
539+ # Run the readline tab-completion function with readline mocks in place
540+ first_match = sc_app .complete (text , state )
541+
542+ assert first_match is not None and sc_app .completion_matches == ['foo' , 'bar' ]
543+
544+ def test_cmd2_subcommand_completion_nomatch (sc_app ):
545+ text = 'z'
546+ line = 'base z'
547+ endidx = len (line )
548+ begidx = endidx - len (text )
549+ state = 0
550+
551+ def get_line ():
552+ return line
553+
554+ def get_begidx ():
555+ return begidx
556+
557+ def get_endidx ():
558+ return endidx
559+
560+ with mock .patch .object (readline , 'get_line_buffer' , get_line ):
561+ with mock .patch .object (readline , 'get_begidx' , get_begidx ):
562+ with mock .patch .object (readline , 'get_endidx' , get_endidx ):
563+ # Run the readline tab-completion function with readline mocks in place
564+ first_match = sc_app .complete (text , state )
565+
566+ assert first_match is None
567+
568+ def test_cmd2_subcommand_completion_after_subcommand (sc_app ):
569+ text = 'f'
570+ line = 'base foo f'
571+ endidx = len (line )
572+ begidx = endidx - len (text )
573+ state = 0
574+
575+ def get_line ():
576+ return line
577+
578+ def get_begidx ():
579+ return begidx
580+
581+ def get_endidx ():
582+ return endidx
583+
584+ with mock .patch .object (readline , 'get_line_buffer' , get_line ):
585+ with mock .patch .object (readline , 'get_begidx' , get_begidx ):
586+ with mock .patch .object (readline , 'get_endidx' , get_endidx ):
587+ # Run the readline tab-completion function with readline mocks in place
588+ first_match = sc_app .complete (text , state )
589+
590+ assert first_match is None
591+
468592def test_complete_subcommand_single_end (sc_app ):
469593 text = 'f'
470594 line = 'base f'
@@ -485,5 +609,6 @@ def get_endidx():
485609 with mock .patch .object (readline , 'get_begidx' , get_begidx ):
486610 with mock .patch .object (readline , 'get_endidx' , get_endidx ):
487611 # Run the readline tab-completion function with readline mocks in place
488- completion = sc_app .complete (text , state )
489- assert completion == 'foo '
612+ first_match = sc_app .complete (text , state )
613+
614+ assert first_match is not None and sc_app .completion_matches == ['foo ' ]
0 commit comments