summaryrefslogtreecommitdiff
path: root/tests/test_argparse_completer.py
blob: 5c6b750fe62ab15081a7c030e19ed985031fba9b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
# coding=utf-8
# flake8: noqa E302
"""
Unit/functional testing for argparse completer in cmd2
"""
import argparse
from typing import List

import pytest

import cmd2
from cmd2 import with_argparser, Cmd2ArgParser, CompletionItem
from cmd2.utils import StdSim, basic_complete
from .conftest import run_cmd, complete_tester

# Lists used in our tests
static_int_choices_list = [1, 2, 3, 4, 5]
static_choices_list = ['static', 'choices', 'stop', 'here']
choices_from_function = ['choices', 'function', 'chatty', 'smith']
choices_from_method = ['choices', 'method', 'most', 'improved']

set_value_choices = ['set', 'value', 'choices']
one_or_more_choices = ['one', 'or', 'more', 'choices']
optional_choices = ['a', 'few', 'optional', 'choices']
range_choices = ['some', 'range', 'choices']
remainder_choices = ['remainder', 'choices']

completions_from_function = ['completions', 'function', 'fairly', 'complete']
completions_from_method = ['completions', 'method', 'missed', 'spot']


def choices_function() -> List[str]:
    """Function that provides choices"""
    return choices_from_function


def completer_function(text: str, line: str, begidx: int, endidx: int) -> List[str]:
    """Tab completion function"""
    return basic_complete(text, line, begidx, endidx, completions_from_function)


