Home | History | Annotate | Download | only in json_schema_compiler
      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 def getParams(schema, name):
     16   function = getFunction(schema, name)
     17   return function['parameters']
     18 
     19 def getType(schema, id):
     20   for item in schema['types']:
     21     if item['id'] == id:
     22       return item
     23 
     24 class IdlSchemaTest(unittest.TestCase):
     25   def setUp(self):
     26     loaded = idl_schema.Load('test/idl_basics.idl')
     27     self.assertEquals(1, len(loaded))
     28     self.assertEquals('idl_basics', loaded[0]['namespace'])
     29     self.idl_basics = loaded[0]
     30 
     31   def testSimpleCallbacks(self):
     32     schema = self.idl_basics
     33     expected = [{'type':'function', 'name':'cb', 'parameters':[]}]
     34     self.assertEquals(expected, getParams(schema, 'function4'))
     35 
     36     expected = [{'type':'function', 'name':'cb',
     37                  'parameters':[{'name':'x', 'type':'integer'}]}]
     38     self.assertEquals(expected, getParams(schema, 'function5'))
     39 
     40     expected = [{'type':'function', 'name':'cb',
     41                  'parameters':[{'name':'arg', '$ref':'MyType1'}]}]
     42     self.assertEquals(expected, getParams(schema, 'function6'))
     43 
     44   def testCallbackWithArrayArgument(self):
     45     schema = self.idl_basics
     46     expected = [{'type':'function', 'name':'cb',
     47                  'parameters':[{'name':'arg', 'type':'array',
     48                                 'items':{'$ref':'MyType2'}}]}]
     49     self.assertEquals(expected, getParams(schema, 'function12'))
     50 
     51 
     52   def testArrayOfCallbacks(self):
     53     schema = idl_schema.Load('test/idl_callback_arrays.idl')[0]
     54     expected = [{'type':'array', 'name':'callbacks',
     55                  'items':{'type':'function', 'name':'MyCallback',
     56                           'parameters':[{'type':'integer', 'name':'x'}]}}]
     57     self.assertEquals(expected, getParams(schema, 'whatever'))
     58 
     59   def testLegalValues(self):
     60     self.assertEquals({
     61         'x': {'name': 'x', 'type': 'integer', 'enum': [1,2],
     62               'description': 'This comment tests "double-quotes".'},
     63         'y': {'name': 'y', 'type': 'string'},
     64         'z': {'name': 'z', 'type': 'string'},
     65         'a': {'name': 'a', 'type': 'string'},
     66         'b': {'name': 'b', 'type': 'string'},
     67         'c': {'name': 'c', 'type': 'string'}},
     68       getType(self.idl_basics, 'MyType1')['properties'])
     69 
     70   def testMemberOrdering(self):
     71     self.assertEquals(
     72         ['x', 'y', 'z', 'a', 'b', 'c'],
     73         getType(self.idl_basics, 'MyType1')['properties'].keys())
     74 
     75   def testEnum(self):
     76     schema = self.idl_basics
     77     expected = {'enum': ['name1', 'name2'], 'description': 'Enum description',
     78                 'type': 'string', 'id': 'EnumType'}
     79     self.assertEquals(expected, getType(schema, expected['id']))
     80 
     81     expected = [{'name':'type', '$ref':'EnumType'},
     82                 {'type':'function', 'name':'cb',
     83                   'parameters':[{'name':'type', '$ref':'EnumType'}]}]
     84     self.assertEquals(expected, getParams(schema, 'function13'))
     85 
     86     expected = [{'items': {'$ref': 'EnumType'}, 'name': 'types',
     87                  'type': 'array'}]
     88     self.assertEquals(expected, getParams(schema, 'function14'))
     89 
     90   def testNoCompile(self):
     91     schema = self.idl_basics
     92     func = getFunction(schema, 'function15')
     93     self.assertTrue(func is not None)
     94     self.assertTrue(func['nocompile'])
     95 
     96   def testNoDocOnEnum(self):
     97     schema = self.idl_basics
     98     enum_with_nodoc = getType(schema, 'EnumTypeWithNoDoc')
     99     self.assertTrue(enum_with_nodoc is not None)
    100     self.assertTrue(enum_with_nodoc['nodoc'])
    101 
    102   def testInternalNamespace(self):
    103     idl_basics  = self.idl_basics
    104     self.assertEquals('idl_basics', idl_basics['namespace'])
    105     self.assertTrue(idl_basics['internal'])
    106     self.assertFalse(idl_basics['nodoc'])
    107 
    108   def testCallbackComment(self):
    109     schema = self.idl_basics
    110     self.assertEquals('A comment on a callback.',
    111                       getParams(schema, 'function16')[0]['description'])
    112     self.assertEquals(
    113         'A parameter.',
    114         getParams(schema, 'function16')[0]['parameters'][0]['description'])
    115     self.assertEquals(
    116         'Just a parameter comment, with no comment on the callback.',
    117         getParams(schema, 'function17')[0]['parameters'][0]['description'])
    118     self.assertEquals(
    119         'Override callback comment.',
    120         getParams(schema, 'function18')[0]['description'])
    121 
    122   def testFunctionComment(self):
    123     schema = self.idl_basics
    124     func = getFunction(schema, 'function3')
    125     self.assertEquals(('This comment should appear in the documentation, '
    126                        'despite occupying multiple lines.'),
    127                       func['description'])
    128     self.assertEquals(
    129         [{'description': ('So should this comment about the argument. '
    130                           '<em>HTML</em> is fine too.'),
    131           'name': 'arg',
    132           '$ref': 'MyType1'}],
    133         func['parameters'])
    134     func = getFunction(schema, 'function4')
    135     self.assertEquals(('This tests if "double-quotes" are escaped correctly.'
    136                        '<br/><br/> It also tests a comment with two newlines.'),
    137                       func['description'])
    138 
    139   def testReservedWords(self):
    140     schema = idl_schema.Load('test/idl_reserved_words.idl')[0]
    141 
    142     foo_type = getType(schema, 'Foo')
    143     self.assertEquals(['float', 'DOMString'], foo_type['enum'])
    144 
    145     enum_type = getType(schema, 'enum')
    146     self.assertEquals(['callback', 'namespace'], enum_type['enum'])
    147 
    148     dictionary = getType(schema, 'dictionary');
    149     self.assertEquals('integer', dictionary['properties']['long']['type'])
    150 
    151     mytype = getType(schema, 'MyType')
    152     self.assertEquals('string', mytype['properties']['interface']['type'])
    153 
    154     params = getParams(schema, 'static')
    155     self.assertEquals('Foo', params[0]['$ref'])
    156     self.assertEquals('enum', params[1]['$ref'])
    157 
    158 if __name__ == '__main__':
    159   unittest.main()
    160