File: test_raise_exception.py

package info (click to toggle)
python-returns 0.26.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,652 kB
  • sloc: python: 11,000; makefile: 18
file content (34 lines) | stat: -rw-r--r-- 877 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import pytest

from returns.functions import raise_exception
from returns.result import Failure, Success


class _CustomError(Exception):
    """Just for the test."""


@pytest.mark.parametrize(
    'exception_type',
    [
        TypeError,
        ValueError,
        _CustomError,
    ],
)
def test_raise_regular_exception(exception_type: type[Exception]):
    """Ensures that regular exception can be thrown."""
    with pytest.raises(exception_type):
        raise_exception(exception_type())


def test_failure_can_be_alted():
    """Ensures that exceptions can work with Failures."""
    failure = Failure(ValueError('Message'))
    with pytest.raises(ValueError, match='Message'):
        failure.alt(raise_exception)


def test_success_is_not_touched():
    """Ensures that exceptions can work with Success."""
    assert Success(1).alt(raise_exception) == Success(1)