bebraw
1/13/2010 - 12:06 PM

test_scenario_tester.py

...

    def test_running(self):
        class Application(AbstractApplication):
            pass
        
        scenario = '''
>>> a = 4
--- running
>>> b = 5
--- running
'''

        scenario_tester = ScenarioTester(Application)
        scenario_tester.parse(scenario)
        lines = scenario_tester.lines

        assert len(lines) == 4
        self.assert_line(lines, 1, Input, 'a = 4')
        self.assert_line(lines, 2, Meta, 'running')
        self.assert_line(lines, 3, Input, 'b = 5')
        self.assert_line(lines, 4, Meta, 'running')

        # this should not trigger any asserts
        scenario_tester.test(scenario)

    def test_running_fail(self):
        class Application(AbstractApplication):
            def interpret(self, input):
                if input == 'quit':
                    raise SystemExit

        scenario = '''
>>> a = 4
--- running
>>> quit
--- running
'''

        scenario_tester = ScenarioTester(Application)
        scenario_tester.parse(scenario)
        lines = scenario_tester.lines

        assert len(lines) == 4
        self.assert_line(lines, 1, Input, 'a = 4')
        self.assert_line(lines, 2, Meta, 'running')
        self.assert_line(lines, 3, Input, 'quit')
        self.assert_line(lines, 4, Meta, 'running')

        raises(NotRunningError, scenario_tester.test, scenario)

    def test_not_running(self):
        class Application(AbstractApplication):
            def interpret(self, input):
                if input == 'quit':
                    raise SystemExit

        scenario = '''
>>> quit
--- not running
'''

        scenario_tester = ScenarioTester(Application)
        scenario_tester.parse(scenario)
        lines = scenario_tester.lines

        assert len(lines) == 2
        self.assert_line(lines, 1, Input, 'quit')
        self.assert_line(lines, 2, Meta, 'not running')

        # this should not trigger any asserts
        scenario_tester.test(scenario)

    def test_not_running_fail(self):
        class Application(AbstractApplication):
            pass

        scenario = '''
>>> a = 5
--- not running
'''

        scenario_tester = ScenarioTester(Application)
        scenario_tester.parse(scenario)
        lines = scenario_tester.lines

        assert len(lines) == 2
        self.assert_line(lines, 1, Input, 'a = 5')
        self.assert_line(lines, 2, Meta, 'not running')

        raises(RunningError, scenario_tester.test, scenario)

    def test_restart(self):
        class Application(AbstractApplication):
            def interpret(self, input):
                if input == 'quit':
                    raise SystemExit

        scenario = '''
>>> quit
--- not running
--- restart
--- running
--- restart
--- running
'''

        scenario_tester = ScenarioTester(Application)
        scenario_tester.parse(scenario)
        lines = scenario_tester.lines

        assert len(lines) == 6
        self.assert_line(lines, 1, Input, 'quit')
        self.assert_line(lines, 2, Meta, 'not running')
        self.assert_line(lines, 3, Meta, 'restart')
        self.assert_line(lines, 4, Meta, 'running')
        self.assert_line(lines, 5, Meta, 'restart')
        self.assert_line(lines, 6, Meta, 'running')

        # this should not trigger any asserts
        scenario_tester.test(scenario)

...