|
18 | 18 | ESFunc, |
19 | 19 | EQDispatcher, |
20 | 20 | EQFDispatcher, |
| 21 | + SequenceDispatcher, |
21 | 22 | fail_effect, |
22 | 23 | resolve_effect, |
23 | 24 | resolve_stubs) |
@@ -276,3 +277,41 @@ def test_perform(self): |
276 | 277 | """When an intent matches, performing it returns the canned result.""" |
277 | 278 | d = EQFDispatcher([('hello', lambda i: (i, 'there'))]) |
278 | 279 | self.assertEqual(sync_perform(d, Effect('hello')), ('hello', 'there')) |
| 280 | + |
| 281 | + |
| 282 | +class SequenceDispatcherTests(TestCase): |
| 283 | + """Tests for :obj:`SequenceDispatcher`.""" |
| 284 | + |
| 285 | + def test_mismatch(self): |
| 286 | + """ |
| 287 | + When an intent isn't expected, a None is returned. |
| 288 | + """ |
| 289 | + d = SequenceDispatcher([('foo', lambda i: 1 / 0)]) |
| 290 | + self.assertEqual(d('hello'), None) |
| 291 | + |
| 292 | + def test_success(self): |
| 293 | + """ |
| 294 | + Each intent is performed in sequence with the provided functions, as |
| 295 | + long as the intents match. |
| 296 | + """ |
| 297 | + d = SequenceDispatcher([ |
| 298 | + ('foo', lambda i: ('performfoo', i)), |
| 299 | + ('bar', lambda i: ('performbar', i)), |
| 300 | + ]) |
| 301 | + eff = Effect('foo').on(lambda r: Effect('bar').on(lambda r2: (r, r2))) |
| 302 | + self.assertEqual( |
| 303 | + sync_perform(d, eff), |
| 304 | + (('performfoo', 'foo'), ('performbar', 'bar'))) |
| 305 | + |
| 306 | + def test_ran_out(self): |
| 307 | + """When there are no more items left, None is returned.""" |
| 308 | + d = SequenceDispatcher([]) |
| 309 | + self.assertEqual(d('foo'), None) |
| 310 | + |
| 311 | + def test_out_of_order(self): |
| 312 | + """Order of items in the sequence matters.""" |
| 313 | + d = SequenceDispatcher([ |
| 314 | + ('bar', lambda i: ('performbar', i)), |
| 315 | + ('foo', lambda i: ('performfoo', i)), |
| 316 | + ]) |
| 317 | + self.assertEqual(d('foo'), None) |
0 commit comments