Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(3)

Side by Side Diff: tools/nixysa/nixysa/syntax_tree_unittest.py

Issue 2043006: WTF NPAPI extension. Early draft. Base URL: http://src.chromium.org/svn/trunk/src/
Patch Set: Created 10 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « tools/nixysa/nixysa/syntax_tree.py ('k') | tools/nixysa/nixysa/unsized_array_binding.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Property Changes:
Added: svn:eol-style
+ LF
OLDNEW
(Empty)
1 #!/usr/bin/python2.4
2 #
3 # Copyright 2008 Google Inc.
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17 """Test for syntax_tree."""
18
19 import unittest
20 import idl_parser
21 import syntax_tree
22
23 _location = idl_parser.SourceLocation(idl_parser.File('test.idl'), 0)
24
25
26 def MakeType(name):
27 return syntax_tree.Typename(_location, {}, name)
28
29
30 def MakeScope(name):
31 return ContextMock([], [], name, None)
32
33
34 class TypeReferenceMock(syntax_tree.TypeReference):
35 def __init__(self, return_type):
36 syntax_tree.TypeReference.__init__(self, _location)
37 self.return_type = return_type
38 self.context = None
39 self.scoped = None
40
41 def GetTypeInternal(self, context, scoped):
42 self.context = context
43 self.scoped = scoped
44 return self.return_type
45
46
47 class ContextMock(syntax_tree.Definition):
48 defn_type = 'ContextMock'
49
50 def __init__(self, types_list, scopes_list, name, parent):
51 syntax_tree.Definition.__init__(self, _location, [], name)
52 self.is_scope = True
53 self.types_dict = dict([(type_defn.name, type_defn) for type_defn
54 in types_list])
55 self.scopes_list = scopes_list
56 for o in scopes_list + types_list:
57 o.parent = self
58 self.parent = parent
59
60 def LookUpType(self, name):
61 if name in self.types_dict:
62 return self.types_dict[name]
63 else:
64 return None
65
66 def FindScopes(self, name):
67 return [scope for scope in self.scopes_list if scope.name == name]
68
69
70 class TypeReferenceTest(unittest.TestCase):
71 def setUp(self):
72 self.type_defn = MakeType('Type')
73 self.context = ContextMock([], [], None, None)
74
75 def testGetTypeSuccess(self):
76 mock = TypeReferenceMock(self.type_defn)
77 return_type = mock.GetType(self.context)
78 self.assertEquals(return_type, self.type_defn)
79 self.assertEquals(mock.context, self.context)
80 self.assertEquals(mock.scoped, False)
81
82 def testGetTypeFailure(self):
83 mock = TypeReferenceMock(None)
84 self.assertRaises(syntax_tree.TypeNotFoundError, mock.GetType, self.context)
85 self.assertEquals(mock.context, self.context)
86 self.assertEquals(mock.scoped, False)
87
88
89 class NameTypeReferenceTest(unittest.TestCase):
90 def setUp(self):
91 # Context2 {
92 # Type1;
93 # Type3;
94 # Context1 {
95 # Type1;
96 # Type2;
97 # }
98 # }
99 self.type1_c1 = MakeType('Type1')
100 self.type2_c1 = MakeType('Type2')
101 self.context1 = ContextMock([self.type1_c1, self.type2_c1], [], 'Context1',
102 None)
103 self.type1_c2 = MakeType('Type1')
104 self.type3_c2 = MakeType('Type3')
105 self.context2 = ContextMock([self.type1_c2, self.type3_c2], [self.context1],
106 'Context2', None)
107 self.type1_ref = syntax_tree.NameTypeReference(_location, 'Type1')
108 self.type2_ref = syntax_tree.NameTypeReference(_location, 'Type2')
109 self.type3_ref = syntax_tree.NameTypeReference(_location, 'Type3')
110
111 def testGetTypeInScope(self):
112 self.assertEquals(self.type1_c1, self.type1_ref.GetType(self.context1))
113 self.assertEquals(self.type2_c1, self.type2_ref.GetType(self.context1))
114 self.assertEquals(self.type3_c2, self.type3_ref.GetType(self.context2))
115
116 def testGetTypeFromOuterScope(self):
117 self.assertEquals(self.type1_c2, self.type1_ref.GetType(self.context2))
118 self.assertRaises(syntax_tree.TypeNotFoundError,
119 self.type2_ref.GetType, self.context2)
120
121 def testGetTypeFromInnerScope(self):
122 self.assertEquals(self.type3_c2, self.type3_ref.GetType(self.context1))
123
124
125 class ScopedTypeReferenceTest(unittest.TestCase):
126 def setUp(self):
127 # Context3 {
128 # Type1;
129 # Type2;
130 # Context1 {
131 # Type1;
132 # }
133 # Context2 {
134 # Type1;
135 # }
136 # }
137 self.type1_c1 = MakeType('Type1')
138 self.context1 = ContextMock([self.type1_c1], [], 'Context1', None)
139 self.type1_c2 = MakeType('Type1')
140 self.context2 = ContextMock([self.type1_c2], [], 'Context2', None)
141 self.type1_c3 = MakeType('Type1')
142 self.type2_c3 = MakeType('Type2')
143 self.context3 = ContextMock([self.type1_c3, self.type2_c3],
144 [self.context1, self.context2], 'Context3',
145 None)
146 self.type1_ref = syntax_tree.NameTypeReference(_location, 'Type1')
147 self.type2_ref = syntax_tree.NameTypeReference(_location, 'Type2')
148 self.c1_t1_ref = syntax_tree.ScopedTypeReference(_location, 'Context1',
149 self.type1_ref)
150 self.c2_t1_ref = syntax_tree.ScopedTypeReference(_location, 'Context2',
151 self.type1_ref)
152 self.c1_t2_ref = syntax_tree.ScopedTypeReference(_location, 'Context1',
153 self.type2_ref)
154
155 def testGetTypeFromOuterScope(self):
156 self.assertEquals(self.type1_c1, self.c1_t1_ref.GetType(self.context3))
157 self.assertEquals(self.type1_c2, self.c2_t1_ref.GetType(self.context3))
158
159 def testGetTypeFromInnerScope(self):
160 self.assertEquals(self.type1_c1, self.c1_t1_ref.GetType(self.context1))
161 self.assertEquals(self.type1_c2, self.c2_t1_ref.GetType(self.context1))
162 self.assertEquals(self.type1_c1, self.c1_t1_ref.GetType(self.context2))
163 self.assertEquals(self.type1_c2, self.c2_t1_ref.GetType(self.context2))
164
165 def testGetInexistentType(self):
166 self.assertRaises(syntax_tree.TypeNotFoundError, self.c1_t2_ref.GetType,
167 self.context1)
168 self.assertRaises(syntax_tree.TypeNotFoundError, self.c1_t2_ref.GetType,
169 self.context2)
170 self.assertRaises(syntax_tree.TypeNotFoundError, self.c1_t2_ref.GetType,
171 self.context3)
172
173
174 class ArrayTypeReferenceTest(unittest.TestCase):
175 def setUp(self):
176 self.type_defn = MakeType('Type')
177 self.context = ContextMock([self.type_defn], [], 'Context', None)
178 self.type_ref = syntax_tree.NameTypeReference(_location, 'Type')
179 self.nonexist_type_ref = syntax_tree.NameTypeReference(_location,
180 'NonexistentType')
181
182 def testGetType(self):
183 unsized_ref = syntax_tree.ArrayTypeReference(_location, self.type_ref, None)
184 unsized_array = self.type_defn.GetArrayType(None)
185 self.assertEquals(unsized_ref.GetType(self.context), unsized_array)
186 sized_ref = syntax_tree.ArrayTypeReference(_location, self.type_ref, 3)
187 sized_array = self.type_defn.GetArrayType(3)
188 self.assertEquals(sized_ref.GetType(self.context), sized_array)
189
190 def testGetInexistentType(self):
191 unsized_ref = syntax_tree.ArrayTypeReference(_location,
192 self.nonexist_type_ref, None)
193 self.assertRaises(syntax_tree.TypeNotFoundError, unsized_ref.GetType,
194 self.context)
195 sized_ref = syntax_tree.ArrayTypeReference(_location,
196 self.nonexist_type_ref, 5)
197 self.assertRaises(syntax_tree.TypeNotFoundError, sized_ref.GetType,
198 self.context)
199
200
201 class QualifiedTypeReferenceTest(unittest.TestCase):
202 def setUp(self):
203 self.type_defn = MakeType('Type')
204 self.context = ContextMock([self.type_defn], [], 'Context', None)
205 self.type_ref = syntax_tree.NameTypeReference(_location, 'Type')
206 self.nonexist_type_ref = syntax_tree.NameTypeReference(_location,
207 'NonexistentType')
208
209 def testGetType(self):
210 qualified_ref = syntax_tree.QualifiedTypeReference(_location, 'const',
211 self.type_ref)
212 self.assertEquals(qualified_ref.GetType(self.context), self.type_defn)
213
214 def testGetInexistentType(self):
215 qualified_ref = syntax_tree.QualifiedTypeReference(_location, 'const',
216 self.nonexist_type_ref)
217 self.assertRaises(syntax_tree.TypeNotFoundError, qualified_ref.GetType,
218 self.context)
219
220
221 class DefinitionTest(unittest.TestCase):
222 def setUp(self):
223 pass
224
225 def testGetParentScopeStack(self):
226 definition1 = syntax_tree.Definition(_location, [], 'Definition1')
227 definition1.is_scope = True
228 definition2 = syntax_tree.Definition(_location, [], 'Definition2')
229 definition2.parent = definition1
230 definition2.is_scope = True
231 definition3 = syntax_tree.Definition(_location, [], 'Definition3')
232 definition3.parent = definition2
233 self.assertEquals(definition1.GetParentScopeStack(), [])
234 self.assertEquals(definition2.GetParentScopeStack(), [definition1])
235 self.assertEquals(definition3.GetParentScopeStack(), [definition1,
236 definition2])
237
238 def testGetDefinitionInclude(self):
239 definition1 = syntax_tree.Definition(_location, [], 'Definition1')
240 self.assertEquals(definition1.GetDefinitionInclude(), _location.file.header)
241 include = '/path/to/header.h'
242 definition2 = syntax_tree.Definition(_location, {'include': include},
243 'Definition2')
244 self.assertEquals(definition2.GetDefinitionInclude(), include)
245
246 def testGetArrayTypeFail(self):
247 definition = syntax_tree.Definition(_location, [], 'Definition')
248 definition.is_type = False
249 self.assertRaises(syntax_tree.ArrayOfNonTypeError, definition.GetArrayType,
250 None)
251 self.assertRaises(syntax_tree.ArrayOfNonTypeError, definition.GetArrayType,
252 5)
253
254 def testGetArrayType(self):
255 definition = syntax_tree.Definition(_location, [], 'Definition')
256 definition.is_type = True
257 unsized = definition.GetArrayType(None)
258 self.assertEquals(unsized.data_type, definition)
259 self.assertEquals(unsized.size, None)
260 self.assertEquals(unsized, definition.GetArrayType(None))
261 sized = definition.GetArrayType(3)
262 self.assertEquals(sized.data_type, definition)
263 self.assertEquals(sized.size, 3)
264 self.assertEquals(sized, definition.GetArrayType(3))
265
266 def testLookUpTypeRecursive(self):
267 type1_c1 = MakeType('Type1')
268 type2_c1 = MakeType('Type2')
269 context1 = ContextMock([type1_c1, type2_c1], [], 'Context1', None)
270 type1_c2 = MakeType('Type1')
271 context2 = ContextMock([type1_c2], [], 'Context2', context1)
272 self.assertEquals(context1.LookUpTypeRecursive('Type1'), type1_c1)
273 self.assertEquals(context1.LookUpTypeRecursive('Type2'), type2_c1)
274 self.assertEquals(context1.LookUpTypeRecursive('Type3'), None)
275 self.assertEquals(context2.LookUpTypeRecursive('Type1'), type1_c2)
276 self.assertEquals(context2.LookUpTypeRecursive('Type2'), type2_c1)
277 self.assertEquals(context2.LookUpTypeRecursive('Type3'), None)
278
279 def testFindScopesRecursive(self):
280 scope1_c1 = MakeScope('Scope1')
281 scope2_c1 = MakeScope('Scope2')
282 context1 = ContextMock([], [scope1_c1, scope2_c1], 'Context1', None)
283 scope1_c2 = MakeScope('Scope1')
284 context2 = ContextMock([], [scope1_c2], 'Context2', context1)
285 self.assertEquals(context1.FindScopesRecursive('Scope1'), [scope1_c1])
286 self.assertEquals(context1.FindScopesRecursive('Scope2'), [scope2_c1])
287 self.assertEquals(context2.FindScopesRecursive('Scope1'), [scope1_c2,
288 scope1_c1])
289 self.assertEquals(context2.FindScopesRecursive('Scope2'), [scope2_c1])
290 context3 = ContextMock([], [context1], 'Context3', None)
291 self.assertEquals(context3.FindScopesRecursive('Scope1'), [])
292 self.assertEquals(context3.FindScopesRecursive('Scope2'), [])
293
294 def testSetBindingModel(self):
295 class DefinitionMock(syntax_tree.Definition):
296 defn_type = 'DefinitionMock'
297
298 def __init__(self, name, binding_model_name):
299 syntax_tree.Definition.__init__(self, _location, [], name)
300 self.binding_model_name = binding_model_name
301 self.is_type = True
302
303 def LookUpBindingModel(self):
304 return self.binding_model_name
305
306 bm_binding_model = object()
307 unsized_array_binding_model = object()
308 sized_array_binding_model = object()
309 binding_models = {'bm': bm_binding_model,
310 'unsized_array': unsized_array_binding_model,
311 'sized_array': sized_array_binding_model}
312 definition1 = DefinitionMock('Definition1', 'bm')
313 definition1.SetBindingModel(binding_models)
314 self.assertEquals(definition1.binding_model, bm_binding_model)
315 definition2 = DefinitionMock('Definition2', 'non_bm')
316 self.assertRaises(syntax_tree.UnknownBindingModelError,
317 definition2.SetBindingModel, binding_models)
318 definition3 = DefinitionMock('Definition3', 'bm')
319 unsized_array = definition3.GetArrayType(None)
320 sized_array = definition3.GetArrayType(21)
321 definition3.SetBindingModel(binding_models)
322 self.assertEquals(unsized_array.binding_model, unsized_array_binding_model)
323 self.assertEquals(sized_array.binding_model, sized_array_binding_model)
324
325
326 class ClassTest(unittest.TestCase):
327 def setUp(self):
328 self.type1_c1 = MakeType('Type1')
329 self.type2 = MakeType('Type2')
330 self.scope1_c1 = MakeScope('Scope1')
331 self.scope2 = MakeScope('Scope2')
332 self.class1 = syntax_tree.Class(_location, {'binding_model': 'bm1'},
333 'Class1', None,
334 [self.type1_c1, self.type2, self.scope1_c1,
335 self.scope2])
336 self.type1_c2 = MakeType('Type1')
337 self.type3 = MakeType('Type3')
338 self.scope1_c2 = MakeScope('Scope1')
339 self.scope3 = MakeScope('Scope3')
340 self.class1_ref = TypeReferenceMock(self.class1)
341 self.class2 = syntax_tree.Class(_location, {}, 'Class2', self.class1_ref,
342 [self.type1_c2, self.type3, self.scope1_c2,
343 self.scope3])
344 self.class3 = syntax_tree.Class(_location, {},
345 'Class3', None, [self.class1, self.class2])
346 invalid_base = MakeType('Type5')
347 self.class4 = syntax_tree.Class(_location, {}, 'Class4',
348 TypeReferenceMock(invalid_base), [])
349 self.type1_global = MakeType('Type1')
350 self.type4 = MakeType('Type4')
351 self.context = ContextMock([self.class3, self.type1_global, self.type4],
352 [self.class3], 'Context', None)
353
354 def testTypeScope(self):
355 for c in [self.class1, self.class2, self.class3, self.class4]:
356 self.assertTrue(c.is_type)
357 self.assertTrue(c.is_scope)
358
359 def testParent(self):
360 self.assertEquals(self.type1_c1.parent, self.class1)
361 self.assertEquals(self.type2.parent, self.class1)
362 self.assertEquals(self.scope1_c1.parent, self.class1)
363 self.assertEquals(self.scope2.parent, self.class1)
364 self.assertEquals(self.type1_c2.parent, self.class2)
365 self.assertEquals(self.type3.parent, self.class2)
366 self.assertEquals(self.scope1_c2.parent, self.class2)
367 self.assertEquals(self.scope3.parent, self.class2)
368 self.assertEquals(self.class1.parent, self.class3)
369 self.assertEquals(self.class2.parent, self.class3)
370
371 def testResolveTypeReferences(self):
372 self.assertEquals(self.class1._types_resolved, False)
373 self.class1.ResolveTypeReferences()
374 self.assertEquals(self.class1.base_type, None)
375 self.assertEquals(self.class1._types_resolved, True)
376 self.class2.ResolveTypeReferences()
377 self.assertEquals(self.class2._types_resolved, True)
378 self.assertEquals(self.class2.base_type, self.class1)
379 # check that the type resolution for class2 happened in the correct scope
380 self.assertEquals(self.class1_ref.context, self.class3)
381 self.assertEquals(self.class1_ref.scoped, False)
382 self.assertRaises(syntax_tree.DerivingFromNonClassError,
383 self.class4.ResolveTypeReferences)
384
385 def testGetBaseSafe(self):
386 self.assertEquals(self.class1.GetBaseSafe(), None)
387 self.assertEquals(self.class2.GetBaseSafe(), self.class1)
388 self.assertEquals(self.class3.GetBaseSafe(), None)
389 self.assertRaises(syntax_tree.DerivingFromNonClassError,
390 self.class4.GetBaseSafe)
391
392 def testGetObjectsRecursive(self):
393 class1_list = self.class1.GetObjectsRecursive()
394 self.assertEquals(class1_list[0], self.class1)
395 class1_list.sort()
396 class1_list_expected = [self.class1, self.type1_c1, self.type2,
397 self.scope1_c1, self.scope2]
398 class1_list_expected.sort()
399 self.assertEquals(class1_list, class1_list_expected)
400 class2_list = self.class2.GetObjectsRecursive()
401 self.assertEquals(class2_list[0], self.class2)
402 class2_list.sort()
403 class2_list_expected = [self.class2, self.type1_c2, self.type3,
404 self.scope1_c2, self.scope3]
405 class2_list_expected.sort()
406 self.assertEquals(class2_list, class2_list_expected)
407 class3_list = self.class3.GetObjectsRecursive()
408 self.assertEquals(class3_list[0], self.class3)
409 class3_list.sort()
410 class3_list_expected = [self.class3] + class1_list + class2_list
411 class3_list_expected.sort()
412 self.assertEquals(class3_list, class3_list_expected)
413
414 def testLookUpType(self):
415 self.assertEquals(self.class1.LookUpType('Type1'), self.type1_c1)
416 self.assertEquals(self.class1.LookUpType('Type2'), self.type2)
417 self.assertEquals(self.class1.LookUpType('Type3'), None)
418 self.assertEquals(self.class1.LookUpType('Type4'), None)
419 self.assertEquals(self.class1.LookUpType('Class1'), None)
420 self.assertEquals(self.class1.LookUpType('Class2'), None)
421 self.assertEquals(self.class1.LookUpType('Class3'), None)
422 self.assertEquals(self.class1.LookUpType('Scope1'), None)
423 self.assertEquals(self.class1.LookUpType('Scope2'), None)
424 self.assertEquals(self.class1.LookUpType('Scope3'), None)
425
426 self.assertEquals(self.class2.LookUpType('Type1'), self.type1_c2)
427 self.assertEquals(self.class2.LookUpType('Type2'), self.type2)
428 self.assertEquals(self.class2.LookUpType('Type3'), self.type3)
429 self.assertEquals(self.class2.LookUpType('Type4'), None)
430 self.assertEquals(self.class2.LookUpType('Class1'), None)
431 self.assertEquals(self.class2.LookUpType('Class2'), None)
432 self.assertEquals(self.class2.LookUpType('Class3'), None)
433 self.assertEquals(self.class2.LookUpType('Scope1'), None)
434 self.assertEquals(self.class2.LookUpType('Scope2'), None)
435 self.assertEquals(self.class2.LookUpType('Scope3'), None)
436
437 self.assertEquals(self.class3.LookUpType('Type1'), None)
438 self.assertEquals(self.class3.LookUpType('Type2'), None)
439 self.assertEquals(self.class3.LookUpType('Type3'), None)
440 self.assertEquals(self.class3.LookUpType('Type4'), None)
441 self.assertEquals(self.class3.LookUpType('Class1'), self.class1)
442 self.assertEquals(self.class3.LookUpType('Class2'), self.class2)
443 self.assertEquals(self.class3.LookUpType('Class3'), None)
444 self.assertEquals(self.class3.LookUpType('Scope1'), None)
445 self.assertEquals(self.class3.LookUpType('Scope2'), None)
446 self.assertEquals(self.class3.LookUpType('Scope3'), None)
447
448 def testFindScopes(self):
449 self.assertEquals(self.class1.FindScopes('Class1'), [])
450 self.assertEquals(self.class1.FindScopes('Class2'), [])
451 self.assertEquals(self.class1.FindScopes('Type1'), [])
452 self.assertEquals(self.class1.FindScopes('Type2'), [])
453 self.assertEquals(self.class1.FindScopes('Scope1'), [self.scope1_c1])
454 self.assertEquals(self.class1.FindScopes('Scope2'), [self.scope2])
455 self.assertEquals(self.class1.FindScopes('Scope3'), [])
456
457 self.assertEquals(self.class2.FindScopes('Class1'), [])
458 self.assertEquals(self.class2.FindScopes('Class2'), [])
459 self.assertEquals(self.class2.FindScopes('Type1'), [])
460 self.assertEquals(self.class2.FindScopes('Type2'), [])
461 self.assertEquals(self.class2.FindScopes('Scope1'), [self.scope1_c2,
462 self.scope1_c1])
463 self.assertEquals(self.class2.FindScopes('Scope2'), [self.scope2])
464 self.assertEquals(self.class2.FindScopes('Scope3'), [self.scope3])
465
466 self.assertEquals(self.class3.FindScopes('Class1'), [self.class1])
467 self.assertEquals(self.class3.FindScopes('Class2'), [self.class2])
468 self.assertEquals(self.class3.FindScopes('Type1'), [])
469 self.assertEquals(self.class3.FindScopes('Type2'), [])
470 self.assertEquals(self.class3.FindScopes('Scope1'), [])
471 self.assertEquals(self.class3.FindScopes('Scope2'), [])
472 self.assertEquals(self.class3.FindScopes('Scope3'), [])
473
474 def testLookUpBindingModel(self):
475 self.assertEquals(self.class1.LookUpBindingModel(), 'bm1')
476 self.assertEquals(self.class2.LookUpBindingModel(), 'bm1')
477 self.assertEquals(self.class3.LookUpBindingModel(), None)
478 self.assertRaises(syntax_tree.DerivingFromNonClassError,
479 self.class4.LookUpBindingModel)
480
481
482 class NamespaceTest(unittest.TestCase):
483 def setUp(self):
484 self.type1_n1 = MakeType('Type1')
485 self.type2 = MakeType('Type2')
486 self.scope1_n1 = MakeScope('Scope1')
487 self.scope2 = MakeScope('Scope2')
488 self.ns1 = syntax_tree.Namespace(_location, {}, 'ns1',
489 [self.type1_n1, self.type2,
490 self.scope1_n1, self.scope2])
491 self.type1_n2 = MakeType('Type1')
492 self.type3 = MakeType('Type3')
493 self.scope1_n2 = MakeScope('Scope1')
494 self.scope3 = MakeScope('Scope3')
495 self.ns2 = syntax_tree.Namespace(_location, {}, 'ns2',
496 [self.type1_n2, self.type3,
497 self.scope1_n2, self.scope3])
498 self.type_ns1 = MakeType('ns1')
499 self.ns3 = syntax_tree.Namespace(_location, {}, 'ns3', [self.ns1, self.ns2,
500 self.type_ns1])
501
502 def testTypeScope(self):
503 for ns in [self.ns1, self.ns2, self.ns3]:
504 self.assertFalse(ns.is_type)
505 self.assertTrue(ns.is_scope)
506
507 def testParents(self):
508 self.assertEquals(self.type1_n1.parent, self.ns1)
509 self.assertEquals(self.type2.parent, self.ns1)
510 self.assertEquals(self.scope1_n1.parent, self.ns1)
511 self.assertEquals(self.scope2.parent, self.ns1)
512 self.assertEquals(self.type1_n2.parent, self.ns2)
513 self.assertEquals(self.type3.parent, self.ns2)
514 self.assertEquals(self.scope1_n2.parent, self.ns2)
515 self.assertEquals(self.scope3.parent, self.ns2)
516 self.assertEquals(self.ns1.parent, self.ns3)
517 self.assertEquals(self.ns2.parent, self.ns3)
518 self.assertEquals(self.type_ns1.parent, self.ns3)
519
520 def testGetObjectsRecursive(self):
521 ns1_list = self.ns1.GetObjectsRecursive()
522 self.assertEquals(ns1_list[0], self.ns1)
523 ns1_list.sort()
524 ns1_list_expected = [self.ns1, self.type1_n1, self.type2,
525 self.scope1_n1, self.scope2]
526 ns1_list_expected.sort()
527 self.assertEquals(ns1_list, ns1_list_expected)
528 ns2_list = self.ns2.GetObjectsRecursive()
529 self.assertEquals(ns2_list[0], self.ns2)
530 ns2_list.sort()
531 ns2_list_expected = [self.ns2, self.type1_n2, self.type3,
532 self.scope1_n2, self.scope3]
533 ns2_list_expected.sort()
534 self.assertEquals(ns2_list, ns2_list_expected)
535 ns3_list = self.ns3.GetObjectsRecursive()
536 self.assertEquals(ns3_list[0], self.ns3)
537 ns3_list.sort()
538 ns3_list_expected = [self.ns3, self.type_ns1] + ns1_list + ns2_list
539 ns3_list_expected.sort()
540 self.assertEquals(ns3_list, ns3_list_expected)
541
542 def testLookUpType(self):
543 self.assertEquals(self.ns1.LookUpType('Type1'), self.type1_n1)
544 self.assertEquals(self.ns1.LookUpType('Type2'), self.type2)
545 self.assertEquals(self.ns1.LookUpType('Type3'), None)
546 self.assertEquals(self.ns1.LookUpType('ns1'), None)
547 self.assertEquals(self.ns1.LookUpType('ns2'), None)
548 self.assertEquals(self.ns1.LookUpType('ns3'), None)
549 self.assertEquals(self.ns1.LookUpType('Scope1'), None)
550 self.assertEquals(self.ns1.LookUpType('Scope2'), None)
551 self.assertEquals(self.ns1.LookUpType('Scope3'), None)
552
553 self.assertEquals(self.ns2.LookUpType('Type1'), self.type1_n2)
554 self.assertEquals(self.ns2.LookUpType('Type2'), None)
555 self.assertEquals(self.ns2.LookUpType('Type3'), self.type3)
556 self.assertEquals(self.ns2.LookUpType('ns1'), None)
557 self.assertEquals(self.ns2.LookUpType('ns2'), None)
558 self.assertEquals(self.ns2.LookUpType('ns3'), None)
559 self.assertEquals(self.ns2.LookUpType('Scope1'), None)
560 self.assertEquals(self.ns2.LookUpType('Scope2'), None)
561 self.assertEquals(self.ns2.LookUpType('Scope3'), None)
562
563 self.assertEquals(self.ns3.LookUpType('Type1'), None)
564 self.assertEquals(self.ns3.LookUpType('Type2'), None)
565 self.assertEquals(self.ns3.LookUpType('Type3'), None)
566 self.assertEquals(self.ns3.LookUpType('ns1'), self.type_ns1)
567 self.assertEquals(self.ns3.LookUpType('ns2'), None)
568 self.assertEquals(self.ns3.LookUpType('ns3'), None)
569 self.assertEquals(self.ns3.LookUpType('Scope1'), None)
570 self.assertEquals(self.ns3.LookUpType('Scope2'), None)
571 self.assertEquals(self.ns3.LookUpType('Scope3'), None)
572
573 def testFindScopes(self):
574 self.assertEquals(self.ns1.FindScopes('ns1'), [])
575 self.assertEquals(self.ns1.FindScopes('ns2'), [])
576 self.assertEquals(self.ns1.FindScopes('Type1'), [])
577 self.assertEquals(self.ns1.FindScopes('Type2'), [])
578 self.assertEquals(self.ns1.FindScopes('Scope1'), [self.scope1_n1])
579 self.assertEquals(self.ns1.FindScopes('Scope2'), [self.scope2])
580 self.assertEquals(self.ns1.FindScopes('Scope3'), [])
581
582 self.assertEquals(self.ns2.FindScopes('ns1'), [])
583 self.assertEquals(self.ns2.FindScopes('ns2'), [])
584 self.assertEquals(self.ns2.FindScopes('Type1'), [])
585 self.assertEquals(self.ns2.FindScopes('Type2'), [])
586 self.assertEquals(self.ns2.FindScopes('Scope1'), [self.scope1_n2])
587 self.assertEquals(self.ns2.FindScopes('Scope2'), [])
588 self.assertEquals(self.ns2.FindScopes('Scope3'), [self.scope3])
589
590 self.assertEquals(self.ns3.FindScopes('ns1'), [self.ns1])
591 self.assertEquals(self.ns3.FindScopes('ns2'), [self.ns2])
592 self.assertEquals(self.ns3.FindScopes('Type1'), [])
593 self.assertEquals(self.ns3.FindScopes('Type2'), [])
594 self.assertEquals(self.ns3.FindScopes('Scope1'), [])
595 self.assertEquals(self.ns3.FindScopes('Scope2'), [])
596 self.assertEquals(self.ns3.FindScopes('Scope3'), [])
597
598 def testMergeLookUpScope(self):
599 type1 = MakeType('Type1')
600 type2 = MakeType('Type2')
601 scope1 = MakeScope('Scope1')
602 scope2 = MakeScope('Scope2')
603 ns1 = syntax_tree.Namespace(_location, {}, 'ns', [type1, type2, scope1,
604 scope2])
605 ns1_list_copy = ns1.defn_list[:]
606 type3 = MakeType('Type3')
607 type4 = MakeType('Type4')
608 scope3 = MakeScope('Scope3')
609 scope4 = MakeScope('Scope4')
610 ns2 = syntax_tree.Namespace(_location, {}, 'ns', [type3, type4, scope3,
611 scope4])
612 ns2_list_copy = ns2.defn_list[:]
613 ns1.MergeLookUpScope(ns2)
614 self.assertEquals(ns1.scope, ns2.scope)
615 for ns in [ns1, ns2]:
616 self.assertEquals(ns.LookUpType('Type1'), type1)
617 self.assertEquals(ns.LookUpType('Type2'), type2)
618 self.assertEquals(ns.LookUpType('Type3'), type3)
619 self.assertEquals(ns.LookUpType('Type4'), type4)
620 self.assertEquals(ns.FindScopes('Scope1'), [scope1])
621 self.assertEquals(ns.FindScopes('Scope2'), [scope2])
622 self.assertEquals(ns.FindScopes('Scope3'), [scope3])
623 self.assertEquals(ns.FindScopes('Scope4'), [scope4])
624 self.assertEquals(ns1.defn_list, ns1_list_copy)
625 self.assertEquals(ns2.defn_list, ns2_list_copy)
626
627
628 class EnumTest(unittest.TestCase):
629 def setUp(self):
630 value1 = syntax_tree.Enum.Value('VALUE1', 1)
631 value2 = syntax_tree.Enum.Value('VALUE2', None)
632 self.enum1 = syntax_tree.Enum(_location, {}, 'Enum1', [value1, value2])
633 self.enum2 = syntax_tree.Enum(_location, {'binding_model': 'ignored'},
634 'Enum1', [value1, value2])
635
636 def testTypeScope(self):
637 for e in [self.enum1, self.enum2]:
638 self.assertTrue(e.is_type)
639 self.assertFalse(e.is_scope)
640
641 def testGetObjectsRecursive(self):
642 for e in [self.enum1, self.enum2]:
643 self.assertEquals(e.GetObjectsRecursive(), [e])
644
645 def testLookUpBindingModel(self):
646 for e in [self.enum1, self.enum2]:
647 self.assertEquals(e.LookUpBindingModel(), 'enum')
648
649
650 class FunctionTest(unittest.TestCase):
651 def setUp(self):
652 self.type1 = MakeType('Type1')
653 self.type2 = MakeType('Type2')
654 self.f1_p1_ref = TypeReferenceMock(self.type1)
655 self.f1_p2_ref = TypeReferenceMock(self.type2)
656 self.function1 = syntax_tree.Function(_location, {}, 'Function1', None,
657 [(self.f1_p1_ref, 'p1'),
658 (self.f1_p2_ref, 'p2')])
659 self.f2_ret_ref = TypeReferenceMock(self.type1)
660 self.function2 = syntax_tree.Function(_location, {}, 'Function2',
661 self.f2_ret_ref, [])
662 self.f3_p1_ref = TypeReferenceMock(None)
663 self.function3 = syntax_tree.Function(_location, {}, 'Function3', None,
664 [(self.f3_p1_ref, 'p1')])
665 self.f4_ret_ref = TypeReferenceMock(None)
666 self.function4 = syntax_tree.Function(_location, {}, 'Function4',
667 self.f4_ret_ref, [])
668 self.scope = ContextMock([self.type1, self.type2, self.function1,
669 self.function2, self.function3, self.function4],
670 [], 'globals', None)
671
672 def testTypeScope(self):
673 for f in [self.function1, self.function2, self.function3, self.function4]:
674 self.assertFalse(f.is_type)
675 self.assertFalse(f.is_scope)
676
677 def testParams(self):
678 self.assertEquals(self.function1.params[0].name, 'p1')
679 self.assertEquals(self.function1.params[0].mutable, False)
680 self.assertEquals(self.function1.params[1].name, 'p2')
681 self.assertEquals(self.function1.params[1].mutable, False)
682 self.assertEquals(self.function2.params, [])
683 self.assertEquals(self.function3.params[0].name, 'p1')
684 self.assertEquals(self.function3.params[0].mutable, False)
685 self.assertEquals(self.function4.params, [])
686
687 def testResolveTypeReferences(self):
688 self.function1.ResolveTypeReferences()
689 self.assertEquals(self.function1.type_defn, None)
690 self.assertEquals(self.function1.params[0].type_defn, self.type1)
691 self.assertEquals(self.function1.params[1].type_defn, self.type2)
692 self.function2.ResolveTypeReferences()
693 self.assertEquals(self.function2.type_defn, self.type1)
694 self.assertRaises(syntax_tree.TypeNotFoundError,
695 self.function3.ResolveTypeReferences)
696 self.assertRaises(syntax_tree.TypeNotFoundError,
697 self.function4.ResolveTypeReferences)
698 for ref in [self.f1_p1_ref, self.f1_p2_ref, self.f2_ret_ref,
699 self.f3_p1_ref, self.f4_ret_ref]:
700 self.assertEquals(ref.context, self.scope)
701 self.assertEquals(ref.scoped, False)
702
703
704 class CallbackTest(unittest.TestCase):
705 def setUp(self):
706 self.type1 = MakeType('Type1')
707 self.type2 = MakeType('Type2')
708 self.c1_p1_ref = TypeReferenceMock(self.type1)
709 self.c1_p2_ref = TypeReferenceMock(self.type2)
710 self.callback1 = syntax_tree.Callback(_location, {}, 'Callback1', None,
711 [(self.c1_p1_ref, 'p1'),
712 (self.c1_p2_ref, 'p2')])
713 self.c2_ret_ref = TypeReferenceMock(self.type1)
714 self.callback2 = syntax_tree.Callback(_location, {}, 'Callback2',
715 self.c2_ret_ref, [])
716 self.c3_p1_ref = TypeReferenceMock(None)
717 self.callback3 = syntax_tree.Callback(_location, {}, 'Callback3', None,
718 [(self.c3_p1_ref, 'p1')])
719 self.c4_ret_ref = TypeReferenceMock(None)
720 self.callback4 = syntax_tree.Callback(_location,
721 {'binding_model': 'test_bm'},
722 'Callback4', self.c4_ret_ref, [])
723 self.scope = ContextMock([self.type1, self.type2, self.callback1,
724 self.callback2, self.callback3, self.callback4],
725 [], 'globals', None)
726
727 def testTypeScope(self):
728 for c in [self.callback1, self.callback2, self.callback3, self.callback4]:
729 self.assertTrue(c.is_type)
730 self.assertFalse(c.is_scope)
731
732 def testParams(self):
733 self.assertEquals(self.callback1.params[0].name, 'p1')
734 self.assertEquals(self.callback1.params[0].mutable, False)
735 self.assertEquals(self.callback1.params[1].name, 'p2')
736 self.assertEquals(self.callback1.params[1].mutable, False)
737 self.assertEquals(self.callback2.params, [])
738 self.assertEquals(self.callback3.params[0].name, 'p1')
739 self.assertEquals(self.callback3.params[0].mutable, False)
740 self.assertEquals(self.callback4.params, [])
741
742 def testResolveTypeReferences(self):
743 self.callback1.ResolveTypeReferences()
744 self.assertEquals(self.callback1.type_defn, None)
745 self.assertEquals(self.callback1.params[0].type_defn, self.type1)
746 self.assertEquals(self.callback1.params[1].type_defn, self.type2)
747 self.callback2.ResolveTypeReferences()
748 self.assertEquals(self.callback2.type_defn, self.type1)
749 self.assertRaises(syntax_tree.TypeNotFoundError,
750 self.callback3.ResolveTypeReferences)
751 self.assertRaises(syntax_tree.TypeNotFoundError,
752 self.callback4.ResolveTypeReferences)
753 for ref in [self.c1_p1_ref, self.c1_p2_ref, self.c2_ret_ref,
754 self.c3_p1_ref, self.c4_ret_ref]:
755 self.assertEquals(ref.context, self.scope)
756 self.assertEquals(ref.scoped, False)
757
758 def testLookUpBindingModel(self):
759 self.assertEquals(self.callback1.LookUpBindingModel(), 'callback')
760 self.assertEquals(self.callback2.LookUpBindingModel(), 'callback')
761 self.assertEquals(self.callback3.LookUpBindingModel(), 'callback')
762 self.assertEquals(self.callback4.LookUpBindingModel(), 'test_bm')
763
764
765 class VariableTest(unittest.TestCase):
766 def setUp(self):
767 self.type_defn = MakeType('Type')
768 self.v1_type_ref = TypeReferenceMock(self.type_defn)
769 self.v2_type_ref = TypeReferenceMock(None)
770 self.variable1 = syntax_tree.Variable(_location, {}, 'Variable1',
771 self.v1_type_ref)
772 self.variable2 = syntax_tree.Variable(_location, {}, 'Variable2',
773 self.v2_type_ref)
774 self.scope = ContextMock([self.type_defn, self.variable1, self.variable2],
775 [], 'globals', None)
776
777 def testTypeScope(self):
778 for v in [self.variable1, self.variable2]:
779 self.assertFalse(v.is_type)
780 self.assertFalse(v.is_scope)
781
782 def testResolveTypeReferences(self):
783 self.variable1.ResolveTypeReferences()
784 self.assertEquals(self.variable1.type_defn, self.type_defn)
785 self.assertRaises(syntax_tree.TypeNotFoundError,
786 self.variable2.ResolveTypeReferences)
787 for ref in [self.v1_type_ref, self.v2_type_ref]:
788 self.assertEquals(ref.context, self.scope)
789 self.assertEquals(ref.scoped, False)
790
791
792 class TypedefTest(unittest.TestCase):
793 def setUp(self):
794 self.base_type1 = syntax_tree.Typename(_location, {'binding_model': 'bm1'},
795 'BaseType1')
796 self.base_type2_t = MakeType('Type')
797 self.base_type2_s = MakeScope('Scope')
798 self.base_type2 = syntax_tree.Class(_location, {'binding_model': 'bm2'},
799 'BaseType2', None, [self.base_type2_t,
800 self.base_type2_s])
801 self.t1_ref = TypeReferenceMock(self.base_type1)
802 self.typedef1 = syntax_tree.Typedef(_location, {}, 'Typedef1', self.t1_ref)
803 self.t2_ref = TypeReferenceMock(self.typedef1)
804 self.typedef2 = syntax_tree.Typedef(_location, {}, 'Typedef2', self.t2_ref)
805 self.t3_ref = TypeReferenceMock(None)
806 self.typedef3 = syntax_tree.Typedef(_location, {}, 'Typedef3', self.t3_ref)
807 self.t4_ref = TypeReferenceMock(self.base_type2)
808 self.typedef4 = syntax_tree.Typedef(_location, {}, 'Typedef4', self.t4_ref)
809 self.t5_ref = TypeReferenceMock(self.base_type2)
810 self.typedef5 = syntax_tree.Typedef(_location, {'binding_model': 'bm3'},
811 'Typedef5', self.t5_ref)
812 self.scope = ContextMock([self.base_type1, self.base_type2, self.typedef1,
813 self.typedef2, self.typedef3, self.typedef4,
814 self.typedef5], [], 'globals', None)
815
816 def testTypeScope(self):
817 for t in [self.typedef1, self.typedef2, self.typedef3, self.typedef4,
818 self.typedef5]:
819 self.assertTrue(t.is_type)
820 self.assertTrue(t.is_scope)
821
822 def testResolveTypeReferences(self):
823 self.typedef1.ResolveTypeReferences()
824 self.assertEquals(self.typedef1.type_defn, self.base_type1)
825 self.typedef2.ResolveTypeReferences()
826 self.assertEquals(self.typedef2.type_defn, self.typedef1)
827 self.assertRaises(syntax_tree.TypeNotFoundError,
828 self.typedef3.ResolveTypeReferences)
829 self.typedef4.ResolveTypeReferences()
830 self.assertEquals(self.typedef4.type_defn, self.base_type2)
831 self.typedef5.ResolveTypeReferences()
832 self.assertEquals(self.typedef5.type_defn, self.base_type2)
833 for typedef in [self.typedef1, self.typedef2, self.typedef3, self.typedef4,
834 self.typedef5]:
835 self.assertTrue(typedef._types_resolved)
836 for ref in [self.t1_ref, self.t2_ref, self.t3_ref, self.t4_ref,
837 self.t5_ref]:
838 self.assertEquals(ref.context, self.scope)
839 self.assertEquals(ref.scoped, False)
840
841 def testGetTypeSafe(self):
842 self.assertEquals(self.typedef1.GetTypeSafe(), self.base_type1)
843 self.assertEquals(self.typedef2.GetTypeSafe(), self.typedef1)
844 self.assertRaises(syntax_tree.TypeNotFoundError, self.typedef3.GetTypeSafe)
845 self.assertEquals(self.typedef4.GetTypeSafe(), self.base_type2)
846 self.assertEquals(self.typedef5.GetTypeSafe(), self.base_type2)
847
848 def testLookUpBindingModel(self):
849 self.assertEquals(self.typedef1.LookUpBindingModel(), 'bm1')
850 self.assertEquals(self.typedef2.LookUpBindingModel(), 'bm1')
851 self.assertRaises(syntax_tree.TypeNotFoundError,
852 self.typedef3.LookUpBindingModel)
853 self.assertEquals(self.typedef4.LookUpBindingModel(), 'bm2')
854 self.assertEquals(self.typedef5.LookUpBindingModel(), 'bm3')
855
856 def testGetFinalType(self):
857 self.assertEquals(self.typedef1.GetFinalType(), self.base_type1)
858 self.assertEquals(self.typedef2.GetFinalType(), self.base_type1)
859 self.assertRaises(syntax_tree.TypeNotFoundError, self.typedef3.GetFinalType)
860 self.assertEquals(self.typedef4.GetFinalType(), self.base_type2)
861 self.assertEquals(self.typedef5.GetFinalType(), self.base_type2)
862
863 def testLookUpType(self):
864 for t in [self.typedef1, self.typedef2]:
865 self.assertEquals(t.LookUpType('Type'), None)
866 self.assertEquals(t.LookUpType('NonType'), None)
867 self.assertRaises(syntax_tree.TypeNotFoundError, self.typedef3.LookUpType,
868 'Type')
869 for t in [self.typedef4, self.typedef5]:
870 self.assertEquals(t.LookUpType('Type'), self.base_type2_t)
871 self.assertEquals(t.LookUpType('NonType'), None)
872
873 def testFindScopes(self):
874 for t in [self.typedef1, self.typedef2]:
875 self.assertEquals(t.FindScopes('Scope'), [])
876 self.assertEquals(t.FindScopes('NonScope'), [])
877 self.assertRaises(syntax_tree.TypeNotFoundError, self.typedef3.FindScopes,
878 'Scope')
879 for t in [self.typedef4, self.typedef5]:
880 self.assertEquals(t.FindScopes('Scope'), [self.base_type2_s])
881 self.assertEquals(t.FindScopes('NonScope'), [])
882
883
884 class TypenameTest(unittest.TestCase):
885 def testTypename(self):
886 typename = syntax_tree.Typename(_location, {'binding_model': 'bm'}, 'Type')
887 self.assertTrue(typename.is_type)
888 self.assertFalse(typename.is_scope)
889 self.assertEquals(typename.LookUpBindingModel(), 'bm')
890
891
892 class VerbatimTest(unittest.TestCase):
893 def testVerbatim(self):
894 verbatim = syntax_tree.Verbatim(_location, {}, 'verbatim')
895 self.assertFalse(verbatim.is_type)
896 self.assertFalse(verbatim.is_scope)
897
898
899 class ArrayTest(unittest.TestCase):
900 def testArray(self):
901 data_type = syntax_tree.Typename(_location, {'binding_model': 'bm1'},
902 'BaseType')
903 unsized_array = syntax_tree.Array(data_type, None)
904 sized_array = syntax_tree.Array(data_type, 42)
905 for a in [unsized_array, sized_array]:
906 self.assertTrue(a.is_type)
907 self.assertFalse(a.is_scope)
908 self.assertEquals(a.GetObjectsRecursive(), [a])
909 self.assertEquals(sized_array.LookUpBindingModel(), 'sized_array')
910 self.assertEquals(unsized_array.LookUpBindingModel(), 'unsized_array')
911
912
913 class CheckTypeInChainTest(unittest.TestCase):
914 def testTypedef2(self):
915 # typedef Typedef1 Typedef2;
916 # typedef Typedef2 Typedef1;
917 type1_ref = syntax_tree.NameTypeReference(_location, 'Typedef2')
918 type1 = syntax_tree.Typedef(_location, {}, 'Typedef1', type1_ref)
919 type2_ref = syntax_tree.NameTypeReference(_location, 'Typedef1')
920 type2 = syntax_tree.Typedef(_location, {}, 'Typedef2', type2_ref)
921 unused_scope1 = ContextMock([type1, type2], [], 'globals', None)
922 syntax_tree.CheckTypeInChain(type1, type2)
923 syntax_tree.CheckTypeInChain(type2, type1)
924 type1.ResolveTypeReferences()
925 syntax_tree.CheckTypeInChain(type1, type2)
926 self.assertRaises(syntax_tree.CircularTypedefError,
927 syntax_tree.CheckTypeInChain, type2, type1)
928 self.assertRaises(syntax_tree.CircularTypedefError,
929 type2.ResolveTypeReferences)
930
931 def testTypedef3(self):
932 # typedef Typedef1 Typedef2;
933 # typedef Typedef2 Typedef3;
934 # typedef Typedef3 Typedef1;
935 type1_ref = syntax_tree.NameTypeReference(_location, 'Typedef2')
936 type1 = syntax_tree.Typedef(_location, {}, 'Typedef1', type1_ref)
937 type2_ref = syntax_tree.NameTypeReference(_location, 'Typedef3')
938 type2 = syntax_tree.Typedef(_location, {}, 'Typedef2', type2_ref)
939 type3_ref = syntax_tree.NameTypeReference(_location, 'Typedef1')
940 type3 = syntax_tree.Typedef(_location, {}, 'Typedef3', type3_ref)
941 unused_scope1 = ContextMock([type1, type2, type3], [], 'globals', None)
942 syntax_tree.CheckTypeInChain(type1, type2)
943 syntax_tree.CheckTypeInChain(type2, type3)
944 syntax_tree.CheckTypeInChain(type3, type1)
945 type1.ResolveTypeReferences()
946 syntax_tree.CheckTypeInChain(type1, type2)
947 syntax_tree.CheckTypeInChain(type2, type3)
948 syntax_tree.CheckTypeInChain(type3, type1)
949 type2.ResolveTypeReferences()
950 syntax_tree.CheckTypeInChain(type1, type2)
951 syntax_tree.CheckTypeInChain(type2, type3)
952 self.assertRaises(syntax_tree.CircularTypedefError,
953 syntax_tree.CheckTypeInChain, type3, type1)
954 self.assertRaises(syntax_tree.CircularTypedefError,
955 type3.ResolveTypeReferences)
956
957 def testClass2(self):
958 # class Class1: Class2 {};
959 # class Class2: Class1 {};
960 type1_ref = syntax_tree.NameTypeReference(_location, 'Class2')
961 type1 = syntax_tree.Class(_location, {}, 'Class1', type1_ref, [])
962 type2_ref = syntax_tree.NameTypeReference(_location, 'Class1')
963 type2 = syntax_tree.Class(_location, {}, 'Class2', type2_ref, [])
964 unused_scope1 = ContextMock([type1, type2], [], 'globals', None)
965 syntax_tree.CheckTypeInChain(type1, type2)
966 syntax_tree.CheckTypeInChain(type2, type1)
967 type1.ResolveTypeReferences()
968 syntax_tree.CheckTypeInChain(type1, type2)
969 self.assertRaises(syntax_tree.CircularTypedefError,
970 syntax_tree.CheckTypeInChain, type2, type1)
971 self.assertRaises(syntax_tree.CircularTypedefError,
972 type2.ResolveTypeReferences)
973
974 def testClass3(self):
975 # class Class1: Class2 {};
976 # class Class2: Class3 {};
977 # class Class3: Class1 {};
978 type1_ref = syntax_tree.NameTypeReference(_location, 'Class2')
979 type1 = syntax_tree.Class(_location, {}, 'Class1', type1_ref, [])
980 type2_ref = syntax_tree.NameTypeReference(_location, 'Class3')
981 type2 = syntax_tree.Class(_location, {}, 'Class2', type2_ref, [])
982 type3_ref = syntax_tree.NameTypeReference(_location, 'Class1')
983 type3 = syntax_tree.Class(_location, {}, 'Class3', type3_ref, [])
984 unused_scope1 = ContextMock([type1, type2, type3], [], 'globals', None)
985 syntax_tree.CheckTypeInChain(type1, type2)
986 syntax_tree.CheckTypeInChain(type2, type3)
987 syntax_tree.CheckTypeInChain(type3, type1)
988 type1.ResolveTypeReferences()
989 syntax_tree.CheckTypeInChain(type1, type2)
990 syntax_tree.CheckTypeInChain(type2, type3)
991 syntax_tree.CheckTypeInChain(type3, type1)
992 type2.ResolveTypeReferences()
993 syntax_tree.CheckTypeInChain(type1, type2)
994 syntax_tree.CheckTypeInChain(type2, type3)
995 self.assertRaises(syntax_tree.CircularTypedefError,
996 syntax_tree.CheckTypeInChain, type3, type1)
997 self.assertRaises(syntax_tree.CircularTypedefError,
998 type3.ResolveTypeReferences)
999
1000 def testArray(self):
1001 # typedef Typedef1[] Typedef1;
1002 type1_ref_ref = syntax_tree.NameTypeReference(_location, 'Typedef1')
1003 type1_ref = syntax_tree.ArrayTypeReference(_location, type1_ref_ref, None)
1004 type1 = syntax_tree.Typedef(_location, {}, 'Typedef1', type1_ref)
1005 unused_scope1 = ContextMock([type1], [], 'globals', None)
1006 self.assertRaises(syntax_tree.CircularTypedefError,
1007 type1.ResolveTypeReferences)
1008
1009 def testMixed(self):
1010 # typedef Class Typedef;
1011 # class Class: Typedef {};
1012 type1_ref = syntax_tree.NameTypeReference(_location, 'Class')
1013 type1 = syntax_tree.Typedef(_location, {}, 'Typedef', type1_ref)
1014 type2_ref = syntax_tree.NameTypeReference(_location, 'Typedef')
1015 type2 = syntax_tree.Class(_location, {}, 'Class', type2_ref, [])
1016 unused_scope1 = ContextMock([type1, type2], [], 'globals', None)
1017 syntax_tree.CheckTypeInChain(type1, type2)
1018 syntax_tree.CheckTypeInChain(type2, type1)
1019 type1.ResolveTypeReferences()
1020 syntax_tree.CheckTypeInChain(type1, type2)
1021 self.assertRaises(syntax_tree.CircularTypedefError,
1022 syntax_tree.CheckTypeInChain, type2, type1)
1023 self.assertRaises(syntax_tree.CircularTypedefError,
1024 type2.ResolveTypeReferences)
1025
1026
1027 class LookUpScopeTest(unittest.TestCase):
1028 def testLookUpScope(self):
1029 type1 = MakeType('Type1')
1030 type2 = MakeType('Type2')
1031 scope1 = MakeScope('Scope1')
1032 scope2 = MakeScope('Scope2')
1033 scope = syntax_tree.LookUpScope([type1, type2, scope1, scope2])
1034 self.assertEquals(scope.LookUpType('Type1'), type1)
1035 self.assertEquals(scope.LookUpType('Type2'), type2)
1036 self.assertEquals(scope.LookUpType('Type3'), None)
1037 self.assertEquals(scope.LookUpType('Scope1'), None)
1038 self.assertEquals(scope.LookUpType('Scope2'), None)
1039 self.assertEquals(scope.LookUpType('Scope3'), None)
1040 self.assertEquals(scope.FindScopes('Type1'), [])
1041 self.assertEquals(scope.FindScopes('Type2'), [])
1042 self.assertEquals(scope.FindScopes('Type3'), [])
1043 self.assertEquals(scope.FindScopes('Scope1'), [scope1])
1044 self.assertEquals(scope.FindScopes('Scope2'), [scope2])
1045 self.assertEquals(scope.FindScopes('Scope3'), [])
1046 type3 = MakeType('Type3')
1047 scope3 = MakeScope('Scope3')
1048 scope3_bis = MakeScope('Scope3')
1049 scope.list.extend([type3, scope3, scope3_bis])
1050 scope.ResetCache()
1051 self.assertEquals(scope.LookUpType('Type1'), type1)
1052 self.assertEquals(scope.LookUpType('Type2'), type2)
1053 self.assertEquals(scope.LookUpType('Type3'), type3)
1054 self.assertEquals(scope.LookUpType('Scope1'), None)
1055 self.assertEquals(scope.LookUpType('Scope2'), None)
1056 self.assertEquals(scope.LookUpType('Scope3'), None)
1057 self.assertEquals(scope.FindScopes('Type1'), [])
1058 self.assertEquals(scope.FindScopes('Type2'), [])
1059 self.assertEquals(scope.FindScopes('Type3'), [])
1060 self.assertEquals(scope.FindScopes('Scope1'), [scope1])
1061 self.assertEquals(scope.FindScopes('Scope2'), [scope2])
1062 self.assertEquals(scope.FindScopes('Scope3'), [scope3, scope3_bis])
1063
1064
1065 class GetObjectsRecursiveTest(unittest.TestCase):
1066 def testGetObjectsRecursive(self):
1067 class1 = syntax_tree.Class(_location, {}, 'Class1', None,
1068 [MakeType('Type1'), MakeType('Type2'),
1069 MakeScope('Scope1'), MakeScope('Scope2')])
1070 class2 = syntax_tree.Class(_location, {}, 'Class1', None,
1071 [MakeType('Type1'), MakeType('Type3'),
1072 MakeScope('Scope2'), MakeScope('Scope3')])
1073 type1 = MakeType('Type1')
1074 scope1 = MakeScope('Scope1')
1075 self.assertEquals(syntax_tree.GetObjectsRecursive([class1, class2, type1,
1076 scope1]),
1077 class1.GetObjectsRecursive() +
1078 class2.GetObjectsRecursive() +
1079 type1.GetObjectsRecursive() +
1080 scope1.GetObjectsRecursive())
1081
1082
1083 class MergeNamespacesRecursive(unittest.TestCase):
1084 def testMergeNamespaceResucrsive(self):
1085 def MakeNamespace(name, defn_list):
1086 return syntax_tree.Namespace(_location, {}, name, defn_list)
1087
1088 ns1_1 = MakeNamespace('Namespace1', [MakeType('Type1_1'),
1089 MakeScope('Scope1_1')])
1090 ns1_2 = MakeNamespace('Namespace1', [MakeType('Type1_2'),
1091 MakeScope('Scope1_2')])
1092 ns2_1 = MakeNamespace('Namespace2', [ns1_1, ns1_2])
1093 ns1_3 = MakeNamespace('Namespace1', [MakeType('Type1_3'),
1094 MakeScope('Scope1_3')])
1095 ns1_4 = MakeNamespace('Namespace1', [MakeType('Type1_4'),
1096 MakeScope('Scope1_4')])
1097 ns2_2 = MakeNamespace('Namespace2', [ns1_3, ns1_4])
1098 ns = MakeNamespace('global', [ns2_1, ns2_2])
1099 syntax_tree.MergeNamespacesRecursive(ns)
1100 self.assertEquals(ns2_1.scope, ns2_2.scope)
1101 self.assertNotEquals(ns2_1.defn_list, ns2_2.defn_list)
1102 self.assertEquals(ns1_1.scope, ns1_2.scope)
1103 self.assertEquals(ns1_1.scope, ns1_3.scope)
1104 self.assertEquals(ns1_1.scope, ns1_4.scope)
1105 self.assertNotEquals(ns1_1.defn_list, ns1_2.defn_list)
1106 self.assertNotEquals(ns1_1.defn_list, ns1_3.defn_list)
1107 self.assertNotEquals(ns1_1.defn_list, ns1_4.defn_list)
1108 self.assertNotEquals(ns1_2.defn_list, ns1_3.defn_list)
1109 self.assertNotEquals(ns1_2.defn_list, ns1_4.defn_list)
1110 self.assertNotEquals(ns1_3.defn_list, ns1_4.defn_list)
1111
1112
1113 if __name__ == '__main__':
1114 unittest.main()
OLDNEW
« no previous file with comments | « tools/nixysa/nixysa/syntax_tree.py ('k') | tools/nixysa/nixysa/unsized_array_binding.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698