##// END OF EJS Templates
Added tests to make sure that components are getting parent class config.
Brian Granger -
Show More
@@ -1,194 +1,214 b''
1 1 #!/usr/bin/env python
2 2 # encoding: utf-8
3 3 """
4 4 Tests for IPython.core.component
5 5
6 6 Authors:
7 7
8 8 * Brian Granger
9 9 * Fernando Perez (design help)
10 10 """
11 11
12 12 #-----------------------------------------------------------------------------
13 13 # Copyright (C) 2008-2009 The IPython Development Team
14 14 #
15 15 # Distributed under the terms of the BSD License. The full license is in
16 16 # the file COPYING, distributed as part of this software.
17 17 #-----------------------------------------------------------------------------
18 18
19 19 #-----------------------------------------------------------------------------
20 20 # Imports
21 21 #-----------------------------------------------------------------------------
22 22
23 23 from unittest import TestCase
24 24
25 25 from IPython.core.component import Component, ComponentError
26 26 from IPython.utils.traitlets import (
27 27 TraitletError, Int, Float, Str
28 28 )
29 29 from IPython.config.loader import Config
30 30
31 31
32 32 #-----------------------------------------------------------------------------
33 33 # Test cases
34 34 #-----------------------------------------------------------------------------
35 35
36 36
37 37 class TestComponentMeta(TestCase):
38 38
39 39 def test_get_instances(self):
40 40 class BaseComponent(Component):
41 41 pass
42 42 c1 = BaseComponent(None)
43 43 c2 = BaseComponent(c1)
44 44 self.assertEquals(BaseComponent.get_instances(),[c1,c2])
45 45
46 46 def test_get_instances_subclass(self):
47 47 class MyComponent(Component):
48 48 pass
49 49 class MyOtherComponent(MyComponent):
50 50 pass
51 51 c1 = MyComponent(None)
52 52 c2 = MyOtherComponent(c1)
53 53 c3 = MyOtherComponent(c2)
54 54 self.assertEquals(MyComponent.get_instances(), [c1, c2, c3])
55 55 self.assertEquals(MyOtherComponent.get_instances(), [c2, c3])
56 56
57 57 def test_get_instances_root(self):
58 58 class MyComponent(Component):
59 59 pass
60 60 class MyOtherComponent(MyComponent):
61 61 pass
62 62 c1 = MyComponent(None)
63 63 c2 = MyOtherComponent(c1)
64 64 c3 = MyOtherComponent(c2)
65 65 c4 = MyComponent(None)
66 66 c5 = MyComponent(c4)
67 67 self.assertEquals(MyComponent.get_instances(root=c1), [c1, c2, c3])
68 68 self.assertEquals(MyComponent.get_instances(root=c4), [c4, c5])
69 69
70 70
71 71 class TestComponent(TestCase):
72 72
73 73 def test_parent_child(self):
74 74 c1 = Component(None)
75 75 c2 = Component(c1)
76 76 c3 = Component(c1)
77 77 c4 = Component(c3)
78 78 self.assertEquals(c1.parent, None)
79 79 self.assertEquals(c2.parent, c1)
80 80 self.assertEquals(c3.parent, c1)
81 81 self.assertEquals(c4.parent, c3)
82 82 self.assertEquals(c1.children, [c2, c3])
83 83 self.assertEquals(c2.children, [])
84 84 self.assertEquals(c3.children, [c4])
85 85 self.assertEquals(c4.children, [])
86 86
87 87 def test_root(self):
88 88 c1 = Component(None)
89 89 c2 = Component(c1)
90 90 c3 = Component(c1)
91 91 c4 = Component(c3)
92 92 self.assertEquals(c1.root, c1.root)
93 93 self.assertEquals(c2.root, c1)
94 94 self.assertEquals(c3.root, c1)
95 95 self.assertEquals(c4.root, c1)
96 96
97 97 def test_change_parent(self):
98 98 c1 = Component(None)
99 99 c2 = Component(None)
100 100 c3 = Component(c1)
101 101 self.assertEquals(c3.root, c1)
102 102 self.assertEquals(c3.parent, c1)
103 103 self.assertEquals(c1.children,[c3])
104 104 c3.parent = c2
105 105 self.assertEquals(c3.root, c2)
106 106 self.assertEquals(c3.parent, c2)
107 107 self.assertEquals(c2.children,[c3])
108 108 self.assertEquals(c1.children,[])
109 109
110 110 def test_subclass_parent(self):
111 111 c1 = Component(None)
112 112 self.assertRaises(TraitletError, setattr, c1, 'parent', 10)
113 113
114 114 class MyComponent(Component):
115 115 pass
116 116 c1 = Component(None)
117 117 c2 = MyComponent(c1)
118 118 self.assertEquals(MyComponent.parent.this_class, Component)
119 119 self.assertEquals(c2.parent, c1)
120 120
121 121 def test_bad_root(self):
122 122 c1 = Component(None)
123 123 c2 = Component(None)
124 124 c3 = Component(None)
125 125 self.assertRaises(ComponentError, setattr, c1, 'root', c2)
126 126 c1.parent = c2
127 127 self.assertEquals(c1.root, c2)
128 128 self.assertRaises(ComponentError, setattr, c1, 'root', c3)
129 129
130 130
131 131 class TestComponentConfig(TestCase):
132 132
133 133 def test_default(self):
134 134 c1 = Component(None)
135 135 c2 = Component(c1)
136 136 c3 = Component(c2)
137 137 self.assertEquals(c1.config, c2.config)
138 138 self.assertEquals(c2.config, c3.config)
139 139
140 140 def test_custom(self):
141 141 config = Config()
142 142 config.foo = 'foo'
143 143 config.bar = 'bar'
144 144 c1 = Component(None, config=config)
145 145 c2 = Component(c1)
146 146 c3 = Component(c2)
147 147 self.assertEquals(c1.config, config)
148 148 self.assertEquals(c2.config, config)
149 149 self.assertEquals(c3.config, config)
150 150 # Test that we always make copies
151 151 self.assert_(c1.config is not config)
152 152 self.assert_(c2.config is not config)
153 153 self.assert_(c3.config is not config)
154 154 self.assert_(c1.config is not c2.config)
155 155 self.assert_(c2.config is not c3.config)
156 156
157 157 def test_inheritance(self):
158 158 class MyComponent(Component):
159 159 a = Int(1, config=True)
160 160 b = Float(1.0, config=True)
161 161 c = Str('no config')
162 162 config = Config()
163 163 config.MyComponent.a = 2
164 164 config.MyComponent.b = 2.0
165 165 c1 = MyComponent(None, config=config)
166 166 c2 = MyComponent(c1)
167 167 self.assertEquals(c1.a, config.MyComponent.a)
168 168 self.assertEquals(c1.b, config.MyComponent.b)
169 169 self.assertEquals(c2.a, config.MyComponent.a)
170 170 self.assertEquals(c2.b, config.MyComponent.b)
171 171 c4 = MyComponent(c2, config=Config())
172 172 self.assertEquals(c4.a, 1)
173 173 self.assertEquals(c4.b, 1.0)
174 174
175 def test_parent(self):
176 class Foo(Component):
177 a = Int(0, config=True)
178 b = Str('nope', config=True)
179 class Bar(Foo):
180 b = Str('gotit', config=False)
181 c = Float(config=True)
182 config = Config()
183 config.Foo.a = 10
184 config.Foo.b = "wow"
185 config.Bar.b = 'later'
186 config.Bar.c = 100.0
187 f = Foo(None, config=config)
188 b = Bar(f)
189 self.assertEquals(f.a, 10)
190 self.assertEquals(f.b, 'wow')
191 self.assertEquals(b.b, 'gotit')
192 self.assertEquals(b.c, 100.0)
193
194
175 195 class TestComponentName(TestCase):
176 196
177 197 def test_default(self):
178 198 class MyComponent(Component):
179 199 pass
180 200 c1 = Component(None)
181 201 c2 = MyComponent(None)
182 202 c3 = Component(c2)
183 203 self.assertNotEquals(c1.name, c2.name)
184 204 self.assertNotEquals(c1.name, c3.name)
185 205
186 206 def test_manual(self):
187 207 class MyComponent(Component):
188 208 pass
189 209 c1 = Component(None, name='foo')
190 210 c2 = MyComponent(None, name='bar')
191 211 c3 = Component(c2, name='bah')
192 212 self.assertEquals(c1.name, 'foo')
193 213 self.assertEquals(c2.name, 'bar')
194 214 self.assertEquals(c3.name, 'bah')
General Comments 0
You need to be logged in to leave comments. Login now