import json
import os
import tempfile
from unittest import TestCase

import pytest

from donjuan import Dungeon, DungeonRandomizer, Renderer


class RendererTest(TestCase):
    def setUp(self):
        super().setUp()
        self.TEMP_DIR = tempfile.mkdtemp()

    def test_smoke(self):
        r = Renderer()
        assert r is not None

    def test_scale(self):
        r = Renderer(scale=3)
        assert r.scale == 3

    @pytest.mark.slow
    def test_render_dummy_dungeon(self):
        inpath = os.path.abspath(os.path.dirname(__file__))
        inpath = os.path.join(inpath, "fixtures/dummy_dungeon.json")
        with open(inpath, "r") as f:
            darr = json.load(f)["dungeon"]
        n_rows = len(darr)
        n_cols = len(darr)
        dungeon = Dungeon(n_rows=n_rows, n_cols=n_cols)
        for i in range(n_rows):
            for j in range(n_cols):
                dungeon.grid.cells[i][j].filled = bool(darr[i][j])

        # Render and check for the file
        fp = os.path.join(self.TEMP_DIR, "rendered_dungeon.png")
        r = Renderer()
        r.render(dungeon, file_path=fp)
        assert os.path.exists(fp)

    @pytest.mark.slow
    def test_render_dungeon_with_rooms(self):
        randomizer = DungeonRandomizer()
        dungeon = Dungeon(10, 10, randomizers=[randomizer])
        dungeon.randomize()
        dungeon.emplace_rooms()
        renderer = Renderer()

        # Render and check for the file
        fp = os.path.join(self.TEMP_DIR, "rendered_dungeon.png")
        renderer.render(dungeon, file_path=fp)
        assert os.path.exists(fp)

[((242, 260), 'tempfile.mkdtemp', 'tempfile.mkdtemp', ([], {}), '()\n', (258, 260), False, 'import tempfile\n'), ((300, 310), 'donjuan.Renderer', 'Renderer', ([], {}), '()\n', (308, 310), False, 'from donjuan import Dungeon, DungeonRandomizer, Renderer\n'), ((379, 396), 'donjuan.Renderer', 'Renderer', ([], {'scale': '(3)'}), '(scale=3)\n', (387, 396), False, 'from donjuan import Dungeon, DungeonRandomizer, Renderer\n'), ((566, 617), 'os.path.join', 'os.path.join', (['inpath', '"""fixtures/dummy_dungeon.json"""'], {}), "(inpath, 'fixtures/dummy_dungeon.json')\n", (578, 617), False, 'import os\n'), ((770, 807), 'donjuan.Dungeon', 'Dungeon', ([], {'n_rows': 'n_rows', 'n_cols': 'n_cols'}), '(n_rows=n_rows, n_cols=n_cols)\n', (777, 807), False, 'from donjuan import Dungeon, DungeonRandomizer, Renderer\n'), ((997, 1048), 'os.path.join', 'os.path.join', (['self.TEMP_DIR', '"""rendered_dungeon.png"""'], {}), "(self.TEMP_DIR, 'rendered_dungeon.png')\n", (1009, 1048), False, 'import os\n'), ((1061, 1071), 'donjuan.Renderer', 'Renderer', ([], {}), '()\n', (1069, 1071), False, 'from donjuan import Dungeon, DungeonRandomizer, Renderer\n'), ((1127, 1145), 'os.path.exists', 'os.path.exists', (['fp'], {}), '(fp)\n', (1141, 1145), False, 'import os\n'), ((1236, 1255), 'donjuan.DungeonRandomizer', 'DungeonRandomizer', ([], {}), '()\n', (1253, 1255), False, 'from donjuan import Dungeon, DungeonRandomizer, Renderer\n'), ((1274, 1315), 'donjuan.Dungeon', 'Dungeon', (['(10)', '(10)'], {'randomizers': '[randomizer]'}), '(10, 10, randomizers=[randomizer])\n', (1281, 1315), False, 'from donjuan import Dungeon, DungeonRandomizer, Renderer\n'), ((1395, 1405), 'donjuan.Renderer', 'Renderer', ([], {}), '()\n', (1403, 1405), False, 'from donjuan import Dungeon, DungeonRandomizer, Renderer\n'), ((1460, 1511), 'os.path.join', 'os.path.join', (['self.TEMP_DIR', '"""rendered_dungeon.png"""'], {}), "(self.TEMP_DIR, 'rendered_dungeon.png')\n", (1472, 1511), False, 'import os\n'), ((1574, 1592), 'os.path.exists', 'os.path.exists', (['fp'], {}), '(fp)\n', (1588, 1592), False, 'import os\n'), ((522, 547), 'os.path.dirname', 'os.path.dirname', (['__file__'], {}), '(__file__)\n', (537, 547), False, 'import os\n'), ((674, 686), 'json.load', 'json.load', (['f'], {}), '(f)\n', (683, 686), False, 'import json\n')]