示例#1
0
 def foo(x):
     check_argument(x,
                    'x',
                    expected_length=3,
                    assign_length_to_others=True,
                    handle_with=Warning)
     pass
示例#2
0
 def foo(x):
     check_argument(
         x,
         expected_choices=("first choice", "second choice"),
         handle_with=Warning,
     )
     pass
示例#3
0
def test_check_argument_length_warnings():
    assert check_argument(5,
                          'my_arg',
                          expected_length=1,
                          assign_length_to_others=True,
                          handle_with=Warning) is None
    assert check_argument(5,
                          expected_length=1,
                          assign_length_to_others=True,
                          handle_with=Warning) is None

    def foo(x):
        check_argument(x,
                       'x',
                       expected_length=3,
                       assign_length_to_others=True,
                       handle_with=Warning)
        pass

    assert foo([1, 2, 3]) is None
    with warnings.catch_warnings(record=True) as w:
        foo(1)
        assert 'length' in str(w[-1].message)

    def foo(x):
        check_argument(x,
                       expected_length=3,
                       assign_length_to_others=True,
                       handle_with=Warning)
        pass

    assert foo([1, 2, 3]) is None
    with warnings.catch_warnings(record=True) as w:
        foo(1)
        assert 'length' in str(w[-1].message)
示例#4
0
def test_check_argument_choices():
    assert check_argument(
        5,
        'my_arg',
        expected_choices=range(10),
    ) is None
    assert check_argument(5, expected_choices=range(10)) is None

    def foo(x):
        check_argument(x, expected_choices=('first choice', 'second choice'))
        pass

    assert foo('first choice') is None
    assert foo('second choice') is None
    with pytest.raises(ArgumentValueError, match='not among valid values'):
        foo('no choice')

    def foo(x):
        check_argument(x, 'x', expected_choices=('one', 'two'))
        pass

    assert foo('one') is None
    with pytest.raises(ArgumentValueError,
                       match="x's value, three, is not among valid values"):
        foo('three')

    def foo(x):
        check_argument(x, expected_choices=('one', 'two'))
        pass

    assert foo('one') is None
    with pytest.raises(
            ArgumentValueError,
            match="argument's value, three, is not among valid values"):
        foo('three')
示例#5
0
 def foo(x):
     check_argument(x,
                    'x',
                    expected_type=str,
                    handle_with=Warning,
                    message='Incorrect argument?')
     pass
示例#6
0
def test_check_argument_type_warning():
    def foo(x):
        check_argument(x,
                       'x',
                       expected_type=str,
                       handle_with=Warning,
                       message='Incorrect argument?')
        pass

    assert foo('one') is None
    with warnings.catch_warnings(record=True) as w:
        foo(4)
        assert 'Incorrect argument' in str(w[-1].message)

    assert check_argument(50, 'my_arg', expected_type=int,
                          handle_with=Warning) is None
    assert check_argument(50, expected_type=int, handle_with=Warning) is None

    with warnings.catch_warnings(record=True) as w:
        check_argument(50, 'my_arg', expected_type=str, handle_with=Warning)
        assert 'my_arg' in str(w[-1].message)
        assert 'Incorrect type' in str(w[-1].message)

    with warnings.catch_warnings(record=True) as w:
        check_argument('one', 'my_arg', expected_type=int, handle_with=Warning)
        assert 'my_arg' in str(w[-1].message)
        assert 'Incorrect type' in str(w[-1].message)

    with warnings.catch_warnings(record=True) as w:
        check_argument(50, expected_type=str, handle_with=Warning)
        assert 'Incorrect type of argument' in str(w[-1].message)

    with warnings.catch_warnings(record=True) as w:
        check_argument('one', expected_type=int, handle_with=Warning)
        assert 'Incorrect type of argument' in str(w[-1].message)
示例#7
0
 def foo(x):
     check_argument(x,
                    "x",
                    expected_type=int,
                    expected_length=3,
                    handle_with=Warning)
     pass
