def _squash_context(*args): """ Unwraps ``RequiresContext`` values, merges them into tuple, and wraps back. .. code:: python >>> from returns.context import RequiresContext >>> from returns.converters import squash_context >>> assert squash_context( ... RequiresContext.from_value(1), ... RequiresContext.from_value('a'), ... RequiresContext.from_value(True), ... )(...) == RequiresContext.from_value((1, 'a', True))(...) See :func:`returns.converters.squash_io` for more docs. """ return RequiresContext(lambda deps: tuple(func(deps) for func in args))
(Some(Some([])), Some([])), # Nope: (Failure(Failure('a')), Failure(Failure('a'))), (IOFailure(IOFailure('a')), IOFailure(IOFailure('a'))), ]) def test_flatten(container, merged): """Ensures that `flatten` is always returning the correct type.""" assert flatten(container) == merged @pytest.mark.parametrize(('container', 'merged'), [ ( RequiresContextResult.from_success( RequiresContextResult.from_success(1), ), RequiresContextResult.from_success(1), ), ( RequiresContextIOResult.from_success( RequiresContextIOResult.from_success(1), ), RequiresContextIOResult.from_success(1), ), ( RequiresContext.from_value(RequiresContext.from_value(1)), RequiresContext.from_value(1), ), ]) def test_flatten_context(container, merged): """Ensures that `flatten` is always returning the correct type.""" assert flatten(container)(...) == merged(...)
def test_bind_with_context(): """Ensures that functions can be composed and return type is correct.""" binded = bind(_context_function) assert binded(RequiresContext(lambda _: 3))(5) == 8
def _context_function(argument: int) -> RequiresContext[int, int]: return RequiresContext(lambda other: argument + other)
def factory(inner_value: int) -> RequiresContext[float, int]: return RequiresContext(lambda deps: inner_value / deps)
from copy import copy, deepcopy import pytest from returns.context import Context, RequiresContext from returns.primitives.exceptions import ImmutableStateError from returns.primitives.interfaces import Bindable, Instanceable, Mappable @pytest.mark.parametrize('container', [ RequiresContext(lambda deps: deps), RequiresContext.from_value(1), Context.ask(), ]) @pytest.mark.parametrize('protocol', [ Bindable, Mappable, Instanceable, ]) def test_protocols(container, protocol): """Ensures that RequiresContext has all the right protocols.""" assert isinstance(container, protocol) def test_context_immutable(): """Ensures that Context is immutable.""" with pytest.raises(ImmutableStateError): Context().abc = 1 def test_context_immutable_copy():
def test_requires_context_from_value(): """Ensures that ``from_value`` method works correctly.""" assert RequiresContext.from_value(1)(RequiresContext.empty) == 1 assert RequiresContext.from_value(2)(1) == 2
def test_nonequality(): """Ensures that containers can be compared.""" assert RequiresContext(_same_function) != RequiresContext(str) assert Context[int].unit(1) != Context[int].unit(1) assert Context.unit(1) != Context[int].unit(1) assert Context.unit(1) != Context.unit(1)
def test_equality(): """Ensures that containers can be compared.""" assert RequiresContext(_same_function) == RequiresContext(_same_function) assert Context[int].ask() == Context[int].ask() assert Context[int].ask() == Context.ask() assert Context.ask() == Context.ask()
def factory(number: float) -> RequiresContext[int, str]: return RequiresContext(lambda deps: str(number + deps))
# -*- coding: utf-8 -*- import pytest from returns.context import Context, RequiresContext from returns.primitives.container import Bindable, Mappable @pytest.mark.parametrize('container', [ RequiresContext(lambda deps: deps), Context.unit(1), Context.ask(), ]) @pytest.mark.parametrize('protocol', [ Bindable, Mappable, ]) def test_protocols(container, protocol): """Ensures that RequiresContext has all the right protocols.""" assert isinstance(container, protocol) def test_context_map(): """Ensures that RequiresContext container supports ``.map()`` method.""" context: RequiresContext[int, str] = Context[int].unit(1.0, ).map(str, ) assert context(3) == Context.unit('1.0')(Context.Empty) def test_context_bind(): """Ensures that RequiresContext container supports ``.bind()`` method.""" def factory(number: float) -> RequiresContext[int, str]:
def test_nonequality(): """Ensures that containers can be compared.""" assert RequiresContext(_same_function) != RequiresContext(str) assert RequiresContext.from_value(1) != RequiresContext.from_value(1)
def test_apply_with_context(): """Ensures that functions can be composed and return type is correct.""" applied = apply(RequiresContext.from_value(_function)) assert applied(RequiresContext.from_value(1))(...) == '2'
def test_requires_context_immutable(): """Ensures that Context is immutable.""" with pytest.raises(ImmutableStateError): RequiresContext.from_value(1).abc = 1
def test_requires_context_immutable_deepcopy(): """Ensures that Context returns it self when passed to deepcopy function.""" context = RequiresContext.from_value(1) assert context is deepcopy(context)