This repository has been archived by the owner on Feb 9, 2020. It is now read-only.
/
test_slots.py
148 lines (109 loc) · 4.62 KB
/
test_slots.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
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
# Test slots
import unittest
from cgkit import _core
from cgkit.all import *
class TestSlot(unittest.TestCase):
def testConstructor(self):
# Standard constructor
asl = _core.DoubleSlot()
self.assertEqual(asl.getValue(), 0)
self.assertEqual(asl.getController(), None)
asl = _core.IntSlot()
self.assertEqual(asl.getValue(), 0)
asl = _core.Vec3Slot()
self.assertEqual(asl.getValue(), vec3(0))
asl = _core.StrSlot()
self.assertEqual(asl.getValue(), "")
# Constructor (initialvalue, flags)
asl = _core.DoubleSlot(12.7, 0)
self.assertEqual(asl.getValue(), 12.7)
self.assertEqual(asl.getController(), None)
asl = _core.IntSlot(5, 0)
self.assertEqual(asl.getValue(), 5)
asl = _core.Vec3Slot(vec3(1,2,3), 0)
self.assertEqual(asl.getValue(), vec3(1,2,3))
asl = _core.StrSlot("spam", 0)
self.assertEqual(asl.getValue(), "spam")
def testNoneConnection(self):
"""Check that connecting to None doesn't crash."""
asl = DoubleSlot()
asl.connect(None)
def testNoneDisconnection(self):
"""Check that disconnecting from None doesn't crash."""
asl = DoubleSlot()
asl.disconnect(None)
def testDisconnect(self):
"""Test the disconnect method."""
ctrl = DoubleSlot(12)
asl = DoubleSlot(5)
# Check that disconnecing a non-existent connection raises an error
self.assertRaises(ValueError, ctrl.disconnect, asl)
# Connect and disconnect and check that the controller doesn't
# influence asl anymore
ctrl.connect(asl)
self.assertEqual(asl.getValue(), 12.0)
ctrl.disconnect(asl)
ctrl.setValue(4)
self.assertEqual(ctrl.getValue(), 4.0)
self.assertEqual(asl.getValue(), 12.0)
##class TestArraySlot(unittest.TestCase):
## def testConstructor(self):
## # Standard constructor (check if items are 0)
## asl = _core.IntArraySlot()
## asl.resize(100)
## self.assertEqual(list(as), 100*[0])
## asl = _core.DoubleArraySlot()
## asl.resize(100)
## self.assertEqual(list(as), 100*[0.0])
class TestProceduralSlot(unittest.TestCase):
def doubleProcedure(self):
"""Dummy procedure to compute a slot value."""
return 42.0
def testGetValue(self):
"""Check that the procedural value is actually obtained."""
ps = ProceduralDoubleSlot(self.doubleProcedure)
self.assertEqual(ps.getValue(), 42.0)
def testSetValue(self):
"""Check that setting a value has no effect."""
ps = ProceduralDoubleSlot(self.doubleProcedure)
ps.setValue(15.5)
self.assertEqual(ps.getValue(), 42.0)
def testInConnection(self):
"""Check that a procedural slot cannot take controllers."""
ctrl = DoubleSlot(15.0)
ps = ProceduralDoubleSlot(self.doubleProcedure)
# The following should not throw an exception
ps.setController(None)
# ctrl.connect(None)
class TestTransformSlots(unittest.TestCase):
def testConstructor(self):
w = WorldObject()
self.assertEqual(w.transform, mat4(1))
self.assertEqual(w.pos, vec3(0))
self.assertEqual(w.rot, mat3(1))
self.assertEqual(w.scale, vec3(1))
def testSetValueWithConnections(self):
"""Check setValue() in the presence of connections."""
# Create an expression (which has a *fixed* output)
e = Expression("(1,2,3)")
w = WorldObject()
w.pos = vec3(-1,5,0.5)
self.assertEqual(w.pos, vec3(-1,5,0.5))
self.assertEqual(w.transform, mat4(1).translation(vec3(-1,5,0.5)))
# Connect the pos slot -> the new position must show up
e.output_slot.connect(w.pos_slot)
self.assertEqual(w.pos, vec3(1,2,3))
self.assertEqual(w.transform, mat4(1).translation(vec3(1,2,3)))
# Set a new pos -> should have no effect as the output slot of the
# expression cannot be changed
w.pos = vec3(0,0.2,0.5)
self.assertEqual(w.pos, vec3(1,2,3))
self.assertEqual(w.transform, mat4(1).translation(vec3(1,2,3)))
# Set a new position via the transform slot -> should also have no
# effect on the position
w.transform = mat4([2,0,0,-1, 0,2,0,0.2, 0,0,2,1.7, 0,0,0,1])
self.assertEqual(w.pos, vec3(1,2,3))
self.assertEqual(w.transform, mat4([2,0,0,1, 0,2,0,2, 0,0,2,3, 0,0,0,1]))
######################################################################
if __name__=="__main__":
unittest.main()