Beispiel #1
0
class EngineWrapperTest(unittest.TestCase):

    def setUp(self):
        self._subprocess_patcher = patch('durak.engine.wrapper.subprocess')
        self.subprocess_mock = self._subprocess_patcher.start()
        self.process = self.subprocess_mock.Popen.return_value
        self.process.stdout.readline.return_value = 'ok'

        self.wrapper = EngineWrapper('path_to_engine')

    def tearDown(self):
        self._subprocess_patcher.stop()

    def test_popen_arguments(self):
        _ = EngineWrapper('path_to_engine')
        self.subprocess_mock.Popen.assert_called_with(
            'path_to_engine',
            stdin=self.subprocess_mock.PIPE,
            stdout=self.subprocess_mock.PIPE
        )

    def test_write_outputs_stripped_line_plus_break(self):
        self.wrapper._write('hi there!    ')
        self.process.stdin.write.assert_called_once_with('hi there!\n')

    def test_write_command_without_gamedata(self):
        self.wrapper._write_command('init', [DurakCard('AH')])
        self.process.stdin.write.assert_called_once_with('init AH\n')

    def test_write_command_with_gamedata(self):
        self.wrapper._write_command(
            'init', [DurakCard('AH')], gamedata={'some': 'value'}
        )
        self.process.stdin.write.assert_called_once_with(
            'init AH ## {"some": "value"}\n'
        )

    def test_write_command_without_cards(self):
        self.wrapper._write_command('move', [])
        self.process.stdin.write.assert_called_once_with('move\n')

    def test_get_output(self):
        self.process.stdout.readline.return_value = 'hi there!   '
        self.assertEqual(self.wrapper._get_output(), 'hi there!')

    def test_game_end_finishes_game_and_kills_process(self):
        self.assertFalse(self.wrapper._process is None)

        self.wrapper.game_end()

        self.assertTrue(self.process.kill.called)
        self.process.stdin.write.assert_called_once_with('game_end\n')
        self.assertTrue(self.wrapper._process is None)

    def test_game_end_is_idempotent(self):
        self.assertFalse(self.wrapper._process is None)

        self.wrapper.game_end()

        self.assertTrue(self.process.kill.called)
        self.assertTrue(self.wrapper._process is None)

        self.wrapper.game_end()
        self.assertTrue(self.wrapper._process is None)

    def test_init_writes_command_and_reads_output(self):
        self.wrapper.init(DurakCard('7H'))

        self.process.stdin.write.assert_called_once_with('init 7H\n')
        self.assertTrue(self.process.stdout.readline.called)

    def test_init_raises_exception_if_engine_response_is_not_ok(self):
        self.process.stdout.readline.return_value = 'error!'
        with self.assertRaises(EngineWrapperException):
            self.wrapper.init(DurakCard('7H'))

    def test_deal_writes_command_and_reads_output(self):
        self.wrapper.deal([DurakCard('7H'), DurakCard('8H')])

        self.process.stdin.write.assert_called_once_with('deal 7H 8H\n')
        self.assertTrue(self.process.stdout.readline.called)

    def test_deal_raises_exception_if_engine_response_is_not_ok(self):
        self.process.stdout.readline.return_value = 'error!'
        with self.assertRaises(EngineWrapperException):
            self.wrapper.deal([DurakCard('7H'), DurakCard('8H')])

    def test_move_converts_output_to_card(self):
        self.process.stdout.readline.return_value = '8S'

        result = self.wrapper.move([DurakCard('7H'), DurakCard('8H')])

        self.assertEqual(result, DurakCard('8S'))
        self.process.stdin.write.assert_called_once_with('move 7H 8H\n')
        self.assertTrue(self.process.stdout.readline.called)

    def test_move_returns_none_if_no_output_given(self):
        self.process.stdout.readline.return_value = ''

        result = self.wrapper.move([DurakCard('7H'), DurakCard('8H')])

        self.assertTrue(result is None)
        self.process.stdin.write.assert_called_once_with('move 7H 8H\n')
        self.assertTrue(self.process.stdout.readline.called)

    def test_move_raises_exception_if_cant_convert_output_to_card(self):
        self.process.stdout.readline.return_value = 'error'
        with self.assertRaises(EngineWrapperException):
            self.wrapper.move([DurakCard('7H'), DurakCard('8H')])

    def test_respond_converts_output_to_card(self):
        self.process.stdout.readline.return_value = '8H'

        result = self.wrapper.respond([DurakCard('7H')])

        self.assertEqual(result, DurakCard('8H'))
        self.process.stdin.write.assert_called_once_with('respond 7H\n')
        self.assertTrue(self.process.stdout.readline.called)

    def test_respond_returns_none_if_no_output_given(self):
        self.process.stdout.readline.return_value = ''

        result = self.wrapper.respond([DurakCard('7H')])

        self.assertTrue(result is None)
        self.process.stdin.write.assert_called_once_with('respond 7H\n')
        self.assertTrue(self.process.stdout.readline.called)

    def test_respond_raises_exception_if_cant_convert_output_to_card(self):
        self.process.stdout.readline.return_value = 'error'
        with self.assertRaises(EngineWrapperException):
            self.wrapper.respond([DurakCard('7H')])

    def test_give_more_converts_output_to_cards(self):
        self.process.stdout.readline.return_value = '8H 9H'

        results = self.wrapper.give_more([DurakCard('7H')])

        self.assertItemsEqual(results, [DurakCard('8H'), DurakCard('9H')])
        self.process.stdin.write.assert_called_once_with('give_more 7H\n')
        self.assertTrue(self.process.stdout.readline.called)

    def test_give_more_returns_none_if_no_output_given(self):
        self.process.stdout.readline.return_value = ''

        results = self.wrapper.give_more([DurakCard('7H')])

        self.assertTrue(results is None)
        self.process.stdin.write.assert_called_once_with('give_more 7H\n')
        self.assertTrue(self.process.stdout.readline.called)

    def test_give_more_raises_exception_if_cant_convert_output_to_cards(self):
        self.process.stdout.readline.return_value = 'error'
        with self.assertRaises(EngineWrapperException):
            self.wrapper.give_more([DurakCard('7H')])