# noinspection PyMethodMayBeStatic,PyUnusedLocal
class AutoCompleteTester(cmd2.Cmd):
    """Cmd2 app that exercises AutoCompleter class"""
    def __init__(self):
        super().__init__()

    ############################################################################################################
    # Begin code related to help and command name completion
    ############################################################################################################
    def _music_create(self, args: argparse.Namespace) -> None:
        """Implements the 'music create' command"""
        self.poutput('music create')

    def _music_create_jazz(self, args: argparse.Namespace) -> None:
        """Implements the 'music create jazz' command"""
        self.poutput('music create jazz')

    def _music_create_rock(self, args: argparse.Namespace) -> None:
        """Implements the 'music create rock' command"""
        self.poutput('music create rock')

    # Top level parser for music command
    music_parser = Cmd2ArgParser(description='Manage music', prog='music')

    # Add sub-commands to music
    music_subparsers = music_parser.add_subparsers()

    # music -> create
    music_create_parser = music_subparsers.add_parser('create', help='Create music')
    music_create_parser.set_defaults(func=_music_create)

    # Add sub-commands to music -> create
    music_create_subparsers = music_create_parser.add_subparsers()

    # music -> create -> jazz
    music_create_jazz_parser = music_create_subparsers.add_parser('jazz', help='Create jazz')
    music_create_jazz_parser.set_defaults(func=_music_create_jazz)

    # music -> create -> rock
    music_create_rock_parser = music_create_subparsers.add_parser('rock', help='Create rocks')
    music_create_rock_parser.set_defaults(func=_music_create_rock)

    @with_argparser(music_parser)
    def do_music(self, args: argparse.Namespace) -> None:
        """Music command"""
        func = getattr(args, 'func', None)
        if func is not None:
            # Call whatever sub-command function was selected
            func(self, args)
        else:
            # No sub-command was provided, so call help
            # noinspection PyTypeChecker
            self.do_help('music')

    ############################################################################################################
    # Begin code related to flag completion
    ############################################################################################################

    # Uses default flag prefix value (-)
    flag_parser = Cmd2ArgParser()
    flag_parser.add_argument('-n', '--normal_flag', help='A normal flag', action='store_true')
    flag_parser.add_argument('-a', '--append_flag', help='Append flag', action='append')
    flag_parser.add_argument('-o', '--append_const_flag', help='Append const flag', action='append_const', const=True)
    flag_parser.add_argument('-c', '--count_flag', help='Count flag', action='count')
    flag_parser.add_argument('-s', '--suppressed_flag', help=argparse.SUPPRESS, action='store_true')
    flag_parser.add_argument('-r', '--remainder_flag', nargs=argparse.REMAINDER, help='a remainder flag')

    @with_argparser(flag_parser)
    def do_flag(self, args: argparse.Namespace) -> None:
        pass

    # Uses non-default flag prefix value (+)
    plus_flag_parser = Cmd2ArgParser(prefix_chars='+')
    plus_flag_parser.add_argument('+n', '++normal_flag', help='A normal flag', action='store_true')

    @with_argparser(plus_flag_parser)
    def do_plus_flag(self, args: argparse.Namespace) -> None:
        pass

    ############################################################################################################
    # Begin code related to testing choices, choices_function, and choices_method parameters
    ############################################################################################################
    def choices_method(self) -> List[str]:
        """Method that provides choices"""
        return choices_from_method

    def completion_item_method(self) -> List[CompletionItem]:
        """Choices method that returns CompletionItems"""
        items = []
        for i in range(0, 10):
            main_str = 'main_str{}'.format(i)
            items.append(CompletionItem(main_str, desc='blah blah'))
        return items

    choices_parser = Cmd2ArgParser()

    # Flag args for choices command. Include string and non-string arg types.
    choices_parser.add_argument("-l", "--list", help="a flag populated with a choices list",
                                choices=static_choices_list)
    choices_parser.add_argument("-f", "--function", help="a flag populated with a choices function",
                                choices_function=choices_function)
    choices_parser.add_argument("-m", "--method", help="a flag populated with a choices method",
                                choices_method=choices_method)
    choices_parser.add_argument('-n', "--no_header", help='this arg has a no descriptive header',
                                choices_method=completion_item_method)
    choices_parser.add_argument('-i', '--int', type=int, help='a flag with an int type',
                                choices=static_int_choices_list)

    # Positional args for choices command
    choices_parser.add_argument("list_pos", help="a positional populated with a choices list",
                                choices=static_choices_list)
    choices_parser.add_argument("function_pos", help="a positional populated with a choices function",
                                choices_function=choices_function)
    choices_parser.add_argument("method_pos", help="a positional populated with a choices method",
                                choices_method=choices_method)

    @with_argparser(choices_parser)
    def do_choices(self, args: argparse.Namespace) -> None:
        pass

    ############################################################################################################
    # Begin code related to testing completer_function and completer_method parameters
    ############################################################################################################
    def completer_method(self, text: str, line: str, begidx: int, endidx: int) -> List[str]:
        """Tab completion method"""
        return basic_complete(text, line, begidx, endidx, completions_from_method)

    completer_parser = Cmd2ArgParser()

    # Flag args for completer command
    completer_parser.add_argument("-f", "--function", help="a flag using a completer function",
                                  completer_function=completer_function)
    completer_parser.add_argument("-m", "--method", help="a flag using a completer method",
                                  completer_method=completer_method)

    # Positional args for completer command
    completer_parser.add_argument("function_pos", help="a positional using a completer function",
                                  completer_function=completer_function)
    completer_parser.add_argument("method_pos", help="a positional using a completer method",
                                  completer_method=completer_method)

    @with_argparser(completer_parser)
    def do_completer(self, args: argparse.Namespace) -> None:
        pass

    ############################################################################################################
    # Begin code related to nargs
    ############################################################################################################
    nargs_parser = Cmd2ArgParser()

    # Flag args for nargs command
    nargs_parser.add_argument("--set_value", help="a flag with a set value for nargs", nargs=2,
                              choices=set_value_choices)
    nargs_parser.add_argument("--one_or_more", help="a flag wanting one or more args", nargs=argparse.ONE_OR_MORE,
                              choices=one_or_more_choices)
    nargs_parser.add_argument("--optional", help="a flag with an optional value", nargs=argparse.OPTIONAL,
                              choices=optional_choices)
    nargs_parser.add_argument("--range", help="a flag with nargs range", nargs=(1, 2),
                              choices=range_choices)
    nargs_parser.add_argument("--remainder", help="a flag wanting remaining", nargs=argparse.REMAINDER,
                              choices=remainder_choices)

    @with_argparser(nargs_parser)
    def do_nargs(self, args: argparse.Namespace) -> None:
        pass

    ############################################################################################################
    # Begin code related to testing tab hints
    ############################################################################################################
    hint_parser = Cmd2ArgParser()
    hint_parser.add_argument('-f', '--flag', help='a flag arg')
    hint_parser.add_argument('-s', '--suppressed_help', help=argparse.SUPPRESS)
    hint_parser.add_argument('-t', '--suppressed_hint', help='a flag arg', suppress_tab_hint=True)

    hint_parser.add_argument('hint_pos', help='here is a hint\nwith new lines')
    hint_parser.add_argument('no_help_pos')

    @with_argparser(hint_parser)
    def do_hint(self, args: argparse.Namespace) -> None:
        pass


