5 Copyright (C) 2008 by Florian Hasheider
6 florian.hasheider@googlemail.com
8 Copyright (C) 2008 by Benjamin Kircher
9 benjamin.kircher@gmail.com
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 2 of the License, or
14 (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the
23 Free Software Foundation, Inc.,
24 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
41 def __init__(self
, width
, height
):
47 """Abstract base class for various tiles.
51 if self
.__class
__ is Tile
:
52 raise NotImplementedError
57 name
= str(self
.__class
__).split('.')[-1].lower()
58 self
.image
= "data/images/" + name
+ ".bmp"
81 self
.watchtower
= None
90 MAPPING
= {'.': Sea
, '_': Plain
, 'M': Mountain
, '^': Forest
, '~': River
}
92 def load_map(filename
):
93 """Load map from given filename. Returns a map object.
95 if type(filename
) == types
.ListType
:
96 f
= [line
.strip().split(',') for line
in filename
]
98 f
= list(csv
.reader(open(filename
, 'r'), delimiter
=','))
99 return Map(width
=len(f
), height
=len(f
[0]))
102 # ----------------------------------- TESTS ---------------------------------- #
106 class ModuleTest(unittest
.TestCase
):
107 """Main test case for this module.
110 def test_mapping(self
):
111 """Ensure correct mapping of tile symbols."""
112 expected
= {'.': Sea
, '_': Plain
, 'M': Mountain
, '^': Forest
,
114 self
.failUnlessEqual(expected
, MAPPING
)
116 def test_load_map_from_file(self
):
117 """Test correct loading of maps from CSV files."""
118 map = load_map('data/maps/empty.map.csv')
119 self
.failUnlessEqual((16, 16), (map.width
, map.height
))
121 def test_load_map_from_list(self
):
122 """Test correct loading of maps from lists."""
123 # empty 16x16 map, all tiles are sea
125 '.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.',
126 '.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.',
127 '.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.',
128 '.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.',
129 '.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.',
130 '.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.',
131 '.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.',
132 '.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.',
133 '.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.',
134 '.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.',
135 '.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.',
136 '.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.',
137 '.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.',
138 '.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.',
139 '.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.',
140 '.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.',
142 map = load_map(empty_map
)
143 self
.failUnlessEqual((16, 16), (map.width
, map.height
))
145 class MapTest(unittest
.TestCase
):
147 """Test correct instantiation of maps."""
149 self
.failUnlessEqual((8, 8), (map.width
, map.height
))
152 class TileTest(unittest
.TestCase
):
153 def test_abstract(self
):
154 """Ensure that tile class is abstract."""
155 self
.failUnlessRaises(NotImplementedError, Tile
)
158 class RiverTest(unittest
.TestCase
):
163 """Test correct instantiation of rivers."""
164 self
.failUnlessEqual(self
.river
.fogofwar
, True)
165 self
.failUnlessEqual(self
.river
.bridge
, None)
166 self
.failUnlessEqual(self
.river
.units
, [])
168 def test_image(self
):
169 """Test if image string is correct."""
170 self
.failUnlessEqual("data/images/river.bmp", self
.river
.image
)
173 class SeaTest(unittest
.TestCase
):
178 """Test correct instantiation of sea tiles."""
179 self
.failUnlessEqual(self
.sea
.fogofwar
, True)
180 self
.failUnlessEqual(self
.sea
.units
, [])
182 def test_image(self
):
183 """Test if image string is correct."""
184 self
.failUnlessEqual("data/images/sea.bmp", self
.sea
.image
)
187 class PlainTest(unittest
.TestCase
):
192 """Test correct instantiation of plains."""
193 self
.failUnlessEqual(self
.plain
.fogofwar
, True)
194 self
.failUnlessEqual(self
.plain
.units
, [])
195 self
.failUnlessEqual(self
.plain
.city
, None)
197 def test_image(self
):
198 """Test if image string is correct."""
199 self
.failUnlessEqual("data/images/plain.bmp", self
.plain
.image
)
201 class MountainTest(unittest
.TestCase
):
203 self
.mountain
= Mountain()
206 """Test correct instantiation of mountains."""
207 self
.failUnlessEqual(self
.mountain
.fogofwar
, True)
208 self
.failUnlessEqual(self
.mountain
.mine
, None)
209 self
.failUnlessEqual(self
.mountain
.units
, [])
211 def test_image(self
):
212 """Test if image string is correct."""
213 self
.failUnlessEqual("data/images/mountain.bmp", self
.mountain
.image
)
216 class ForestTest(unittest
.TestCase
):
218 self
.forest
= Forest()
221 """Test correct instantiation of forest tiles."""
222 self
.failUnlessEqual(self
.forest
.fogofwar
, True)
223 self
.failUnlessEqual(self
.forest
.watchtower
, None)
224 self
.failUnlessEqual(self
.forest
.units
, [])
226 def test_image(self
):
227 """Test if image string is correct."""
228 self
.failUnlessEqual("data/images/forest.bmp", self
.forest
.image
)
234 if __name__
== '__main__':