Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(8)

Side by Side Diff: third_party/WebKit/Source/bindings/scripts/code_generator_web_agent_api_test.py

Issue 2703653004: [WebAgentsAPI]: Start adding support for method arguments. (Closed)
Patch Set: Rebased. Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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
76 class InterfaceContextBuilderTest(unittest.TestCase): 202 class InterfaceContextBuilderTest(unittest.TestCase):
77 203
78 def test_empty(self): 204 def test_empty(self):
79 builder = InterfaceContextBuilder('test', TypeResolver({})) 205 builder = InterfaceContextBuilder('test', TypeResolver({}))
80 206
81 self.assertEqual({'code_generator': 'test'}, builder.build()) 207 self.assertEqual({'code_generator': 'test'}, builder.build())
82 208
83 def test_set_name(self): 209 def test_set_name(self):
84 helper = IdlTestingHelper() 210 helper = IdlTestingHelper()
85 builder = InterfaceContextBuilder( 211 builder = InterfaceContextBuilder(
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
124 'test', TypeResolver(helper.make_stub_interfaces_info({ 250 'test', TypeResolver(helper.make_stub_interfaces_info({
125 'foo': 'path_to_foo', 251 'foo': 'path_to_foo',
126 'bar': 'path_to_bar' 252 'bar': 'path_to_bar'
127 }))) 253 })))
128 254
129 attribute = helper.make_stub_idl_attribute('foo', 'bar') 255 attribute = helper.make_stub_idl_attribute('foo', 'bar')
130 builder.add_attribute(attribute) 256 builder.add_attribute(attribute)
131 self.assertEqual({ 257 self.assertEqual({
132 'code_generator': 'test', 258 'code_generator': 'test',
133 'cpp_includes': set(['path_to_bar']), 259 'cpp_includes': set(['path_to_bar']),
134 'attributes': [{'name': 'foo', 'return_type': 'bar'}], 260 'attributes': [{'name': 'foo', 'type': 'bar'}],
135 }, builder.build()) 261 }, builder.build())
136 262
137 def test_add_method(self): 263 def test_add_method(self):
138 helper = IdlTestingHelper() 264 helper = IdlTestingHelper()
139 builder = InterfaceContextBuilder( 265 builder = InterfaceContextBuilder(
140 'test', TypeResolver(helper.make_stub_interfaces_info({ 266 'test', TypeResolver(helper.make_stub_interfaces_info({
141 'foo': 'path_to_foo', 267 'foo': 'path_to_foo',
142 'bar': 'path_to_bar' 268 'bar': 'path_to_bar',
269 'garply': 'path_to_garply',
143 }))) 270 })))
144 271
145 operation = helper.make_stub_idl_operation('foo', 'bar') 272 operation = helper.make_stub_idl_operation('foo', 'bar')
146 builder.add_operation(operation) 273 builder.add_operation(operation)
147 self.assertEqual({ 274 self.assertEqual({
148 'code_generator': 'test', 275 'code_generator': 'test',
149 'cpp_includes': set(['path_to_bar']), 276 'cpp_includes': set(['path_to_bar']),
150 'methods': [{'name': 'foo', 'return_type': 'bar'}], 277 'methods': [{
278 'arguments': [],
279 'name': 'Foo',
280 'type': 'bar'
281 }],
151 }, builder.build()) 282 }, builder.build())
283
284 operation = helper.make_stub_idl_operation('quux', 'garply')
285 operation.arguments = [
286 helper.make_stub_idl_argument('barBaz', IdlType('qux'))
287 ]
288 builder.add_operation(operation)
289 self.assertEqual({
290 'code_generator': 'test',
291 'cpp_includes': set(['path_to_bar', 'path_to_garply']),
292 'methods': [
293 {
294 'arguments': [],
295 'name': 'Foo',
296 'type': 'bar'
297 },
298 {
299 'arguments': [
300 {'name': 'bar_baz', 'type': 'qux'}
301 ],
302 'name': 'Quux',
303 'type': 'garply'
304 }
305 ],
306 }, builder.build())
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698