@pytest.fixture
def ac_app():
    app = AutoCompleteTester()
    app.stdout = StdSim(app.stdout)
    return app


@pytest.mark.parametrize('command', [
    'music',
    'music create',
    'music create rock',
    'music create jazz'
])
def test_help(ac_app, command):
    out1, err1 = run_cmd(ac_app, '{} -h'.format(command))
    out2, err2 = run_cmd(ac_app, 'help {}'.format(command))
    assert out1 == out2


@pytest.mark.parametrize('command, text, completions', [
    ('', 'mu', ['music ']),
    ('music', 'cre', ['create ']),
    ('music create', '', ['jazz', 'rock'])
])
def test_complete_help(ac_app, command, text, completions):
    line = 'help {} {}'.format(command, text)
    endidx = len(line)
    begidx = endidx - len(text)

    first_match = complete_tester(text, line, begidx, endidx, ac_app)
    if completions:
        assert first_match is not None
    else:
        assert first_match is None

    assert ac_app.completion_matches == sorted(completions, key=ac_app.matches_sort_key)


@pytest.mark.parametrize('command_and_args, text, completions', [
    # Complete all flags (suppressed will not show)
    ('flag', '-', ['--append_const_flag', '--append_flag', '--count_flag', '--help', '--normal_flag',
                   '--remainder_flag', '-a', '-c', '-h', '-n', '-o', '-r']),
    ('flag', '--', ['--append_const_flag', '--append_flag', '--count_flag', '--help',
                    '--normal_flag', '--remainder_flag']),

    # Complete individual flag
    ('flag', '-n', ['-n ']),
    ('flag', '--n', ['--normal_flag ']),

    # No flags should complete until current flag has its args
    ('flag --append_flag', '-', []),

    # Complete REMAINDER flag name
    ('flag', '-r', ['-r ']),
    ('flag', '--r', ['--remainder_flag ']),

    # No flags after a REMAINDER should complete
    ('flag -r value', '-', []),
    ('flag --remainder_flag value', '--', []),

    # Suppressed flag should not complete
    ('flag', '-s', []),
    ('flag', '--s', []),

    # A used flag should not show in completions
    ('flag -n', '--', ['--append_const_flag', '--append_flag', '--count_flag', '--help', '--remainder_flag']),

    # Flags with actions set to append, append_const, and count will always show even if they've been used
    ('flag --append_const_flag -c --append_flag value', '--', ['--append_const_flag', '--append_flag', '--count_flag',
                                                               '--help', '--normal_flag', '--remainder_flag']),

    # Non-default flag prefix character (+)
    ('plus_flag', '+', ['++help', '++normal_flag', '+h', '+n']),
    ('plus_flag', '++', ['++help', '++normal_flag']),

    # Flag completion should not occur after '--' since that tells argparse all remaining arguments are non-flags
    ('flag --', '--', []),
    ('flag --help --', '--', []),
    ('plus_flag --', '++', []),
    ('plus_flag ++help --', '++', [])
])
def test_autcomp_flag_completion(ac_app, command_and_args, text, completions):
    line = '{} {}'.format(command_and_args, text)
    endidx = len(line)
    begidx = endidx - len(text)

    first_match = complete_tester(text, line, begidx, endidx, ac_app)
    if completions:
        assert first_match is not None
    else:
        assert first_match is None

    assert ac_app.completion_matches == sorted(completions, key=ac_app.matches_sort_key)


@pytest.mark.parametrize('flag, text, completions', [
    ('-l', '', static_choices_list),
    ('--list', 's', ['static', 'stop']),
    ('-f', '', choices_from_function),
    ('--function', 'ch', ['choices', 'chatty']),
    ('-m', '', choices_from_method),
    ('--method', 'm', ['method', 'most']),
    ('-i', '', [str(i) for i in static_int_choices_list]),
    ('--int', '1', ['1 '])
])
def test_autocomp_flag_choices_completion(ac_app, flag, text, completions):
    line = 'choices {} {}'.format(flag, text)
    endidx = len(line)
    begidx = endidx - len(text)

    first_match = complete_tester(text, line, begidx, endidx, ac_app)
    if completions:
        assert first_match is not None
    else:
        assert first_match is None

    assert ac_app.completion_matches == sorted(completions, key=ac_app.matches_sort_key)


