class App(object): def __init__(self): self.window = Window(width=800, height=600, title='Breakdown') self.window.add_scene(scene=GameScene(self.window)) def run(self): return self.window.run()
def test_should_run_scenes(self): window = Window(800, 600, 'Test') scene = Scene(window) with unittest.mock.patch.object(scene, 'run', wraps=scene.run) as spy: window.add_scene(scene) window.run() spy.assert_called()
class App(object): def __init__(self): self.window = Window(800, 600, 'Japong!') self.add_scene(StartScene(self.window)) self.add_scene(GameScene(self.window)) self.add_scene(EndScene(self.window)) def run(self): pygame.init() code = self.window.run() pygame.quit() return code def add_scene(self, scene): self.window.add_scene(scene)
class TestWindow(TestCase): def setUp(self) -> None: self.window = Window(800, 600, 'Test') scene_ref = 'app.scene.scene.Scene' @unittest.mock.patch('pygame.event.get', return_value=[EventMother.quit()]) @unittest.mock.patch(scene_ref) def test_exit_if_scene_ends_without_error(self, events, scene): scene.run.return_value = 0 self.window.add_scene(scene) self.assertEqual(0, self.window.run()) @unittest.mock.patch('pygame.event.get', return_value=[EventMother.quit()]) @unittest.mock.patch(scene_ref) def test_exit_with_error_if_scene_returns_error(self, events, scene): scene.run.return_value = -1 self.window.add_scene(scene) self.assertEqual(-1, self.window.run()) @unittest.mock.patch('pygame.event.get', return_value=[EventMother.quit()]) @unittest.mock.patch(scene_ref) @unittest.mock.patch(scene_ref) def test_exit_with_error_if_any_scene_returns_error( self, events, scene_1, scene_2): scene_1.run.return_value = -1 scene_2.run.return_value = 0 self.window.add_scene(scene_1) self.window.add_scene(scene_2) self.assertEqual(-1, self.window.run())
def test_should_allow_play_again(self): window = Window(800, 600, 'Test') play_again_scene = Scene(window) with unittest.mock.patch.object(play_again_scene, 'run', wraps=play_again_scene.run, side_effect=[1, 0]) as spy: window.add_scene(play_again_scene) self.assertEqual(0, window.run())
def test_should_exit_with_error(self): window = Window(800, 600, 'Test') error_scene = Scene(window) with unittest.mock.patch.object(error_scene, 'run', wraps=error_scene.run, return_value=-1) as spy: window.add_scene(error_scene) self.assertEqual(-1, window.run())
def test_should_add_scenes(self): window = Window(800, 600, 'Test') window.add_scene(Scene(window)) self.assertEqual(1, len(window.scenes))