def test_get_action_success(self): # Test that RPP can successfully request, receive, and decode an Action rpp = RemotePlayerProxy('name', 1.0, self.dummy_socket) rpp._RemotePlayerProxy__socket = self.mock_socket self.mock_socket.recv.return_value = b'[[0, 1], [2, 2]]' state = State(self.__b, players=[self.__p1, self.__p2, self.__p3]) json_state = _state_to_json(state) expected_client_request = f'["take-turn", [{json.dumps(json_state)}, []]]' with patch.object(rpp, '_RemotePlayerProxy__send_message') as mock: action = rpp.get_action(state) mock.assert_called_with(expected_client_request) self.assertEqual(action, Action(Position(0, 1), Position(2, 2)))
def test_get_action_returns_position(self): # Test that receiving a position when asking for an action throws JsonDecodeException and returns None rpp = RemotePlayerProxy('name', 1.0, self.dummy_socket) rpp._RemotePlayerProxy__socket = self.mock_socket self.mock_socket.recv.return_value = b'[0, 1]' state = State(self.__b, players=[self.__p1, self.__p2, self.__p3]) json_state = _state_to_json(state) expected_client_request = f'["take-turn", [{json.dumps(json_state)}, []]]' with patch.object(rpp, '_RemotePlayerProxy__send_message') as mock: with self.assertRaises(JsonDecodeException): action = rpp.get_action(state) mock.assert_called_with(expected_client_request) self.assertEqual(action, None)