def test_context_bind(): """Ensures that RequiresContext container supports ``.bind()`` method.""" def factory(number: float) -> RequiresContext[int, str]: return RequiresContext(lambda deps: str(number + deps)) context: RequiresContext[int, str] = Context[int].unit(1.0, ).bind(factory, ) assert context(3) == Context.unit('4.0')(Context.Empty)
def test_flatten_context(): """Ensures that `join` works with Context.""" assert flatten( Context.unit(Context.unit(1)), )(Context.Empty) == 1
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_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)
# -*- 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_context_unit(): """Ensures that ``unit`` method works correctly.""" assert Context.unit(1)(Context.Empty) == 1 assert Context[int].unit(2)(1) == 2