@pytest.mark.parametrize('pos, text, completions', [
    (1, '', static_choices_list),
    (1, 's', ['static', 'stop']),
    (2, '', choices_from_function),
    (2, 'ch', ['choices', 'chatty']),
    (3, '', choices_from_method),
    (3, 'm', ['method', 'most'])
])
def test_autocomp_positional_choices_completion(ac_app, pos, text, completions):
    # Generate line were preceding positionals are already filled
    line = 'choices {} {}'.format('foo ' * (pos - 1), text)
    endidx = len(line)
    begidx = endidx - len(text)

    first_match = complete_tester(text, line, begidx, endidx, ac_app)
    if completions:
        assert first_match is not None
    else:
        assert first_match is None

    assert ac_app.completion_matches == sorted(completions, key=ac_app.matches_sort_key)


@pytest.mark.parametrize('flag, text, completions', [
    ('-f', '', completions_from_function),
    ('--function', 'f', ['function', 'fairly']),
    ('-m', '', completions_from_method),
    ('--method', 'm', ['method', 'missed'])
])
def test_autocomp_flag_completers(ac_app, flag, text, completions):
    line = 'completer {} {}'.format(flag, text)
    endidx = len(line)
    begidx = endidx - len(text)

    first_match = complete_tester(text, line, begidx, endidx, ac_app)
    if completions:
        assert first_match is not None
    else:
        assert first_match is None

    assert ac_app.completion_matches == sorted(completions, key=ac_app.matches_sort_key)


@pytest.mark.parametrize('pos, text, completions', [
    (1, '', completions_from_function),
    (1, 'c', ['completions', 'complete']),
    (2, '', completions_from_method),
    (2, 'm', ['method', 'missed'])
])
def test_autocomp_positional_completers(ac_app, pos, text, completions):
    # Generate line were preceding positionals are already filled
    line = 'completer {} {}'.format('foo ' * (pos - 1), text)
    endidx = len(line)
    begidx = endidx - len(text)

    first_match = complete_tester(text, line, begidx, endidx, ac_app)
    if completions:
        assert first_match is not None
    else:
        assert first_match is None

    assert ac_app.completion_matches == sorted(completions, key=ac_app.matches_sort_key)


@pytest.mark.parametrize('num_aliases, show_description', [
    # The number of completion results determines if the description field of CompletionItems gets displayed
    # in the tab completions. The count must be greater than 1 and less than ac_app.max_completion_items,
    # which defaults to 50.
    (1, False),
    (5, True),
    (100, False)
])
def test_completion_items(ac_app, num_aliases, show_description):
    # Create aliases
    for i in range(0, num_aliases):
        run_cmd(ac_app, 'alias create fake{} help'.format(i))

    assert len(ac_app.aliases) == num_aliases

    text = 'fake'
    line = 'alias list {}'.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) == num_aliases
    assert len(ac_app.display_matches) == num_aliases

    # If show_description is True, the alias's value will be in the display text
    assert ('help' in ac_app.display_matches[0]) == show_description


@pytest.mark.parametrize('args, completions', [
    # Flag with nargs = 2
    ('--set_value', set_value_choices),
    ('--set_value set', ['value', 'choices']),
    ('--set_value set value choices', []),

    # Another flag can't start until all expected args are filled out
    ('--set_value --one_or_more', set_value_choices),

    # Using the flag again will reset the choices available
    ('--set_value set value --set_value', set_value_choices),

    # Flag with nargs = ONE_OR_MORE
    ('--one_or_more', one_or_more_choices),
    ('--one_or_more one', ['or', 'more', 'choices']),

    # Flag with nargs = OPTIONAL
    ('--optional', optional_choices),

    # Only one arg allowed for an OPTIONAL to completions are now empty
    ('--optional optional', []),

    # Flag with nargs range (1, 2)
    ('--range', range_choices),
    ('--range some', ['range', 'choices']),

    # Already used 2 args so no more completions
    ('--range some range', []),

    # Flag with nargs = REMAINDER
    ('--remainder', remainder_choices),
    ('--remainder remainder ', ['choices ']),

    # No more flags can appear after a REMAINDER flag)
    ('--remainder choices --set_value', ['remainder ']),

    # Double dash ends the current flag (even if all expected args aren't entered)
    ('--set_value --', []),

    # Double dash ends a REMAINDER flag
    ('--remainder remainder --', []),

    # No more flags after a double dash
    ('-- --one_or_more ', []),
])
def test_autcomp_nargs(ac_app, args, completions):
    text = ''
    line = 'nargs {} {}'.format(args, text)
    endidx = len(line)
    begidx = endidx - len(text)

    first_match = complete_tester(text, line, begidx, endidx, ac_app)
    if completions:
        assert first_match is not None
    else:
        assert first_match is None

    assert ac_app.completion_matches == sorted(completions, key=ac_app.matches_sort_key)


