示例#1
0
def test_parse(fractal: Fractal, drawer: mock.MagicMock) -> None:
    parse("+F--F+", fractal)
    assert drawer.method_calls == [
        call.turn(45),
        call.draw(1),
        call.turn(315),
        call.turn(315),
        call.draw(1),
        call.turn(45),
    ]
示例#2
0
    def test_play_turn_loose(self, ch):
        """Test a turn/game looser"""
        # Prepare test
        ch.return_value = i18n.OUT_MSG_COMPLAINTS[0]
        key = 'a'
        self.game.hangman.attempt.__bool__.side_effect = [True, False]
        self.game.ui.accept_letter.return_value = key
        self.game.word.unmask.return_value = False

        # Run test
        self.game.play_turn(key)

        # Evaluate test
        calls = [
            call.attempt.__bool__(),
            call.missed.__iadd__(1),
            call.draw(),
            call.attempt.__bool__()
        ]
        self.game.hangman.assert_has_calls(calls)
        calls = [
            call.accept_letter(key),
            call.end_turn(i18n.OUT_MSG_COMPLAINTS[0]),
            call.end_game(i18n.OUT_MSG_LOSER),
            call.ask_play_again()
        ]
        self.game.ui.assert_has_calls(calls)
        calls = [call.__bool__(), call.unmask(key)]
        self.game.word.assert_has_calls(calls)
示例#3
0
    def test_StorageLimit(self):
        storage = self.storage_mock
        renderer = self.renderer_mock
        ui = self.ui_mock

        storage.mock_add_spec(['get', 'step_next'], spec_set=True)
        img_next = Mock(spec_set=[], name='img_next')
        imgs = {
            -3: None,
            -2: Mock(spec_set=[], name='img_-2'),
            -1: Mock(spec_set=[], name='img_-1'),
            0: Mock(spec_set=[], name='img_0'),
            1: img_next,
            2: Mock(spec_set=[], name='img_2'),
            3: Mock(spec_set=[], name='img_3'),
            4: None,
        }

        def _get(offset):
            return imgs[offset]

        storage.get.side_effect = _get
        expect_storage_get = [
            call.get(0), call.get(-1), call.get(-2), call.get(-3),
            call.get(1), call.get(2), call.get(3), call.get(4)
        ]

        renderer.mock_add_spec(['calc', 'render_to_left'], spec_set=True)
        calc_result = [
            # left side
            {'left': 0, 'left_done': False, 'right': 0, 'right_done': False},
            {'left': 0, 'left_done': False, 'right': 1, 'right_done': False},
            {'left': 0, 'left_done': False, 'right': 2, 'right_done': False},
            # right side
            {'left': 0, 'left_done': False, 'right': 0, 'right_done': False},
            {'left': 0, 'left_done': False, 'right': 1, 'right_done': False},
            {'left': 0, 'left_done': False, 'right': 2, 'right_done': False}
        ]
        renderer.calc.side_effect = calc_result
        expect_renderer_calc = list(itertools.repeat(call.calc(ANY, ANY), len(calc_result)))

        render_result = list(itertools.repeat(Mock(spec_set=[]), 101))
        renderer.render_to_left.side_effect = render_result
        args_render = [imgs[-2], imgs[-1], imgs[0], imgs[1], imgs[2], imgs[3]]
        expect_renderer_render = [
            call.render_to_left(args_render, img_next, i) for i in range(101)
        ]

        expect_ui = [call.draw(pic) for pic in render_result]

        self.instance.to_left()

        storage.assert_has_calls(expect_storage_get + [call.step_next()])
        self.assertEqual(renderer.calc.call_count, len(expect_renderer_calc))
        self.assertEqual(renderer.render_to_left.call_count, len(expect_renderer_render))
        renderer.assert_has_calls(expect_renderer_calc + expect_renderer_render)
        ui.assert_has_calls(expect_ui)
示例#4
0
    def test_run_loop(self):
        # Prepare test
        self.game.play_turn = MagicMock(side_effect=[True, False])

        # Run test
        self.game.run_loop()

        # Evaluate test
        calls = [call.draw(hanged=True), call.draw()]
        self.game.hangman.assert_has_calls(calls)
        calls = [
            call.welcome_player(),
            call.init_game_metrics(),
            call.in_valid_letter(),
            call.init_game_metrics(),
            call.in_valid_letter()
        ]
        self.game.ui.assert_has_calls(calls)
