|
42 | 42 | {'name': 'intersection_for', 'kwargs': {}, 'expected': '\n\nintersection_for(n = [0, 1, 2]);', 'args': {'n': [0, 1, 2]}, },
|
43 | 43 | ]
|
44 | 44 |
|
| 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 | + |
45 | 71 |
|
46 | 72 | class TestSolidPython(DiffOutput):
|
47 | 73 | # test cases will be dynamically added to this instance
|
@@ -202,36 +228,35 @@ def my_animate(_time=0):
|
202 | 228 | rad = 15
|
203 | 229 | c = translate([rad * math.cos(rads), rad * math.sin(rads)])(square(10))
|
204 | 230 | 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 | + |
213 | 238 | self.assertEqual(expected, actual)
|
214 | 239 |
|
215 | 240 | def test_scad_render_to_file(self):
|
216 | 241 | a = circle(10)
|
217 | 242 |
|
218 | 243 | # 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 | + |
225 | 250 | self.assertEqual(expected, actual)
|
226 | 251 |
|
227 | 252 | # 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 | + |
235 | 260 | self.assertEqual(expected, actual)
|
236 | 261 |
|
237 | 262 | # TODO: test include_orig_code=True, but that would have to
|
|
0 commit comments