/
test_tools_camera.py
107 lines (75 loc) · 2.89 KB
/
test_tools_camera.py
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
"""Tests for the tools.camera
"""
import unittest
from contextlib import contextmanager
from numpy import array
from mayavi import mlab
from mayavi.sources.builtin_image import BuiltinImage
from mayavi.tools import camera as camera_tools
from common import TestCase
@contextmanager
def check_attrs_change(test_case, obj, attrs):
old_attrs = {attr: array(getattr(obj, attr))
for attr in attrs}
try:
yield
finally:
unchanged = []
for attr, old_value in old_attrs.items():
new_value = array(getattr(obj, attr))
if all(new_value == old_value):
unchanged.append(attr)
if unchanged:
msg = "Expect {} to change. They stayed the same."
test_case.fail(msg.format(", ".join(unchanged)))
@contextmanager
def check_attrs_do_not_change(test_case, obj, attrs):
old_attrs = {attr: array(getattr(obj, attr))
for attr in attrs}
try:
yield
finally:
changed = []
for attr, old_value in old_attrs.items():
new_value = array(getattr(obj, attr))
if any((new_value - old_value) > 1.e-5):
changed.append((attr, old_value, new_value))
msg = ("'{0}' changed: \n"
"old value: {1} New value: {2}")
if changed:
all_messages = "\n".join([msg.format(attr, old, new)
for attr, old, new in changed])
test_case.fail(all_messages)
class TestCameraUnitTest(unittest.TestCase):
def setUp(self, figure=None):
self.engine = mlab.get_engine()
fig = mlab.figure()
mlab.pipeline.surface(BuiltinImage(), figure=fig)
self.camera = self.engine.current_scene.scene.camera
def tearDown(self):
mlab.close(all=True)
def test_move_with_forward(self):
camera = self.engine.current_scene.scene.camera
with check_attrs_change(self, camera,
("focal_point", "position", "clipping_range")):
camera_tools.move(forward=20.)
def test_camera_move_with_right(self):
camera = self.engine.current_scene.scene.camera
with check_attrs_change(self, camera, ("focal_point", "position")), \
check_attrs_do_not_change(self, camera, ("clipping_range",)):
camera_tools.move(right=20.)
def test_camera_move_with_up(self):
camera = self.engine.current_scene.scene.camera
with check_attrs_change(self, camera,
("focal_point", "position", "clipping_range")):
camera_tools.move(up=20.)
class TestCamera(TestCase):
def test(self):
self.main()
def do(self):
suite = unittest.TestLoader().loadTestsFromTestCase(TestCameraUnitTest)
for test in suite:
test.run()
if __name__ == "__main__":
t = TestCamera()
t.test()