From 0aa4eb9c0b88369b3619bad368ebd27f042c6741 Mon Sep 17 00:00:00 2001 From: Charles Peyrat Date: Fri, 8 Jan 2021 16:11:17 +0100 Subject: [PATCH] Refactoring in tests to allow for easy connexity verification --- squirrelbattle/tests/mapgeneration_test.py | 35 ++++++++++++++++------ 1 file changed, 26 insertions(+), 9 deletions(-) diff --git a/squirrelbattle/tests/mapgeneration_test.py b/squirrelbattle/tests/mapgeneration_test.py index 394977b..f546669 100644 --- a/squirrelbattle/tests/mapgeneration_test.py +++ b/squirrelbattle/tests/mapgeneration_test.py @@ -4,7 +4,22 @@ import unittest from squirrelbattle.interfaces import Map, Tile -from squirrelbattle.mapgeneration import randomwalk +from squirrelbattle.mapgeneration import randomwalk, broguelike + +def is_connex(grid): + h, w = len(grid), len(grid[0]) + y, x = randint(0, h-1), randint(0, w-1) + while not(grid[y][x].can_walk()): + y, x = randint(0, h-1), randint(0, w-1) + queue = Map.neighbourhood(grid, y, x) + while queue != []: + y, x = queue.pop() + if m.tiles[y][x].can_walk(): + m.tiles[y][x] = Tile.WALL + queue += Map.neighbourhood(grid, y, x) + return not(any([any([t.can_walk() for t in l]) for l in m.tiles])) + + class TestRandomWalk(unittest.TestCase): @@ -22,12 +37,14 @@ class TestRandomWalk(unittest.TestCase): m = self.generator.run() self.assertTrue(m.tiles[m.start_y][m.start_x].can_walk()) - #DFS - queue = Map.neighbourhood(m.tiles, m.start_y, m.start_x) - while queue != []: - y, x = queue.pop() - if m.tiles[y][x].can_walk(): - m.tiles[y][x] = Tile.WALL - queue += Map.neighbourhood(m.tiles, y, x) + def test_connexity(self) -> None: + m = self.generator.run() + self.assertTrue(is_connex(m.tiles)) - self.assertFalse(any([any([t.can_walk() for t in l]) for l in m.tiles])) +class TestBroguelike(unittest.TestCase): + def setUp(self) -> None: + self.generator = broguelike.Generator() + + def test_connexity(self) -> None: + m = self.generator.run() + self.assertTrue(is_connex(m))