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_context_immutable_deepcopy(): """Ensures that Context returns it self when passed to deepcopy function.""" context: Context = Context() assert context is deepcopy(context)
def test_context_immutable(): """Ensures that Context is immutable.""" with pytest.raises(ImmutableStateError): Context().abc = 1
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_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 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_immutable(): """Ensures that RequiresContext container supports ``.map()`` method.""" with pytest.raises(ImmutableStateError): Context().abc = 1
def test_context_unit(): """Ensures that ``unit`` method works correctly.""" assert Context.unit(1)(Context.Empty) == 1 assert Context[int].unit(2)(1) == 2