Ejemplo n.º 1
0
def test_dynamic_deletion_inherited_alias():
    class Base(Namespace):
        base1 = Argument(type=int)
        base2 = base22 = Argument(type=str)
        base3 = Argument(type=int)

    class Args(Base):
        derived1 = Argument(type=float)
        derived2 = Argument(type=int)

    ns_remove(Args, 'base22')

    args = Parser(Args).parse(['6', '9', '1', '2.5', '7'])
    assert args.base1 == 6
    with pytest.raises(AttributeError):
        args.base22
    assert args.base2 == '9'
    assert args.base3 == 1
    assert args.derived1 == 2.5
    assert args.derived2 == 7

    args2 = Parser(Base).parse(['2', '3', '4'])
    assert args2.base1 == 2
    assert args2.base2 == '3'
    assert args2.base22 == '3'  # must NOT be deleted from base
    assert args2.base3 == 4
Ejemplo n.º 2
0
def test_multilevel_commands():
    class Cmd1_1(Namespace):
        arg1 = Argument(type=str)

    class Cmd2_1(Namespace):
        pass

    class Cmd2_2(Namespace):
        arg1 = Argument(type=int)
        arg2 = Argument(type=int)

    class Cmd1(Namespace):
        cmd = Commands({'sub': Cmd1_1})

    class Cmd2(Namespace):
        cmd = Commands({'sub1': Cmd2_1, 'sub2': Cmd2_2})

    class Args(Namespace):
        cmd = Commands({'cmd1': Cmd1, 'cmd2': Cmd2})

    parser = Parser(Args)

    args = parser.parse(['cmd2', 'sub2', '1', '2'])
    assert type(args.cmd) is Cmd2
    assert type(args.cmd.cmd) is Cmd2_2
    assert args.cmd.cmd.arg1 == 1
    assert args.cmd.cmd.arg2 == 2
Ejemplo n.º 3
0
def test_choices():
    class Args(Namespace):
        arg = Option(type=int, choices=[2, 3, 5])

    args = Parser(Args).parse(['--arg', '5'])
    assert args.arg == 5

    with pytest.raises(SystemExit):
        Parser(Args).parse(['--arg', '4'])
Ejemplo n.º 4
0
def test_default() -> Any:
    class Args(Namespace):
        arg = Option(type=int, default=5)
        arg2 = Option(type=int, nargs=2, default=[6, 7])

    args = Parser(Args).parse([])
    assert args.arg == 5
    assert args.arg2 == [6, 7]

    args = Parser(Args).parse(['--arg', '99'])
    assert args.arg == 99
Ejemplo n.º 5
0
def test_default():
    class Args(Namespace):
        arg = Argument(type=int, nargs='?', default=5)
        arg2 = Argument(type=int, nargs='*', default=[6, 7])

    args = Parser(Args).parse([])
    assert args.arg == 5
    assert args.arg2 == [6, 7]

    args = Parser(Args).parse(['9'])
    assert args.arg == 9
    assert args.arg2 == [6, 7]

    args = Parser(Args).parse(['9', '4'])
    assert args.arg == 9
    assert args.arg2 == [4]
Ejemplo n.º 6
0
def test_nargs_good():
    class Args(Namespace):
        single = Option(type=int)
        one = Option(type=int, nargs=1)
        three = Option(type=int, nargs=3)
        any = Option(type=int, nargs='*')
        any2 = Option(type=int, nargs='*')
        opt = Option(type=int, nargs='?')
        opt2 = Option(type=int, nargs='?')
        some = Option(type=int, nargs='+')

    args = Parser(Args).parse([
        '--single', '1',
        '--one', '2',
        '--three', '3', '4', '5',
        '--any',
        '--any2', '6', '7',
        '--opt',
        '--opt2', '8',
        '--some', '9', '10',
    ])  # yapf: disable

    assert args.single == 1
    assert args.one == [2]
    assert args.three == [3, 4, 5]
    assert args.any == []
    assert args.any2 == [6, 7]
    assert args.opt is None
    assert args.opt2 == 8
    assert args.some == [9, 10]
Ejemplo n.º 7
0
def test_required_good():
    class Args(Namespace):
        optional = Option(type=int)
        mandatory = Option(type=int, required=True)

    assert Args.optional.required is False
    assert Args.mandatory.required is True

    args = Parser(Args).parse(['--mandatory', '123'])
    assert args.optional is None
    assert isinstance(args.mandatory, int)
    assert args.mandatory == 123
Ejemplo n.º 8
0
def test_dynamic_deletion():
    class Args(Namespace):
        arg1 = Argument(type=int)
        arg2 = Argument(type=float)
        arg3 = Argument(type=str)

    ns_remove(Args, 'arg2')

    args = Parser(Args).parse(['6', 'some text'])
    assert args.arg1 == 6
    assert args.arg3 == 'some text'
    with pytest.raises(AttributeError):
        args.arg2
Ejemplo n.º 9
0
def test_nargs_optional_in_middle_exists():
    class Args(Namespace):
        req1 = Argument(type=int)
        opt2 = Argument(type=int, nargs='?')
        req3 = Argument(type=int)

    args = Parser(Args).parse([
        '2',
        '1',
    ])
    assert args.req1 == 2
    assert args.opt2 is None
    assert args.req3 == 1
Ejemplo n.º 10
0
def test_overlap_and_hide():
    class Base(Namespace):
        arg1 = Argument(type=int)
        arg2 = Argument(type=float)
        arg3 = Argument(type=str)

    class Args(Base):
        arg2 = None

    args = Parser(Args).parse(['6', '3'])
    assert args.arg1 == 6
    assert args.arg2 == None
    assert args.arg3 == '3'
