Beispiel #1
0
    def test_7th_floor_never_reached_fails(self):
        tested = Interpreter(io.import_from_yaml(open('docs/examples/elevator.yaml')))
        story = Story([Event('floorSelected', floor=4), Pause(2), Event('floorSelected', floor=7)])
        trace = story.tell(tested).trace

        test_story = teststory_from_trace(trace)

        tester = Interpreter(io.import_from_yaml(open('docs/examples/tester_elevator_7th_floor_never_reached.yaml')))
        self.assertFalse(test_story.tell(tester).final)
Beispiel #2
0
    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/elevator/tester_elevator_7th_floor_never_reached.yaml') as f:
            tester = Interpreter(io.import_from_yaml(f))
        test_story.tell(tester)
        self.assertTrue(tester.final)
Beispiel #3
0
    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)
Beispiel #4
0
    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)
Beispiel #5
0
    def test_7th_floor_never_reached(self):
        story = Story([Event('floorSelected', floor=8)])
        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.assertFalse(tester.final)
Beispiel #6
0
    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)
Beispiel #7
0
    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)
Beispiel #8
0
    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)
Beispiel #9
0
    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)
Beispiel #10
0
    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)
Beispiel #11
0
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
Beispiel #12
0
    def __execute_once(self, *args, **kwargs) -> MacroStep:
        # Execute tester
        time = self._tested.time
        step = self._tested_execute_once_function(*args, **kwargs)  # type: ignore

        story = _teststory_from_macrostep(step) if step else Story()

        # Send to testers
        for tester in self._testers:
            tester.time = time
            story.tell(tester)

        return step
Beispiel #13
0
    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)
Beispiel #14
0
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
Beispiel #15
0
def _teststory_from_macrostep(macrostep: MacroStep) -> Story:
    story = Story()
    story.append(Event('step started'))

    if macrostep.event:
        story.append(Event('event consumed', event=macrostep.event))

    for microstep in macrostep.steps:
        for state in microstep.exited_states:
            story.append(Event('state exited', state=state))
        if microstep.transition:
            story.append(Event('transition processed', source=microstep.transition.source,
                               target=microstep.transition.target,
                               event=macrostep.event))
        for state in microstep.entered_states:
            story.append(Event('state entered', state=state))

    story.append(Event('step ended'))
    return story
Beispiel #16
0
 def setUp(self):
     self.story = Story(
         [Event('a'),
          Event('b'),
          Event('c'),
          Pause(1), Pause(2)])
Beispiel #17
0
def _teststory_from_macrostep(macrostep: MacroStep) -> Story:
    story = Story()
    story.append(Event('step started'))

    if macrostep.event:
        story.append(Event('event consumed', event=macrostep.event))

    for microstep in macrostep.steps:
        for state in microstep.exited_states:
            story.append(Event('state exited', state=state))
        if microstep.transition:
            story.append(Event('transition processed', source=microstep.transition.source,
                               target=microstep.transition.target,
                               event=macrostep.event))
        for state in microstep.entered_states:
            story.append(Event('state entered', state=state))

    story.append(Event('step ended'))
    return story