Пример #1
0
from haskpy.testing import make_test_class
from haskpy import Dictionary

TestDictionary = make_test_class(Dictionary)


def test_dictionary_apply():
    """Test Dictionary.apply

    Laws don't guarantee a specific behavior, only consistent, so let's check
    that the output is what we want.

    """
    fs = Dictionary(
        foo=lambda x: x + 1,
        bar=lambda x: 2 * x,
    )
    xs = Dictionary(
        bar=42,
        baz=666,
    )
    assert (fs @ xs) == Dictionary(bar=84)
    return


def test_dictionary_eq():
    """Test Dictionary.eq

    Laws don't guarantee a specific behavior, only consistent, so let's check
    that the output is what we want.
Пример #2
0
from haskpy import Maybe, Just, Nothing, MaybeT, List, Function, Compose, Either
from haskpy.testing import make_test_class


# Test typeclass laws for Maybe
TestMaybe = make_test_class(Maybe)


def test_maybe_match():
    """Make sure the given value is actually stored"""
    assert Just(42).match(Nothing=lambda: 666, Just=lambda x: x) == 42
    assert Nothing.match(Nothing=lambda: 666, Just=lambda x: x) == 666
    return


def test_maybe_map():
    """Make sure the originally given value isn't kept constant"""
    assert Just(42).map(lambda x: x + 1) == Just(43)
    return


def test_maybe_foldl():
    """Make sure the folding is done as we expect"""
    assert Just("foo").foldl(lambda x: lambda y: x + y, "bar") == "barfoo"
    return


# Test typeclass laws for MaybeT monad transformer (using some example monad).
TestMaybeT = make_test_class(MaybeT(Either))
TestMaybeT = make_test_class(MaybeT(Function))
TestMaybeT = make_test_class(MaybeT(List))
Пример #3
0
from haskpy.types.list import List
from haskpy.testing import make_test_class

# Test typeclass laws for List
TestList = make_test_class(List)


def test_list_map():
    # Test that the list elements are correctly modified. Just obeying the laws
    # doesn't force that because, for instance, keeping values as constant
    # would be a trivial solution but not what we want here.
    assert List(1, 2, 3).map(lambda x: x * 10) == List(10, 20, 30)
    return


def test_list_apply():
    """Test apply puts values in desired order

    Again, the laws don't force the ordering but we want them in particular
    order.

    """
    assert List(1, 2).apply(List(lambda x: x+10, lambda x: x+100)) == \
        List(11, 12, 101, 102)


def test_list_foldr():
    # Foldable laws are tested already, but let's check that folding also does
    # what we expect, because the laws can be satisfied by some trivial
    # solutions too (e.g., foldr/foldl returns initial). There's nothing wrong
    # in such solutions, we just don't want those for our List. Also, these
Пример #4
0
from haskpy.types.either import Either, Left, Right
from haskpy.testing import make_test_class

# Test typeclass laws for Either
TestEither = make_test_class(Either)


def test_either_match():
    """Make sure the given value is actually stored"""
    assert Left(42).match(Left=lambda x: x - 1, Right=lambda x: 666) == 41
    assert Right(42).match(Left=lambda x: 666, Right=lambda x: x + 1) == 43
    return


def test_either_map():
    """Make sure the originally given value isn't kept constant"""
    assert Right(42).map(lambda x: x + 1) == Right(43)
    return
Пример #5
0
from haskpy import Identity, IdentityT, Function, List, Either
from haskpy.testing import make_test_class

# Test typeclass laws for Identity
TestIdentity = make_test_class(Identity)


def test_identity_map():
    """Make sure the originally given value isn't kept constant"""
    assert Identity(42).map(lambda x: x + 1) == Identity(43)
    return


TestIdentityT = make_test_class(IdentityT(Either))
TestIdentityT = make_test_class(IdentityT(List))
TestIdentityT = make_test_class(IdentityT(Function))
Пример #6
0
from hypothesis import given
import hypothesis.strategies as st

from haskpy.testing import make_test_class
from haskpy import Sum, All, Any, String, Endo, Function
from haskpy import testing

TestSum = make_test_class(Sum)

TestAll = make_test_class(All)

TestAny = make_test_class(Any)

TestString = make_test_class(String)

TestEndo = make_test_class(Endo)


@given(st.data())
def test_function_type_in_endo(data):
    """Test functions as the input/output type of endo functions

    This needs to be written manually because functions aren't hashable so we
    cannot use them as the input type of functions automatically.

    Endofunction has type ``a -> a``. This test will use ``(a -> b) -> (a ->
    b)``.

    """
    def scale_output(f):
        def scaled(x):
Пример #7
0
from haskpy import Function, FunctionMonoid, Sum
from haskpy.testing import make_test_class

TestFunction = make_test_class(Function)

# Testing the Monoid instance requires some Monoid type. Otherwise, only
# Semigroup laws can be tested.
TestFunctionMonoid = make_test_class(FunctionMonoid(Sum))


def test_function_nesting():
    @Function
    def f(a, b, c):
        return a + b + c

    g1 = f("a")
    h1 = g1("b")
    assert h1("c") == "abc"
    g2 = Function(f("a"))
    h2 = Function(g2("b"))
    assert h2("c") == "abc"


def test_function_composition():
    assert (Function(lambda x: 10 * x)**Function(lambda x: x + 1)**Function(
        lambda x: 2 * x))(3) == 70
Пример #8
0
from haskpy import UncurriedFunction, UncurriedFunctionMonoid, Sum
from haskpy.testing import make_test_class

TestUncurriedFunction = make_test_class(UncurriedFunction)

# Testing the Monoid instance requires some Monoid type. Otherwise, only
# Semigroup laws can be tested.
TestUncurriedFunctionMonoid = make_test_class(UncurriedFunctionMonoid(Sum))


def test_uncurried_function_apply():
    @UncurriedFunction
    def f(a, *_, b=42):
        return a + b

    @UncurriedFunction
    def g(a, *_, b=1):
        return lambda y: (a + b) * y

    assert (g @ f)(2) == 44 * 3
    assert (g @ f)(2, b=5) == 7 * 7

    return
Пример #9
0
from haskpy.testing import make_test_class
from haskpy import Compose, Maybe, Identity, Just

MaybeIdentity = Compose(Maybe, Identity)

TestMaybeIdentity = make_test_class(MaybeIdentity)


def test_maybe_identity_pure():
    assert MaybeIdentity.pure(42).decomposed == Maybe.pure(Identity.pure(42))
    return


def test_maybe_identity_apply():
    x = MaybeIdentity(Just(Identity(42)))
    f = MaybeIdentity(Just(Identity(lambda x: x + 1)))
    assert x.apply(f).decomposed == Just(Identity(43))
    return