def test_game_pick_taken(self): game = self._init_game() game.apply_event(Event(Player('bob'), PickingMove(Piece(0)))) game.apply_event(Event(Player('sam'), PlacementMove(0, 0))) with self.assertRaises(Exception): game.apply_event(Event(Player('sam'), PickingMove(Piece(0))))
def test_game_good_placement(self): game = self._init_game() game.apply_event(Event(Player('bob'), PickingMove(Piece(0)))) game.apply_event(Event(Player('sam'), PlacementMove(0, 0))) self.assertEqual(game.turn, 2) self.assertEqual(game.next_piece, None)
def test_game_2nd_pick_good(self): game = self._init_game() game.apply_event(Event(Player('bob'), PickingMove(Piece(0)))) game.apply_event(Event(Player('sam'), PlacementMove(0, 0))) game.apply_event(Event(Player('sam'), PickingMove(Piece(1)))) self.assertEqual(game.turn, 3) self.assertEqual(game.next_piece.value, 1)
def placement_move(self, game_uuid, player_name, x, y): game = self.load_game(game_uuid) player = Player(player_name) move = PlacementMove(x, y) self.apply_event(game, player, move) self._store_game(game, game_uuid) return game
def test_game_good_move(self): game = self._init_game() game.apply_event(Event(Player('bob'), PickingMove(Piece(0)))) self.assertEqual(game.turn, 1) self.assertEqual(game.next_piece, Piece(0))
def picking_move(self, game_uuid, player_name, number): game = self.load_game(game_uuid) player = Player(player_name) piece = Piece(number) move = PickingMove(piece) self.apply_event(game, player, move) self._store_game(game, game_uuid) return game
def test_to_dict(self): self.assertEqual( Event(Player('bob'), PickingMove(Piece(2))).to_dict(), { 'player': { 'name': 'bob' }, 'move': { 'type': 'PickingMove', 'piece': { 'value': 2 } } })
def test_game_victory_for_bob(self): game = self._init_game() game.apply_event(Event(Player('bob'), PickingMove(Piece(0)))) game.apply_event(Event(Player('sam'), PlacementMove(0, 0))) game.apply_event(Event(Player('sam'), PickingMove(Piece(1)))) game.apply_event(Event(Player('bob'), PlacementMove(0, 1))) game.apply_event(Event(Player('bob'), PickingMove(Piece(2)))) game.apply_event(Event(Player('sam'), PlacementMove(0, 2))) game.apply_event(Event(Player('sam'), PickingMove(Piece(3)))) self.assertEqual(game.winner, None) game.apply_event(Event(Player('bob'), PlacementMove(0, 3))) self.assertEqual(game.winner, Player('bob')) with self.assertRaises(Exception): game.apply_event(Event(Player('bob'), PickingMove(Piece(4))))
def join_game(self, game_uuid, player_name): game = self.load_game(game_uuid) player = Player(player_name) game.join_game(player) self._store_game(game, game_uuid) return game
def create_game(self, player_name): game = Game() game.join_game(Player(player_name)) game_uuid = self._store_game(game) return game_uuid, game
def test_game_bad_player(self): game = self._init_game() with self.assertRaises(Exception): game.apply_event(Event(Player('sam'), PickingMove(Piece(1))))
def test_game_bad_move(self): game = self._init_game() with self.assertRaises(Exception): game.apply_event(Event(Player('bob'), PlacementMove(0, 1)))
def _init_game(self): game = Game() game.player_a = Player('bob') game.player_b = Player('sam') return game
def test_from_dict_crappy(self): self.assertNotEqual(Player.from_dict({'name': 'sam'}), Player('same'))
def test_from_dict(self): self.assertEqual(Player.from_dict({'name': 'sam'}), Player('sam'))
def test_to_dict(self): self.assertEqual(Player('sam').to_dict(), {'name': 'sam'})