mirror of
https://github.com/correl/typesafe-monads.git
synced 2024-11-28 11:09:58 +00:00
35 lines
919 B
Python
35 lines
919 B
Python
import pytest # type: ignore
|
|
from typing import Any, Callable, Type
|
|
from monads.monoid import Monoid
|
|
from monads.maybe import First, Last, Just
|
|
|
|
Constructor = Callable[[Any], Monoid]
|
|
|
|
|
|
@pytest.fixture(
|
|
scope="module", params=[lambda x: First(Just(x)), lambda x: Last(Just(x))]
|
|
)
|
|
def monoid(request) -> Constructor:
|
|
return request.param
|
|
|
|
|
|
def test_associative(monoid: Constructor) -> None:
|
|
a: Monoid = monoid(1)
|
|
b: Monoid = monoid(2)
|
|
c: Monoid = monoid(3)
|
|
|
|
assert (a + b) + c == a + (b + c)
|
|
|
|
|
|
def test_mconcat_empty(monoid: Constructor) -> None:
|
|
cls: Type = type(monoid(1))
|
|
zero: Monoid = cls.mzero()
|
|
assert zero == cls.mconcat([])
|
|
|
|
def test_mconcat(monoid: Constructor) -> None:
|
|
cls: Type = type(monoid(1))
|
|
a: Monoid = monoid(1)
|
|
b: Monoid = monoid(2)
|
|
c: Monoid = monoid(3)
|
|
expected: Monoid = a.mappend(b).mappend(c)
|
|
assert expected == cls.mconcat([a, b, c])
|