Esempio 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)
Esempio n. 2
0
def test_flatten_context():
    """Ensures that `join` works with Context."""
    assert flatten(
        Context.unit(Context.unit(1)),
    )(Context.Empty) == 1
Esempio 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)
Esempio 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)
Esempio 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]:
Esempio 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