OLD | NEW |
1 # Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 # pylint: disable=import-error,print-statement,relative-import,protected-access | 5 # pylint: disable=import-error,print-statement,relative-import,protected-access |
6 | 6 |
7 """Unit tests for code_generator_web_agent_api.py.""" | 7 """Unit tests for code_generator_web_agent_api.py.""" |
8 | 8 |
9 import unittest | 9 import unittest |
10 | 10 |
11 from code_generator_web_agent_api import InterfaceContextBuilder | 11 from code_generator_web_agent_api import InterfaceContextBuilder |
| 12 from code_generator_web_agent_api import MethodOverloadSplitter |
12 from code_generator_web_agent_api import STRING_INCLUDE_PATH | 13 from code_generator_web_agent_api import STRING_INCLUDE_PATH |
13 from code_generator_web_agent_api import TypeResolver | 14 from code_generator_web_agent_api import TypeResolver |
| 15 from idl_definitions import IdlArgument |
14 from idl_definitions import IdlAttribute | 16 from idl_definitions import IdlAttribute |
15 from idl_definitions import IdlOperation | 17 from idl_definitions import IdlOperation |
16 from idl_types import IdlType | 18 from idl_types import IdlType |
| 19 from idl_types import IdlNullableType |
| 20 from idl_types import IdlUnionType |
17 from idl_types import PRIMITIVE_TYPES | 21 from idl_types import PRIMITIVE_TYPES |
18 from idl_types import STRING_TYPES | 22 from idl_types import STRING_TYPES |
19 | 23 |
20 | 24 |
21 # TODO(dglazkov): Convert to use actual objects, not stubs. | 25 # TODO(dglazkov): Convert to use actual objects, not stubs. |
22 # See http://crbug.com/673214 for more details. | 26 # See http://crbug.com/673214 for more details. |
23 class IdlTestingHelper(object): | 27 class IdlTestingHelper(object): |
24 """A collection of stub makers and helper utils to make testing code | 28 """A collection of stub makers and helper utils to make testing code |
25 generation easy.""" | 29 generation easy.""" |
26 | 30 |
| 31 def make_stub_idl_argument(self, name, idl_type, is_optional=False): |
| 32 idl_argument = IdlArgument() |
| 33 idl_argument.name = name |
| 34 idl_argument.idl_type = idl_type |
| 35 idl_argument.is_optional = is_optional |
| 36 return idl_argument |
| 37 |
27 def make_stub_idl_attribute(self, name, return_type): | 38 def make_stub_idl_attribute(self, name, return_type): |
28 idl_attribute_stub = IdlAttribute() | 39 idl_attribute_stub = IdlAttribute() |
29 idl_attribute_stub.name = name | 40 idl_attribute_stub.name = name |
30 idl_attribute_stub.idl_type = IdlType(return_type) | 41 idl_attribute_stub.idl_type = IdlType(return_type) |
31 return idl_attribute_stub | 42 return idl_attribute_stub |
32 | 43 |
33 def make_stub_idl_operation(self, name, return_type): | 44 def make_stub_idl_operation(self, name, return_type): |
34 idl_operation_stub = IdlOperation() | 45 idl_operation_stub = IdlOperation() |
35 idl_operation_stub.name = name | 46 idl_operation_stub.name = name |
36 idl_operation_stub.idl_type = IdlType(return_type) | 47 idl_operation_stub.idl_type = IdlType(return_type) |
(...skipping 29 matching lines...) Expand all Loading... |
66 def test_includes_from_type_should_handle_string(self): | 77 def test_includes_from_type_should_handle_string(self): |
67 type_resolver = TypeResolver({}) | 78 type_resolver = TypeResolver({}) |
68 helper = IdlTestingHelper() | 79 helper = IdlTestingHelper() |
69 for string_type in STRING_TYPES: | 80 for string_type in STRING_TYPES: |
70 idl_type = helper.make_stub_idl_type(string_type) | 81 idl_type = helper.make_stub_idl_type(string_type) |
71 self.assertEqual( | 82 self.assertEqual( |
72 type_resolver._includes_from_type(idl_type), | 83 type_resolver._includes_from_type(idl_type), |
73 set([STRING_INCLUDE_PATH])) | 84 set([STRING_INCLUDE_PATH])) |
74 | 85 |
75 | 86 |
| 87 class MethodOverloadSplitterTest(unittest.TestCase): |
| 88 |
| 89 def test_enumerate_argument_types(self): |
| 90 splitter = MethodOverloadSplitter(IdlOperation()) |
| 91 nullable_and_primitive = IdlArgument() |
| 92 nullable_and_primitive.idl_type = IdlNullableType(IdlType('double')) |
| 93 with self.assertRaises(ValueError): |
| 94 splitter._enumerate_argument_types(nullable_and_primitive) |
| 95 |
| 96 argument = IdlArgument() |
| 97 foo_type = IdlType('Foo') |
| 98 bar_type = IdlType('Bar') |
| 99 |
| 100 argument.idl_type = foo_type |
| 101 self.assertEqual( |
| 102 splitter._enumerate_argument_types(argument), [foo_type]) |
| 103 |
| 104 argument.is_optional = True |
| 105 self.assertEqual( |
| 106 splitter._enumerate_argument_types(argument), [None, foo_type]) |
| 107 |
| 108 argument.is_optional = False |
| 109 argument.idl_type = IdlUnionType([foo_type, bar_type]) |
| 110 self.assertEqual( |
| 111 splitter._enumerate_argument_types(argument), [foo_type, bar_type]) |
| 112 |
| 113 argument.is_optional = True |
| 114 self.assertEqual( |
| 115 splitter._enumerate_argument_types(argument), |
| 116 [None, foo_type, bar_type]) |
| 117 |
| 118 def test_update_argument_lists(self): |
| 119 splitter = MethodOverloadSplitter(IdlOperation()) |
| 120 foo_type = IdlType('Foo') |
| 121 bar_type = IdlType('Bar') |
| 122 baz_type = IdlType('Baz') |
| 123 qux_type = IdlType('Qux') |
| 124 |
| 125 result = splitter._update_argument_lists([[]], [foo_type]) |
| 126 self.assertEqual(result, [[foo_type]]) |
| 127 |
| 128 result = splitter._update_argument_lists([[]], [foo_type, bar_type]) |
| 129 self.assertEqual(result, [[foo_type], [bar_type]]) |
| 130 |
| 131 existing_list = [[foo_type]] |
| 132 result = splitter._update_argument_lists(existing_list, [bar_type]) |
| 133 self.assertEqual(result, [[foo_type, bar_type]]) |
| 134 |
| 135 existing_list = [[foo_type]] |
| 136 result = splitter._update_argument_lists(existing_list, |
| 137 [None, bar_type]) |
| 138 self.assertEqual(result, [[foo_type], [foo_type, bar_type]]) |
| 139 |
| 140 existing_list = [[foo_type]] |
| 141 result = splitter._update_argument_lists(existing_list, |
| 142 [bar_type, baz_type]) |
| 143 self.assertEqual(result, [[foo_type, bar_type], [foo_type, baz_type]]) |
| 144 |
| 145 existing_list = [[foo_type], [qux_type]] |
| 146 result = splitter._update_argument_lists(existing_list, |
| 147 [bar_type, baz_type]) |
| 148 self.assertEqual(result, [ |
| 149 [foo_type, bar_type], |
| 150 [foo_type, baz_type], |
| 151 [qux_type, bar_type], |
| 152 [qux_type, baz_type], |
| 153 ]) |
| 154 |
| 155 existing_list = [[foo_type], [qux_type]] |
| 156 result = splitter._update_argument_lists(existing_list, |
| 157 [None, bar_type, baz_type]) |
| 158 self.assertEqual(result, [ |
| 159 [foo_type], |
| 160 [foo_type, bar_type], |
| 161 [foo_type, baz_type], |
| 162 [qux_type], |
| 163 [qux_type, bar_type], |
| 164 [qux_type, baz_type], |
| 165 ]) |
| 166 |
| 167 existing_list = [[foo_type, qux_type]] |
| 168 result = splitter._update_argument_lists(existing_list, |
| 169 [bar_type, baz_type]) |
| 170 self.assertEqual(result, [ |
| 171 [foo_type, qux_type, bar_type], |
| 172 [foo_type, qux_type, baz_type], |
| 173 ]) |
| 174 |
| 175 def test_split_into_overloads(self): |
| 176 helper = IdlTestingHelper() |
| 177 type_double = IdlType('double') |
| 178 type_foo = IdlType('foo') |
| 179 type_double_or_foo = IdlUnionType([type_double, type_foo]) |
| 180 idl_operation = IdlOperation() |
| 181 |
| 182 idl_operation.arguments = [] |
| 183 splitter = MethodOverloadSplitter(idl_operation) |
| 184 result = splitter.split_into_overloads() |
| 185 self.assertEqual(result, [[]]) |
| 186 |
| 187 idl_operation.arguments = [ |
| 188 helper.make_stub_idl_argument(None, type_double, True) |
| 189 ] |
| 190 splitter = MethodOverloadSplitter(idl_operation) |
| 191 result = splitter.split_into_overloads() |
| 192 self.assertEqual(result, [[], [type_double]]) |
| 193 |
| 194 idl_operation.arguments = [ |
| 195 helper.make_stub_idl_argument(None, type_double_or_foo) |
| 196 ] |
| 197 splitter = MethodOverloadSplitter(idl_operation) |
| 198 result = splitter.split_into_overloads() |
| 199 self.assertEqual(result, [[type_double], [type_foo]]) |
| 200 |
| 201 def test_split_add_event_listener(self): |
| 202 """Tests how EventTarget.addEventListener is split into respective |
| 203 overloads. From: |
| 204 |
| 205 void addEventListener( |
| 206 DOMString type, |
| 207 EventListener? listener, |
| 208 optional (AddEventListenerOptions or boolean) options) |
| 209 |
| 210 produces: """ |
| 211 |
| 212 helper = IdlTestingHelper() |
| 213 type_dom_string = IdlType('DOMString') |
| 214 type_listener = IdlType('EventListener') |
| 215 type_options = IdlType('AddEventListenerOptions') |
| 216 type_boolean = IdlType('boolean') |
| 217 type_union = IdlUnionType([type_options, type_boolean]) |
| 218 idl_operation = IdlOperation() |
| 219 idl_operation.arguments = [ |
| 220 helper.make_stub_idl_argument('type', type_dom_string), |
| 221 helper.make_stub_idl_argument('listener', type_listener), |
| 222 helper.make_stub_idl_argument('options', type_union, |
| 223 is_optional=True)] |
| 224 splitter = MethodOverloadSplitter(idl_operation) |
| 225 result = splitter.split_into_overloads() |
| 226 self.assertEqual( |
| 227 result, |
| 228 [[type_dom_string, type_listener], |
| 229 [type_dom_string, type_listener, type_options], |
| 230 [type_dom_string, type_listener, type_boolean]]) |
| 231 |
| 232 |
76 class InterfaceContextBuilderTest(unittest.TestCase): | 233 class InterfaceContextBuilderTest(unittest.TestCase): |
77 | 234 |
78 def test_empty(self): | 235 def test_empty(self): |
79 builder = InterfaceContextBuilder('test', TypeResolver({})) | 236 builder = InterfaceContextBuilder('test', TypeResolver({})) |
80 | 237 |
81 self.assertEqual({'code_generator': 'test'}, builder.build()) | 238 self.assertEqual({'code_generator': 'test'}, builder.build()) |
82 | 239 |
83 def test_set_name(self): | 240 def test_set_name(self): |
84 helper = IdlTestingHelper() | 241 helper = IdlTestingHelper() |
85 builder = InterfaceContextBuilder( | 242 builder = InterfaceContextBuilder( |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
124 'test', TypeResolver(helper.make_stub_interfaces_info({ | 281 'test', TypeResolver(helper.make_stub_interfaces_info({ |
125 'foo': 'path_to_foo', | 282 'foo': 'path_to_foo', |
126 'bar': 'path_to_bar' | 283 'bar': 'path_to_bar' |
127 }))) | 284 }))) |
128 | 285 |
129 attribute = helper.make_stub_idl_attribute('foo', 'bar') | 286 attribute = helper.make_stub_idl_attribute('foo', 'bar') |
130 builder.add_attribute(attribute) | 287 builder.add_attribute(attribute) |
131 self.assertEqual({ | 288 self.assertEqual({ |
132 'code_generator': 'test', | 289 'code_generator': 'test', |
133 'cpp_includes': set(['path_to_bar']), | 290 'cpp_includes': set(['path_to_bar']), |
134 'attributes': [{'name': 'foo', 'return_type': 'bar'}], | 291 'attributes': [{'name': 'foo', 'type': 'bar'}], |
135 }, builder.build()) | 292 }, builder.build()) |
136 | 293 |
137 def test_add_method(self): | 294 def test_add_method(self): |
138 helper = IdlTestingHelper() | 295 helper = IdlTestingHelper() |
139 builder = InterfaceContextBuilder( | 296 builder = InterfaceContextBuilder( |
140 'test', TypeResolver(helper.make_stub_interfaces_info({ | 297 'test', TypeResolver(helper.make_stub_interfaces_info({ |
141 'foo': 'path_to_foo', | 298 'foo': 'path_to_foo', |
142 'bar': 'path_to_bar' | 299 'bar': 'path_to_bar', |
| 300 'garply': 'path_to_garply', |
143 }))) | 301 }))) |
144 | 302 |
145 operation = helper.make_stub_idl_operation('foo', 'bar') | 303 operation = helper.make_stub_idl_operation('foo', 'bar') |
146 builder.add_operation(operation) | 304 builder.add_operation(operation) |
147 self.assertEqual({ | 305 self.assertEqual({ |
148 'code_generator': 'test', | 306 'code_generator': 'test', |
149 'cpp_includes': set(['path_to_bar']), | 307 'cpp_includes': set(['path_to_bar']), |
150 'methods': [{'name': 'foo', 'return_type': 'bar'}], | 308 'methods': [{ |
| 309 'arguments': [], |
| 310 'name': 'Foo', |
| 311 'type': 'bar' |
| 312 }], |
151 }, builder.build()) | 313 }, builder.build()) |
| 314 |
| 315 operation = helper.make_stub_idl_operation('quux', 'garply') |
| 316 operation.arguments = [ |
| 317 helper.make_stub_idl_argument('barBaz', IdlType('qux')) |
| 318 ] |
| 319 builder.add_operation(operation) |
| 320 self.assertEqual({ |
| 321 'code_generator': 'test', |
| 322 'cpp_includes': set(['path_to_bar', 'path_to_garply']), |
| 323 'methods': [ |
| 324 { |
| 325 'arguments': [], |
| 326 'name': 'Foo', |
| 327 'type': 'bar' |
| 328 }, |
| 329 { |
| 330 'arguments': [ |
| 331 {'name': 'bar_baz', 'type': 'qux'} |
| 332 ], |
| 333 'name': 'Quux', |
| 334 'type': 'garply' |
| 335 } |
| 336 ], |
| 337 }, builder.build()) |
OLD | NEW |