MapTests   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 84
rs 10
wmc 16

9 Methods

Rating   Name   Duplication   Size   Complexity  
A test_map_specials() 0 4 2
A test_bresenham() 0 4 2
A test_quick_fov() 0 4 1
A map_is_transparant() 0 6 2
A setUp() 0 8 3
A test_astar() 0 10 1
A test_map_compute_fov() 0 7 2
A test_map_compute_path() 0 5 1
A path_cost() 0 5 2
1
#!/usr/bin/env python
2
3
import unittest
4
import itertools
5
6
import tdl
7
8
class MapTests(unittest.TestCase):
9
10
    MAP = (
11
           '############',
12
           '#   ###    #',
13
           '#   ###    #',
14
           '#   ### ####',
15
           '## #### # ##',
16
           '##      ####',
17
           '############',
18
           )
19
20
    WIDTH = len(MAP[0])
21
    HEIGHT = len(MAP)
22
23
    POINT_A = (2, 2)
24
    POINT_B = (9, 2)
25
    POINT_C = (9, 4)
26
27
    POINTS_AB = POINT_A + POINT_B
28
    POINTS_AC = POINT_A + POINT_C
29
30
    @classmethod
31
    def map_is_transparant(cls, x, y):
32
        try:
33
            return cls.MAP[y][x] == ' '
34
        except IndexError:
35
            return False
36
37
    @classmethod
38
    def path_cost(cls, src_x, src_y, dest_x, dest_y):
39
        if cls.map_is_transparant(dest_x, dest_y):
40
            return 1
41
        return 0
42
43
44
    def setUp(self):
45
        self.map = tdl.map.Map(self.WIDTH, self.HEIGHT)
46
        for y, line in enumerate(self.MAP):
47
            for x, ch in enumerate(line):
48
                trans = ch == ' '
49
                self.map.transparent[x,y] = self.map.walkable[x,y] = trans
50
                self.assertEquals(self.map.transparent[x,y], trans)
51
                self.assertEquals(self.map.walkable[x,y], trans)
52
53
    def test_map_compute_fov(self):
54
        fov = self.map.compute_fov(*self.POINT_A)
55
        self.assertTrue(list(fov), 'should be non-empty')
56
        fov = self.map.compute_fov(*self.POINT_A, fov='PERMISSIVE8')
57
        self.assertTrue(list(fov), 'should be non-empty')
58
        with self.assertRaises(tdl.TDLError):
59
            self.map.compute_fov(*self.POINT_A, fov='invalid option')
60
61
    def test_map_compute_path(self):
62
        self.assertTrue(self.map.compute_path(*self.POINTS_AB),
63
                        'should be non-empty')
64
        self.assertFalse(self.map.compute_path(*self.POINTS_AC),
65
                        'invalid path should return an empty list')
66
67
    def test_map_specials(self):
68
        for x,y in self.map:
69
            self.assertTrue((x, y) in self.map)
70
        self.assertFalse((-1, -1) in self.map)
71
72
    def test_quick_fov(self):
73
        fov = tdl.map.quick_fov(self.POINT_B[0], self.POINT_B[1],
74
                                self.map_is_transparant, radius=2.5)
75
        self.assertTrue(fov, 'should be non-empty')
76
77
    def test_bresenham(self):
78
        for x1, x2, y1, y2 in itertools.permutations([-4, -2, 4, 4], 4):
79
            self.assertTrue(tdl.map.bresenham(x1, x2, y1, y2),
80
                            'should be non-empty')
81
82
    def test_astar(self):
83
        pathfinder = tdl.map.AStar(self.WIDTH, self.HEIGHT,
84
                                   self.map_is_transparant)
85
        self.assertTrue(pathfinder.get_path(*self.POINTS_AB))
86
87
        pathfinder = tdl.map.AStar(self.WIDTH, self.HEIGHT,
88
                                   self.path_cost, None, True)
89
        self.assertTrue(pathfinder.get_path(*self.POINTS_AB))
90
        self.assertFalse(pathfinder.get_path(*self.POINTS_AC),
91
                         'invalid path should return an empty list')
92
93
94
def test_map_fov_cumulative():
95
    map_ = tdl.map.Map(3, 3)
96
    map_.transparent[::2,:] = True # Add map with small divider.
97
    assert not map_.fov.any()
98
    map_.compute_fov(1, 0, cumulative=True) # Light left side.
99
    assert map_.fov.any()
100
    assert not map_.fov.all()
101
    map_.compute_fov(1, 2, cumulative=True) # Light both sides.
102
    assert map_.fov.all()
103