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.
9 def getFunction(schema, name):
10 for item in schema['functions']:
11 if item['name'] == name:
13 raise KeyError('Missing function %s' % name)
16 def getParams(schema, name):
17 function = getFunction(schema, name)
18 return function['parameters']
21 def getReturns(schema, name):
22 function = getFunction(schema, name)
23 return function['returns']
26 def getType(schema, id):
27 for item in schema['types']:
32 class IdlSchemaTest(unittest.TestCase):
34 loaded = idl_schema.Load('test/idl_basics.idl')
35 self.assertEquals(1, len(loaded))
36 self.assertEquals('idl_basics', loaded[0]['namespace'])
37 self.idl_basics = loaded[0]
39 def testSimpleCallbacks(self):
40 schema = self.idl_basics
41 expected = [{'type':'function', 'name':'cb', 'parameters':[]}]
42 self.assertEquals(expected, getParams(schema, 'function4'))
44 expected = [{'type':'function', 'name':'cb',
45 'parameters':[{'name':'x', 'type':'integer'}]}]
46 self.assertEquals(expected, getParams(schema, 'function5'))
48 expected = [{'type':'function', 'name':'cb',
49 'parameters':[{'name':'arg', '$ref':'MyType1'}]}]
50 self.assertEquals(expected, getParams(schema, 'function6'))
52 def testCallbackWithArrayArgument(self):
53 schema = self.idl_basics
54 expected = [{'type':'function', 'name':'cb',
55 'parameters':[{'name':'arg', 'type':'array',
56 'items':{'$ref':'MyType2'}}]}]
57 self.assertEquals(expected, getParams(schema, 'function12'))
60 def testArrayOfCallbacks(self):
61 schema = idl_schema.Load('test/idl_callback_arrays.idl')[0]
62 expected = [{'type':'array', 'name':'callbacks',
63 'items':{'type':'function', 'name':'MyCallback',
64 'parameters':[{'type':'integer', 'name':'x'}]}}]
65 self.assertEquals(expected, getParams(schema, 'whatever'))
67 def testLegalValues(self):
69 'x': {'name': 'x', 'type': 'integer', 'enum': [1,2],
70 'description': 'This comment tests "double-quotes".'},
71 'y': {'name': 'y', 'type': 'string'},
72 'z': {'name': 'z', 'type': 'string'},
73 'a': {'name': 'a', 'type': 'string'},
74 'b': {'name': 'b', 'type': 'string'},
75 'c': {'name': 'c', 'type': 'string'}},
76 getType(self.idl_basics, 'MyType1')['properties'])
78 def testMemberOrdering(self):
80 ['x', 'y', 'z', 'a', 'b', 'c'],
81 getType(self.idl_basics, 'MyType1')['properties'].keys())
84 schema = self.idl_basics
85 expected = {'enum': [{'name': 'name1', 'description': 'comment1'},
87 'description': 'Enum description',
88 'type': 'string', 'id': 'EnumType'}
89 self.assertEquals(expected, getType(schema, expected['id']))
91 expected = [{'name':'type', '$ref':'EnumType'},
92 {'type':'function', 'name':'cb',
93 'parameters':[{'name':'type', '$ref':'EnumType'}]}]
94 self.assertEquals(expected, getParams(schema, 'function13'))
96 expected = [{'items': {'$ref': 'EnumType'}, 'name': 'types',
98 self.assertEquals(expected, getParams(schema, 'function14'))
100 def testNoCompile(self):
101 schema = self.idl_basics
102 func = getFunction(schema, 'function15')
103 self.assertTrue(func is not None)
104 self.assertTrue(func['nocompile'])
106 def testNoDocOnEnum(self):
107 schema = self.idl_basics
108 enum_with_nodoc = getType(schema, 'EnumTypeWithNoDoc')
109 self.assertTrue(enum_with_nodoc is not None)
110 self.assertTrue(enum_with_nodoc['nodoc'])
112 def testInternalNamespace(self):
113 idl_basics = self.idl_basics
114 self.assertEquals('idl_basics', idl_basics['namespace'])
115 self.assertTrue(idl_basics['internal'])
116 self.assertFalse(idl_basics['nodoc'])
118 def testReturnTypes(self):
119 schema = self.idl_basics
120 self.assertEquals({'name': 'function19', 'type': 'integer'},
121 getReturns(schema, 'function19'))
122 self.assertEquals({'name': 'function20', '$ref': 'MyType1',
124 getReturns(schema, 'function20'))
125 self.assertEquals({'name': 'function21', 'type': 'array',
126 'items': {'$ref': 'MyType1'}},
127 getReturns(schema, 'function21'))
128 self.assertEquals({'name': 'function22', '$ref': 'EnumType',
130 getReturns(schema, 'function22'))
131 self.assertEquals({'name': 'function23', 'type': 'array',
132 'items': {'$ref': 'EnumType'}},
133 getReturns(schema, 'function23'))
135 def testChromeOSPlatformsNamespace(self):
136 schema = idl_schema.Load('test/idl_namespace_chromeos.idl')[0]
137 self.assertEquals('idl_namespace_chromeos', schema['namespace'])
138 expected = ['chromeos']
139 self.assertEquals(expected, schema['platforms'])
141 def testAllPlatformsNamespace(self):
142 schema = idl_schema.Load('test/idl_namespace_all_platforms.idl')[0]
143 self.assertEquals('idl_namespace_all_platforms', schema['namespace'])
144 expected = ['chromeos', 'chromeos_touch', 'linux', 'mac', 'win']
145 self.assertEquals(expected, schema['platforms'])
147 def testNonSpecificPlatformsNamespace(self):
148 schema = idl_schema.Load('test/idl_namespace_non_specific_platforms.idl')[0]
149 self.assertEquals('idl_namespace_non_specific_platforms',
152 self.assertEquals(expected, schema['platforms'])
154 def testSpecificImplementNamespace(self):
155 schema = idl_schema.Load('test/idl_namespace_specific_implement.idl')[0]
156 self.assertEquals('idl_namespace_specific_implement',
158 expected = 'idl_namespace_specific_implement.idl'
159 self.assertEquals(expected, schema['compiler_options']['implemented_in'])
161 def testSpecificImplementOnChromeOSNamespace(self):
162 schema = idl_schema.Load(
163 'test/idl_namespace_specific_implement_chromeos.idl')[0]
164 self.assertEquals('idl_namespace_specific_implement_chromeos',
166 expected_implemented_path = 'idl_namespace_specific_implement_chromeos.idl'
167 expected_platform = ['chromeos']
168 self.assertEquals(expected_implemented_path,
169 schema['compiler_options']['implemented_in'])
170 self.assertEquals(expected_platform, schema['platforms'])
172 def testCallbackComment(self):
173 schema = self.idl_basics
174 self.assertEquals('A comment on a callback.',
175 getParams(schema, 'function16')[0]['description'])
178 getParams(schema, 'function16')[0]['parameters'][0]['description'])
180 'Just a parameter comment, with no comment on the callback.',
181 getParams(schema, 'function17')[0]['parameters'][0]['description'])
183 'Override callback comment.',
184 getParams(schema, 'function18')[0]['description'])
186 def testFunctionComment(self):
187 schema = self.idl_basics
188 func = getFunction(schema, 'function3')
189 self.assertEquals(('This comment should appear in the documentation, '
190 'despite occupying multiple lines.'),
193 [{'description': ('So should this comment about the argument. '
194 '<em>HTML</em> is fine too.'),
198 func = getFunction(schema, 'function4')
199 self.assertEquals(('This tests if "double-quotes" are escaped correctly.'
200 '<br/><br/> It also tests a comment with two newlines.'),
203 def testReservedWords(self):
204 schema = idl_schema.Load('test/idl_reserved_words.idl')[0]
206 foo_type = getType(schema, 'Foo')
207 self.assertEquals([{'name': 'float'}, {'name': 'DOMString'}],
210 enum_type = getType(schema, 'enum')
211 self.assertEquals([{'name': 'callback'}, {'name': 'namespace'}],
214 dictionary = getType(schema, 'dictionary')
215 self.assertEquals('integer', dictionary['properties']['long']['type'])
217 mytype = getType(schema, 'MyType')
218 self.assertEquals('string', mytype['properties']['interface']['type'])
220 params = getParams(schema, 'static')
221 self.assertEquals('Foo', params[0]['$ref'])
222 self.assertEquals('enum', params[1]['$ref'])
225 if __name__ == '__main__':