Ejemplo n.º 1
0
def test_rescue_nothing():
    """Ensures that rescue works for Nothing container."""
    def factory(arg) -> Maybe[int]:
        return Some(1)

    bound = Nothing.rescue(factory)
    bound2 = Nothing.rescue(lambda _: Some(1))

    assert bound == bound2
Ejemplo n.º 2
0
def test_is_compare():
    """Ensures that `is` operator works correctly."""
    some_container = Some(1)

    assert Nothing.bind(lambda state: state) is Nothing
    assert some_container.rescue(lambda: Some('fix')) is some_container
    assert some_container is not Some(1)
Ejemplo n.º 3
0
def test_bind_optional():
    """Ensures that bind_optional works correctly."""
    def factory(inner_value: int) -> Optional[int]:
        return inner_value if inner_value else None

    assert Some(1).bind_optional(factory) == Some(1)
    assert Some(0).bind_optional(factory) == Nothing
    assert Nothing.bind_optional(factory) == Nothing
Ejemplo n.º 4
0
def test_bind_nothing():
    """Ensures that left identity works for Nothing container."""
    def factory(inner_value) -> Maybe[int]:
        return Some(1)

    bound = Nothing.bind(factory)

    assert bound == Nothing
    assert str(bound) == '<Nothing>'
Ejemplo n.º 5
0
def _maybe_pipeline(number: int) -> Maybe[int]:
    first: int = Some(number).unwrap() if number else Nothing.unwrap()
    return Some(first + number)
Ejemplo n.º 6
0
def test_unwrap_failure():
    """Ensures that unwrap works for Nothing container."""
    with pytest.raises(UnwrapFailedError):
        assert Nothing.unwrap()
Ejemplo n.º 7
0
def test_nothing_value():
    """Ensures that value is fetch correctly from the Nothing."""
    assert Nothing.value_or(default_value=1) == 1
Ejemplo n.º 8
0
def test_equals():
    """Ensures that ``.equals`` method works correctly."""
    inner_value = 1

    assert Some(inner_value).equals(Some(inner_value))
    assert Nothing.equals(Nothing)
Ejemplo n.º 9
0
def test_not_equals():
    """Ensures that ``.equals`` method works correctly."""
    assert not Some(1).equals(Nothing)
    assert not Some(1).equals(Some(0))
    assert not Nothing.equals(Some(1))
Ejemplo n.º 10
0
def test_fix_nothing():
    """Ensures that fix works for Nothing container."""
    assert Nothing.fix(lambda _: None) == Nothing
    assert Nothing.fix(lambda _: 1) == Some(1)
Ejemplo n.º 11
0
def test_map_nothing():
    """Ensures that map works for Nothing container."""
    assert Nothing.map(str) == Nothing
Ejemplo n.º 12
0
def test_unwrap_failure_from_failure():
    """Ensures that failure works for Nothing container."""
    assert Nothing.failure() is None  # type: ignore
Ejemplo n.º 13
0
def test_unwrap_failure():
    """Ensures that unwrap works for Nothing container."""
    assert Nothing.failure() is None