Skip to content

Commit 2f5446f

Browse files
authored
Merge pull request SolidCode#52 from eric-wieser/fix-tests
Fixes SolidCode#35
2 parents ce992e3 + 9bba273 commit 2f5446f

File tree

1 file changed

+46
-21
lines changed

1 file changed

+46
-21
lines changed

solid/test/test_solidpython.py

Lines changed: 46 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,32 @@
4242
{'name': 'intersection_for', 'kwargs': {}, 'expected': '\n\nintersection_for(n = [0, 1, 2]);', 'args': {'n': [0, 1, 2]}, },
4343
]
4444

45+
class TemporaryFileBuffer(object):
46+
name = None
47+
contents = None
48+
def __enter__(self):
49+
f = tempfile.NamedTemporaryFile(delete=False)
50+
self.name = f.name
51+
try:
52+
f.close()
53+
except:
54+
self._cleanup()
55+
raise
56+
return self
57+
58+
def __exit__(self, exc_type, exc_val, exc_tb):
59+
try:
60+
with open(self.name, 'r') as f:
61+
self.contents = f.read()
62+
finally:
63+
self._cleanup()
64+
65+
def _cleanup(self):
66+
try:
67+
os.unlink(self.name)
68+
except:
69+
pass
70+
4571

4672
class TestSolidPython(DiffOutput):
4773
# test cases will be dynamically added to this instance
@@ -202,36 +228,35 @@ def my_animate(_time=0):
202228
rad = 15
203229
c = translate([rad * math.cos(rads), rad * math.sin(rads)])(square(10))
204230
return c
205-
tmp = tempfile.NamedTemporaryFile()
206-
207-
scad_render_animated_file(my_animate, steps=2, back_and_forth=False,
208-
filepath=tmp.name, include_orig_code=False)
209-
tmp.seek(0)
210-
actual = tmp.read()
211-
expected = b'\nif ($t >= 0.0 && $t < 0.5){ \n\ttranslate(v = [15.0000000000, 0.0000000000]) {\n\t\tsquare(size = 10);\n\t}\n}\nif ($t >= 0.5 && $t < 1.0){ \n\ttranslate(v = [-15.0000000000, 0.0000000000]) {\n\t\tsquare(size = 10);\n\t}\n}\n'
212-
tmp.close()
231+
with TemporaryFileBuffer() as tmp:
232+
scad_render_animated_file(my_animate, steps=2, back_and_forth=False,
233+
filepath=tmp.name, include_orig_code=False)
234+
235+
actual = tmp.contents
236+
expected = '\nif ($t >= 0.0 && $t < 0.5){ \n\ttranslate(v = [15.0000000000, 0.0000000000]) {\n\t\tsquare(size = 10);\n\t}\n}\nif ($t >= 0.5 && $t < 1.0){ \n\ttranslate(v = [-15.0000000000, 0.0000000000]) {\n\t\tsquare(size = 10);\n\t}\n}\n'
237+
213238
self.assertEqual(expected, actual)
214239

215240
def test_scad_render_to_file(self):
216241
a = circle(10)
217242

218243
# No header, no included original code
219-
tmp = tempfile.NamedTemporaryFile()
220-
scad_render_to_file(a, filepath=tmp.name, include_orig_code=False)
221-
tmp.seek(0)
222-
actual = tmp.read()
223-
expected = b'\n\ncircle(r = 10);'
224-
tmp.close()
244+
with TemporaryFileBuffer() as tmp:
245+
scad_render_to_file(a, filepath=tmp.name, include_orig_code=False)
246+
247+
actual = tmp.contents
248+
expected = '\n\ncircle(r = 10);'
249+
225250
self.assertEqual(expected, actual)
226251

227252
# Header
228-
tmp = tempfile.NamedTemporaryFile()
229-
scad_render_to_file(a, filepath=tmp.name, include_orig_code=False,
230-
file_header='$fn = 24;')
231-
tmp.seek(0)
232-
actual = tmp.read()
233-
expected = b'$fn = 24;\n\ncircle(r = 10);'
234-
tmp.close()
253+
with TemporaryFileBuffer() as tmp:
254+
scad_render_to_file(a, filepath=tmp.name, include_orig_code=False,
255+
file_header='$fn = 24;')
256+
257+
actual = tmp.contents
258+
expected = '$fn = 24;\n\ncircle(r = 10);'
259+
235260
self.assertEqual(expected, actual)
236261

237262
# TODO: test include_orig_code=True, but that would have to

0 commit comments

Comments
 (0)