forked from mattloper/chumpy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_inner_composition.py
executable file
·80 lines (58 loc) · 2.03 KB
/
test_inner_composition.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
#!/usr/bin/env python
# encoding: utf-8
"""
Author(s): Matthew Loper
See LICENCE.txt for licensing and contact information.
"""
import unittest
from ch import Ch, depends_on
class TestInnerComposition(unittest.TestCase):
def test_ic(self):
child = Child(a=Ch(10))
parent = Parent(child=child, aliased=Ch(50))
junk = [parent.aliased_dependency for k in range(3)]
self.assertTrue(parent.dcount == 1)
self.assertTrue(parent.ocount == 0)
self.assertTrue(parent.rcount == 0)
junk = [parent.r for k in range(3)]
self.assertTrue(parent.dcount == 1)
self.assertTrue(parent.ocount == 1)
self.assertTrue(parent.rcount == 1)
parent.aliased = Ch(20)
junk = [parent.aliased_dependency for k in range(3)]
self.assertTrue(parent.dcount == 2)
self.assertTrue(parent.ocount == 1)
self.assertTrue(parent.rcount == 1)
junk = [parent.r for k in range(3)]
self.assertTrue(parent.dcount == 2)
self.assertTrue(parent.ocount == 2)
self.assertTrue(parent.rcount == 2)
class Parent(Ch):
dterms = ('aliased', 'child')
def __init__(self, *args, **kwargs):
self.dcount = 0
self.ocount = 0
self.rcount = 0
def on_changed(self, which):
assert('aliased' in which and 'child' in which)
if 'aliased' in which:
self.ocount += 1
@depends_on('aliased')
def aliased_dependency(self):
self.dcount += 1
@property
def aliased(self):
return self.child.a
@aliased.setter
def aliased(self, val):
self.child.a = val
def compute_r(self):
self.rcount += 1
return 0
def compute_dr_wrt(self, wrt):
pass
class Child(Ch):
dterms = ('a',)
if __name__ == '__main__':
suite = unittest.TestLoader().loadTestsFromTestCase(TestInnerComposition)
unittest.TextTestRunner(verbosity=2).run(suite)