def setUp(self): self.engine = BaseEngine() self.engine.only_one_iteration = True self._sys_patcher = patch('durak.engine.base.sys') sys_mock = self._sys_patcher.start() self.stdin_mock = sys_mock.stdin self.stdout_mock = sys_mock.stdout
def setUp(self): self.engine = BaseEngine() self.engine.only_one_iteration = True self._sys_patcher = patch("durak.engine.base.sys") sys_mock = self._sys_patcher.start() self.stdin_mock = sys_mock.stdin self.stdout_mock = sys_mock.stdout
class BaseEngineTest(unittest.TestCase): def setUp(self): self.engine = BaseEngine() self.engine.only_one_iteration = True self._sys_patcher = patch('durak.engine.base.sys') sys_mock = self._sys_patcher.start() self.stdin_mock = sys_mock.stdin self.stdout_mock = sys_mock.stdout def tearDown(self): self._sys_patcher.stop() def test_not_implemented_methods(self): with self.assertRaises(NotImplementedError): self.engine.init(None) other_methods = ['deal', 'move', 'respond', 'give_more'] for method_name in other_methods: method = getattr(self.engine, method_name) with self.assertRaises(NotImplementedError): method(None, None) def test_output_prints_line_and_flushes_stdout(self): SOME_LINE = 'some_line' self.engine._output(SOME_LINE) self.stdout_mock.write.assert_called_once_with(SOME_LINE + '\n') self.assertTrue(self.stdout_mock.flush.called) def test_parse_line_loads_gamedata_if_present(self): LINE = 'command ## {"some": "value"}' command_name, args, gamedata = self.engine._parse_line(LINE) self.assertEqual(command_name, 'command') self.assertDictEqual(gamedata, {'some': 'value'}) def test_parse_line_without_gamedata_is_ok_too(self): LINE = 'command' command_name, args, gamedata = self.engine._parse_line(LINE) self.assertEqual(command_name, 'command') self.assertDictEqual(gamedata, {}) def test_parse_line_loads_args(self): LINE = 'command arg1 arg2 arg3' command_name, args, gamedata = self.engine._parse_line(LINE) self.assertEqual(command_name, 'command') self.assertEqual(args, ['arg1', 'arg2', 'arg3']) def test_command_calling(self): self.stdin_mock.readline.return_value = 'init trump1' with patch.object(self.engine, 'init') as init_mock: init_mock.return_value = 'some' self.engine.run() init_mock.assert_called_once_with('trump1') self.stdout_mock.write.assert_called_with('some\n') other_methods = ['deal', 'move', 'respond', 'give_more'] for method_name in other_methods: self.stdin_mock.readline.return_value = ( '%s arg1 arg2 arg3 ## {"some": "value"}' % method_name ) with patch.object(self.engine, method_name) as method_mock: method_mock.return_value = method_name self.engine.run() method_mock.assert_called_once_with( ['arg1', 'arg2', 'arg3'], gamedata={'some': 'value'} ) self.stdout_mock.write.assert_called_with( method_name + '\n' ) def test_game_end_command(self): self.stdin_mock.readline.return_value = 'game_end' self.engine.run() self.assertFalse(self.stdout_mock.write.called) def test_unknown_command(self): self.stdin_mock.readline.return_value = 'hahaha arg1 arg2' self.engine.run() self.stdout_mock.write.assert_called_once_with( 'Error: Unknown command "hahaha"\n' )
class BaseEngineTest(unittest.TestCase): def setUp(self): self.engine = BaseEngine() self.engine.only_one_iteration = True self._sys_patcher = patch("durak.engine.base.sys") sys_mock = self._sys_patcher.start() self.stdin_mock = sys_mock.stdin self.stdout_mock = sys_mock.stdout def tearDown(self): self._sys_patcher.stop() def test_not_implemented_methods(self): with self.assertRaises(NotImplementedError): self.engine.init(None) other_methods = ["deal", "move", "respond", "give_more"] for method_name in other_methods: method = getattr(self.engine, method_name) with self.assertRaises(NotImplementedError): method(None, None) def test_output_prints_line_and_flushes_stdout(self): SOME_LINE = "some_line" self.engine._output(SOME_LINE) self.stdout_mock.write.assert_called_once_with(SOME_LINE + "\n") self.assertTrue(self.stdout_mock.flush.called) def test_parse_line_loads_gamedata_if_present(self): LINE = 'command ## {"some": "value"}' command_name, args, gamedata = self.engine._parse_line(LINE) self.assertEqual(command_name, "command") self.assertDictEqual(gamedata, {"some": "value"}) def test_parse_line_without_gamedata_is_ok_too(self): LINE = "command" command_name, args, gamedata = self.engine._parse_line(LINE) self.assertEqual(command_name, "command") self.assertDictEqual(gamedata, {}) def test_parse_line_loads_args(self): LINE = "command arg1 arg2 arg3" command_name, args, gamedata = self.engine._parse_line(LINE) self.assertEqual(command_name, "command") self.assertEqual(args, ["arg1", "arg2", "arg3"]) def test_command_calling(self): self.stdin_mock.readline.return_value = "init trump1" with patch.object(self.engine, "init") as init_mock: init_mock.return_value = "some" self.engine.run() init_mock.assert_called_once_with("trump1") self.stdout_mock.write.assert_called_with("some\n") other_methods = ["deal", "move", "respond", "give_more"] for method_name in other_methods: self.stdin_mock.readline.return_value = '%s arg1 arg2 arg3 ## {"some": "value"}' % method_name with patch.object(self.engine, method_name) as method_mock: method_mock.return_value = method_name self.engine.run() method_mock.assert_called_once_with(["arg1", "arg2", "arg3"], gamedata={"some": "value"}) self.stdout_mock.write.assert_called_with(method_name + "\n") def test_game_end_command(self): self.stdin_mock.readline.return_value = "game_end" self.engine.run() self.assertFalse(self.stdout_mock.write.called) def test_unknown_command(self): self.stdin_mock.readline.return_value = "hahaha arg1 arg2" self.engine.run() self.stdout_mock.write.assert_called_once_with('Error: Unknown command "hahaha"\n')