comparison src/test/test_plugin_misc_room_game.py @ 829:187d2443c82d

test: improvements for the helpers classes: - simplification for retrieving the sent messages - an expected bridge call which is already set can no more be overwritten with new arguments for the same method name. The new arguments are stored in a FIFO list instead, and each call of the method also registers the arguments for the next call.
author souliane <souliane@mailoo.org>
date Wed, 15 Jan 2014 23:22:07 +0100
parents 8f335c03eebb
children 3c270d691e56
comparison
equal deleted inserted replaced
828:8f335c03eebb 829:187d2443c82d
68 68
69 def initGame(self, muc_index, user_index): 69 def initGame(self, muc_index, user_index):
70 self.plugin_0045.joinRoom(user_index, muc_index) 70 self.plugin_0045.joinRoom(user_index, muc_index)
71 self.plugin._initGame(Const.MUC_STR[muc_index], Const.JID[user_index].user) 71 self.plugin._initGame(Const.MUC_STR[muc_index], Const.JID[user_index].user)
72 72
73 def expectedMessage(self, to, type_, tag, players=[]): 73 def _expectedMessage(self, to, type_, tag, players=[]):
74 content = "<%s" % tag 74 content = "<%s" % tag
75 if not players: 75 if not players:
76 content += "/>" 76 content += "/>"
77 else: 77 else:
78 content += ">" 78 content += ">"
140 140
141 def test_synchronizeRoom(self): 141 def test_synchronizeRoom(self):
142 self.init() 142 self.init()
143 self.initGame(0, 0) 143 self.initGame(0, 0)
144 self.plugin._synchronizeRoom(ROOM_JID_S, [Const.MUC[0]], Const.PROFILE[0]) 144 self.plugin._synchronizeRoom(ROOM_JID_S, [Const.MUC[0]], Const.PROFILE[0])
145 self.assertEqual(self.host.getSentMessage(0, 0), self.expectedMessage(ROOM_JID_S, "groupchat", "players", [])) 145 self.assertEqual(self.host.getSentMessage(0), self._expectedMessage(ROOM_JID_S, "groupchat", "players", []))
146 self.plugin.games[ROOM_JID_S]['players'].append("test1") 146 self.plugin.games[ROOM_JID_S]['players'].append("test1")
147 self.plugin._synchronizeRoom(ROOM_JID_S, [Const.MUC[0]], Const.PROFILE[0]) 147 self.plugin._synchronizeRoom(ROOM_JID_S, [Const.MUC[0]], Const.PROFILE[0])
148 self.assertEqual(self.host.getSentMessage(1, 0), self.expectedMessage(ROOM_JID_S, "groupchat", "players", ["test1"])) 148 self.assertEqual(self.host.getSentMessage(0), self._expectedMessage(ROOM_JID_S, "groupchat", "players", ["test1"]))
149 self.plugin.games[ROOM_JID_S]['started'] = True 149 self.plugin.games[ROOM_JID_S]['started'] = True
150 self.plugin.games[ROOM_JID_S]['players'].append("test2") 150 self.plugin.games[ROOM_JID_S]['players'].append("test2")
151 self.plugin._synchronizeRoom(ROOM_JID_S, [Const.MUC[0]], Const.PROFILE[0]) 151 self.plugin._synchronizeRoom(ROOM_JID_S, [Const.MUC[0]], Const.PROFILE[0])
152 self.assertEqual(self.host.getSentMessage(2, 0), self.expectedMessage(ROOM_JID_S, "groupchat", "started", ["test1", "test2"])) 152 self.assertEqual(self.host.getSentMessage(0), self._expectedMessage(ROOM_JID_S, "groupchat", "started", ["test1", "test2"]))
153 self.plugin.games[ROOM_JID_S]['players'].append("test3") 153 self.plugin.games[ROOM_JID_S]['players'].append("test3")
154 self.plugin.games[ROOM_JID_S]['players'].append("test4") 154 self.plugin.games[ROOM_JID_S]['players'].append("test4")
155 user1 = JID(ROOM_JID_S + "/" + Const.JID[0].user) 155 user1 = JID(ROOM_JID_S + "/" + Const.JID[0].user)
156 user2 = JID(ROOM_JID_S + "/" + Const.JID[1].user) 156 user2 = JID(ROOM_JID_S + "/" + Const.JID[1].user)
157 self.plugin._synchronizeRoom(ROOM_JID_S, [user1, user2], Const.PROFILE[0]) 157 self.plugin._synchronizeRoom(ROOM_JID_S, [user1, user2], Const.PROFILE[0])
158 self.assertEqualXML(self.host.getSentMessage(3, 0), self.expectedMessage(user1.full(), "normal", "started", ["test1", "test2", "test3", "test4"])) 158 self.assertEqualXML(self.host.getSentMessage(0), self._expectedMessage(user1.full(), "normal", "started", ["test1", "test2", "test3", "test4"]))
159 self.assertEqualXML(self.host.getSentMessage(4, 0), self.expectedMessage(user2.full(), "normal", "started", ["test1", "test2", "test3", "test4"])) 159 self.assertEqualXML(self.host.getSentMessage(0), self._expectedMessage(user2.full(), "normal", "started", ["test1", "test2", "test3", "test4"]))
160 160
161 def test_invitePlayers(self): 161 def test_invitePlayers(self):
162 self.init() 162 self.init()
163 self.initGame(0, 0) 163 self.initGame(0, 0)
164 self.plugin_0045.joinRoom(0, 1) 164 self.plugin_0045.joinRoom(0, 1)
326 self.init(player_init={"xxx": "xyz"}) 326 self.init(player_init={"xxx": "xyz"})
327 other_players = [Const.JID_STR[1], Const.JID_STR[3]] 327 other_players = [Const.JID_STR[1], Const.JID_STR[3]]
328 self.plugin.prepareRoom(other_players, ROOM_JID_S, PROFILE) 328 self.plugin.prepareRoom(other_players, ROOM_JID_S, PROFILE)
329 nicks = [self.plugin_0045.getNick(0, 0)] 329 nicks = [self.plugin_0045.getNick(0, 0)]
330 330
331 self.assertEqual(self.host.countSentMessages(), [1, 0, 0, 0, 0]) # init messages 331 self.assertEqual(self.host.getSentMessage(0), self._expectedMessage(ROOM_JID_S, "groupchat", "players", nicks))
332 self.assertEqual(self.host.getSentMessage(0, 0), self.expectedMessage(ROOM_JID_S, "groupchat", "players", nicks))
333 self.assertTrue(len(self.plugin.invitations[ROOM_JID_S]) == 1) 332 self.assertTrue(len(self.plugin.invitations[ROOM_JID_S]) == 1)
334 333
335 # wrong profile 334 # wrong profile
336 user_nick = self.plugin_0045.joinRoom(0, 1) 335 user_nick = self.plugin_0045.joinRoom(0, 1)
337 room = self.plugin_0045.getRoom(0, 1) 336 room = self.plugin_0045.getRoom(0, 1)
338 self.plugin.userJoinedTrigger(room, User(user_nick, Const.JID[1]), OTHER_PROFILE) 337 self.plugin.userJoinedTrigger(room, User(user_nick, Const.JID[1]), OTHER_PROFILE)
339 self.assertEqual(self.host.countSentMessages(), [1, 0, 0, 0, 0]) # no new message has been sent 338 self.assertEqual(self.host.getSentMessageRaw(0), None) # no new message has been sent
340 self.assertFalse(self.plugin._gameExists(ROOM_JID_S, True)) # game not started 339 self.assertFalse(self.plugin._gameExists(ROOM_JID_S, True)) # game not started
341 340
342 # referee profile, user is allowed, wait for one more 341 # referee profile, user is allowed, wait for one more
343 room = self.plugin_0045.getRoom(0, 0) 342 room = self.plugin_0045.getRoom(0, 0)
344 self.plugin.userJoinedTrigger(room, User(user_nick, Const.JID[1]), PROFILE) 343 self.plugin.userJoinedTrigger(room, User(user_nick, Const.JID[1]), PROFILE)
345 nicks.append(user_nick) 344 nicks.append(user_nick)
346 self.assertEqual(self.host.countSentMessages(), [2, 0, 0, 0, 0]) 345 self.assertEqual(self.host.getSentMessage(0), self._expectedMessage(ROOM_JID_S, "groupchat", "players", nicks))
347 self.assertEqual(self.host.getSentMessage(1, 0), self.expectedMessage(ROOM_JID_S, "groupchat", "players", nicks))
348 self.assertFalse(self.plugin._gameExists(ROOM_JID_S, True)) # game not started 346 self.assertFalse(self.plugin._gameExists(ROOM_JID_S, True)) # game not started
349 347
350 # referee profile, user is not allowed 348 # referee profile, user is not allowed
351 user_nick = self.plugin_0045.joinRoom(0, 4) 349 user_nick = self.plugin_0045.joinRoom(0, 4)
352 self.plugin.userJoinedTrigger(room, User(user_nick, Const.JID[4]), PROFILE) 350 self.plugin.userJoinedTrigger(room, User(user_nick, Const.JID[4]), PROFILE)
353 self.assertEqual(self.host.countSentMessages(), [3, 0, 0, 0, 0]) 351 self.assertEqual(self.host.getSentMessage(0), self._expectedMessage(ROOM_JID_S + '/' + user_nick, "normal", "players", nicks))
354 self.assertEqual(self.host.getSentMessage(2, 0), self.expectedMessage(ROOM_JID_S + '/' + user_nick, "normal", "players", nicks))
355 self.assertFalse(self.plugin._gameExists(ROOM_JID_S, True)) # game not started 352 self.assertFalse(self.plugin._gameExists(ROOM_JID_S, True)) # game not started
356 353
357 # referee profile, user is allowed, everybody here 354 # referee profile, user is allowed, everybody here
358 user_nick = self.plugin_0045.joinRoom(0, 3) 355 user_nick = self.plugin_0045.joinRoom(0, 3)
359 self.plugin.userJoinedTrigger(room, User(user_nick, Const.JID[3]), PROFILE) 356 self.plugin.userJoinedTrigger(room, User(user_nick, Const.JID[3]), PROFILE)
360 nicks.append(user_nick) 357 nicks.append(user_nick)
361 self.assertEqual(self.host.getSentMessage(3, 0), self.expectedMessage(ROOM_JID_S, "groupchat", "started", nicks)) 358 self.assertEqual(self.host.getSentMessage(0), self._expectedMessage(ROOM_JID_S, "groupchat", "started", nicks))
362 self.assertTrue(self.plugin._gameExists(ROOM_JID_S, True)) # game started 359 self.assertTrue(self.plugin._gameExists(ROOM_JID_S, True)) # game started
363 self.assertTrue(len(self.plugin.invitations[ROOM_JID_S]) == 0) 360 self.assertTrue(len(self.plugin.invitations[ROOM_JID_S]) == 0)
364 361
365 # wait for none 362 # wait for none
366 self.init() 363 self.init()
367 self.plugin.prepareRoom(other_players, ROOM_JID_S, PROFILE) 364 self.plugin.prepareRoom(other_players, ROOM_JID_S, PROFILE)
368 self.assertEqual(self.host.countSentMessages(), [1, 0, 0, 0, 0]) # init messages 365 self.assertNotEqual(self.host.getSentMessageRaw(0), None) # init messages
369 room = self.plugin_0045.getRoom(0, 0) 366 room = self.plugin_0045.getRoom(0, 0)
370 nicks = [self.plugin_0045.getNick(0, 0)] 367 nicks = [self.plugin_0045.getNick(0, 0)]
371 user_nick = self.plugin_0045.joinRoom(0, 3) 368 user_nick = self.plugin_0045.joinRoom(0, 3)
372 self.plugin.userJoinedTrigger(room, User(user_nick, Const.JID[3]), PROFILE) 369 self.plugin.userJoinedTrigger(room, User(user_nick, Const.JID[3]), PROFILE)
373 nicks.append(user_nick) 370 nicks.append(user_nick)
374 self.assertEqual(self.host.getSentMessage(1, 0), self.expectedMessage(ROOM_JID_S, "groupchat", "started", nicks)) 371 self.assertEqual(self.host.getSentMessage(0), self._expectedMessage(ROOM_JID_S, "groupchat", "started", nicks))
375 self.assertTrue(self.plugin._gameExists(ROOM_JID_S, True)) 372 self.assertTrue(self.plugin._gameExists(ROOM_JID_S, True))
376 373
377 def test_userLeftTrigger(self): 374 def test_userLeftTrigger(self):
378 self.init(player_init={"xxx": "xyz"}) 375 self.init(player_init={"xxx": "xyz"})
379 other_players = [Const.JID_STR[1], Const.JID_STR[3], Const.JID_STR[4]] 376 other_players = [Const.JID_STR[1], Const.JID_STR[3], Const.JID_STR[4]]
457 454
458 # game not exists 455 # game not exists
459 self.plugin.createGame(ROOM_JID_S, nicks, PROFILE) 456 self.plugin.createGame(ROOM_JID_S, nicks, PROFILE)
460 self.assertTrue(self.plugin._gameExists(ROOM_JID_S, True)) 457 self.assertTrue(self.plugin._gameExists(ROOM_JID_S, True))
461 self.assertEqual(self.plugin.games[ROOM_JID_S]['players'], nicks) 458 self.assertEqual(self.plugin.games[ROOM_JID_S]['players'], nicks)
462 self.assertEqual(self.host.getSentMessage(0, 0), self.expectedMessage(ROOM_JID_S, "groupchat", "started", nicks)) 459 self.assertEqual(self.host.getSentMessage(0), self._expectedMessage(ROOM_JID_S, "groupchat", "started", nicks))
463 for nick in nicks: 460 for nick in nicks:
464 self.assertEqual('init', self.plugin.games[ROOM_JID_S]['status'][nick]) 461 self.assertEqual('init', self.plugin.games[ROOM_JID_S]['status'][nick])
465 self.assertEqual(self.plugin.player_init, self.plugin.games[ROOM_JID_S]['players_data'][nick]) 462 self.assertEqual(self.plugin.player_init, self.plugin.games[ROOM_JID_S]['players_data'][nick])
466 self.plugin.games[ROOM_JID_S]['players_data'][nick]["xxx"] = nick 463 self.plugin.games[ROOM_JID_S]['players_data'][nick]["xxx"] = nick
467 for nick in nicks: 464 for nick in nicks:
471 # game exists, current profile is referee 468 # game exists, current profile is referee
472 self.init(player_init={"xxx": "xyz"}) 469 self.init(player_init={"xxx": "xyz"})
473 self.initGame(0, 0) 470 self.initGame(0, 0)
474 self.plugin.games[ROOM_JID_S]['started'] = True 471 self.plugin.games[ROOM_JID_S]['started'] = True
475 self.plugin.createGame(ROOM_JID_S, nicks, PROFILE) 472 self.plugin.createGame(ROOM_JID_S, nicks, PROFILE)
476 self.assertEqual(self.host.getSentMessage(0, 0), self.expectedMessage(ROOM_JID_S, "groupchat", "started", nicks)) 473 self.assertEqual(self.host.getSentMessage(0), self._expectedMessage(ROOM_JID_S, "groupchat", "started", nicks))
477 474
478 # game exists, current profile is not referee 475 # game exists, current profile is not referee
479 self.init(player_init={"xxx": "xyz"}) 476 self.init(player_init={"xxx": "xyz"})
480 self.initGame(0, 0) 477 self.initGame(0, 0)
481 self.plugin.games[ROOM_JID_S]['started'] = True 478 self.plugin.games[ROOM_JID_S]['started'] = True
482 self.plugin_0045.joinRoom(0, 1) 479 self.plugin_0045.joinRoom(0, 1)
483 self.plugin.createGame(ROOM_JID_S, nicks, OTHER_PROFILE) 480 self.plugin.createGame(ROOM_JID_S, nicks, OTHER_PROFILE)
484 self.assertEqual(self.host.countSentMessages(), [0, 0, 0, 0, 0]) # no sync message has been sent by other_profile 481 self.assertEqual(self.host.getSentMessageRaw(0), None) # no sync message has been sent by other_profile