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 getType(schema, id):
22 for item in schema['types']:
27 class IdlSchemaTest(unittest.TestCase):
29 loaded = idl_schema.Load('test/idl_basics.idl')
30 self.assertEquals(1, len(loaded))
31 self.assertEquals('idl_basics', loaded[0]['namespace'])
32 self.idl_basics = loaded[0]
34 def testSimpleCallbacks(self):
35 schema = self.idl_basics
36 expected = [{'type':'function', 'name':'cb', 'parameters':[]}]
37 self.assertEquals(expected, getParams(schema, 'function4'))
39 expected = [{'type':'function', 'name':'cb',
40 'parameters':[{'name':'x', 'type':'integer'}]}]
41 self.assertEquals(expected, getParams(schema, 'function5'))
43 expected = [{'type':'function', 'name':'cb',
44 'parameters':[{'name':'arg', '$ref':'MyType1'}]}]
45 self.assertEquals(expected, getParams(schema, 'function6'))
47 def testCallbackWithArrayArgument(self):
48 schema = self.idl_basics
49 expected = [{'type':'function', 'name':'cb',
50 'parameters':[{'name':'arg', 'type':'array',
51 'items':{'$ref':'MyType2'}}]}]
52 self.assertEquals(expected, getParams(schema, 'function12'))
55 def testArrayOfCallbacks(self):
56 schema = idl_schema.Load('test/idl_callback_arrays.idl')[0]
57 expected = [{'type':'array', 'name':'callbacks',
58 'items':{'type':'function', 'name':'MyCallback',
59 'parameters':[{'type':'integer', 'name':'x'}]}}]
60 self.assertEquals(expected, getParams(schema, 'whatever'))
62 def testLegalValues(self):
64 'x': {'name': 'x', 'type': 'integer', 'enum': [1,2],
65 'description': 'This comment tests "double-quotes".'},
66 'y': {'name': 'y', 'type': 'string'},
67 'z': {'name': 'z', 'type': 'string'},
68 'a': {'name': 'a', 'type': 'string'},
69 'b': {'name': 'b', 'type': 'string'},
70 'c': {'name': 'c', 'type': 'string'}},
71 getType(self.idl_basics, 'MyType1')['properties'])
73 def testMemberOrdering(self):
75 ['x', 'y', 'z', 'a', 'b', 'c'],
76 getType(self.idl_basics, 'MyType1')['properties'].keys())
79 schema = self.idl_basics
80 expected = {'enum': [{'name': 'name1', 'description': 'comment1'},
82 'description': 'Enum description',
83 'type': 'string', 'id': 'EnumType'}
84 self.assertEquals(expected, getType(schema, expected['id']))
86 expected = [{'name':'type', '$ref':'EnumType'},
87 {'type':'function', 'name':'cb',
88 'parameters':[{'name':'type', '$ref':'EnumType'}]}]
89 self.assertEquals(expected, getParams(schema, 'function13'))
91 expected = [{'items': {'$ref': 'EnumType'}, 'name': 'types',
93 self.assertEquals(expected, getParams(schema, 'function14'))
95 def testNoCompile(self):
96 schema = self.idl_basics
97 func = getFunction(schema, 'function15')
98 self.assertTrue(func is not None)
99 self.assertTrue(func['nocompile'])
101 def testNoDocOnEnum(self):
102 schema = self.idl_basics
103 enum_with_nodoc = getType(schema, 'EnumTypeWithNoDoc')
104 self.assertTrue(enum_with_nodoc is not None)
105 self.assertTrue(enum_with_nodoc['nodoc'])
107 def testInternalNamespace(self):
108 idl_basics = self.idl_basics
109 self.assertEquals('idl_basics', idl_basics['namespace'])
110 self.assertTrue(idl_basics['internal'])
111 self.assertFalse(idl_basics['nodoc'])
113 def testChromeOSPlatformsNamespace(self):
114 schema = idl_schema.Load('test/idl_namespace_chromeos.idl')[0]
115 self.assertEquals('idl_namespace_chromeos', schema['namespace'])
116 expected = ['chromeos']
117 self.assertEquals(expected, schema['platforms'])
119 def testAllPlatformsNamespace(self):
120 schema = idl_schema.Load('test/idl_namespace_all_platforms.idl')[0]
121 self.assertEquals('idl_namespace_all_platforms', schema['namespace'])
122 expected = ['chromeos', 'chromeos_touch', 'linux', 'mac', 'win']
123 self.assertEquals(expected, schema['platforms'])
125 def testNonSpecificPlatformsNamespace(self):
126 schema = idl_schema.Load('test/idl_namespace_non_specific_platforms.idl')[0]
127 self.assertEquals('idl_namespace_non_specific_platforms',
130 self.assertEquals(expected, schema['platforms'])
132 def testCallbackComment(self):
133 schema = self.idl_basics
134 self.assertEquals('A comment on a callback.',
135 getParams(schema, 'function16')[0]['description'])
138 getParams(schema, 'function16')[0]['parameters'][0]['description'])
140 'Just a parameter comment, with no comment on the callback.',
141 getParams(schema, 'function17')[0]['parameters'][0]['description'])
143 'Override callback comment.',
144 getParams(schema, 'function18')[0]['description'])
146 def testFunctionComment(self):
147 schema = self.idl_basics
148 func = getFunction(schema, 'function3')
149 self.assertEquals(('This comment should appear in the documentation, '
150 'despite occupying multiple lines.'),
153 [{'description': ('So should this comment about the argument. '
154 '<em>HTML</em> is fine too.'),
158 func = getFunction(schema, 'function4')
159 self.assertEquals(('This tests if "double-quotes" are escaped correctly.'
160 '<br/><br/> It also tests a comment with two newlines.'),
163 def testReservedWords(self):
164 schema = idl_schema.Load('test/idl_reserved_words.idl')[0]
166 foo_type = getType(schema, 'Foo')
167 self.assertEquals([{'name': 'float'}, {'name': 'DOMString'}],
170 enum_type = getType(schema, 'enum')
171 self.assertEquals([{'name': 'callback'}, {'name': 'namespace'}],
174 dictionary = getType(schema, 'dictionary')
175 self.assertEquals('integer', dictionary['properties']['long']['type'])
177 mytype = getType(schema, 'MyType')
178 self.assertEquals('string', mytype['properties']['interface']['type'])
180 params = getParams(schema, 'static')
181 self.assertEquals('Foo', params[0]['$ref'])
182 self.assertEquals('enum', params[1]['$ref'])
185 if __name__ == '__main__':