Home | History | Annotate | Download | only in json_to_struct
      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 from struct_generator import GenerateField
      7 from struct_generator import GenerateStruct
      8 import unittest
      9 
     10 class StructGeneratorTest(unittest.TestCase):
     11   def testGenerateIntField(self):
     12     self.assertEquals('const int foo_bar',
     13         GenerateField({'type': 'int', 'field': 'foo_bar'}))
     14 
     15   def testGenerateStringField(self):
     16     self.assertEquals('const char* const bar_foo',
     17         GenerateField({'type': 'string', 'field': 'bar_foo'}))
     18 
     19   def testGenerateString16Field(self):
     20     self.assertEquals('const wchar_t* const foo_bar',
     21         GenerateField({'type': 'string16', 'field': 'foo_bar'}))
     22 
     23   def testGenerateEnumField(self):
     24     self.assertEquals('const MyEnumType foo_foo',
     25         GenerateField({'type': 'enum',
     26                        'field': 'foo_foo',
     27                        'ctype': 'MyEnumType'}))
     28 
     29   def testGenerateArrayField(self):
     30     self.assertEquals('const int * bar_bar;\n'
     31                       '  const size_t bar_bar_size',
     32         GenerateField({'type': 'array',
     33                        'field': 'bar_bar',
     34                        'contents': {'type': 'int'}}))
     35 
     36   def testGenerateStruct(self):
     37     schema = [
     38       {'type': 'int', 'field': 'foo_bar'},
     39       {'type': 'string', 'field': 'bar_foo', 'default': 'dummy'},
     40       {
     41         'type': 'array',
     42         'field': 'bar_bar',
     43         'contents': {
     44           'type': 'enum',
     45           'ctype': 'MyEnumType'
     46         }
     47       }
     48     ]
     49     struct = ('struct MyTypeName {\n'
     50         '  const int foo_bar;\n'
     51         '  const char* const bar_foo;\n'
     52         '  const MyEnumType * bar_bar;\n'
     53         '  const size_t bar_bar_size;\n'
     54         '};\n')
     55     self.assertEquals(struct, GenerateStruct('MyTypeName', schema))
     56 
     57 if __name__ == '__main__':
     58   unittest.main()
     59