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

Unified 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: Comments addressed. 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 side-by-side diff with in-line comments
Download patch
Index: third_party/WebKit/Source/bindings/scripts/code_generator_web_agent_api_test.py
diff --git a/third_party/WebKit/Source/bindings/scripts/code_generator_web_agent_api_test.py b/third_party/WebKit/Source/bindings/scripts/code_generator_web_agent_api_test.py
index 9baed1816761a8dd00144c9db21b816c48fd3874..7f7b96ebe2c8bc5c548cad30602198526a9a8058 100644
--- a/third_party/WebKit/Source/bindings/scripts/code_generator_web_agent_api_test.py
+++ b/third_party/WebKit/Source/bindings/scripts/code_generator_web_agent_api_test.py
@@ -9,11 +9,15 @@
import unittest
from code_generator_web_agent_api import InterfaceContextBuilder
+from code_generator_web_agent_api import MethodOverloadSplitter
from code_generator_web_agent_api import STRING_INCLUDE_PATH
from code_generator_web_agent_api import TypeResolver
+from idl_definitions import IdlArgument
from idl_definitions import IdlAttribute
from idl_definitions import IdlOperation
from idl_types import IdlType
+from idl_types import IdlNullableType
+from idl_types import IdlUnionType
from idl_types import PRIMITIVE_TYPES
from idl_types import STRING_TYPES
@@ -24,6 +28,13 @@ class IdlTestingHelper(object):
"""A collection of stub makers and helper utils to make testing code
generation easy."""
+ def make_stub_idl_argument(self, name, idl_type, is_optional=False):
+ idl_argument = IdlArgument()
+ idl_argument.name = name
+ idl_argument.idl_type = idl_type
+ idl_argument.is_optional = is_optional
+ return idl_argument
+
def make_stub_idl_attribute(self, name, return_type):
idl_attribute_stub = IdlAttribute()
idl_attribute_stub.name = name
@@ -73,6 +84,152 @@ class TypeResolverTest(unittest.TestCase):
set([STRING_INCLUDE_PATH]))
+class MethodOverloadSplitterTest(unittest.TestCase):
+
+ def test_enumerate_argument_types(self):
+ splitter = MethodOverloadSplitter(IdlOperation())
+ nullable_and_primitive = IdlArgument()
+ nullable_and_primitive.idl_type = IdlNullableType(IdlType('double'))
+ with self.assertRaises(ValueError):
+ splitter._enumerate_argument_types(nullable_and_primitive)
+
+ argument = IdlArgument()
+ foo_type = IdlType('Foo')
+ bar_type = IdlType('Bar')
+
+ argument.idl_type = foo_type
+ self.assertEqual(
+ splitter._enumerate_argument_types(argument), [foo_type])
+
+ argument.is_optional = True
+ self.assertEqual(
+ splitter._enumerate_argument_types(argument), [None, foo_type])
+
+ argument.is_optional = False
+ argument.idl_type = IdlUnionType([foo_type, bar_type])
+ self.assertEqual(
+ splitter._enumerate_argument_types(argument), [foo_type, bar_type])
+
+ argument.is_optional = True
+ self.assertEqual(
+ splitter._enumerate_argument_types(argument),
+ [None, foo_type, bar_type])
+
+ def test_update_argument_lists(self):
+ splitter = MethodOverloadSplitter(IdlOperation())
+ foo_type = IdlType('Foo')
+ bar_type = IdlType('Bar')
+ baz_type = IdlType('Baz')
+ qux_type = IdlType('Qux')
+
+ result = splitter._update_argument_lists([[]], [foo_type])
+ self.assertEqual(result, [[foo_type]])
+
+ result = splitter._update_argument_lists([[]], [foo_type, bar_type])
+ self.assertEqual(result, [[foo_type], [bar_type]])
+
+ existing_list = [[foo_type]]
+ result = splitter._update_argument_lists(existing_list, [bar_type])
+ self.assertEqual(result, [[foo_type, bar_type]])
+
+ existing_list = [[foo_type]]
+ result = splitter._update_argument_lists(existing_list,
+ [None, bar_type])
+ self.assertEqual(result, [[foo_type], [foo_type, bar_type]])
+
+ existing_list = [[foo_type]]
+ result = splitter._update_argument_lists(existing_list,
+ [bar_type, baz_type])
+ self.assertEqual(result, [[foo_type, bar_type], [foo_type, baz_type]])
+
+ existing_list = [[foo_type], [qux_type]]
+ result = splitter._update_argument_lists(existing_list,
+ [bar_type, baz_type])
+ self.assertEqual(result, [
+ [foo_type, bar_type],
+ [foo_type, baz_type],
+ [qux_type, bar_type],
+ [qux_type, baz_type],
+ ])
+
+ existing_list = [[foo_type], [qux_type]]
+ result = splitter._update_argument_lists(existing_list,
+ [None, bar_type, baz_type])
+ self.assertEqual(result, [
+ [foo_type],
+ [foo_type, bar_type],
+ [foo_type, baz_type],
+ [qux_type],
+ [qux_type, bar_type],
+ [qux_type, baz_type],
+ ])
+
+ existing_list = [[foo_type, qux_type]]
+ result = splitter._update_argument_lists(existing_list,
+ [bar_type, baz_type])
+ self.assertEqual(result, [
+ [foo_type, qux_type, bar_type],
+ [foo_type, qux_type, baz_type],
+ ])
+
+ def test_split_into_overloads(self):
+ helper = IdlTestingHelper()
+ type_double = IdlType('double')
+ type_foo = IdlType('foo')
+ type_double_or_foo = IdlUnionType([type_double, type_foo])
+ idl_operation = IdlOperation()
+
+ idl_operation.arguments = []
+ splitter = MethodOverloadSplitter(idl_operation)
+ result = splitter.split_into_overloads()
+ self.assertEqual(result, [[]])
+
+ idl_operation.arguments = [
+ helper.make_stub_idl_argument(None, type_double, True)
+ ]
+ splitter = MethodOverloadSplitter(idl_operation)
+ result = splitter.split_into_overloads()
+ self.assertEqual(result, [[], [type_double]])
+
+ idl_operation.arguments = [
+ helper.make_stub_idl_argument(None, type_double_or_foo)
+ ]
+ splitter = MethodOverloadSplitter(idl_operation)
+ result = splitter.split_into_overloads()
+ self.assertEqual(result, [[type_double], [type_foo]])
+
+ def test_split_add_event_listener(self):
+ """Tests how EventTarget.addEventListener is split into respective
+ overloads. From:
+
+ void addEventListener(
+ DOMString type,
+ EventListener? listener,
+ optional (AddEventListenerOptions or boolean) options)
+
+ produces: """
+
+ helper = IdlTestingHelper()
+ type_dom_string = IdlType('DOMString')
+ type_listener = IdlType('EventListener')
+ type_options = IdlType('AddEventListenerOptions')
+ type_boolean = IdlType('boolean')
+ type_union = IdlUnionType([type_options, type_boolean])
+ idl_operation = IdlOperation()
+ idl_operation.arguments = [
+ helper.make_stub_idl_argument('type', type_dom_string),
+ helper.make_stub_idl_argument('listener', type_listener),
+ helper.make_stub_idl_argument('options', type_union,
+ is_optional=True)]
+ splitter = MethodOverloadSplitter(idl_operation)
+ result = splitter.split_into_overloads()
+ self.assertEqual(
+ result,
+ [[type_dom_string, type_listener],
+ [type_dom_string, type_listener, type_options],
+ [type_dom_string, type_listener, type_boolean]])
+
+
class InterfaceContextBuilderTest(unittest.TestCase):
def test_empty(self):
@@ -131,7 +288,7 @@ class InterfaceContextBuilderTest(unittest.TestCase):
self.assertEqual({
'code_generator': 'test',
'cpp_includes': set(['path_to_bar']),
- 'attributes': [{'name': 'foo', 'return_type': 'bar'}],
+ 'attributes': [{'name': 'foo', 'type': 'bar'}],
}, builder.build())
def test_add_method(self):
@@ -139,7 +296,8 @@ class InterfaceContextBuilderTest(unittest.TestCase):
builder = InterfaceContextBuilder(
'test', TypeResolver(helper.make_stub_interfaces_info({
'foo': 'path_to_foo',
- 'bar': 'path_to_bar'
+ 'bar': 'path_to_bar',
+ 'garply': 'path_to_garply',
})))
operation = helper.make_stub_idl_operation('foo', 'bar')
@@ -147,5 +305,33 @@ class InterfaceContextBuilderTest(unittest.TestCase):
self.assertEqual({
'code_generator': 'test',
'cpp_includes': set(['path_to_bar']),
- 'methods': [{'name': 'foo', 'return_type': 'bar'}],
+ 'methods': [{
+ 'arguments': [],
+ 'name': 'Foo',
+ 'type': 'bar'
+ }],
+ }, builder.build())
+
+ operation = helper.make_stub_idl_operation('quux', 'garply')
+ operation.arguments = [
+ helper.make_stub_idl_argument('barBaz', IdlType('qux'))
+ ]
+ builder.add_operation(operation)
+ self.assertEqual({
+ 'code_generator': 'test',
+ 'cpp_includes': set(['path_to_bar', 'path_to_garply']),
+ 'methods': [
+ {
+ 'arguments': [],
+ 'name': 'Foo',
+ 'type': 'bar'
+ },
+ {
+ 'arguments': [
+ {'name': 'bar_baz', 'type': 'qux'}
+ ],
+ 'name': 'Quux',
+ 'type': 'garply'
+ }
+ ],
}, builder.build())

Powered by Google App Engine
This is Rietveld 408576698