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)

[((14, 24, 14, 42), 'tempfile.mkdtemp', 'tempfile.mkdtemp', ({}, {}), '()', False, 'import tempfile\n'), ((17, 12, 17, 22), 'donjuan.Renderer', 'Renderer', ({}, {}), '()', False, 'from donjuan import Dungeon, DungeonRandomizer, Renderer\n'), ((21, 12, 21, 29), 'donjuan.Renderer', 'Renderer', (), '', False, 'from donjuan import Dungeon, DungeonRandomizer, Renderer\n'), ((27, 17, 27, 68), 'os.path.join', 'os.path.join', ({(27, 30, 27, 36): 'inpath', (27, 38, 27, 67): '"""fixtures/dummy_dungeon.json"""'}, {}), "(inpath, 'fixtures/dummy_dungeon.json')", False, 'import os\n'), ((32, 18, 32, 55), 'donjuan.Dungeon', 'Dungeon', (), '', False, 'from donjuan import Dungeon, DungeonRandomizer, Renderer\n'), ((38, 13, 38, 64), 'os.path.join', 'os.path.join', ({(38, 26, 38, 39): 'self.TEMP_DIR', (38, 41, 38, 63): '"""rendered_dungeon.png"""'}, {}), "(self.TEMP_DIR, 'rendered_dungeon.png')", False, 'import os\n'), ((39, 12, 39, 22), 'donjuan.Renderer', 'Renderer', ({}, {}), '()', False, 'from donjuan import Dungeon, DungeonRandomizer, Renderer\n'), ((41, 15, 41, 33), 'os.path.exists', 'os.path.exists', ({(41, 30, 41, 32): 'fp'}, {}), '(fp)', False, 'import os\n'), ((45, 21, 45, 40), 'donjuan.DungeonRandomizer', 'DungeonRandomizer', ({}, {}), '()', False, 'from donjuan import Dungeon, DungeonRandomizer, Renderer\n'), ((46, 18, 46, 59), 'donjuan.Dungeon', 'Dungeon', (), '', False, 'from donjuan import Dungeon, DungeonRandomizer, Renderer\n'), ((49, 19, 49, 29), 'donjuan.Renderer', 'Renderer', ({}, {}), '()', False, 'from donjuan import Dungeon, DungeonRandomizer, Renderer\n'), ((52, 13, 52, 64), 'os.path.join', 'os.path.join', ({(52, 26, 52, 39): 'self.TEMP_DIR', (52, 41, 52, 63): '"""rendered_dungeon.png"""'}, {}), "(self.TEMP_DIR, 'rendered_dungeon.png')", False, 'import os\n'), ((54, 15, 54, 33), 'os.path.exists', 'os.path.exists', ({(54, 30, 54, 32): 'fp'}, {}), '(fp)', False, 'import os\n'), ((26, 33, 26, 58), 'os.path.dirname', 'os.path.dirname', ({(26, 49, 26, 57): '__file__'}, {}), '(__file__)', False, 'import os\n'), ((29, 19, 29, 31), 'json.load', 'json.load', ({(29, 29, 29, 30): 'f'}, {}), '(f)', False, 'import json\n')]