def testSerializableEqual(self): testobj1 = _RPCTestClass() testobj2 = _RPCTestClass() self.assertEqual(testobj1, testobj2) self.assertFalse(testobj1 != testobj2) decoded1 = rpc.rpc_decode(_RPCTestClass, rpc.rpc_encode(testobj1)) decoded2 = rpc.rpc_decode(_RPCTestClass, rpc.rpc_encode(testobj2)) self.assertEqual(decoded1, decoded2) self.assertFalse(decoded1 != decoded2)
def testCardRPC(self): card = Card(common.HEART, 5) encoded = rpc.rpc_encode(card) self.assertFalse(isinstance(encoded, common.Card)) decoded = rpc.rpc_decode(common.Card, encoded) self.assert_(isinstance(decoded, common.Card)) self.assertEqual(card, decoded)
def testGame(self): iface = I() gamelist = iface.game_list() self.assert_(isinstance(gamelist, dict)) self.assertEqual(len(gamelist), 0) # register p_encoded = self._encoded_player() p_data = iface.player_register(p_encoded) # create game g_id = iface.game_create(p_data['akey']) # list gamelist = iface.game_list() self.assert_(gamelist.has_key(str(g_id))) players_raw = gamelist[str(g_id)] self.assert_(isinstance(players_raw, list)) # decode players = [rpc.rpc_decode(Player, pl) for pl in players_raw] self.assert_(p_data['id'] in [pl.id for pl in players]) # get_info info = iface.game_get_info(g_id) self.assert_(info == players_raw) # leave ret = iface.player_quit(p_data['akey']) self.assertEqual(ret, True) # after the only player leaves, the game should get deleted gamelist = iface.game_list() self.assertFalse(gamelist.has_key(g_id))
def testMessage(self): e = events.MessageEvent('from', 'lorem ipsum') e2 = rpc.rpc_decode(events.Event, rpc.rpc_encode(e)) self.assert_(isinstance(e2, events.MessageEvent)) self.assertEqual(e.type, e2.type) self.assertEqual(e.sender, e2.sender) self.assertEqual(e.message, e2.message)
def testSuitRPC(self): heart = common.HEART encoded = rpc.rpc_encode(heart) decoded = rpc.rpc_decode(common.Suit, encoded) self.assert_(isinstance(decoded, common.Suit)) self.assertEqual(heart.value, decoded.value) self.assertEqual(heart.name, decoded.name)
def testGameStateRPC(self): gs = common.GameState() gs.table.append(Card(common.HEART, 5)) encoded = rpc.rpc_encode(gs) gs2 = rpc.rpc_decode(common.GameState, encoded) self.assert_(isinstance(gs2, common.GameState)) self.assert_(isinstance(gs2.table, gs.table.__class__)) self.assertEqual(len(gs2.table), len(gs.table))
def testSerializeSimple(self): testobj = _RPCTestClass() encoded = rpc.rpc_encode(testobj) self.assertEqual(encoded, {"a": 1, "b": "2"}) # change something encoded["a"] = 2 decoded = rpc.rpc_decode(_RPCTestClass, encoded) self.assert_(isinstance(decoded, _RPCTestClass)) self.assertEqual(decoded.a, 2)
def testStateChanged(self): cls = events.StateChangedEvent game_state = common.GameState() game_state.state = common.GameState.VOTING e = cls(game_state) self.assert_(e.game_state is not None) e2 = rpc.rpc_decode(events.Event, rpc.rpc_encode(e)) self.assert_(isinstance(e2, cls)) self.assertEqual(e.type, e2.type) self.assertEqual(e.game_state, e2.game_state)
def testEventList(self): el = events.EventList() el.append(events.MessageEvent('a', 'b')) el.append(events.MessageEvent('c', 'd')) num = len(el) self.assertEqual(num, 2) el2 = rpc.rpc_decode(events.EventList, rpc.rpc_encode(el)) self.assert_(isinstance(el2, events.EventList)) self.assertEqual(len(el2), num) for i in range(0, num): self.assertEqual(el[i], el2[i])
def testCardPlayed(self): cls = events.CardPlayedEvent plr = players.Player('Seppo') card = common.Card(common.HEART, 5) e = cls(plr, card) e2 = rpc.rpc_decode(events.Event, rpc.rpc_encode(e)) self.assert_(isinstance(e2, cls)) self.assertEqual(e.type, e2.type) self.assertEqual(e.player.player_name, e2.player.player_name) self.assertEqual(e.card, e2.card) self.assertEqual(e2.game_state, None)
def testFullGame(self): iface = I() plrs = [] for i in range(1, 5): plrs.append(self._encoded_player(str(i))) p_datas = [] for p in plrs: p_datas.append(iface.player_register(p)) g_id = iface.game_create(p_datas[0]['akey']) ret = iface.game_enter(p_datas[1]['akey'], g_id) self.assertEqual(ret, g_id) ret = iface.game_enter(p_datas[2]['akey'], g_id) self.assertEqual(ret, g_id) ret = iface.game_enter(p_datas[3]['akey'], g_id) self.assertEqual(ret, g_id) gamelist = iface.game_list() self.assert_(gamelist.has_key(str(g_id))) players = gamelist[str(g_id)] self.assertEqual(len(players), len(p_datas)) # decode players = [rpc.rpc_decode(Player, pl) for pl in players] for p_data in p_datas: self.assert_(p_data['id'] in [pl.id for pl in players]) state = iface.game_get_state(p_datas[0]['akey'], g_id) self.assert_(state.has_key('game_state')) self.assertEqual(state['game_state']['state'], 0) try: ret = iface.game_start(p_datas[0]['akey'], g_id) self.assertEqual(ret, True) state = iface.game_get_state(p_datas[0]['akey'], g_id) self.assert_(state.has_key('game_state')) self.assertEqual(state['game_state']['state'], 1) self.assert_(state.has_key('hand')) ret = iface.game_leave(p_datas[0]['akey'], g_id) self.assertEqual(ret, True) for p_data in p_datas: ret = iface.player_quit(p_data['akey']) self.assertEqual(ret, True) finally: for game in iface.games: game._reset()
def testRegisterPlayer(self): iface = I() encoded = self._encoded_player() p_data = iface.player_register(encoded) self.assert_(isinstance(p_data['id'], basestring)) self.assert_(isinstance(p_data['akey'], basestring)) plr = iface._ensure_auth(p_data['akey']) self.assertEqual(plr.id, p_data['id']) # list players players_raw = iface.player_list(p_data['akey']) self.assert_(isinstance(players_raw, list)) players = [rpc.rpc_decode(Player, pl) for pl in players_raw] me = None for pl in players: if pl.id == p_data['id']: me = pl break self.assert_(me is not None) self.assertEqual(me.player_name, encoded['player_name']) iface._clear_auth()
def testSerializeCustom(self): testobj = _CustomClass() encoded = rpc.rpc_encode(testobj) self.assertEqual(encoded, "hilipati") decoded = rpc.rpc_decode(_CustomClass, encoded) self.assertEqual(decoded, "joop")
def testEventListEmpty(self): el = events.EventList() self.assertEqual(len(el), 0) el2 = rpc.rpc_decode(events.EventList, rpc.rpc_encode(el)) self.assert_(isinstance(el2, events.EventList)) self.assertEqual(len(el2), 0)
def testMessageEmpty(self): cls = events.MessageEvent e = cls() e2 = rpc.rpc_decode(events.Event, rpc.rpc_encode(e)) self.assert_(isinstance(e2, cls)) self.assertEqual(e.type, e2.type)
def testEvent(self, cls=None): cls = events.Event e = cls() e2 = rpc.rpc_decode(events.Event, rpc.rpc_encode(e)) self.assert_(isinstance(e2, cls)) self.assertEqual(e.type, e2.type)