示例#1
0
def test_wrap_message_prefix():
    with pytest.raises(CustomException) as exc_info:
        with exceptional.wrap(ValueError, CustomException, prefix="foo"):
            raise ValueError("bar")
    assert str(exc_info.value) == "foo: bar"

    with pytest.raises(CustomException) as exc_info:
        with exceptional.wrap(ValueError, CustomException, prefix="{}"):
            raise ValueError("bar")
    assert str(exc_info.value) == "{}: bar"
示例#2
0
def test_wrap_cause():
    with pytest.raises(CustomException) as exc_info:
        with exceptional.wrap(ValueError, CustomException):
            raise ValueError("foo")
    assert isinstance(exc_info.value.__cause__, ValueError)
    assert str(exc_info.value.__cause__) == "foo"

    with pytest.raises(CustomException) as exc_info:
        with exceptional.wrap(ValueError, CustomException, set_cause=False):
            raise ValueError("foo")
    assert exc_info.value.__cause__ is None
示例#3
0
def test_wrap_context():
    with pytest.raises(CustomException) as exc_info:
        with exceptional.wrap(ValueError, CustomException):
            raise ValueError("foo")
    assert isinstance(exc_info.value.__context__, ValueError)
    assert str(exc_info.value.__context__) == "foo"

    with pytest.raises(CustomException) as exc_info:
        with exceptional.wrap(ValueError, CustomException, suppress_context=True):
            raise ValueError
    assert isinstance(exc_info.value.__cause__, ValueError)
    assert isinstance(exc_info.value.__context__, ValueError)
    assert exc_info.value.__suppress_context__
示例#4
0
def test_wrap_repr():
    wrapper = exceptional.wrap(ValueError, CustomException)
    expected = "exceptional.wrap(ValueError, CustomException, ...)"
    assert str(wrapper) == repr(wrapper) == expected

    mapping = {}
    wrapper = exceptional.wrap(mapping)
    expected = "exceptional.wrap({}, ...)"
    assert str(wrapper) == repr(wrapper) == expected

    mapping = {ValueError: CustomException, IndexError: CustomExceptionTwo}
    wrapper = exceptional.wrap(mapping)
    expected = "exceptional.wrap({IndexError: CustomExceptionTwo, ...}, ...)"
    assert str(wrapper) == repr(wrapper) == expected
示例#5
0
def test_wrap_multiple_exceptions():
    wrapper = exceptional.wrap((KeyError, IndexError), CustomException)

    with pytest.raises(CustomException), wrapper:
        raise KeyError

    with pytest.raises(CustomException), wrapper:
        raise IndexError
示例#6
0
def test_wrap_message_propagated_by_default():
    """
    By default, the exception message is propagated as-is.
    """
    with pytest.raises(CustomException) as exc_info:
        with exceptional.wrap(ValueError, CustomException):
            raise ValueError("foo")
    assert str(exc_info.value) == "foo"
示例#7
0
def test_wrap_empty_message():
    """
    The replacement message can be empty.
    """
    with pytest.raises(CustomException) as exc_info:
        with exceptional.wrap(ValueError, CustomException, message=None):
            raise ValueError("foo")
    assert exc_info.value.args == ()
    assert str(exc_info.value) == ""
示例#8
0
def test_wrap_invalid_usage():
    with pytest.raises(TypeError) as exc_info:
        exceptional.wrap({KeyError: CustomException}, CustomException)
    expected = "cannot specify both a mapping and a replacement exception type"
    assert str(exc_info.value) == expected

    with pytest.raises(TypeError) as exc_info:
        exceptional.wrap(KeyError, 123)
    assert str(exc_info.value) == "not an exception class: 123"

    with pytest.raises(TypeError) as exc_info:
        exceptional.wrap(123, CustomException)
    assert str(exc_info.value) == "not an exception class (or tuple): 123"

    with pytest.raises(TypeError) as exc_info:
        exceptional.wrap(ValueError, CustomException, message="foo", format="bar")
    expected = "specify at most one of 'message', 'prefix', or 'format'"
    assert str(exc_info.value) == expected
示例#9
0
def test_wrap_multiple_exceptions_mapping():
    mapping = {
        KeyError: CustomException,
        LookupError: CustomExceptionTwo,
        (ValueError, TypeError): CustomExceptionThree,
    }
    wrapper = exceptional.wrap(mapping)

    with pytest.raises(CustomException), wrapper:
        raise KeyError

    with pytest.raises(CustomExceptionTwo), wrapper:
        assert issubclass(IndexError, LookupError)
        raise IndexError

    with pytest.raises(CustomExceptionThree), wrapper:
        raise ValueError

    with pytest.raises(CustomExceptionThree), wrapper:
        raise TypeError
示例#10
0
def test_wrap_not_handled():
    with pytest.raises(IndexError):
        with exceptional.wrap(ValueError, CustomException):
            raise IndexError
示例#11
0
def test_wrap_no_exception():
    with exceptional.wrap(ValueError, CustomException):
        pass
示例#12
0
def test_wrap():
    with pytest.raises(CustomException):
        with exceptional.wrap(ValueError, CustomException):
            raise ValueError
示例#13
0
def test_wrap_message_format():
    with pytest.raises(CustomException) as exc_info:
        with exceptional.wrap(ValueError, CustomException, format="oops ({})"):
            raise ValueError("foo")
    assert str(exc_info.value) == "oops (foo)"
示例#14
0
def test_wrap_message():
    with pytest.raises(CustomException) as exc_info:
        with exceptional.wrap(ValueError, CustomException, message="foo {}"):
            raise ValueError("bar")
    assert str(exc_info.value) == "foo {}"