()
| 5 | |
| 6 | |
| 7 | def test_lazy_choices(): |
| 8 | mock = Mock() |
| 9 | getter = mock.getter |
| 10 | getter.return_value = ['a', 'b', 'c'] |
| 11 | |
| 12 | parser = ArgumentParser() |
| 13 | parser.register('action', 'lazy_choices', LazyChoices) |
| 14 | parser.add_argument( |
| 15 | '--option', |
| 16 | help="the regular option", |
| 17 | default='a', |
| 18 | metavar='SYMBOL', |
| 19 | choices=['a', 'b'], |
| 20 | ) |
| 21 | parser.add_argument( |
| 22 | '--lazy-option', |
| 23 | help="the lazy option", |
| 24 | default='a', |
| 25 | metavar='SYMBOL', |
| 26 | action='lazy_choices', |
| 27 | getter=getter, |
| 28 | cache=False # for test purposes |
| 29 | ) |
| 30 | |
| 31 | # Parser initialization doesn't call it. |
| 32 | getter.assert_not_called() |
| 33 | |
| 34 | # If we don't use --lazy-option, we don't retrieve it. |
| 35 | parser.parse_args([]) |
| 36 | getter.assert_not_called() |
| 37 | |
| 38 | parser.parse_args(['--option', 'b']) |
| 39 | getter.assert_not_called() |
| 40 | |
| 41 | # If we pass a value, it will retrieve to verify. |
| 42 | parser.parse_args(['--lazy-option', 'c']) |
| 43 | getter.assert_called() |
| 44 | getter.reset_mock() |
| 45 | |
| 46 | with pytest.raises(SystemExit): |
| 47 | parser.parse_args(['--lazy-option', 'z']) |
| 48 | getter.assert_called() |
| 49 | getter.reset_mock() |
| 50 | |
| 51 | |
| 52 | def test_lazy_choices_help(): |
nothing calls this directly
no test coverage detected