def test_completion_items_default_header(ac_app):
    from cmd2.argparse_completer import DEFAULT_DESCRIPTIVE_HEADER

    text = ''
    line = 'choices -n {}'.format(text)
    endidx = len(line)
    begidx = endidx - len(text)

    # This positional argument did not provide a descriptive header, so it should be DEFAULT_DESCRIPTIVE_HEADER
    complete_tester(text, line, begidx, endidx, ac_app)
    assert DEFAULT_DESCRIPTIVE_HEADER in ac_app.completion_header


def test_autocomp_hint_flag(ac_app, capsys):
    text = ''
    line = 'hint --flag {}'.format(text)
    endidx = len(line)
    begidx = endidx - len(text)

    first_match = complete_tester(text, line, begidx, endidx, ac_app)
    out, err = capsys.readouterr()

    assert first_match is None
    assert out == '''
Hint:
  -f, --flag FLAG         a flag arg

'''


def test_autocomp_hint_suppressed_help(ac_app, capsys):
    text = ''
    line = 'hint --suppressed_help {}'.format(text)
    endidx = len(line)
    begidx = endidx - len(text)

    first_match = complete_tester(text, line, begidx, endidx, ac_app)
    out, err = capsys.readouterr()

    assert first_match is None
    assert not out


def test_autocomp_hint_suppressed_hint(ac_app, capsys):
    text = ''
    line = 'hint --suppressed_hint {}'.format(text)
    endidx = len(line)
    begidx = endidx - len(text)

    first_match = complete_tester(text, line, begidx, endidx, ac_app)
    out, err = capsys.readouterr()

    assert first_match is None
    assert not out


def test_autocomp_hint_pos(ac_app, capsys):
    text = ''
    line = 'hint {}'.format(text)
    endidx = len(line)
    begidx = endidx - len(text)

    first_match = complete_tester(text, line, begidx, endidx, ac_app)
    out, err = capsys.readouterr()

    assert first_match is None
    assert out == '''
Hint:
  HINT_POS                here is a hint
                          with new lines

'''


def test_autocomp_hint_no_help(ac_app, capsys):
    text = ''
    line = 'hint foo {}'.format(text)
    endidx = len(line)
    begidx = endidx - len(text)

    first_match = complete_tester(text, line, begidx, endidx, ac_app)
    out, err = capsys.readouterr()

    assert first_match is None
    assert not out == '''
Hint:
  NO_HELP_POS            

'''

