- add sources.
[platform/framework/web/crosswalk.git] / src / tools / json_schema_compiler / idl_schema_test.py
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.
5
6 import idl_schema
7 import unittest
8
9 def getFunction(schema, name):
10   for item in schema['functions']:
11     if item['name'] == name:
12       return item
13   raise KeyError('Missing function %s' % name)
14
15
16 def getParams(schema, name):
17   function = getFunction(schema, name)
18   return function['parameters']
19
20
21 def getType(schema, id):
22   for item in schema['types']:
23     if item['id'] == id:
24       return item
25
26
27 class IdlSchemaTest(unittest.TestCase):
28   def setUp(self):
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]
33
34   def testSimpleCallbacks(self):
35     schema = self.idl_basics
36     expected = [{'type':'function', 'name':'cb', 'parameters':[]}]
37     self.assertEquals(expected, getParams(schema, 'function4'))
38
39     expected = [{'type':'function', 'name':'cb',
40                  'parameters':[{'name':'x', 'type':'integer'}]}]
41     self.assertEquals(expected, getParams(schema, 'function5'))
42
43     expected = [{'type':'function', 'name':'cb',
44                  'parameters':[{'name':'arg', '$ref':'MyType1'}]}]
45     self.assertEquals(expected, getParams(schema, 'function6'))
46
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'))
53
54
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'))
61
62   def testLegalValues(self):
63     self.assertEquals({
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'])
72
73   def testMemberOrdering(self):
74     self.assertEquals(
75         ['x', 'y', 'z', 'a', 'b', 'c'],
76         getType(self.idl_basics, 'MyType1')['properties'].keys())
77
78   def testEnum(self):
79     schema = self.idl_basics
80     expected = {'enum': [{'name': 'name1', 'description': 'comment1'},
81                          {'name': 'name2'}],
82                 'description': 'Enum description',
83                 'type': 'string', 'id': 'EnumType'}
84     self.assertEquals(expected, getType(schema, expected['id']))
85
86     expected = [{'name':'type', '$ref':'EnumType'},
87                 {'type':'function', 'name':'cb',
88                   'parameters':[{'name':'type', '$ref':'EnumType'}]}]
89     self.assertEquals(expected, getParams(schema, 'function13'))
90
91     expected = [{'items': {'$ref': 'EnumType'}, 'name': 'types',
92                  'type': 'array'}]
93     self.assertEquals(expected, getParams(schema, 'function14'))
94
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'])
100
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'])
106
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'])
112
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'])
118
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'])
124
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',
128                       schema['namespace'])
129     expected = None
130     self.assertEquals(expected, schema['platforms'])
131
132   def testCallbackComment(self):
133     schema = self.idl_basics
134     self.assertEquals('A comment on a callback.',
135                       getParams(schema, 'function16')[0]['description'])
136     self.assertEquals(
137         'A parameter.',
138         getParams(schema, 'function16')[0]['parameters'][0]['description'])
139     self.assertEquals(
140         'Just a parameter comment, with no comment on the callback.',
141         getParams(schema, 'function17')[0]['parameters'][0]['description'])
142     self.assertEquals(
143         'Override callback comment.',
144         getParams(schema, 'function18')[0]['description'])
145
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.'),
151                       func['description'])
152     self.assertEquals(
153         [{'description': ('So should this comment about the argument. '
154                           '<em>HTML</em> is fine too.'),
155           'name': 'arg',
156           '$ref': 'MyType1'}],
157         func['parameters'])
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.'),
161                       func['description'])
162
163   def testReservedWords(self):
164     schema = idl_schema.Load('test/idl_reserved_words.idl')[0]
165
166     foo_type = getType(schema, 'Foo')
167     self.assertEquals([{'name': 'float'}, {'name': 'DOMString'}],
168                       foo_type['enum'])
169
170     enum_type = getType(schema, 'enum')
171     self.assertEquals([{'name': 'callback'}, {'name': 'namespace'}],
172                       enum_type['enum'])
173
174     dictionary = getType(schema, 'dictionary')
175     self.assertEquals('integer', dictionary['properties']['long']['type'])
176
177     mytype = getType(schema, 'MyType')
178     self.assertEquals('string', mytype['properties']['interface']['type'])
179
180     params = getParams(schema, 'static')
181     self.assertEquals('Foo', params[0]['$ref'])
182     self.assertEquals('enum', params[1]['$ref'])
183
184
185 if __name__ == '__main__':
186   unittest.main()