def foo(x): check_argument(x, 'x', expected_length=3, assign_length_to_others=True, handle_with=Warning) pass
def foo(x): check_argument( x, expected_choices=("first choice", "second choice"), handle_with=Warning, ) pass
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)
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')
def foo(x): check_argument(x, 'x', expected_type=str, handle_with=Warning, message='Incorrect argument?') pass
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)
def foo(x): check_argument(x, "x", expected_type=int, expected_length=3, handle_with=Warning) pass
def foo(x): check_argument( x, "x", expected_type=str, handle_with=Warning, message="Incorrect argument?", ) pass
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')
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")
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)
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)
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)
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)
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)
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
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
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)
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")
def foo(x): check_argument(x, expected_choices=('first choice', 'second choice')) pass
def foo(x): check_argument(x, expected_choices=('one', 'two')) pass
def foo(x): check_argument(x, expected_choices=("one", "two")) pass
def foo(x): check_argument(x, expected_choices=("first choice", "second choice")) pass
def foo(x): check_argument(x, expected_choices=('first choice', 'second choice'), handle_with=Warning) pass
def foo(x): check_argument(x, "x", expected_type=str) pass
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)
def foo(x): check_argument(x, expected_length=3, assign_length_to_others=True) pass
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)
def foo(big_x): check_argument(big_x, "big_x", expected_length=3, assign_length_to_others=True) pass
def foo(x): check_argument(x, "x", expected_type=int, condition=x % 2 == 0) pass