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
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)
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
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>'
def _maybe_pipeline(number: int) -> Maybe[int]: first: int = Some(number).unwrap() if number else Nothing.unwrap() return Some(first + number)
def test_unwrap_failure(): """Ensures that unwrap works for Nothing container.""" with pytest.raises(UnwrapFailedError): assert Nothing.unwrap()
def test_nothing_value(): """Ensures that value is fetch correctly from the Nothing.""" assert Nothing.value_or(default_value=1) == 1
def test_equals(): """Ensures that ``.equals`` method works correctly.""" inner_value = 1 assert Some(inner_value).equals(Some(inner_value)) assert Nothing.equals(Nothing)
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))
def test_fix_nothing(): """Ensures that fix works for Nothing container.""" assert Nothing.fix(lambda _: None) == Nothing assert Nothing.fix(lambda _: 1) == Some(1)
def test_map_nothing(): """Ensures that map works for Nothing container.""" assert Nothing.map(str) == Nothing
def test_unwrap_failure_from_failure(): """Ensures that failure works for Nothing container.""" assert Nothing.failure() is None # type: ignore
def test_unwrap_failure(): """Ensures that unwrap works for Nothing container.""" assert Nothing.failure() is None