|
6 | 6 | from effect import ( |
7 | 7 | ComposedDispatcher, Effect, Error, |
8 | 8 | base_dispatcher, sync_perform) |
9 | | -from effect.fold import FoldError, fold_effect |
| 9 | +from effect.fold import FoldError, fold_effect, sequence |
10 | 10 | from effect.testing import SequenceDispatcher |
11 | 11 |
|
12 | 12 |
|
@@ -62,3 +62,44 @@ def test_fold_effect_errors(): |
62 | 62 | assert excinfo.value.accumulator == 'NilEi' |
63 | 63 | assert excinfo.value.wrapped_exception[0] is ZeroDivisionError |
64 | 64 | assert str(excinfo.value.wrapped_exception[1]) == 'foo' |
| 65 | + |
| 66 | + |
| 67 | +def test_sequence(): |
| 68 | + """Collects each Effectful result into a list.""" |
| 69 | + effs = [Effect('a'), Effect('b'), Effect('c')] |
| 70 | + dispatcher = SequenceDispatcher([ |
| 71 | + ('a', lambda i: 'Ei'), |
| 72 | + ('b', lambda i: 'Bee'), |
| 73 | + ('c', lambda i: 'Cee'), |
| 74 | + ]) |
| 75 | + eff = sequence(effs) |
| 76 | + |
| 77 | + with dispatcher.consume(): |
| 78 | + result = sync_perform(_base_and(dispatcher), eff) |
| 79 | + assert result == ['Ei', 'Bee', 'Cee'] |
| 80 | + |
| 81 | + |
| 82 | +def test_sequence_empty(): |
| 83 | + """Returns an empty list when there are no Effects.""" |
| 84 | + assert sync_perform(base_dispatcher, sequence([])) == [] |
| 85 | + |
| 86 | + |
| 87 | +def test_sequence_error(): |
| 88 | + """ |
| 89 | + Allows :obj:`FoldError` to be raised when an Effect fails. The list |
| 90 | + accumulated so far is the `accumulator` value in the :obj:`FoldError`. |
| 91 | + """ |
| 92 | + effs = [Effect('a'), Effect(Error(ZeroDivisionError('foo'))), Effect('c')] |
| 93 | + |
| 94 | + dispatcher = SequenceDispatcher([ |
| 95 | + ('a', lambda i: 'Ei'), |
| 96 | + ]) |
| 97 | + |
| 98 | + eff = sequence(effs) |
| 99 | + |
| 100 | + with dispatcher.consume(): |
| 101 | + with raises(FoldError) as excinfo: |
| 102 | + sync_perform(_base_and(dispatcher), eff) |
| 103 | + assert excinfo.value.accumulator == ['Ei'] |
| 104 | + assert excinfo.value.wrapped_exception[0] is ZeroDivisionError |
| 105 | + assert str(excinfo.value.wrapped_exception[1]) == 'foo' |
0 commit comments