| OLD | NEW |
| (Empty) |
| 1 # Copyright 2016 The Chromium Authors. All rights reserved. | |
| 2 # Use of this source code is governed by a BSD-style license that can be | |
| 3 # found in the LICENSE file. | |
| 4 | |
| 5 # pylint: disable=import-error,print-statement,relative-import,protected-access | |
| 6 | |
| 7 """Unit tests for code_generator_web_module.py.""" | |
| 8 | |
| 9 import unittest | |
| 10 | |
| 11 from code_generator_web_module import InterfaceContextBuilder | |
| 12 from code_generator_web_module import STRING_INCLUDE_PATH | |
| 13 from code_generator_web_module import TypeResolver | |
| 14 from idl_definitions import IdlAttribute | |
| 15 from idl_definitions import IdlOperation | |
| 16 from idl_types import IdlType | |
| 17 from idl_types import PRIMITIVE_TYPES | |
| 18 from idl_types import STRING_TYPES | |
| 19 | |
| 20 | |
| 21 # TODO(dglazkov): Convert to use actual objects, not stubs. | |
| 22 # See http://crbug.com/673214 for more details. | |
| 23 class IdlTestingHelper(object): | |
| 24 """A collection of stub makers and helper utils to make testing code | |
| 25 generation easy.""" | |
| 26 | |
| 27 def make_stub_idl_attribute(self, name, return_type): | |
| 28 idl_attribute_stub = IdlAttribute() | |
| 29 idl_attribute_stub.name = name | |
| 30 idl_attribute_stub.idl_type = IdlType(return_type) | |
| 31 return idl_attribute_stub | |
| 32 | |
| 33 def make_stub_idl_operation(self, name, return_type): | |
| 34 idl_operation_stub = IdlOperation() | |
| 35 idl_operation_stub.name = name | |
| 36 idl_operation_stub.idl_type = IdlType(return_type) | |
| 37 return idl_operation_stub | |
| 38 | |
| 39 def make_stub_idl_type(self, base_type): | |
| 40 return IdlType(base_type) | |
| 41 | |
| 42 def make_stub_interfaces_info(self, classes_to_paths): | |
| 43 result = {} | |
| 44 for class_name, path in classes_to_paths.iteritems(): | |
| 45 result[class_name] = {'include_path': path} | |
| 46 return result | |
| 47 | |
| 48 class TypeResolverTest(unittest.TestCase): | |
| 49 | |
| 50 def test_includes_from_type_should_filter_primitive_types(self): | |
| 51 helper = IdlTestingHelper() | |
| 52 type_resolver = TypeResolver({}) | |
| 53 for primitive_type in PRIMITIVE_TYPES: | |
| 54 idl_type = helper.make_stub_idl_type(primitive_type) | |
| 55 self.assertEqual( | |
| 56 type_resolver._includes_from_type(idl_type), set()) | |
| 57 | |
| 58 def test_includes_from_type_should_filter_void(self): | |
| 59 type_resolver = TypeResolver({}) | |
| 60 helper = IdlTestingHelper() | |
| 61 idl_type = helper.make_stub_idl_type('void') | |
| 62 self.assertEqual( | |
| 63 type_resolver._includes_from_type(idl_type), set()) | |
| 64 | |
| 65 def test_includes_from_type_should_handle_string(self): | |
| 66 type_resolver = TypeResolver({}) | |
| 67 helper = IdlTestingHelper() | |
| 68 for string_type in STRING_TYPES: | |
| 69 idl_type = helper.make_stub_idl_type(string_type) | |
| 70 self.assertEqual( | |
| 71 type_resolver._includes_from_type(idl_type), | |
| 72 set([STRING_INCLUDE_PATH])) | |
| 73 | |
| 74 | |
| 75 class InterfaceContextBuilderTest(unittest.TestCase): | |
| 76 | |
| 77 def test_empty(self): | |
| 78 builder = InterfaceContextBuilder('test', TypeResolver({})) | |
| 79 | |
| 80 self.assertEqual({'code_generator': 'test'}, builder.build()) | |
| 81 | |
| 82 def test_set_name(self): | |
| 83 helper = IdlTestingHelper() | |
| 84 builder = InterfaceContextBuilder( | |
| 85 'test', TypeResolver(helper.make_stub_interfaces_info({ | |
| 86 'foo': 'path_to_foo', | |
| 87 }))) | |
| 88 | |
| 89 builder.set_class_name('foo') | |
| 90 self.assertEqual({ | |
| 91 'code_generator': 'test', | |
| 92 'cpp_includes': set(['path_to_foo']), | |
| 93 'class_name': { | |
| 94 'snake_case': 'foo', | |
| 95 'macro_case': 'FOO', | |
| 96 'upper_camel_case': 'Foo' | |
| 97 }, | |
| 98 }, builder.build()) | |
| 99 | |
| 100 def test_set_inheritance(self): | |
| 101 helper = IdlTestingHelper() | |
| 102 builder = InterfaceContextBuilder( | |
| 103 'test', TypeResolver(helper.make_stub_interfaces_info({ | |
| 104 'foo': 'path_to_foo', | |
| 105 }))) | |
| 106 builder.set_inheritance('foo') | |
| 107 self.assertEqual({ | |
| 108 'base_class': 'foo', | |
| 109 'code_generator': 'test', | |
| 110 'header_includes': set(['path_to_foo']), | |
| 111 }, builder.build()) | |
| 112 | |
| 113 builder = InterfaceContextBuilder('test', TypeResolver({})) | |
| 114 builder.set_inheritance(None) | |
| 115 self.assertEqual({ | |
| 116 'code_generator': 'test', | |
| 117 'header_includes': set(['platform/heap/Handle.h']), | |
| 118 }, builder.build()) | |
| 119 | |
| 120 | |
| 121 def test_add_attribute(self): | |
| 122 helper = IdlTestingHelper() | |
| 123 builder = InterfaceContextBuilder( | |
| 124 'test', TypeResolver(helper.make_stub_interfaces_info({ | |
| 125 'foo': 'path_to_foo', | |
| 126 'bar': 'path_to_bar' | |
| 127 }))) | |
| 128 | |
| 129 attribute = helper.make_stub_idl_attribute('foo', 'bar') | |
| 130 builder.add_attribute(attribute) | |
| 131 self.assertEqual({ | |
| 132 'code_generator': 'test', | |
| 133 'cpp_includes': set(['path_to_bar']), | |
| 134 'attributes': [{'name': 'foo', 'return_type': 'bar'}], | |
| 135 }, builder.build()) | |
| 136 | |
| 137 def test_add_method(self): | |
| 138 helper = IdlTestingHelper() | |
| 139 builder = InterfaceContextBuilder( | |
| 140 'test', TypeResolver(helper.make_stub_interfaces_info({ | |
| 141 'foo': 'path_to_foo', | |
| 142 'bar': 'path_to_bar' | |
| 143 }))) | |
| 144 | |
| 145 operation = helper.make_stub_idl_operation('foo', 'bar') | |
| 146 builder.add_operation(operation) | |
| 147 self.assertEqual({ | |
| 148 'code_generator': 'test', | |
| 149 'cpp_includes': set(['path_to_bar']), | |
| 150 'methods': [{'name': 'foo', 'return_type': 'bar'}], | |
| 151 }, builder.build()) | |
| OLD | NEW |