Exemplo n.º 1
0
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)
Exemplo n.º 2
0
def test_flatten_context():
    """Ensures that `join` works with Context."""
    assert flatten(
        Context.unit(Context.unit(1)),
    )(Context.Empty) == 1
Exemplo n.º 3
0
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)
Exemplo n.º 4
0
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)
Exemplo n.º 5
0
# -*- 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]:
Exemplo n.º 6
0
def test_context_unit():
    """Ensures that ``unit`` method works correctly."""
    assert Context.unit(1)(Context.Empty) == 1
    assert Context[int].unit(2)(1) == 2