diff options
author | asargent@chromium.org <asargent@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-04-12 18:39:27 +0000 |
---|---|---|
committer | asargent@chromium.org <asargent@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-04-12 18:39:27 +0000 |
commit | 4424aee1cf954aad998d2dd76e806e67841bae5c (patch) | |
tree | 7bdcaf921b1197e001fbdc6494536196ba03679c | |
parent | e9b26098616235783c9acc25421c46e800c34ac6 (diff) | |
download | chromium_src-4424aee1cf954aad998d2dd76e806e67841bae5c.zip chromium_src-4424aee1cf954aad998d2dd76e806e67841bae5c.tar.gz chromium_src-4424aee1cf954aad998d2dd76e806e67841bae5c.tar.bz2 |
Support array types for function and callback arguments in IDL APIs.
I realized we completely omitted array support with our initial IDl work. This
patch fixes that for 2 important cases, but doesn't yet add it in for
dictionary members (that can be a separate patch).
BUG=122875
TEST=Included unit tests should pass
Review URL: http://codereview.chromium.org/10054039
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@132023 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r-- | tools/json_schema_compiler/idl_schema.py | 15 | ||||
-rwxr-xr-x | tools/json_schema_compiler/idl_schema_test.py | 47 | ||||
-rw-r--r-- | tools/json_schema_compiler/test/idl_basics.idl | 6 | ||||
-rw-r--r-- | tools/json_schema_compiler/test/idl_schemas_unittest.cc | 50 |
4 files changed, 115 insertions, 3 deletions
diff --git a/tools/json_schema_compiler/idl_schema.py b/tools/json_schema_compiler/idl_schema.py index 9779e27..1d3c83a 100644 --- a/tools/json_schema_compiler/idl_schema.py +++ b/tools/json_schema_compiler/idl_schema.py @@ -108,10 +108,20 @@ class Typeref(object): def process(self, refs): properties = self.additional_properties + result = properties if self.parent.GetProperty('OPTIONAL', False): properties['optional'] = True + # The IDL parser denotes array types by adding a child 'Array' node onto + # the Param node in the Callspec. + for sibling in self.parent.GetChildren(): + if sibling.cls == 'Array' and sibling.GetName() == self.parent.GetName(): + properties['type'] = 'array' + properties['items'] = {} + properties = properties['items'] + break + if self.typeref == 'DOMString': properties['type'] = 'string' elif self.typeref == 'boolean': @@ -132,10 +142,11 @@ class Typeref(object): properties['type'] = 'function' else: try: - properties = refs[self.typeref] + result = refs[self.typeref] except KeyError, e: properties['$ref'] = self.typeref - return properties + + return result class Namespace(object): ''' diff --git a/tools/json_schema_compiler/idl_schema_test.py b/tools/json_schema_compiler/idl_schema_test.py new file mode 100755 index 0000000..59fcd6d --- /dev/null +++ b/tools/json_schema_compiler/idl_schema_test.py @@ -0,0 +1,47 @@ +#!/usr/bin/env python +# Copyright (c) 2012 The Chromium Authors. All rights reserved. +# Use of this source code is governed by a BSD-style license that can be +# found in the LICENSE file. + +import idl_schema +import unittest + +def getFunction(schema, name): + for item in schema['functions']: + if item['name'] == name: + return item + raise KeyError('Missing function %s' % name) + +def getParams(schema, name): + function = getFunction(schema, name) + return function['parameters'] + +class IdlSchemaTest(unittest.TestCase): + def setUp(self): + loaded = idl_schema.Load('test/idl_basics.idl') + self.assertEquals(1, len(loaded)) + self.assertEquals('idl_basics', loaded[0]['namespace']) + self.idl_basics = loaded[0] + + def testSimpleCallbacks(self): + schema = self.idl_basics + expected = [{'type':'function', 'name':'Callback1', 'parameters':[]}] + self.assertEquals(expected, getParams(schema, 'function4')) + + expected = [{'type':'function', 'name':'Callback2', + 'parameters':[{'name':'x', 'type':'integer'}]}] + self.assertEquals(expected, getParams(schema, 'function5')) + + expected = [{'type':'function', 'name':'Callback3', + 'parameters':[{'name':'arg', '$ref':'MyType1'}]}] + self.assertEquals(expected, getParams(schema, 'function6')) + + def testCallbackWithArrayArgument(self): + schema = self.idl_basics + expected = [{'type':'function', 'name':'Callback4', + 'parameters':[{'name':'arg', 'type':'array', + 'items':{'$ref':'MyType2'}}]}] + self.assertEquals(expected, getParams(schema, 'function12')) + +if __name__ == '__main__': + unittest.main() diff --git a/tools/json_schema_compiler/test/idl_basics.idl b/tools/json_schema_compiler/test/idl_basics.idl index 32489ff..3947ed9 100644 --- a/tools/json_schema_compiler/test/idl_basics.idl +++ b/tools/json_schema_compiler/test/idl_basics.idl @@ -17,6 +17,7 @@ namespace idl_basics { callback Callback1 = void(); callback Callback2 = void(long x); callback Callback3 = void(MyType1 arg); + callback Callback4 = void(MyType2[] arg); interface Functions { static void function1(); @@ -30,6 +31,11 @@ namespace idl_basics { static void function7(optional long arg); static void function8(long arg1, optional DOMString arg2); static void function9(optional MyType1 arg); + + static void function10(long x, long[] y); + static void function11(MyType1[] arg); + + static void function12(Callback4 cb); }; interface Events { diff --git a/tools/json_schema_compiler/test/idl_schemas_unittest.cc b/tools/json_schema_compiler/test/idl_schemas_unittest.cc index 6bad12f..4969a9b 100644 --- a/tools/json_schema_compiler/test/idl_schemas_unittest.cc +++ b/tools/json_schema_compiler/test/idl_schemas_unittest.cc @@ -21,6 +21,8 @@ namespace Function6 = test::api::idl_basics::Function6; namespace Function7 = test::api::idl_basics::Function7; namespace Function8 = test::api::idl_basics::Function8; namespace Function9 = test::api::idl_basics::Function9; +namespace Function10 = test::api::idl_basics::Function10; +namespace Function11 = test::api::idl_basics::Function11; namespace ObjectFunction1 = test::api::idl_object_types::ObjectFunction1; TEST(IdlCompiler, Basics) { @@ -66,7 +68,7 @@ TEST(IdlCompiler, Basics) { } TEST(IdlCompiler, OptionalArguments) { - // Test a function that takes one optional argument, but without and with + // Test a function that takes one optional argument, both without and with // that argument. ListValue list; scoped_ptr<Function7::Params> f7_params = Function7::Params::Create(list); @@ -103,6 +105,52 @@ TEST(IdlCompiler, OptionalArguments) { EXPECT_EQ("hello", t1->y); } +TEST(IdlCompiler, ArrayTypes) { + // Tests of a function that takes an integer and an array of integers. First + // use an empty array. + ListValue list; + list.Append(Value::CreateIntegerValue(33)); + list.Append(new ListValue); + scoped_ptr<Function10::Params> f10_params = Function10::Params::Create(list); + ASSERT_TRUE(f10_params != NULL); + EXPECT_EQ(33, f10_params->x); + EXPECT_TRUE(f10_params->y.empty()); + + // Same function, but this time with 2 values in the array. + list.Clear(); + list.Append(Value::CreateIntegerValue(33)); + ListValue* sublist = new ListValue; + sublist->Append(Value::CreateIntegerValue(34)); + sublist->Append(Value::CreateIntegerValue(35)); + list.Append(sublist); + f10_params = Function10::Params::Create(list); + ASSERT_TRUE(f10_params != NULL); + EXPECT_EQ(33, f10_params->x); + ASSERT_EQ(2u, f10_params->y.size()); + EXPECT_EQ(34, f10_params->y[0]); + EXPECT_EQ(35, f10_params->y[1]); + + // Now test a function which takes an array of a defined type. + list.Clear(); + MyType1 a; + MyType1 b; + a.x = 5; + b.x = 6; + a.y = std::string("foo"); + b.y = std::string("bar"); + ListValue* sublist2 = new ListValue; + sublist2->Append(a.ToValue().release()); + sublist2->Append(b.ToValue().release()); + list.Append(sublist2); + scoped_ptr<Function11::Params> f11_params = Function11::Params::Create(list); + ASSERT_TRUE(f11_params != NULL); + ASSERT_EQ(2u, f11_params->arg.size()); + EXPECT_EQ(5, f11_params->arg[0]->x); + EXPECT_EQ("foo", f11_params->arg[0]->y); + EXPECT_EQ(6, f11_params->arg[1]->x); + EXPECT_EQ("bar", f11_params->arg[1]->y); +} + TEST(IdlCompiler, ObjectTypes) { // Test the FooType type. FooType f1; |