Ejemplo n.º 11
0
def test_dynamic_addition():
    class AddCmd(Namespace):
        name = Argument(type=str)
        surname = Argument(type=str)
        age = Argument(type=int)

    class SearchCmd(Namespace):
        name = Option(type=str)
        surname = Option(type=str)
        age = Option(type=int)

    class DeleteCmd(Namespace):
        id = Argument(type=int)

    class Args(Namespace):
        db = Option(type=str, default='data.bin')
        cmd = Commands(
            {
                'add': AddCmd,
                'search': SearchCmd,
                'delete': DeleteCmd,
            },
            required=True)

    parser = Parser(Args)

    args = parser.parse(['--db', 'new.bin', 'search', '--name', 'Martin'])
    assert args.db == 'new.bin'
    assert type(args.cmd) is SearchCmd
    assert args.cmd.name == 'Martin'
    assert args.cmd.surname is None
    assert args.cmd.age is None

    args = parser.parse(['add', 'John', 'Smith', '23'])
    assert args.db == 'data.bin'
    assert type(args.cmd) is AddCmd
    assert args.cmd.name == 'John'
    assert args.cmd.surname == 'Smith'
    assert args.cmd.age == 23
Ejemplo n.º 12
0
def test_dynamic_addition():
    class Args(Namespace):
        arg1 = Argument(type=int)
        arg2 = Argument(type=float)
        arg3 = Argument(type=str)

    ns_add(Args, 'arg4', Argument(type=int))

    args = Parser(Args).parse(['6', '3.0', 'some text', '2'])
    assert args.arg1 == 6
    assert args.arg2 == 3.0
    assert args.arg3 == 'some text'
    assert args.arg4 == 2
Ejemplo n.º 13
0
def test_nargs_optional_in_middle_absent():
    class Args(Namespace):
        req1 = Argument(type=int)
        opt2 = Argument(type=int, nargs='?')
        req3 = Argument(type=int)

    args = Parser(Args).parse([
        '5',
        '1',
        '7',
    ])
    assert args.req1 == 5
    assert args.opt2 == 1
    assert args.req3 == 7
Ejemplo n.º 14
0
def test_dynamic_addition_in_base():
    class Base(Namespace):
        base1 = Argument(type=int)
        base2 = Argument(type=str)

    class Args(Base):
        derived1 = Argument(type=float)
        derived2 = Argument(type=int)

    ns_add(Base, 'base3', Argument(type=int))

    args = Parser(Args).parse(['6', 'some text', '1', '2.5', '7'])
    assert args.base1 == 6
    assert args.base2 == 'some text'
    assert args.base3 == 1
    assert args.derived1 == 2.5
    assert args.derived2 == 7
Ejemplo n.º 15
0
def test_inheritance():
    class Base(Namespace):
        arg1 = Argument(type=int)

    class Derived1(Base):
        arg2 = Argument(type=float)

    class Derived2(Base):
        arg3 = Argument(type=str)

    class Args(Derived1, Derived2):
        arg4 = Argument(type=int)

    args = Parser(Args).parse(['6', '3.0', '2', '7'])
    assert args.arg1 == 6
    assert args.arg2 == 3.0
    assert args.arg3 == '2'
    assert args.arg4 == 7
Ejemplo n.º 16
0
def test_dynamic_deletion_in_base():
    class Base(Namespace):
        base1 = Argument(type=int)
        base2 = Argument(type=str)
        base3 = Argument(type=int)

    class Args(Base):
        derived1 = Argument(type=float)
        derived2 = Argument(type=int)

    ns_remove(Base, 'base2')

    args = Parser(Args).parse(['6', '1', '2.5', '7'])
    assert args.base1 == 6
    with pytest.raises(AttributeError):
        args.base2
    assert args.base3 == 1
    assert args.derived1 == 2.5
    assert args.derived2 == 7
Ejemplo n.º 17
0
def test_multiple():
    class Args(Namespace):
        single = Option(type=int)
        multiple = Option(type=int, multiple=True)
        combo = Option(type=int, multiple=True, nargs=3)
        combo_single = Option(type=int, multiple=True, nargs=1)

    assert Args.single.multiple is False
    assert Args.multiple.multiple is True
    assert Args.combo.multiple is True

    args = Parser(Args).parse([
        '--single', '1',
        '--multiple', '2', '--multiple', '3',
        '--combo', '4', '5', '6', '--combo', '7', '8', '9',
        '--combo_single', '10', '--combo_single', '11',
    ])  # yapf: disable

    assert args.single == 1
    assert args.multiple == [2, 3]
    assert args.combo == [[4, 5, 6], [7, 8, 9]]
    assert args.combo_single == [[10], [11]]
Ejemplo n.º 18
0
def test_choices_bad():
    class Args(Namespace):
        arg = Argument(type=int, choices=[2, 3, 5])

    with pytest.raises(SystemExit):
        Parser(Args).parse(['--arg', '4'])
Ejemplo n.º 19
0
def test_choices_good():
    class Args(Namespace):
        arg = Argument(type=int, choices=[2, 3, 5])

    args = Parser(Args).parse(['5'])
    assert args.arg == 5
Ejemplo n.º 20
0
def test_required_missed():
    class Args(Namespace):
        mandatory = Option(type=int, required=True)

    with pytest.raises(SystemExit):
        Parser(Args).parse([])