def test_lens_tuple_l(): data = {'hello': 0, 'world': 1} my_lens = lenses.tuple_l(lenses.getitem('hello'), lenses.getitem('world')) assert my_lens.get(data) == (0, 1) assert my_lens.set(data, (3, 4)) == {'hello': 3, 'world': 4}
def test_lens_getitem(): assert lenses.getitem(0).get([1, 2, 3]) == 1 assert lenses.getitem(0).set([1, 2, 3], 4) == [4, 2, 3]
def test_boundlens_get_all(): my_lens = lenses.both.compose(lenses.getitem(1)) assert lens([[1, 2], [3, 4]], my_lens).get_all() == (2, 4)
def test_boundlens_add_lens(): assert lens([1, 2]).add_lens(lenses.trivial) + [3] == [1, 2, 3] assert lens([1, 2]).add_lens(lenses.getitem(1)).set(3) == [1, 3]
import collections import pytest import lenses from lenses import lens LensAndState = collections.namedtuple('LensAndState', 'lens state') lenses_and_states = [ LensAndState(lenses.trivial, None), LensAndState(lenses.getitem(0), [1, 2, 3]), LensAndState(lenses.getitem(0), (1, 2, 3)), LensAndState(lenses.getitem(0), {0: 'hello', 1: 'world'}), ] # yapf: disable @pytest.fixture(params=lenses_and_states) def lns(request): return request.param # Tests for lens rules and other invariants def test_get_then_set(lns): '''if we get from a state and then immediately set it again we should get back the same state''' assert lns.lens.set(lns.state, lns.lens.get(lns.state)) == lns.state def test_set_then_get(lns): '''if we set a state and immediately get it we should get back what we set'''