|
| 1 | +"""Tests of additional argument types""" |
| 2 | + |
| 3 | +import argparse |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | +import cmd2 |
| 8 | +from cmd2.argtypes import IntSet, Range, hexadecimal, integer |
| 9 | + |
| 10 | +from .conftest import ( |
| 11 | + run_cmd, |
| 12 | +) |
| 13 | + |
| 14 | + |
| 15 | +class CustomTypesApp(cmd2.Cmd): |
| 16 | + def __init__(self) -> None: |
| 17 | + cmd2.Cmd.__init__(self) |
| 18 | + |
| 19 | + def namespace_provider(self) -> argparse.Namespace: |
| 20 | + ns = argparse.Namespace() |
| 21 | + ns.custom_stuff = "custom" |
| 22 | + return ns |
| 23 | + |
| 24 | + @staticmethod |
| 25 | + def _test_parser_builder() -> cmd2.Cmd2ArgumentParser: |
| 26 | + test_parser = cmd2.Cmd2ArgumentParser() |
| 27 | + test_parser.add_argument('--int', dest='intarg', type=integer, help='IntegerHelp') |
| 28 | + test_parser.add_argument('--hex', dest='hexarg', type=hexadecimal, help='HexHelp') |
| 29 | + test_parser.add_argument('--range', dest='rangearg', type=Range(10), help='RangeHelp') |
| 30 | + test_parser.add_argument('--set', dest='setarg', type=IntSet(5), help='SetHelp') |
| 31 | + test_parser.add_argument('--highset', dest='setarg', type=IntSet(5, 10), help='SetHelp') |
| 32 | + return test_parser |
| 33 | + |
| 34 | + @cmd2.with_argparser(_test_parser_builder) |
| 35 | + def do_test(self, args, *, keyword_arg: str | None = None) -> None: |
| 36 | + """Test custom types |
| 37 | + :param args: argparse namespace |
| 38 | + :param keyword_arg: Optional keyword arguments |
| 39 | + """ |
| 40 | + if args.intarg is not None: |
| 41 | + self.stdout.write(f"Integer {args.intarg}") |
| 42 | + if args.hexarg is not None: |
| 43 | + print(f"Hex {args.hexarg}") |
| 44 | + if args.rangearg is not None: |
| 45 | + print(f"Range {args.rangearg}") |
| 46 | + if args.setarg is not None: |
| 47 | + print(f"Set {list(args.setarg)}") |
| 48 | + |
| 49 | + |
| 50 | +@pytest.fixture |
| 51 | +def custom_types_app(): |
| 52 | + return CustomTypesApp() |
| 53 | + |
| 54 | + |
| 55 | +def test_int_basic(custom_types_app) -> None: |
| 56 | + out, _err = run_cmd(custom_types_app, 'test --int 5') |
| 57 | + assert out == ['Integer 5'] |
| 58 | + |
| 59 | + |
| 60 | +def test_int_hex(custom_types_app) -> None: |
| 61 | + out, _err = run_cmd(custom_types_app, 'test --int 0xf') |
| 62 | + assert out == ['Integer 15'] |
| 63 | + |
| 64 | + |
| 65 | +def test_int_bin(custom_types_app) -> None: |
| 66 | + out, _err = run_cmd(custom_types_app, 'test --int 0b1000_0000') |
| 67 | + assert out == ['Integer 128'] |
| 68 | + |
| 69 | + |
| 70 | +def test_int_si_suffix(custom_types_app) -> None: |
| 71 | + out, _err = run_cmd(custom_types_app, 'test --int 5M') |
| 72 | + assert out == ['Integer 5000000'] |
| 73 | + |
| 74 | + |
| 75 | +def test_int_iec_suffix(custom_types_app) -> None: |
| 76 | + out, _err = run_cmd(custom_types_app, 'test --int 10Ki') |
| 77 | + assert out == ['Integer 10240'] |
| 78 | + |
| 79 | + |
| 80 | +def test_int_failure(custom_types_app) -> None: |
| 81 | + _out, err = run_cmd(custom_types_app, 'test --int 5bob') |
| 82 | + assert err[-1:] == ["Error: argument --int: invalid integer value: '5bob'"] |
| 83 | + |
| 84 | + |
| 85 | +def test_hex_basic(custom_types_app) -> None: |
| 86 | + out, _err = run_cmd(custom_types_app, 'test --hex 10') |
| 87 | + assert out == ['Hex 16'] |
| 88 | + |
| 89 | + |
| 90 | +def test_hex_prefixed(custom_types_app) -> None: |
| 91 | + out, _err = run_cmd(custom_types_app, 'test --hex 0x10') |
| 92 | + assert out == ['Hex 16'] |
| 93 | + |
| 94 | + |
| 95 | +def test_hex_failure(custom_types_app) -> None: |
| 96 | + _out, err = run_cmd(custom_types_app, 'test --hex 5bob') |
| 97 | + assert err[-1:] == ["Error: argument --hex: invalid hexadecimal value: '5bob'"] |
| 98 | + |
| 99 | + |
| 100 | +def test_range(custom_types_app) -> None: |
| 101 | + out, _err = run_cmd(custom_types_app, 'test --range 4') |
| 102 | + assert out == ['Range 4'] |
| 103 | + |
| 104 | + |
| 105 | +def test_range_outside(custom_types_app) -> None: |
| 106 | + _out, err = run_cmd(custom_types_app, 'test --range 10') |
| 107 | + assert err[-1:] == ["Error: argument --range: invalid Range[0..9] value: '10'"] |
| 108 | + |
| 109 | + |
| 110 | +def test_intset_single(custom_types_app) -> None: |
| 111 | + out, _err = run_cmd(custom_types_app, 'test --set 4') |
| 112 | + assert out == ['Set [4]'] |
| 113 | + |
| 114 | + |
| 115 | +def test_intset_single_out_of_range(custom_types_app) -> None: |
| 116 | + _out, err = run_cmd(custom_types_app, 'test --set 5') |
| 117 | + assert err[-1:] == ["Error: argument --set: invalid IntSet[0..4] value: '5'"] |
| 118 | + |
| 119 | + |
| 120 | +def test_intset_high_multi(custom_types_app) -> None: |
| 121 | + out, _err = run_cmd(custom_types_app, 'test --highset 5,7-9') |
| 122 | + print(_err) |
| 123 | + print(out) |
| 124 | + assert out == ['Set [5, 7, 8, 9]'] |
| 125 | + |
| 126 | + |
| 127 | +def test_intset_high_all(custom_types_app) -> None: |
| 128 | + out, _err = run_cmd(custom_types_app, 'test --highset all') |
| 129 | + assert out == ['Set [5, 6, 7, 8, 9]'] |
0 commit comments