示例#8
0
 def foo(x):
     check_argument(
         x,
         "x",
         expected_type=str,
         handle_with=Warning,
         message="Incorrect argument?",
     )
     pass
示例#9
0
def test_check_argument_mix():
    def foo(x):
        check_argument(x, 'x', expected_type=int, condition=x % 2 == 0)
        pass

    with pytest.raises(TypeError):
        x = 'one'
        check_argument(argument=x,
                       argument_name='x',
                       expected_type=int,
                       condition=x % 2 == 0)
    with pytest.raises(TypeError):
        foo('one')
示例#10
0
def test_check_argument_mix():
    def foo(x):
        check_argument(x, "x", expected_type=int, condition=x % 2 == 0)
        pass

    with pytest.raises(TypeError):
        x = "one"
        check_argument(
            argument=x,
            argument_name="x",
            expected_type=int,
            condition=x % 2 == 0,
        )
    with pytest.raises(TypeError):
        foo("one")
示例#11
0
def test_check_argument_choices_warnings():
    assert check_argument(
        5, 'my_arg', expected_choices=range(10), handle_with=Warning) is None
    assert check_argument(5, expected_choices=range(10),
                          handle_with=Warning) is None

    def foo(x):
        check_argument(x,
                       expected_choices=('first choice', 'second choice'),
                       handle_with=Warning)
        pass

    assert foo('first choice') is None
    assert foo('second choice') is None
    with warnings.catch_warnings(record=True) as w:
        foo('no choice')
        assert 'no choice' in str(w[-1].message)
        assert 'not among valid values' in str(w[-1].message)
示例#12
0
def test_check_argument_length_warnings():
    assert (check_argument(
        5,
        "my_arg",
        expected_length=1,
        assign_length_to_others=True,
        handle_with=Warning,
    ) is None)
    assert (check_argument(
        5,
        expected_length=1,
        assign_length_to_others=True,
        handle_with=Warning,
    ) is None)

    def foo(x):
        check_argument(
            x,
            "x",
            expected_length=3,
            assign_length_to_others=True,
            handle_with=Warning,
        )
        pass

    assert foo([1, 2, 3]) is None
    with warnings.catch_warnings(record=True) as w:
        foo(1)
        assert "length" in str(w[-1].message)

    def foo(x):
        check_argument(
            x,
            expected_length=3,
            assign_length_to_others=True,
            handle_with=Warning,
        )
        pass

    assert foo([1, 2, 3]) is None
    with warnings.catch_warnings(record=True) as w:
        foo(1)
        assert "length" in str(w[-1].message)
示例#13
0
def test_check_argument_choices_warnings():
    assert (check_argument(
        5, "my_arg", expected_choices=range(10), handle_with=Warning) is None)
    assert (check_argument(5, expected_choices=range(10), handle_with=Warning)
            is None)

    def foo(x):
        check_argument(
            x,
            expected_choices=("first choice", "second choice"),
            handle_with=Warning,
        )
        pass

    assert foo("first choice") is None
    assert foo("second choice") is None
    with warnings.catch_warnings(record=True) as w:
        foo("no choice")
        assert "no choice" in str(w[-1].message)
        assert "not among valid values" in str(w[-1].message)
示例#14
0
def test_check_argument_length():
    assert check_argument(
        5, 'my_arg', expected_length=1, assign_length_to_others=True) is None
    assert check_argument(5, expected_length=1,
                          assign_length_to_others=True) is None

    def foo(x):
        check_argument(x, 'x', expected_length=3, assign_length_to_others=True)
        pass

    assert foo([1, 2, 3]) is None
    with pytest.raises(ArgumentValueError):
        foo(1)

    def foo(x):
        check_argument(x, expected_length=3, assign_length_to_others=True)
        pass

    assert foo([1, 2, 3]) is None
    with pytest.raises(ArgumentValueError):
        foo(1)

    def foo(big_x):
        check_argument(big_x,
                       'big_x',
                       expected_length=3,
                       assign_length_to_others=True)
        pass

    assert foo([1, 2, 3]) is None
    with pytest.raises(ArgumentValueError, match='big_x'):
        foo(1)

    def foo(big_x):
        check_argument(big_x, expected_length=3, assign_length_to_others=True)
        pass

    assert foo([1, 2, 3]) is None
    with pytest.raises(ArgumentValueError, match='argument'):
        foo(1)
