Пример #1
0
    def __init__(self):
        self.clock = Mock()
        self.clock.sleep.return_value = effect.success(None)
        self.clock.now.return_value = effect.success(datetime.fromtimestamp(0))

        self.random = Mock()
        self.random.random.return_value = effect.success(0.0)
        self.random.randint.return_value = effect.success(0)
Пример #2
0
    def test_gather(self):
        assert effect.gather_async(
            [effect.success(v) for v in range(3)]
        ).run(None) == (0, 1, 2)

        assert effect.gather(
            [effect.success(v) for v in range(3)]
        ).run(None) == (0, 1, 2)
Пример #3
0
 def test_absolve(self):
     right = either.Right(1)
     left = either.Left('error')
     right_effect = effect.success(right)
     left_effect = effect.success(left)
     assert effect.absolve(right_effect).run(None) == 1
     with pytest.raises(Exception):
         # todo
         effect.absolve(left_effect).run(None)
Пример #4
0
 def test_filter_generator(self):
     e = effect.filter_(
         lambda v: effect.success(v % 2 == 0),
         (v for v in range(5))
     )
     assert e.run(None) == (0, 2, 4)
     assert e.run(None) == (0, 2, 4)
Пример #5
0
 def test_memoize(self):
     state = ref.Ref(())
     e = state.modify(lambda t: t + ('modify was called', )
                      ).discard_and_then(effect.success('result')).memoize()
     double_e = e.discard_and_then(e)
     assert double_e.run(None) == 'result'
     assert state.value == ('modify was called', )
Пример #6
0
    def test_stack_safety(self):
        with recursion_limit(100):
            effect.sequence_async([effect.success(v)
                                   for v in range(500)]).run(None)

        e = effect.error('')
        for _ in range(500):
            e = e.recover(lambda _: effect.error(''))
        e = e.recover(lambda _: effect.success(''))
        with recursion_limit(100):
            e.run(None)

        e = effect.success('')
        for _ in range(500):
            e = e.either()
        with recursion_limit(100):
            e.run(None)
Пример #7
0
 def test_for_each_repr(self):
     f = lambda _: effect.success(True)
     assert repr(effect.for_each(f, [0])) == f'for_each({repr(f)})((0,))'
Пример #8
0
 def test_filter_repr(self):
     f = lambda _: effect.success(True)
     assert repr(effect.filter_(f, [0])) == f'filter_({repr(f)})((0,))'
Пример #9
0
 def test_combine_cpu_bound_repr(self):
     f = lambda _: _
     assert (
         repr(effect.combine_cpu_bound(effect.success(0))(f)
              ) == f'combine_cpu_bound(success(0))({repr(f)})'
     )
Пример #10
0
 def test_sequence(self):
     assert effect.sequence_async([effect.success(v) for v in range(3)]
                                  ).run(None) == (0, 1, 2)
Пример #11
0
 def test_equality(self, value, env):
     assert effect.success(value).run(env) == effect.success(value).run(env)
Пример #12
0
 def test_left_identity_law(self, f, value, env):
     assert (
         effect.success(value).and_then(f).run(env) == f(value).run(env)
     )
Пример #13
0
 def test_memoize_repr(self):
     e = effect.success(0)
     assert repr(e.memoize()) == 'success(0).memoize()'
Пример #14
0
 def test_recover_repr(self):
     e = effect.success(0)
     f = lambda _: _
     assert repr(e.recover(f)) == f'success(0).recover({repr(f)})'
Пример #15
0
 def test_either_repr(self):
     e = effect.success(0)
     assert repr(e.either()) == 'success(0).either()'
Пример #16
0
 def test_discard_and_then_repr(self):
     e = effect.success('value')
     assert (
         repr(e.discard_and_then(e)
              ) == 'success(\'value\').discard_and_then(success(\'value\'))'
     )
Пример #17
0
 def test_map_repr(self):
     f = lambda _: _
     assert (
         repr(effect.success('value').map(f)
              ) == f'success(\'value\').map({repr(f)})'
     )
Пример #18
0
 def test_and_then_repr(self):
     f = lambda _: _
     assert (
         repr(effect.success('value').and_then(f)
              ) == f'success(\'value\').and_then({repr(f)})'
     )
Пример #19
0
 def test_success_repr(self):
     assert repr(effect.success('value')) == 'success(\'value\')'
Пример #20
0
 def test_absolve_repr(self):
     assert repr(effect.absolve(effect.success(0))) == 'absolve(success(0))'
Пример #21
0
 def test_identity_law(self, value, env):
     assert (
         effect.success(value).map(identity).run(env) ==
         effect.success(value).run(env)
     )
Пример #22
0
 def test_ensure_repr(self):
     e = effect.success(0)
     assert repr(e.ensure(e)) == 'success(0).ensure(success(0))'
Пример #23
0
 def test_right_identity_law(self, value, env):
     assert (
         effect.success(value).and_then(
             effect.success
         ).run(env) == effect.success(value).run(env)
     )
Пример #24
0
 def test_composition_law(self, f, g, value, env):
     h = compose(f, g)
     assert (
         effect.success(value).map(h).run(env) ==
         effect.success(value).map(g).map(f).run(env)
     )
Пример #25
0
 def test_inequality(self, first, second, env):
     assume(first != second)
     assert effect.success(first).run(env) != effect.success(second
                                                             ).run(env)
Пример #26
0
    def test_combine(self):
        def f(a, b):
            return a + b

        assert effect.combine(effect.success('a'),
                              effect.success('b'))(f).run(None) == 'ab'
Пример #27
0
 def test_sequence_generator(self):
     e = effect.sequence_async(effect.success(v) for v in range(3))
     assert e.run(None) == (0, 1, 2)
     assert e.run(None) == (0, 1, 2)
Пример #28
0
    def test_lift(self):
        def f(a, b):
            return a + b

        assert effect.lift(f)(effect.success(2),
                              effect.success(2)).run(None) == 4
Пример #29
0
 def test_sequence_repr(self):
     e = effect.sequence([effect.success(0)])
     assert (repr(e)) == 'sequence((success(0),))'
Пример #30
0
 def test_lift_cpu_bound_repr(self):
     f = lambda _: _
     assert (
         repr(effect.lift_cpu_bound(f)(effect.success(0))
              ) == f'lift_cpu_bound({repr(f)})(success(0))'
     )