def test_stop(self): class Stoppable(Runnable): def init(self, state): self.stopped = False def next(self, state): return state def halt(self): self.stopped = True branch = Branch([Stoppable()]) branch.run(State()) branch.stop() self.assertTrue(next(iter(branch)).stopped)
def test_composition(self): class A(Runnable): def next(self, state): return state.updated(x=state.x + 1) class B(Runnable): def next(self, state): return state.updated(x=state.x * 7) a, b = A(), B() s = State(x=1) b1 = Branch(components=(a, b)) self.assertEqual(b1.components, (a, b)) self.assertEqual(b1.run(s).result().x, (s.x + 1) * 7) b2 = b1 | b | a self.assertEqual(b2.components, (a, b, b, a)) self.assertEqual(b2.run(s).result().x, (s.x + 1) * 7 * 7 + 1) with self.assertRaises(TypeError): b1 | 1