~widelands-dev/widelands-website/django_staticfiles

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#!/usr/bin/env python -tt
# encoding: utf-8
#

import sys
sys.path.append("..")

from numpy import *
import unittest
import base64
from cStringIO import StringIO
from itertools import *

from map import *

# General base class for tests that raise
# an exception
class _WLSetup( unittest.TestCase ):
    def setUp(self):
        self.m = WidelandsMap()


# {{{ Elemental package 
class _WLElemental_Base( unittest.TestCase ):
    def setUp(self):
        self.m = WidelandsMap()
        self.m._read_elemental( StringIO(self.input) )
#################
# Working Tests #
#################
class TestElementalReading_ValidInput_ExceptCorrectResult( _WLElemental_Base ):
    # {{{ Data
    input = """[global]
packet_version=1
map_w=160
map_h=132
nr_players=4
world=greenland
name=_Elven Forests
author=Winterwind
descr=_"The breath-taking beauty of these ..."
"""
    # }}}
    def testDimensions( self ):
        self.assertEqual( self.m.w, 160 )
        self.assertEqual( self.m.h, 132 )
        self.assertEqual( self.m.dim, (132,160) )
    def testNrPlayers( self ):
        self.assertEqual( self.m.nr_players, 4 )
    def testWorldName( self ):
        self.assertEqual( self.m.world_name, "greenland" )
    def testName(self):
        self.assertEqual( self.m.name, "Elven Forests")
    def testAuthor(self):
        self.assertEqual( self.m.author, "Winterwind" )
    def testDescr(self):
        self.assertEqual( self.m.descr, "The breath-taking beauty of these ..." )

##########
# Raises #
##########
class TestElementalReading_TestMissingSection_ExceptRaises(_WLSetup ):
    # {{{ Data
    input="""[globalnoglbale]
blah = 1
"""
    # }}}
    def runTest(self):
        self.assertRaises( InvalidMapPackage, self.m._read_elemental,
                          StringIO(self.input) )
# }}}

# {{{ Height package tests
class _WLHeight_Base( unittest.TestCase ):
    def setUp(self):
        self.m = WidelandsMap()
        self.m._dim = self.dim
        self.m._read_heights( StringIO(self.input) )
class _WLHeight_Setup( unittest.TestCase ):
    def setUp(self):
        self.m = WidelandsMap()
        self.m._dim = self.dim
        self.input = StringIO(self.input)

#################
# Working Tests #
#################
class TestHeightReading_ValidInput_ExceptCorrectResult( _WLHeight_Base ):
    # {{{ Data
    dim = (4,6) # w6, h4
    input = "\x01\x00)!\x08*\x0b1\x18-0\x1d\x03\x08.\x1e\x01\x19\x1f\x1d\x17\x17\x1c\x07#5"
    wanted = array([[41, 33,  8, 42, 11, 49],
           [24, 45, 48, 29,  3,  8],
           [46, 30,  1, 25, 31, 29],
           [23, 23, 28,  7, 35, 53]])
    # }}}
    def runTest(self):
        self.assertTrue( all(self.wanted == self.m.heights ) )

##########
# Raises #
##########
class TestHeightReading_WrongVersion_ExceptRaises(_WLHeight_Setup ):
    dim = (5,7)
    input = "\x00\x02jdhf"
    def runTest(self):
        self.assertRaises( InvalidMapPackage, self.m._read_heights, self.input )
class TestHeightReading_PackageToShort_ExceptRaises(_WLHeight_Setup ):
    dim = (5,7)
    input = "\x01\x00jdhf"
    def runTest(self):
        self.assertRaises( InvalidMapPackage, self.m._read_heights, self.input )
class TestHeightReading_PackageToLong(_WLHeight_Setup ):
    dim = (2,2)
    input = "\x01\x00jdhkkf"
    def runTest(self):
        self.assertRaises( InvalidMapPackage, self.m._read_heights, self.input )
# }}}

# {{{ Terrain package tests
class _WLTerrain_Base( unittest.TestCase ):
    def setUp(self):
        self.m = WidelandsMap()
        self.m._dim = self.dim
        self.m._read_terrains( StringIO(self.input) )
class _WLTerrain_Setup( unittest.TestCase ):
    def setUp(self):
        self.m = WidelandsMap()
        self.m._dim = self.dim
        self.input = StringIO(self.input)

#################
# Working Tests #
#################
class TestTerrainReading_ValidInput_ExceptCorrectResult( _WLTerrain_Base ):
    # {{{ Data
    dim = (1,2) # w6, h4
    input = ("\x01\x00" + # Package version
            "\x02\x00" +  # Nr of terrain types
            "\x00\x00Baum\x00\x01\x00Haus\x00" + # Terrains (id,Name\x00)
            "\x01\x00\x00\x01" # Terrain data (ter_r,ter_d)*nr_of_fields
    )
    ter_r_names = array([[ "Haus", "Baum" ]] )
    ter_d_names = array([[ "Baum", "Haus" ]] )
    # }}}
    def test_R(self):
        r = [ i.name == j for i,j in izip(self.m.ter_r.flat,self.ter_r_names.flat) ]
        self.assertTrue( all(r))
    def test_D(self):
        d = [ i.name == j for i,j in izip(self.m.ter_d.flat,self.ter_d_names.flat) ]
        self.assertTrue( all(d))

##########
# Raises #
##########
class TestTerrainReading_WrongVersion_ExceptRaises(_WLTerrain_Setup ):
    dim = (5,7)
    input = "\x00\x02jdhf"
    def runTest(self):
        self.assertRaises( InvalidMapPackage, self.m._read_terrains, self.input )
class TestTerrainReading_PackageToShort_ExceptRaises(_WLTerrain_Setup ):
    dim = (5,7)
    input = "\x01\x00jdhf"
    def runTest(self):
        self.assertRaises( InvalidMapPackage, self.m._read_terrains, self.input )
class TestTerrainReading_PackageToLong(_WLTerrain_Setup ):
    dim = (1,2) # w6, h4
    input = ("\x01\x00" + # Package version
            "\x02\x00" +  # Nr of terrain types
            "\x00\x00Baum\x00\x01\x00Haus\x00" + # Terrains (id,Name\x00)
            "\x02\x03\x04\x00\x00\x01" # Terrain data (ter_r,ter_d)*nr_of_fields
    )
    ter_r_names = array([[ "Haus", "Baum" ]] )
    ter_d_names = array([[ "Baum", "Haus" ]] )
    def runTest(self):
        self.assertRaises( InvalidMapPackage, self.m._read_terrains, self.input )
class TestTerrainReading_WrongTerrainId(_WLTerrain_Setup ):
    dim = (1,2) # w6, h4
    input = ("\x01\x00" + # Package version
            "\x02\x00" +  # Nr of terrain types
            "\x00\x00Baum\x00\x01\x00Haus\x00" + # Terrains (id,Name\x00)
            "\x00\x00\x02\x00" # Terrain data (ter_r,ter_d)*nr_of_fields
    )
    def runTest(self):
        self.assertRaises( InvalidMapPackage, self.m._read_terrains, self.input )
# }}}