def test_reading_and_writing_compact(self): file_match = re.compile(r'.*\.rep') files = [] def process_line(line): line = re.sub(r'\s*,\s*', ',', line) line = re.sub(r'\s*\(\s*', '(', line) line = re.sub(r'\s+\)', ')', line) return re.sub(r'(^\s+)|(\s*(;.*)?$)', '', line) def get_files_from(folder_name): for file in listdir(folder_name): if file_match.match(file): files.append(folder_name + "/" + file) elif isdir(folder_name + "/" + file): get_files_from(folder_name + "/" + file) get_files_from("tests/replays/compact") for rfile in files: replay = Replay() replay.read(rfile) output = StringIO() replay.write(output) f = open(rfile, 'r') file_string = f.read() f.close() file_string = "\n".join(map(process_line, file_string.split("\n"))) self.assertEqual(output.getvalue(), file_string, "File '" + rfile + "' did not match")
def test_json_saving(self): self.maxDiff = 6000 deck1 = hearthbreaker.game_objects.Deck( [RagnarosTheFirelord() for i in range(0, 30)], CHARACTER_CLASS.MAGE) deck2 = hearthbreaker.game_objects.Deck( [StonetuskBoar() for i in range(0, 30)], CHARACTER_CLASS.DRUID) agent1 = PlayAndAttackAgent() agent2 = OneCardPlayingAgent() random.seed(4879) game = Game([deck1, deck2], [agent1, agent2]) replay = record(game) game.pre_game() for turn in range(0, 17): game.play_single_turn() output = StringIO() replay.write_json(output) inp = StringIO(output.getvalue()) new_replay = Replay() new_replay.read_json(inp) old_output = output.getvalue() other_output = StringIO() new_replay.write_json(other_output) self.assertEqual(other_output.getvalue(), old_output)
def test_random_character_saving(self): deck1 = hearthbreaker.engine.Deck( [RagnarosTheFirelord() for i in range(0, 30)], Jaina()) deck2 = hearthbreaker.engine.Deck( [StonetuskBoar() for i in range(0, 30)], Malfurion()) agent1 = PlayAndAttackAgent() agent2 = OneCardPlayingAgent() random.seed(4879) game = Game([deck1, deck2], [agent1, agent2]) replay = record(game) game.pre_game() for turn in range(0, 17): game.play_single_turn() output = StringIO() replay.write_json(output) random.seed(4879) new_game = playback(Replay(StringIO(output.getvalue()))) new_game.pre_game() for turn in range(0, 17): new_game.play_single_turn() self.assertEqual(2, len(new_game.current_player.minions)) self.assertEqual(30, new_game.other_player.hero.health) self.assertEqual(5, len(new_game.other_player.minions))
def test_SoulOfTheForest(self): game = playback(Replay("tests/replays/card_tests/SoulOfTheForest.hsreplay")) game.start() self.assertEqual(2, len(game.other_player.minions)) self.assertEqual(2, game.other_player.minions[0].calculate_attack()) self.assertEqual(2, game.other_player.minions[0].health) self.assertEqual("Treant", game.other_player.minions[0].card.name)
def test_BlessingOfWisdom(self): game = playback(Replay("tests/replays/card_tests/BlessingOfWisdom.hsreplay")) game.start() self.assertEqual(3, len(game.current_player.minions)) # 7 cards have been drawn. # 3 for starting first, 3 for new turn and 1 for minion attack with Blessing of Wisdom # (the second minion who had it got silenced) self.assertEqual(23, game.other_player.deck.left)
def test_option_replay(self): game = playback(Replay("tests/replays/stonetusk_power.hsreplay")) game.start() self.assertEqual(1, len(game.other_player.minions)) panther = game.other_player.minions[0] self.assertEqual(panther.card.name, "Panther") self.assertEqual(panther.health, 3) self.assertEqual(panther.calculate_attack(), 4) self.assertEqual(panther.index, 0)
def test_ArcaneMissilesWithSpellPower(self): game = playback(Replay("tests/replays/card_tests/ArcaneMissilesWithSpellDamage.hsreplay")) game.start() self.assertEqual(1, len(game.current_player.minions)) self.assertEqual(1, len(game.other_player.minions)) self.assertEqual(1, game.other_player.minions[0].health) self.assertEqual(2, game.other_player.minions[0].calculate_max_health()) self.assertEqual(27, game.other_player.hero.health) return game
def test_loading_game(self): game = playback(Replay("tests/replays/example.hsreplay")) game.start() self.assertEqual(game.current_player.deck.hero.name, "Malfurion Stormrage") self.assertEqual(game.other_player.deck.hero.name, "Jaina Proudmoore") self.assertEqual(game.current_player.hero.health, 29) self.assertTrue(game.current_player.hero.dead)
def test_loading_game(self): game = playback(Replay("tests/replays/example.hsreplay")) game.start() self.assertEqual(game.current_player.deck.character_class, CHARACTER_CLASS.DRUID) self.assertEqual(game.other_player.deck.character_class, CHARACTER_CLASS.MAGE) self.assertEqual(game.current_player.hero.health, 29) self.assertTrue(game.current_player.hero.dead)
def test_reading_and_writing(self): def process_line(line): line = re.sub(r'\s*,\s*', ',', line) line = re.sub(r'\s*\(\s*', '(', line) line = re.sub(r'\s+\)', ')', line) return re.sub(r'(^\s+)|(\s*(;.*)?$)', '', line) self.maxDiff = None for rfile in filter(lambda file: re.compile(r'.*\.rep$').match(file), listdir("tests/replays")): replay = Replay() replay.parse_replay("tests/replays/" + rfile) output = StringIO() replay.write_replay(output) f = open("tests/replays/" + rfile, 'r') file_string = f.read() f.close() file_string = "\n".join(map(process_line, file_string.split("\n"))) self.assertEqual(output.getvalue(), file_string)
def test_NobleSacrifice(self): game = generate_game_for(NobleSacrifice, StonetuskBoar, CardTestingAgent, PlayAndAttackAgent) game.play_single_turn() # NobleSacrifice should be played self.assertEqual(1, len(game.players[0].secrets)) self.assertEqual("Noble Sacrifice", game.players[0].secrets[0].name) game.play_single_turn() # Attack with Stonetusk should happen, and the secret should trigger. Both minions should die. self.assertEqual(0, len(game.players[0].secrets)) self.assertEqual(0, len(game.players[0].minions)) self.assertEqual(0, len(game.players[1].minions)) self.assertEqual(30, game.players[0].hero.health) # Test with 7 minions game = playback(Replay("tests/replays/card_tests/NobleSacrifice.hsreplay")) game.start() self.assertEqual(7, len(game.players[0].minions)) self.assertEqual(29, game.players[0].hero.health) self.assertEqual(1, len(game.players[0].secrets)) self.assertEqual("Noble Sacrifice", game.players[0].secrets[0].name)
def test_Shadowform(self): game = generate_game_for(IronfurGrizzly, Shadowform, OneCardPlayingAgent, PredictableAgent) for turn in range(0, 9): game.play_single_turn() self.assertEqual("Lesser Heal", game.players[1].hero.power.__str__()) # Shadowform should be played game.play_single_turn() self.assertEqual("Mind Spike", game.players[1].hero.power.__str__()) # Nothing special game.play_single_turn() # Mind Spike should be used, and Shadowform should be played game.play_single_turn() self.assertEqual(1, game.players[0].minions[0].health) self.assertEqual("Mind Shatter", game.players[1].hero.power.__str__()) # Nothing special game.play_single_turn() self.assertEqual(5, len(game.players[0].minions)) # Mind Shatter should be used, and Shadowform should be played # (but nothing will happen, we are already at Mind Shatter) game.play_single_turn() self.assertEqual("Mind Shatter", game.players[1].hero.power.__str__()) self.assertEqual(4, len(game.players[0].minions)) # Test using the hero power, then cast Shadowform and use the new power (this is possible) game = playback(Replay("tests/replays/card_tests/Shadowform.hsreplay")) game.start() self.assertEqual(10, game.players[0].max_mana) self.assertEqual(3, game.players[0].mana) self.assertEqual(28, game.players[1].hero.health)