2 # Copyright (c) 2012 The Chromium Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file.
6 """ Hierarchical property system for IDL AST """
10 from idl_log
import ErrOut
, InfoOut
, WarnOut
15 # A property node is a hierarchically aware system for mapping
16 # keys to values, such that a local dictionary is search first,
17 # followed by parent dictionaries in order.
19 class IDLPropertyNode(object):
22 self
.property_map
= {}
24 def AddParent(self
, parent
):
26 self
.parents
.append(parent
)
28 def SetProperty(self
, name
, val
):
29 self
.property_map
[name
] = val
31 def GetProperty(self
, name
):
32 # Check locally for the property, and return it if found.
33 prop
= self
.property_map
.get(name
, None)
36 # If not, seach parents in order
37 for parent
in self
.parents
:
38 prop
= parent
.GetProperty(name
)
41 # Otherwise, it can not be found.
44 def GetPropertyLocal(self
, name
):
45 # Search for the property, but only locally.
46 return self
.property_map
.get(name
, None)
48 def GetPropertyList(self
):
49 return self
.property_map
.keys()
55 # Build a property node, setting the properties including a name, and
56 # associate the children with this new node.
58 def BuildNode(name
, props
, children
=None, parents
=None):
59 node
= IDLPropertyNode()
60 node
.SetProperty('NAME', name
)
62 toks
= prop
.split('=')
63 node
.SetProperty(toks
[0], toks
[1])
65 for child
in children
:
68 for parent
in parents
:
69 node
.AddParent(parent
)
72 def ExpectProp(node
, name
, val
):
73 found
= node
.GetProperty(name
)
75 ErrOut
.Log('Got property %s expecting %s' % (found
, val
))
80 # Verify property inheritance
84 left
= BuildNode('Left', ['Left=Left'])
85 right
= BuildNode('Right', ['Right=Right'])
86 top
= BuildNode('Top', ['Left=Top', 'Right=Top'], [left
, right
])
88 errors
+= ExpectProp(top
, 'Left', 'Top')
89 errors
+= ExpectProp(top
, 'Right', 'Top')
91 errors
+= ExpectProp(left
, 'Left', 'Left')
92 errors
+= ExpectProp(left
, 'Right', 'Top')
94 errors
+= ExpectProp(right
, 'Left', 'Top')
95 errors
+= ExpectProp(right
, 'Right', 'Right')
98 InfoOut
.Log('Passed PropertyTest')
104 errors
+= PropertyTest()
107 ErrOut
.Log('IDLNode failed with %d errors.' % errors
)
112 if __name__
== '__main__':