def test_compact_to_json_conversion(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)
            json_output = StringIO()
            replay.write_json(json_output)
            json_replay = Replay()
            json_input = StringIO(json_output.getvalue())
            json_replay.read_json(json_input)
            output = StringIO()
            json_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")
Esempio n. 2
0
    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_json_saving(self):
        self.maxDiff = 6000
        deck1 = hearthbreaker.engine.Deck([RagnarosTheFirelord() for i in range(0, 30)], CHARACTER_CLASS.MAGE)
        deck2 = hearthbreaker.engine.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)
Esempio n. 4
0
    def test_compact_to_json_conversion(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)
            json_output = StringIO()
            replay.write_json(json_output)
            json_replay = Replay()
            json_input = StringIO(json_output.getvalue())
            json_replay.read_json(json_input)
            output = StringIO()
            json_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")