struct_generator_test.py 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #!/usr/bin/env python3
  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. from struct_generator import GenerateField
  6. from struct_generator import GenerateStruct
  7. import unittest
  8. class StructGeneratorTest(unittest.TestCase):
  9. def testGenerateIntField(self):
  10. self.assertEquals('const int foo_bar',
  11. GenerateField({'type': 'int', 'field': 'foo_bar'}))
  12. def testGenerateStringField(self):
  13. self.assertEquals('const char* const bar_foo',
  14. GenerateField({'type': 'string', 'field': 'bar_foo'}))
  15. def testGenerateString16Field(self):
  16. self.assertEquals('const wchar_t* const foo_bar',
  17. GenerateField({'type': 'string16', 'field': 'foo_bar'}))
  18. def testGenerateEnumField(self):
  19. self.assertEquals('const MyEnumType foo_foo',
  20. GenerateField({'type': 'enum',
  21. 'field': 'foo_foo',
  22. 'ctype': 'MyEnumType'}))
  23. def testGenerateArrayField(self):
  24. self.assertEquals('const int * bar_bar;\n'
  25. ' const size_t bar_bar_size',
  26. GenerateField({'type': 'array',
  27. 'field': 'bar_bar',
  28. 'contents': {'type': 'int'}}))
  29. def testGenerateClassField(self):
  30. self.assertEquals(
  31. 'const absl::optional<bool> bar',
  32. GenerateField({
  33. 'type': 'class',
  34. 'field': 'bar',
  35. 'ctype': 'absl::optional<bool>'
  36. }))
  37. def testGenerateStruct(self):
  38. schema = [
  39. {'type': 'int', 'field': 'foo_bar'},
  40. {'type': 'string', 'field': 'bar_foo', 'default': 'dummy'},
  41. {
  42. 'type': 'array',
  43. 'field': 'bar_bar',
  44. 'contents': {
  45. 'type': 'enum',
  46. 'ctype': 'MyEnumType'
  47. }
  48. }
  49. ]
  50. struct = ('struct MyTypeName {\n'
  51. ' const int foo_bar;\n'
  52. ' const char* const bar_foo;\n'
  53. ' const MyEnumType * bar_bar;\n'
  54. ' const size_t bar_bar_size;\n'
  55. '};\n')
  56. self.assertEquals(struct, GenerateStruct('MyTypeName', schema))
  57. def testGenerateArrayOfStruct(self):
  58. schema = [
  59. {
  60. 'type': 'array',
  61. 'field': 'bar_bar',
  62. 'contents': {
  63. 'type': 'struct',
  64. 'type_name': 'InnerTypeName',
  65. 'fields': [
  66. {'type': 'string', 'field': 'key'},
  67. {'type': 'string', 'field': 'value'},
  68. ]
  69. }
  70. }
  71. ]
  72. struct = (
  73. 'struct InnerTypeName {\n'
  74. ' const char* const key;\n'
  75. ' const char* const value;\n'
  76. '};\n'
  77. '\n'
  78. 'struct MyTypeName {\n'
  79. ' const InnerTypeName * bar_bar;\n'
  80. ' const size_t bar_bar_size;\n'
  81. '};\n')
  82. self.assertEquals(struct, GenerateStruct('MyTypeName', schema))
  83. if __name__ == '__main__':
  84. unittest.main()