MCPcopy Index your code
hub / github.com/python-cmd2/cmd2 / test_completion_items_as_choices

Function test_completion_items_as_choices

tests/test_argparse_utils.py:509–556  ·  view source on GitHub ↗

Test cmd2's patch to Argparse._check_value() which supports CompletionItems as choices. Choices are compared to CompletionItems.orig_value instead of the CompletionItem instance.

(capsys)

Source from the content-addressed store, hash-verified

507
508
509def test_completion_items_as_choices(capsys) -> None:
510 """Test cmd2's patch to Argparse._check_value() which supports CompletionItems as choices.
511 Choices are compared to CompletionItems.orig_value instead of the CompletionItem instance.
512 """
513
514 ##############################################################
515 # Test CompletionItems with str values
516 ##############################################################
517 choices = Choices.from_values(["1", "2"])
518 parser = Cmd2ArgumentParser()
519 parser.add_argument("choices_arg", type=str, choices=choices)
520
521 # First test valid choices. Confirm the parsed data matches the correct type of str.
522 args = parser.parse_args(["1"])
523 assert args.choices_arg == "1"
524
525 args = parser.parse_args(["2"])
526 assert args.choices_arg == "2"
527
528 # Next test invalid choice
529 with pytest.raises(SystemExit):
530 args = parser.parse_args(["3"])
531
532 # Confirm error text contains correct value type of str
533 _out, err = capsys.readouterr()
534 assert "invalid choice: '3' (choose from '1', '2')" in err
535
536 ##############################################################
537 # Test CompletionItems with int values
538 ##############################################################
539 choices = Choices.from_values([1, 2])
540 parser = Cmd2ArgumentParser()
541 parser.add_argument("choices_arg", type=int, choices=choices)
542
543 # First test valid choices. Confirm the parsed data matches the correct type of int.
544 args = parser.parse_args(["1"])
545 assert args.choices_arg == 1
546
547 args = parser.parse_args(["2"])
548 assert args.choices_arg == 2
549
550 # Next test invalid choice
551 with pytest.raises(SystemExit):
552 args = parser.parse_args(["3"])
553
554 # Confirm error text contains correct value type of int
555 _out, err = capsys.readouterr()
556 assert "invalid choice: 3 (choose from 1, 2)" in err
557
558
559def test_update_prog() -> None:

Callers

nothing calls this directly

Calls 2

Cmd2ArgumentParserClass · 0.90
from_valuesMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…