def test_List_add_other_iters(): control = List(1, 2, 3, 4) m = List(1, 2, 3) n = iter([4]) o = (4, ) p = {4} assert all(control == (m + x) for x in [n, o, p])
def test_build_chessboard(): # shamelessly stolen from: # https://github.com/dustingetz/pymonads/blob/master/list.py ranks = 'abcdefg' files = range(1, 9) l = List(*ranks) >> (lambda r: List(*files) >> (lambda f: List.unit( (r, f)))) hardway = tuple((r, f) for r in ranks for f in files) sliced = List(('a', 1), ('a', 2), ('a', 3)) assert l.v == hardway assert l[:3] == sliced
def test_List_add_raises_with_mapping(): l = List() with pytest.raises(TypeError) as error: l + {'a': 10} assert 'dict' in str(error.value)
def test_List_add_raises_with_str(): l = List() with pytest.raises(TypeError) as error: l + 'a' assert 'str' in str(error.value)
def test_List_len(): assert len(List(1, 2, 3)) == 3
from pynads import Writer, Mempty, List from pynads.funcs import identity, multibind import pytest add_two = lambda x: x + 2 m_add_two = lambda x: Writer(x + 2, List.unit('added two')) str_add_two = lambda x: Writer(add_two(x), ' added two') int_add_two = lambda x: Writer(add_two(x), 4) dict_add_two = lambda x: Writer(add_two(x), {'added': 2}) dict_div_two = lambda x: Writer(x // 2, {'divided': 2}) def test_writer_unit(): w = Writer.unit(2) assert w.v == (2, Mempty) def test_fmap_id(): w = Writer.unit(2).fmap(identity) assert w.v == (2, Mempty) def test_writer_fmap(): w = Writer.unit(2).fmap(add_two) assert w.v == (4, Mempty) def test_writer_apply(): w = (Writer.unit(add_two)) * (Writer.unit(2)) assert w.v == (4, Mempty)
def test_sequence(): justs = [Just(x) for x in range(5)] assert lifted.sequence(*justs) == Just(List(0, 1, 2, 3, 4)) assert lifted.sequence(Nothing, *justs) is Nothing
def test_cons(): assert lifted.cons(1, List(2, 3)) == List(1, 2, 3) assert lifted.cons(1, lifted.cons(2, lifted.cons(3, lifted.cons( 4, List())))) == List(1, 2, 3, 4)
@pytest.mark.parametrize('monoid, generic', [('', str), ([], Sequence), ({}, Mapping)]) def test_make_generic_mconcat_on_selected(monoid, generic): assert m._make_generic_mconcat(monoid) is m._generic_mconcats[generic] @pytest.mark.parametrize( 'objs, expected', [(ints, 6), (list_of(float, ints), 6.0), (list_of(complex, ints), 6 + 0j), (list_of(Decimal, ints), Decimal(6)), (list_of(lambda a: {a}, ints), set(ints)), (list_of(lambda a: frozenset([a]), ints), frozenset(ints)), (list_of(lambda a: [a], ints), list(ints)), (list_of(str, ints), '123'), (list_of(lambda a: {a: a}, ints), { 1: 1, 2: 2, 3: 3 }), (list_of(lambda a: (a, ), ints), list(ints)), ([True, False, True], True), ([False, False, False], False)]) def test_generic_mconcat(objs, expected): assert m.generic_mconcat(*objs) == expected @pytest.mark.parametrize('obj, is_monoidal', [(Decimal(1), True), (List(), True), (1, True), ([], True), (object(), False), (Map(), True), (DummyMonoid(), True), (True, True), (WeakSet(), False)]) def test_is_monoid(obj, is_monoidal): assert m.is_monoid(obj) == is_monoidal
def test_mappend(): m = List(1) n = List(2) assert monoid.mappend(m, n) == List(1,2)
def test_List_multi_apply(): ls = [List(*x) for x in ([1, 2, 3], [1, 2, 3])] l = multiapply(List(plus_other), *ls) assert l.v == (2, 3, 4, 3, 4, 5, 4, 5, 6)
def test_List_reversed(): assert reversed(List(1, 2, 3)) == List(3, 2, 1)
def test_List_count(): assert List(1, 1, 3).count(1) == 2
def test_List_index(): assert List(1, 2, 3).index(3) == 2
def test_List_iadd(): l1 = l2 = List.unit(1) l1 += List.unit(2) assert l1 == List(1, 2) assert l1 is not l2
def test_List_bool(): assert List(1, 2) assert not List()
def test_List_apply_two_funcs(): l = List(add_two, add_two) * List(1, 2, 3) assert l.v == (3, 4, 5, 3, 4, 5)
def test_List_monoidal_add(): l = List.unit(1) assert l + (List.unit(2)) == List(1, 2) assert l + (2, ) == List(1, 2)
def test_mempty_with_monoid(): assert monoid.mempty(List) == List()
def test_List_mconcat(): ls = [List.unit(x) for x in range(4)] final = List.mconcat(*ls) assert final == List(0, 1, 2, 3)
def test_mconcat(): ls = [List(x) for x in range(4)] assert monoid.mconcat(*ls) == List(0,1,2,3)
def test_List_is_monoidal(): ls = [List.unit(x) for x in [1, 2, 3]] assert List.mappend(ls[0], List.mappend(ls[1], ls[2])) == \ List.mappend(List.mappend(ls[0], ls[1]), ls[2])
def test_Mempty_is_singleton(): assert Mempty is _Mempty() def test_Mempty_mempty_is_Mempty(): assert Mempty.mempty is Mempty def test_Mempty_is_false(): assert not Mempty @pytest.mark.parametrize( 'monoid', [[4], [{1, 2}], [List(1, 2)], [Map({'a': 10})], [True], pytest.mark.xfail([Decimal('0')])]) def test_Mempty_mappend(monoid): assert Mempty.mappend(monoid) == monoid @pytest.mark.parametrize('monoids, result', [((1, 2, 3), 6), ((1, Mempty, 2, 3), 6), ([[1], [2], [3]], [1, 2, 3]), ([Mempty, [1], [2], [3]], [1, 2, 3])]) def test_Mempty_mconcat(monoids, result): assert Mempty.mconcat(*monoids) == result def test_Mempty_mconcat_with_all_mempties(): assert Mempty.mconcat(Mempty, Mempty) is Mempty
def test_List_repr(): assert repr(List(1, 2, 3)) == "List(1, 2, 3)" assert "...5 more..." in repr(List(*range(15)))
def test_mcons(): assert lifted.mcons(Just(0), Just(List(1, 2, 3))) == Just(List(0, 1, 2, 3)) assert lifted.mcons(Nothing, Just(List(0))) is Nothing
def test_List_unit(): assert List.unit(1).v == (1, ) assert List.unit('fred').v == ('fred', ) assert List.unit([1, 2, 3]).v == ([1, 2, 3], )
def test_mapM(): assert lifted.mapM(m_add_two, *range(5)) == Just(List(2, 3, 4, 5, 6))
def test_List_fmap(): l = List(1, 2, 3) assert l.fmap(add_two).v == (3, 4, 5)
def test_writer_bind(): w = Writer.unit(2) >> m_add_two assert w.v == (4, List.unit('added two'))
def test_List_apply(): l = List(add_two) * List(1, 2, 3) assert l.v == (3, 4, 5)