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)
| 507 | |
| 508 | |
| 509 | def 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 | |
| 559 | def test_update_prog() -> None: |
nothing calls this directly
no test coverage detected
searching dependent graphs…