Beispiel #2
0
class EngineWrapperTest(unittest.TestCase):
    def setUp(self):
        self._subprocess_patcher = patch("durak.engine.wrapper.subprocess")
        self.subprocess_mock = self._subprocess_patcher.start()
        self.process = self.subprocess_mock.Popen.return_value
        self.process.stdout.readline.return_value = "ok"

        self.wrapper = EngineWrapper("path_to_engine")

    def tearDown(self):
        self._subprocess_patcher.stop()

    def test_popen_arguments(self):
        _ = EngineWrapper("path_to_engine")
        self.subprocess_mock.Popen.assert_called_with(
            "path_to_engine", stdin=self.subprocess_mock.PIPE, stdout=self.subprocess_mock.PIPE
        )

    def test_write_outputs_stripped_line_plus_break(self):
        self.wrapper._write("hi there!    ")
        self.process.stdin.write.assert_called_once_with("hi there!\n")

    def test_write_command_without_gamedata(self):
        self.wrapper._write_command("init", [DurakCard("AH")])
        self.process.stdin.write.assert_called_once_with("init AH\n")

    def test_write_command_with_gamedata(self):
        self.wrapper._write_command("init", [DurakCard("AH")], gamedata={"some": "value"})
        self.process.stdin.write.assert_called_once_with('init AH ## {"some": "value"}\n')

    def test_write_command_without_cards(self):
        self.wrapper._write_command("move", [])
        self.process.stdin.write.assert_called_once_with("move\n")

    def test_get_output(self):
        self.process.stdout.readline.return_value = "hi there!   "
        self.assertEqual(self.wrapper._get_output(), "hi there!")

    def test_game_end_finishes_game_and_kills_process(self):
        self.assertFalse(self.wrapper._process is None)

        self.wrapper.game_end()

        self.assertTrue(self.process.kill.called)
        self.process.stdin.write.assert_called_once_with("game_end\n")
        self.assertTrue(self.wrapper._process is None)

    def test_game_end_is_idempotent(self):
        self.assertFalse(self.wrapper._process is None)

        self.wrapper.game_end()

        self.assertTrue(self.process.kill.called)
        self.assertTrue(self.wrapper._process is None)

        self.wrapper.game_end()
        self.assertTrue(self.wrapper._process is None)

    def test_init_writes_command_and_reads_output(self):
        self.wrapper.init(DurakCard("7H"))

        self.process.stdin.write.assert_called_once_with("init 7H\n")
        self.assertTrue(self.process.stdout.readline.called)

    def test_init_raises_exception_if_engine_response_is_not_ok(self):
        self.process.stdout.readline.return_value = "error!"
        with self.assertRaises(EngineWrapperException):
            self.wrapper.init(DurakCard("7H"))

    def test_deal_writes_command_and_reads_output(self):
        self.wrapper.deal([DurakCard("7H"), DurakCard("8H")])

        self.process.stdin.write.assert_called_once_with("deal 7H 8H\n")
        self.assertTrue(self.process.stdout.readline.called)

    def test_deal_raises_exception_if_engine_response_is_not_ok(self):
        self.process.stdout.readline.return_value = "error!"
        with self.assertRaises(EngineWrapperException):
            self.wrapper.deal([DurakCard("7H"), DurakCard("8H")])

    def test_move_converts_output_to_card(self):
        self.process.stdout.readline.return_value = "8S"

        result = self.wrapper.move([DurakCard("7H"), DurakCard("8H")])

        self.assertEqual(result, DurakCard("8S"))
        self.process.stdin.write.assert_called_once_with("move 7H 8H\n")
        self.assertTrue(self.process.stdout.readline.called)

    def test_move_returns_none_if_no_output_given(self):
        self.process.stdout.readline.return_value = ""

        result = self.wrapper.move([DurakCard("7H"), DurakCard("8H")])

        self.assertTrue(result is None)
        self.process.stdin.write.assert_called_once_with("move 7H 8H\n")
        self.assertTrue(self.process.stdout.readline.called)

    def test_move_raises_exception_if_cant_convert_output_to_card(self):
        self.process.stdout.readline.return_value = "error"
        with self.assertRaises(EngineWrapperException):
            self.wrapper.move([DurakCard("7H"), DurakCard("8H")])

    def test_respond_converts_output_to_card(self):
        self.process.stdout.readline.return_value = "8H"

        result = self.wrapper.respond([DurakCard("7H")])

        self.assertEqual(result, DurakCard("8H"))
        self.process.stdin.write.assert_called_once_with("respond 7H\n")
        self.assertTrue(self.process.stdout.readline.called)

    def test_respond_returns_none_if_no_output_given(self):
        self.process.stdout.readline.return_value = ""

        result = self.wrapper.respond([DurakCard("7H")])

        self.assertTrue(result is None)
        self.process.stdin.write.assert_called_once_with("respond 7H\n")
        self.assertTrue(self.process.stdout.readline.called)

    def test_respond_raises_exception_if_cant_convert_output_to_card(self):
        self.process.stdout.readline.return_value = "error"
        with self.assertRaises(EngineWrapperException):
            self.wrapper.respond([DurakCard("7H")])

    def test_give_more_converts_output_to_cards(self):
        self.process.stdout.readline.return_value = "8H 9H"

        results = self.wrapper.give_more([DurakCard("7H")])

        self.assertItemsEqual(results, [DurakCard("8H"), DurakCard("9H")])
        self.process.stdin.write.assert_called_once_with("give_more 7H\n")
        self.assertTrue(self.process.stdout.readline.called)

    def test_give_more_returns_none_if_no_output_given(self):
        self.process.stdout.readline.return_value = ""

        results = self.wrapper.give_more([DurakCard("7H")])

        self.assertTrue(results is None)
        self.process.stdin.write.assert_called_once_with("give_more 7H\n")
        self.assertTrue(self.process.stdout.readline.called)

    def test_give_more_raises_exception_if_cant_convert_output_to_cards(self):
        self.process.stdout.readline.return_value = "error"
        with self.assertRaises(EngineWrapperException):
            self.wrapper.give_more([DurakCard("7H")])