# def test_autcomp_hint_in_narg_range(cmd2_app, capsys):
#     text = ''
#     line = 'suggest -d 2 {}'.format(text)
#     endidx = len(line)
#     begidx = endidx - len(text)
#
#     first_match = complete_tester(text, line, begidx, endidx, cmd2_app)
#     out, err = capsys.readouterr()
#
#     assert out == '''
# Hint:
#   -d, --duration DURATION    Duration constraint in minutes.
#                              	single value - maximum duration
#                              	[a, b] - duration range
#
# '''
#
# def test_autocomp_flags_narg_max(cmd2_app):
#     text = ''
#     line = 'suggest d 2 3 {}'.format(text)
#     endidx = len(line)
#     begidx = endidx - len(text)
#
#     first_match = complete_tester(text, line, begidx, endidx, cmd2_app)
#     assert first_match is None
#
#
# def test_autcomp_narg_beyond_max(cmd2_app):
#     out, err = run_cmd(cmd2_app, 'suggest -t movie -d 3 4 5')
#     assert 'Error: unrecognized arguments: 5' in err[1]
#
#
# def test_autocomp_subcmd_flag_comp_func_attr(cmd2_app):
#     text = 'A'
#     line = 'video movies list -a "{}'.format(text)
#     endidx = len(line)
#     begidx = endidx - len(text)
#
#     first_match = complete_tester(text, line, begidx, endidx, cmd2_app)
#     assert first_match is not None and \
#            cmd2_app.completion_matches == ['Adam Driver', 'Alec Guinness', 'Andy Serkis', 'Anthony Daniels']
#
#
# def test_autocomp_subcmd_flag_comp_list_attr(cmd2_app):
#     text = 'G'
#     line = 'video movies list -d {}'.format(text)
#     endidx = len(line)
#     begidx = endidx - len(text)
#
#     first_match = complete_tester(text, line, begidx, endidx, cmd2_app)
#     assert first_match is not None and first_match == '"Gareth Edwards'
#
#
# def test_autocomp_pos_consumed(cmd2_app):
#     text = ''
#     line = 'library movie add SW_EP01 {}'.format(text)
#     endidx = len(line)
#     begidx = endidx - len(text)
#
#     first_match = complete_tester(text, line, begidx, endidx, cmd2_app)
#     assert first_match is None
#
#
# def test_autocomp_pos_after_flag(cmd2_app):
#     text = 'Joh'
#     line = 'video movies add -d "George Lucas" -- "Han Solo" PG "Emilia Clarke" "{}'.format(text)
#     endidx = len(line)
#     begidx = endidx - len(text)
#
#     first_match = complete_tester(text, line, begidx, endidx, cmd2_app)
#     assert first_match is not None and \
#            cmd2_app.completion_matches == ['John Boyega" ']
#
#
#
# def test_argparse_remainder_flag_completion(cmd2_app):
#     import cmd2
#     import argparse
#
#     # Test flag completion as first arg of positional with nargs=argparse.REMAINDER
#     text = '--h'
#     line = 'help command {}'.format(text)
#     endidx = len(line)
#     begidx = endidx - len(text)
#
#     # --h should not complete into --help because we are in the argparse.REMAINDER section
#     assert complete_tester(text, line, begidx, endidx, cmd2_app) is None
#
#     # Test flag completion within an already started positional with nargs=argparse.REMAINDER
#     text = '--h'
#     line = 'help command subcommand {}'.format(text)
#     endidx = len(line)
#     begidx = endidx - len(text)
#
#     # --h should not complete into --help because we are in the argparse.REMAINDER section
#     assert complete_tester(text, line, begidx, endidx, cmd2_app) is None
#
#     # Test a flag with nargs=argparse.REMAINDER
#     parser = argparse.ArgumentParser()
#     parser.add_argument('-f', nargs=argparse.REMAINDER)
#
#     # Overwrite eof's parser for this test
#     cmd2.Cmd.do_eof.argparser = parser
#
#     text = '--h'
#     line = 'eof -f {}'.format(text)
#     endidx = len(line)
#     begidx = endidx - len(text)
#
#     # --h should not complete into --help because we are in the argparse.REMAINDER section
#     assert complete_tester(text, line, begidx, endidx, cmd2_app) is None
#
#


def test_is_potential_flag():
    from cmd2.argparse_completer import is_potential_flag
    parser = Cmd2ArgParser()

    # Not potential flags
    assert not is_potential_flag('', parser)
    assert not is_potential_flag('non-flag', parser)
    assert not is_potential_flag('--has space', parser)
    assert not is_potential_flag('-2', parser)

    # Potential flags
    assert is_potential_flag('-', parser)
    assert is_potential_flag('--', parser)
    assert is_potential_flag('-flag', parser)
    assert is_potential_flag('--flag', parser)


def test_complete_command_no_tokens(ac_app):
    from cmd2.argparse_completer import AutoCompleter

    parser = Cmd2ArgParser()
    ac = AutoCompleter(parser, ac_app)

    completions = ac.complete_command(tokens=[], text='', line='', begidx=0, endidx=0)
    assert not completions


def test_complete_command_help_no_tokens(ac_app):
    from cmd2.argparse_completer import AutoCompleter

    parser = Cmd2ArgParser()
    ac = AutoCompleter(parser, ac_app)

    completions = ac.complete_command_help(tokens=[], text='', line='', begidx=0, endidx=0)
    assert not completions