示例#15
0
def test_check_argument_length():
    assert (check_argument(
        5, "my_arg", expected_length=1, assign_length_to_others=True) is None)
    assert (check_argument(5, expected_length=1, assign_length_to_others=True)
            is None)

    def foo(x):
        check_argument(x, "x", expected_length=3, assign_length_to_others=True)
        pass

    assert foo([1, 2, 3]) is None
    with pytest.raises(ArgumentValueError):
        foo(1)

    def foo(x):
        check_argument(x, expected_length=3, assign_length_to_others=True)
        pass

    assert foo([1, 2, 3]) is None
    with pytest.raises(ArgumentValueError):
        foo(1)

    def foo(big_x):
        check_argument(big_x,
                       "big_x",
                       expected_length=3,
                       assign_length_to_others=True)
        pass

    assert foo([1, 2, 3]) is None
    with pytest.raises(ArgumentValueError, match="big_x"):
        foo(1)

    def foo(big_x):
        check_argument(big_x, expected_length=3, assign_length_to_others=True)
        pass

    assert foo([1, 2, 3]) is None
    with pytest.raises(ArgumentValueError, match="argument"):
        foo(1)
示例#16
0
def test_check_argument_edge_cases():
    with pytest.raises(TypeError, match="required positional argument"):
        check_argument(Argument="x")

    msg = "check_argument() requires at least one condition to be checked"
    with pytest.raises(ValueError) as msg_error:
        check_argument(10)
    assert str(msg_error.value) == msg
    with pytest.raises(ValueError) as msg_error:
        check_argument(10, message="Error!")
    assert str(msg_error.value) == msg
    with pytest.raises(ValueError) as msg_error:
        check_argument("x", 10, handle_with=TypeError)
    assert str(msg_error.value) == msg
示例#17
0
def test_check_argument_edge_cases():
    with pytest.raises(TypeError, match='required positional argument'):
        check_argument(Argument='x')

    msg = 'check_argument() requires at least one condition to be checked'
    with pytest.raises(ValueError) as msg_error:
        check_argument(10)
    assert str(msg_error.value) == msg
    with pytest.raises(ValueError) as msg_error:
        check_argument(10, message='Error!')
    assert str(msg_error.value) == msg
    with pytest.raises(ValueError) as msg_error:
        check_argument('x', 10, handle_with=TypeError)
    assert str(msg_error.value) == msg
示例#18
0
def test_check_argument_type_warning():
    def foo(x):
        check_argument(
            x,
            "x",
            expected_type=str,
            handle_with=Warning,
            message="Incorrect argument?",
        )
        pass

    assert foo("one") is None
    with warnings.catch_warnings(record=True) as w:
        foo(4)
        assert "Incorrect argument" in str(w[-1].message)

    assert (check_argument(
        50, "my_arg", expected_type=int, handle_with=Warning) is None)
    assert check_argument(50, expected_type=int, handle_with=Warning) is None

    with warnings.catch_warnings(record=True) as w:
        check_argument(50, "my_arg", expected_type=str, handle_with=Warning)
        assert "my_arg" in str(w[-1].message)
        assert "Incorrect type" in str(w[-1].message)

    with warnings.catch_warnings(record=True) as w:
        check_argument("one", "my_arg", expected_type=int, handle_with=Warning)
        assert "my_arg" in str(w[-1].message)
        assert "Incorrect type" in str(w[-1].message)

    with warnings.catch_warnings(record=True) as w:
        check_argument(50, expected_type=str, handle_with=Warning)
        assert "Incorrect type of argument" in str(w[-1].message)

    with warnings.catch_warnings(record=True) as w:
        check_argument("one", expected_type=int, handle_with=Warning)
        assert "Incorrect type of argument" in str(w[-1].message)
