Elim cr-checkbox
[chromium-blink-merge.git] / ppapi / generators / idl_propertynode.py
blobe15354ad0a4d78be2cb6782ae3507f5cb87356ec
1 #!/usr/bin/env python
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 """
7 import re
8 import sys
10 from idl_log import ErrOut, InfoOut, WarnOut
13 # IDLPropertyNode
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):
20 def __init__(self):
21 self.parents = []
22 self.property_map = {}
24 def AddParent(self, parent):
25 assert 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)
34 if prop is not None:
35 return prop
36 # If not, seach parents in order
37 for parent in self.parents:
38 prop = parent.GetProperty(name)
39 if prop is not None:
40 return prop
41 # Otherwise, it can not be found.
42 return None
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()
52 # Testing functions
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)
61 for prop in props:
62 toks = prop.split('=')
63 node.SetProperty(toks[0], toks[1])
64 if children:
65 for child in children:
66 child.AddParent(node)
67 if parents:
68 for parent in parents:
69 node.AddParent(parent)
70 return node
72 def ExpectProp(node, name, val):
73 found = node.GetProperty(name)
74 if found != val:
75 ErrOut.Log('Got property %s expecting %s' % (found, val))
76 return 1
77 return 0
80 # Verify property inheritance
82 def PropertyTest():
83 errors = 0
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')
97 if not errors:
98 InfoOut.Log('Passed PropertyTest')
99 return errors
102 def Main():
103 errors = 0
104 errors += PropertyTest()
106 if errors:
107 ErrOut.Log('IDLNode failed with %d errors.' % errors)
108 return -1
109 return 0
112 if __name__ == '__main__':
113 sys.exit(Main())