def test_elevator_moves_after_10s(self): stories = [ Story([Event('floorSelected', floor=4)]), Story([Event('floorSelected', floor=0)]), Story([Event('floorSelected', floor=4), Pause(10)]), Story([Event('floorSelected', floor=0), Pause(10)]), Story([Event('floorSelected', floor=4), Pause(9)]), Story([Event('floorSelected', floor=0), Pause(9)]), ] for story in stories: with self.subTest(story=story): # Reopen because we need to reset it with open('docs/examples/elevator.yaml') as f: sc = io.import_from_yaml(f) tested = Interpreter(sc) test_story = teststory_from_trace(story.tell(tested)) with open('docs/examples/tester_elevator_moves_after_10s.yaml' ) as f: tester = Interpreter(io.import_from_yaml(f)) test_story.tell(tester) self.assertFalse(tester.final)
def test_pauses(self): trace = [ MacroStep(0, []), MacroStep(10, []), MacroStep(10, []), MacroStep(15, []), ] self.assertListEqual(story_from_trace(trace), [Pause(10), Pause(5)])
def test_tell_by_step(self): story = Story([Event('goto s2'), Pause(5), Event('goto final')]) with open('tests/yaml/simple.yaml') as f: sc = io.import_from_yaml(f) interpreter = Interpreter(sc) teller = story.tell_by_step(interpreter) self.assertEqual(Event('goto s2'), next(teller)[0]) self.assertEqual(Pause(5), next(teller)[0]) self.assertEqual(Event('goto final'), next(teller)[0]) with self.assertRaises(StopIteration): next(teller) self.assertTrue(interpreter.final) self.assertEqual(interpreter.time, 5)
def test_ignore_internal_events(self): trace = [ MacroStep(2, [MicroStep(Event('a'))]), MacroStep(5, [MicroStep(Event('b'))]), MacroStep(9, [MicroStep(InternalEvent('c'))]), MacroStep(14, [MicroStep(Event('d'))]), ] self.assertListEqual(story_from_trace(trace), [ Pause(2), Event('a'), Pause(3), Event('b'), Pause(4), Pause(5), Event('d') ])
def test_storycreation(self): story = Story() story.append(Event('a')) story.append(Event('b')) story.append(Pause(10)) story.append(Event('c')) self.assertEqual(len(story), 4)
def test_events_and_pauses(self): trace = [ MacroStep(2, [MicroStep(event=Event('a'))]), MacroStep(5, [MicroStep(event=Event('b'))]), MacroStep(9, [MicroStep(event=Event('c'))]), MacroStep(14, [MicroStep(event=Event('d'))]), ] self.assertListEqual(story_from_trace(trace), [ Pause(2), Event('a'), Pause(3), Event('b'), Pause(4), Event('c'), Pause(5), Event('d') ])
def test_tell(self): story = Story([Event('goto s2'), Pause(5), Event('goto final')]) with open('tests/yaml/simple.yaml') as f: sc = io.import_from_yaml(f) interpreter = Interpreter(sc) trace = story.tell(interpreter) self.assertTrue(interpreter.final) self.assertEqual(interpreter.time, 5) self.assertEqual(len(trace), 4)
def test_simple(self): with open('tests/yaml/simple.yaml') as f: sc = io.import_from_yaml(f) interpreter = Interpreter(sc) trace = Story([Pause(2), Event('goto s2'), Pause(3)]).tell(interpreter) story = teststory_from_trace(trace) expected = Story([ Event('execution started'), Pause(2), Event('step started'), Event('state entered', state='root'), Event('state entered', state='s1'), Event('step ended'), Event('step started'), Event('event consumed', event=Event('goto s2')), Event('state exited', state='s1'), Event('transition processed', source='s1', target='s2', event=Event('goto s2')), Event('state entered', state='s2'), Event('step ended'), Event('step started'), Event('state exited', state='s2'), Event('transition processed', source='s2', target='s3', event=None), Event('state entered', state='s3'), Event('step ended'), Event('execution stopped') ]) for a, b in zip(story, expected): self.assertEqual(a, b) if isinstance(a, Event): self.assertEqual(a.data.items(), b.data.items()) else: self.assertEqual(a.duration, b.duration)
def test_7th_floor_never_reached_fails(self): story = Story([ Event('floorSelected', floor=4), Pause(2), Event('floorSelected', floor=7) ]) trace = story.tell( self.tested) # self.tested is an interpreter for our elevator test_story = teststory_from_trace(trace) with open('docs/examples/tester_elevator_7th_floor_never_reached.yaml' ) as f: tester = Interpreter(io.import_from_yaml(f)) test_story.tell(tester) self.assertTrue(tester.final)
def teststory_from_trace(trace: list) -> Story: """ Return a test story based on the given *trace*, a list of macro steps. See documentation to see which are the events that are generated. Notice that this function adds a *pause* if there is any delay between pairs of consecutive steps. :param trace: a list of *MacroStep* instances :return: A story """ story = Story() story.append(Event('execution started')) time = 0 for macrostep in trace: if macrostep.time > time: story.append(Pause(macrostep.time - time)) story.extend(_teststory_from_macrostep(macrostep)) time = macrostep.time story.append(Event('execution stopped')) return story
def test_initial_pause(self): trace = [MacroStep(10, [])] self.assertListEqual(story_from_trace(trace), [Pause(10)])
def setUp(self): self.story = Story( [Event('a'), Event('b'), Event('c'), Pause(1), Pause(2)])