示例#19
0
def test_check_argument_choices():
    assert (check_argument(
        5,
        "my_arg",
        expected_choices=range(10),
    ) is None)
    assert check_argument(5, expected_choices=range(10)) is None

    def foo(x):
        check_argument(x, expected_choices=("first choice", "second choice"))
        pass

    assert foo("first choice") is None
    assert foo("second choice") is None
    with pytest.raises(ArgumentValueError, match="not among valid values"):
        foo("no choice")

    def foo(x):
        check_argument(x, "x", expected_choices=("one", "two"))
        pass

    assert foo("one") is None
    with pytest.raises(ArgumentValueError,
                       match="x's value, three, is not among valid values"):
        foo("three")

    def foo(x):
        check_argument(x, expected_choices=("one", "two"))
        pass

    assert foo("one") is None
    with pytest.raises(
            ArgumentValueError,
            match="argument's value, three, is not among valid values",
    ):
        foo("three")
示例#20
0
 def foo(x):
     check_argument(x, expected_choices=('first choice', 'second choice'))
     pass
示例#21
0
 def foo(x):
     check_argument(x, expected_choices=('one', 'two'))
     pass
示例#22
0
 def foo(x):
     check_argument(x, expected_choices=("one", "two"))
     pass
示例#23
0
 def foo(x):
     check_argument(x, expected_choices=("first choice", "second choice"))
     pass
示例#24
0
 def foo(x):
     check_argument(x,
                    expected_choices=('first choice', 'second choice'),
                    handle_with=Warning)
     pass
示例#25
0
 def foo(x):
     check_argument(x, "x", expected_type=str)
     pass
示例#26
0
def test_check_argument_type():
    def foo(x):
        check_argument(x, "x", expected_type=str)
        pass

    assert foo("one") is None
    with pytest.raises(ArgumentValueError):
        foo(4)
    with pytest.raises(ArgumentValueError):
        foo(("one", "two"))

    assert check_argument(50, "my_arg", expected_type=int) is None
    assert check_argument(50, expected_type=int) is None
    assert check_argument("my_arg", "one", expected_type=str) is None
    assert check_argument("my_arg", expected_type=str) is None

    with pytest.raises(ArgumentValueError, match="my_arg"):
        check_argument(50, "my_arg", expected_type=str)
    with pytest.raises(ArgumentValueError, match="my_arg"):
        check_argument("one", "my_arg", expected_type=int)
    with pytest.raises(ArgumentValueError, match="argument"):
        check_argument(50, expected_type=str)
    with pytest.raises(ArgumentValueError, match="argument"):
        check_argument("one", expected_type=int)
示例#27
0
 def foo(x):
     check_argument(x, expected_length=3, assign_length_to_others=True)
     pass
示例#28
0
def test_check_argument_type():
    def foo(x):
        check_argument(x, 'x', expected_type=str)
        pass

    assert foo('one') is None
    with pytest.raises(ArgumentValueError):
        foo(4)
    with pytest.raises(ArgumentValueError):
        foo(('one', 'two'))

    assert check_argument(50, 'my_arg', expected_type=int) is None
    assert check_argument(50, expected_type=int) is None
    assert check_argument('my_arg', 'one', expected_type=str) is None
    assert check_argument('my_arg', expected_type=str) is None

    with pytest.raises(ArgumentValueError, match='my_arg'):
        check_argument(50, 'my_arg', expected_type=str)
    with pytest.raises(ArgumentValueError, match='my_arg'):
        check_argument('one', 'my_arg', expected_type=int)
    with pytest.raises(ArgumentValueError, match='argument'):
        check_argument(50, expected_type=str)
    with pytest.raises(ArgumentValueError, match='argument'):
        check_argument('one', expected_type=int)
示例#29
0
 def foo(big_x):
     check_argument(big_x,
                    "big_x",
                    expected_length=3,
                    assign_length_to_others=True)
     pass
示例#30
0
 def foo(x):
     check_argument(x, "x", expected_type=int, condition=x % 2 == 0)
     pass