-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtests.py
More file actions
348 lines (298 loc) · 13.9 KB
/
tests.py
File metadata and controls
348 lines (298 loc) · 13.9 KB
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
import unittest
from wall import *
from scale import *
from laser import *
from buildingdata import *
from buildingtemplate import *
from featuretemplate import *
from interlocking import *
from helpers import *
from texture import *
from lcconfig import LCConfig
from builder import Builder
# Test loading of config values
# Just few example values tested
class TestConfig(unittest.TestCase):
# May need to update if changing configuration - these are default settings
def test_config_read(self):
self.config = LCConfig()
self.assertEqual(self.config.stroke_width, 1)
self.assertEqual(self.config.etch_strokes[0][1], 0)
class TestWall(unittest.TestCase):
def test_wall_add(self):
# Test parameters
depth = 1826
height = 1864
test_wall = Wall("Wall test add", [(0,0),(depth,0),(depth,height),(0,height),(0,0)])
cuts = test_wall.get_cuts(True, True)
self.assertEqual(cuts[0].get_type(), "line")
def test_wall_maxsize(self):
depth = 1826
height = 1864
test_wall = Wall("Wall test maxsize", [(0,0),(depth,0),(depth,height),(0,height),(0,0)])
max_size = test_wall.get_maxsize()
self.assertEqual(max_size[0], depth)
self.assertEqual(max_size[1], height)
class TestScale(unittest.TestCase):
def test_get_scales(self):
sc = Scale()
list_scales = sc.get_scales()
self.assertTrue('OO' in list_scales)
self.assertFalse('invalid' in list_scales)
def test_scale_convert(self):
sc = Scale()
# Test with default 'OO'
# Test based on rounded number
self.assertEqual(round(sc.scale_convert(2000)), 26)
# Change to G scale
sc.set_scale('G')
self.assertEqual(round(sc.scale_convert(2000)), 89)
# Change back to OO - ensure same
sc.set_scale('OO')
self.assertEqual(round(sc.scale_convert(2000)), 26)
def test_mm_to_pixels(self):
sc = Scale()
self.assertEqual(round(sc.mm_to_pixel(89)), 338)
# Change scale - should not change value
sc.set_scale('N')
self.assertEqual(round(sc.mm_to_pixel(89)), 338)
# Use single value
def test_convert_single(self):
# start with different scale on constructor
sc = Scale('N')
self.assertEqual(round(sc.convert(2000)), 51)
# Change scale
sc.set_scale('OO')
self.assertEqual(round(sc.convert(2000)), 100)
def test_convert_list(self):
# start with different scale on constructor
sc = Scale('N')
scaled_values = sc.convert([2000, 1000])
self.assertEqual(round(scaled_values[0]), 51)
self.assertEqual(round(scaled_values[1]), 26)
# Change scale
sc.set_scale('OO')
# Use tuples
scaled_values = sc.convert((2000, 1000))
self.assertEqual(round(scaled_values[0]), 100)
self.assertEqual(round(scaled_values[1]), 50)
# Based on example template - test loading file etc.
## Note based on old template - needs to be updated when template is updated
class TestTemplate(unittest.TestCase):
def test_load_file(self):
filename = "templates/building_shed_apex_1.json"
template = BuildingTemplate()
template.load_template (filename)
data = template.get_data()
# Text some particular values
self.assertEqual(data["name"], "Apex shed")
self.assertEqual(data["defaults"]["depth"], 1826)
self.assertEqual(data["typical"]["wood_height"], 150)
self.assertEqual(data["walls"][0][0], "WallApex")
self.assertEqual(data["roofs"][0][0], "RoofApexLeft")
self.assertEqual(data["options"][0], "door_shed_1")
self.assertEqual(data["options"][1], "window_shed_1")
def test_feature_window(self):
filename = "templates/window_shed_1.json"
template = FeatureTemplate()
template.load_template (filename)
data = template.get_data()
self.assertEqual(data["type"], "Window")
self.assertEqual(data["defaults"]["width"], 400)
self.assertEqual(data["cuts"][0][0], "rect")
def test_feature_window_cuts(self):
filename = "templates/window_shed_1.json"
template = FeatureTemplate()
template.load_template (filename)
cuts = template.get_cuts()
self.assertEqual(cuts[0][0], "rect")
# Test parsing of strings
def test_token_process_1(self):
filename = "templates/window_shed_1.json"
template = FeatureTemplate()
template.load_template (filename)
test_string = "x+3"
output_string = template.process_token_str(test_string)
self.assertEqual(output_string, "0+3")
output = template.process_token(test_string)
self.assertEqual(output, 3)
test_string = "y"
output_string = template.process_token_str(test_string)
self.assertEqual(output_string, "0")
output = template.process_token(test_string)
self.assertEqual(output, 0)
test_string = "((7+x+width/2))"
output_string = template.process_token_str(test_string)
self.assertEqual(output_string, "((7+0+400/2))")
output = template.process_token(test_string)
self.assertEqual(output, 207.0)
# Interlocking is based on lines in clockwise direction
# Primary go outwards from start to end
# Secondary to inwards from end to start
class TestInterlocking(unittest.TestCase):
def test_line_interlocking_primary_1(self):
# Primary so start at beginning of line
Interlocking.material_thickness = 10
il = Interlocking (100, 1, "primary")
# Vertical line from bottom to top
line = [(0,1000), (0, 0)]
# Appears that this is repeating args - but the line could actually be a segment
# of a longer line
segments = il.add_interlock_line (line[0], line[1], line)
expected_output = [((0, 1000), (0, 900)), ((0, 900), (-10, 900)), ((-10, 900), (-10, 800)), ((-10, 800), (0, 800)), ((0, 800), (0, 700)), ((0, 700), (-10, 700)), ((-10, 700), (-10, 600)), ((-10, 600), (0, 600)), ((0, 600), (0, 500)), ((0, 500), (-10, 500)), ((-10, 500), (-10, 400)), ((-10, 400), (0, 400)), ((0, 400), (0, 300)), ((0, 300), (-10, 300)), ((-10, 300), (-10, 200)), ((-10, 200), (0, 200)), ((0, 200), (0, 100)), ((0, 100), (0, 0))]
self.assertEqual(segments, expected_output)
def test_line_interlocking_secondary_1(self):
# Secondary so start at end of line
il = Interlocking (100, 1, "secondary")
Interlocking.material_thickness = 20
# Vertical line from top to bottom
line = [(100, 0), (100, 1000)]
# Appears that this is repeating args - but the line could actually be a segment
# of a longer line
segments = il.add_interlock_line (line[0], line[1], line)
expected_output = [((100, 1000), (100, 900)), ((100, 900), (80, 900)), ((80, 900), (80, 800)), ((80, 800), (100, 800)), ((100, 800), (100, 700)), ((100, 700), (80, 700)), ((80, 700), (80, 600)), ((80, 600), (100, 600)), ((100, 600), (100, 500)), ((100, 500), (80, 500)), ((80, 500), (80, 400)), ((80, 400), (100, 400)), ((100, 400), (100, 300)), ((100, 300), (80, 300)), ((80, 300), (80, 200)), ((80, 200), (100, 200)), ((100, 200), (100, 100)), ((100, 100), (100, 0))]
self.assertEqual(segments, expected_output)
# Horizontal line
def test_line_horizontal_primary_1(self):
Interlocking.material_thickness = 8
# Primary so start at beginning of line
il = Interlocking (10, 1, "primary")
# From left to right
line = [(0,0), (60, 0)]
# Appears that this is repeating args - but the line could actually be a segment
# of a longer line
segments = il.add_interlock_line (line[0], line[1], line)
expected_output = [((0, 0), (10, 0)), ((10, 0), (10, -8)), ((10, -8), (20, -8)), ((20, -8), (20, 0)), ((20, 0), (30, 0)), ((30, 0), (30, -8)), ((30, -8), (40, -8)), ((40, -8), (40, 0)), ((40, 0), (50, 0)), ((50, 0), (60, 0))]
self.assertEqual(segments, expected_output)
# Texture
class TestTexture(unittest.TestCase):
def test_horizontal_line_extend_right(self):
depth = 200
height = 180
texture = Texture([(0,0),(depth,0),(depth,height),(0,height),(0,0)])
lines = texture._line([1, 20],[2000,20])
self.assertEqual (lines[0][0][0], 1)
self.assertEqual (lines[0][0][1], 20)
self.assertEqual (lines[0][1][0], 200)
self.assertEqual (lines[0][1][1], 20)
def test_horizontal_line_extend_left(self):
depth = 200
height = 180
texture = Texture([(20,0),(depth,0),(depth,height),(20,height),(20,0)])
lines = texture._line([1, 49],[100,49])
self.assertEqual (lines[0][0][0], 20)
self.assertEqual (lines[0][0][1], 49)
self.assertEqual (lines[0][1][0], 100)
self.assertEqual (lines[0][1][1], 49)
def test_horizontal_line_inside(self):
depth = 200
height = 180
texture = Texture([(0,0),(depth,0),(depth,height),(0,height),(0,0)])
lines = texture._line([1, 1],[150, 1])
self.assertEqual (lines[0][0][0], 1)
self.assertEqual (lines[0][0][1], 1)
self.assertEqual (lines[0][1][0], 150)
self.assertEqual (lines[0][1][1], 1)
def test_vertical_line_extend_above(self):
depth = 200
height = 180
texture = Texture([(0,20),(depth,20),(depth,height+20),(0,height+20),(0,20)])
lines = texture._line([1, 100],[1,5])
self.assertEqual (lines[0][0][0], 1)
self.assertEqual (lines[0][0][1], 100)
self.assertEqual (lines[0][1][0], 1)
self.assertEqual (lines[0][1][1], 20)
def test_vertical_line_extend_below(self):
depth = 200
height = 180
texture = Texture([(0,0),(depth,0),(depth,height),(0,height),(0,0)])
lines = texture._line([41, 225],[41,100])
self.assertEqual (lines[0][0][0], 41)
self.assertEqual (lines[0][0][1], 180)
self.assertEqual (lines[0][1][0], 41)
self.assertEqual (lines[0][1][1], 100)
def test_vertical_line_inside(self):
depth = 200
height = 180
texture = Texture([(0,0),(depth,0),(depth,height),(0,height),(0,0)])
lines = texture._line([150, 177],[150,10])
self.assertEqual (lines[0][0][0], 150)
self.assertEqual (lines[0][0][1], 177)
self.assertEqual (lines[0][1][0], 150)
self.assertEqual (lines[0][1][1], 10)
def test_angle_line_both(self):
depth = 200
height = 200
texture = Texture([(0,0),(depth,0),(depth,height),(0,height),(0,0)])
lines = texture._line([100, 220],[220,100])
self.assertEqual (lines[0][0][0], 120)
self.assertEqual (lines[0][0][1], 200)
self.assertEqual (lines[0][1][0], 200)
self.assertEqual (lines[0][1][1], 120)
# Tests line is split where interacting with edges of polygon
def test_line_polygon_split(self):
# Texture is two apex
texture = Texture([(0,20),(20,0),(40,20),(60,0),(80,20),(80,120),(0,120), (0,20)])
lines = texture._line([0, 10],[200,10])
self.assertEqual (lines[0][0][0], 10)
self.assertEqual (lines[0][0][1], 10)
self.assertEqual (lines[0][1][0], 30)
self.assertEqual (lines[0][1][1], 10)
self.assertEqual (lines[1][0][0], 50)
self.assertEqual (lines[1][0][1], 10)
self.assertEqual (lines[1][1][0], 70)
self.assertEqual (lines[1][1][1], 10)
# Tests line is split where interacting with edges of polygon
# 3 peaks
def test_line_polygon_split_2(self):
# Texture is 3 apex
texture = Texture([(0,20),(20,0),(40,20),(60,0),(80,20),(100,0), (120,20),(120,120),(0,120), (0,20)])
lines = texture._line([0, 10],[200,10])
self.assertEqual (lines[0][0][0], 10)
self.assertEqual (lines[0][0][1], 10)
self.assertEqual (lines[0][1][0], 30)
self.assertEqual (lines[0][1][1], 10)
self.assertEqual (lines[1][0][0], 50)
self.assertEqual (lines[1][0][1], 10)
self.assertEqual (lines[1][1][0], 70)
self.assertEqual (lines[1][1][1], 10)
# Test for final section
self.assertEqual (lines[2][0][0], 90)
self.assertEqual (lines[2][0][1], 10)
self.assertEqual (lines[2][1][0], 110)
self.assertEqual (lines[2][1][1], 10)
# Test the Builder class - along with subclasses that are read
class TestBuilder(unittest.TestCase):
# Read data file, write it out, read it in and compare
def test_read_file(self):
config = LCConfig()
builder = Builder(config)
builder.load_file("tests/building1.json")
walls = builder.building.get_walls()
# Take first entry and see if it's expected
self.assertEqual (walls[0][0], 'Front with window')
# Now save as a new file
builder.save_file("tests/building1a.json")
# Create new builder object load and compare
builder1a = Builder(config)
builder1a.load_file("tests/building1a.json")
walls1a = builder1a.building.get_walls()
# Take first entry and see if it's expected
self.assertEqual (walls1a[0][0], 'Front with window')
# helper functions
class TestHelpers(unittest.TestCase):
def test_get_angles(self):
# Horizontal line (left to right)
angle = get_angle([[0, 0], [100, 0]])
self.assertEqual(angle, 90)
# Horizontal line (right to left)
angle = get_angle([[50, 50], [0, 50]])
self.assertEqual(angle, 270)
# Vertical line upwards
angle = get_angle([[200, 1000], [200, 100]])
self.assertEqual(angle, 180)
# Vertical line downwards
angle = get_angle([[20, 20], [20, 200]])
self.assertEqual(angle, 0)
if __name__ == '__main__':
unittest.main()