示例#5
0
    def test_play_turn_win(self, ch):
        """Test a turn/game winner"""
        # Prepare test
        ch.return_value = i18n.OUT_MSG_CONGRATS[0]
        key = 'a'
        self.game.ui.accept_letter.return_value = key
        self.game.word.unmask.return_value = True
        self.game.word.is_mask.return_value = False

        # Run test
        self.game.play_turn(key)

        # Evaluate test
        calls = [call.attempt.__bool__(), call.draw(saved=True)]
        self.game.hangman.assert_has_calls(calls)
        calls = [
            call.accept_letter(key),
            call.end_turn(i18n.OUT_MSG_CONGRATS[0]),
            call.end_game(i18n.OUT_MSG_WINNER),
            call.ask_play_again()
        ]
        self.game.ui.assert_has_calls(calls)
        calls = [call.__bool__(), call.unmask(key), call.is_mask()]
        self.game.word.assert_has_calls(calls)
示例#6
0
    def test_RendererLimit(self):
        storage = self.storage_mock
        renderer = self.renderer_mock
        ui = self.ui_mock

        storage.mock_add_spec(['get', 'step_previous'], spec_set=True)
        img_current = Mock(spec_set=[], name='img_next')
        img_previous = Mock(spec_set=[], name='img_next')
        imgs = {
            -3: Mock(spec_set=[], name='img_-3'),
            -2: Mock(spec_set=[], name='img_-2'),
            -1: img_previous,
            0: img_current,
            1: Mock(spec_set=[], name='img_1'),
            2: Mock(spec_set=[], name='img_2')
        }

        def _get(offset):
            return imgs[offset]

        storage.get.side_effect = _get
        expect_storage_get = [
            call.get(0), call.get(1), call.get(2),
            call.get(-1), call.get(-2), call.get(-3)
        ]

        renderer.mock_add_spec(['calc', 'render_to_right'], spec_set=True)
        calc_result = [
            # right side
            {'left': 0, 'left_done': False, 'right': 0, 'right_done': False},
            {'left': 0, 'left_done': False, 'right': 1, 'right_done': False},
            {'left': 0, 'left_done': False, 'right': 2, 'right_done': True},
            # left side
            {'left': 0, 'left_done': False, 'right': 0, 'right_done': False},
            {'left': 0, 'left_done': False, 'right': 1, 'right_done': False},
            {'left': 0, 'left_done': True, 'right': 2, 'right_done': False}
        ]
        renderer.calc.side_effect = calc_result

        expect_renderer_calc = [
            #left side
            call.calc(ANY, img_current),
            call.calc(ANY, img_current),
            call.calc([imgs[0], imgs[1], imgs[2]], img_current),
            #right side
            call.calc(ANY, img_previous),
            call.calc(ANY, img_previous),
            call.calc([imgs[-3], imgs[-2], imgs[-1]], img_previous)
        ]

        renderer.render_to_right.side_effect = list(itertools.repeat(Mock(spec_set=[]), 101))
        args_render = [imgs[-3], imgs[-2], imgs[-1], imgs[0], imgs[1], imgs[2]]
        expect_renderer_render = [
            call.render_to_right(args_render, img_previous, i) for i in range(101)
        ]

        expect_ui = list(itertools.repeat(call.draw(ANY), 101))

        self.instance.to_right()

        storage.assert_has_calls(expect_storage_get + [call.step_previous()])
        self.assertEqual(renderer.calc.call_count, len(expect_renderer_calc))
        self.assertEqual(renderer.render_to_right.call_count, len(expect_renderer_render))
        renderer.assert_has_calls(expect_renderer_calc + expect_renderer_render)
        ui.assert_has_calls(expect_ui)
示例#7
0
 def test_when_run_then_main_view_mainloop_is_executed_after_it_is_drawn(self) -> None:
     self.tkinter_ui.run()
     calls = self.main_view.mock_calls
     expected_calls = [call.draw(), call.mainloop()]
     self.assertListEqual(expected_calls, calls)