diff options
author | sergeyu@chromium.org <sergeyu@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-07-25 19:26:52 +0000 |
---|---|---|
committer | sergeyu@chromium.org <sergeyu@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-07-25 19:26:52 +0000 |
commit | 4b2896a1977254ef154b16cadffd4ba6033087b0 (patch) | |
tree | d9c61531aaaafc3c4afdd4038af7f2a32f7080e4 /tools | |
parent | bfd80f0bbc586d6254724530c1dbe376fd18e7eb (diff) | |
download | chromium_src-4b2896a1977254ef154b16cadffd4ba6033087b0.zip chromium_src-4b2896a1977254ef154b16cadffd4ba6033087b0.tar.gz chromium_src-4b2896a1977254ef154b16cadffd4ba6033087b0.tar.bz2 |
Fix json_schema_compiler to properly handle enum arrays.
Also added new tests that would detect the problem.
BUG=263924
R=kalman@chromium.org
Review URL: https://codereview.chromium.org/20168004
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@213673 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'tools')
-rw-r--r-- | tools/json_schema_compiler/cc_generator.py | 2 | ||||
-rw-r--r-- | tools/json_schema_compiler/test/enums.json | 39 | ||||
-rw-r--r-- | tools/json_schema_compiler/test/enums_unittest.cc | 44 | ||||
-rw-r--r-- | tools/json_schema_compiler/util.h | 11 |
4 files changed, 84 insertions, 12 deletions
diff --git a/tools/json_schema_compiler/cc_generator.py b/tools/json_schema_compiler/cc_generator.py index 6f201fd..cab4650 100644 --- a/tools/json_schema_compiler/cc_generator.py +++ b/tools/json_schema_compiler/cc_generator.py @@ -620,7 +620,7 @@ class _Generator(object): (c.Append('const base::ListValue* list = NULL;') .Append('if (!%(src_var)s->GetAsList(&list))') .Append(' return %(failure_value)s;')) - item_type = underlying_type.item_type + item_type = self._type_helper.FollowRef(underlying_type.item_type) if item_type.property_type == PropertyType.ENUM: c.Concat(self._GenerateListValueToEnumArrayConversion( item_type, diff --git a/tools/json_schema_compiler/test/enums.json b/tools/json_schema_compiler/test/enums.json index 19229b0..8891dc7 100644 --- a/tools/json_schema_compiler/test/enums.json +++ b/tools/json_schema_compiler/test/enums.json @@ -62,6 +62,26 @@ ] }, { + "name": "takesEnumArray", + "type": "function", + "description": "Takes an enum array as its parameter.", + "parameters": [ + { + "name": "values", + "type": "array", + "items": { + "type": "string", + "enum": ["foo", "bar", "baz"] + } + }, + { + "name": "callback", + "type": "function", + "parameters": [] + } + ] + }, + { "name": "takesEnumAsType", "type": "function", "description": "Takes an enum type as its parameter.", @@ -78,6 +98,25 @@ ] }, { + "name": "takesEnumArrayAsType", + "type": "function", + "description": "Takes an enum type array as its parameter.", + "parameters": [ + { + "name": "values", + "type": "array", + "items": { + "$ref": "Enumeration" + } + }, + { + "name": "callback", + "type": "function", + "parameters": [] + } + ] + }, + { "name": "returnsEnum", "type": "function", "description": "Returns an enum through the callback", diff --git a/tools/json_schema_compiler/test/enums_unittest.cc b/tools/json_schema_compiler/test/enums_unittest.cc index 81bfe8d..144c3cc 100644 --- a/tools/json_schema_compiler/test/enums_unittest.cc +++ b/tools/json_schema_compiler/test/enums_unittest.cc @@ -5,8 +5,10 @@ #include "tools/json_schema_compiler/test/enums.h" #include "testing/gtest/include/gtest/gtest.h" +#include "tools/json_schema_compiler/test/test_util.h" using namespace test::api::enums; +using json_schema_compiler::test_util::List; TEST(JsonSchemaCompilerEnumsTest, EnumTypePopulate) { { @@ -53,6 +55,27 @@ TEST(JsonSchemaCompilerEnumsTest, EnumsAsTypes) { } } +TEST(JsonSchemaCompilerEnumsTest, EnumsArrayAsType) { + { + ListValue params_value; + params_value.Append(List(Value::CreateStringValue("one"), + Value::CreateStringValue("two")).release()); + scoped_ptr<TakesEnumArrayAsType::Params> params( + TakesEnumArrayAsType::Params::Create(params_value)); + ASSERT_TRUE(params); + EXPECT_EQ(2U, params->values.size()); + EXPECT_EQ(ENUMERATION_ONE, params->values[0]); + EXPECT_EQ(ENUMERATION_TWO, params->values[1]); + } + { + ListValue params_value; + params_value.Append(List(Value::CreateStringValue("invalid")).release()); + scoped_ptr<TakesEnumArrayAsType::Params> params( + TakesEnumArrayAsType::Params::Create(params_value)); + EXPECT_FALSE(params); + } +} + TEST(JsonSchemaCompilerEnumsTest, ReturnsEnumCreate) { { ReturnsEnum::Results::State state = ReturnsEnum::Results::STATE_FOO; @@ -124,6 +147,27 @@ TEST(JsonSchemaCompilerEnumsTest, TakesEnumParamsCreate) { } } +TEST(JsonSchemaCompilerEnumsTest, TakesEnumArrayParamsCreate) { + { + ListValue params_value; + params_value.Append(List(Value::CreateStringValue("foo"), + Value::CreateStringValue("bar")).release()); + scoped_ptr<TakesEnumArray::Params> params( + TakesEnumArray::Params::Create(params_value)); + ASSERT_TRUE(params); + EXPECT_EQ(2U, params->values.size()); + EXPECT_EQ(TakesEnumArray::Params::VALUES_TYPE_FOO, params->values[0]); + EXPECT_EQ(TakesEnumArray::Params::VALUES_TYPE_BAR, params->values[1]); + } + { + ListValue params_value; + params_value.Append(List(Value::CreateStringValue("invalid")).release()); + scoped_ptr<TakesEnumArray::Params> params( + TakesEnumArray::Params::Create(params_value)); + EXPECT_FALSE(params); + } +} + TEST(JsonSchemaCompilerEnumsTest, TakesOptionalEnumParamsCreate) { { ListValue params_value; diff --git a/tools/json_schema_compiler/util.h b/tools/json_schema_compiler/util.h index 7957830..a921179 100644 --- a/tools/json_schema_compiler/util.h +++ b/tools/json_schema_compiler/util.h @@ -44,17 +44,6 @@ bool GetItemFromList(const base::ListValue& from, return true; } -// This is used for getting an enum out of a ListValue, which will happen if an -// array of enums is a parameter to a function. -template<class T> -bool GetItemFromList(const base::ListValue& from, int index, T* out) { - int value; - if (!from.GetInteger(index, &value)) - return false; - *out = static_cast<T>(value); - return true; -} - // Populates |out| with |list|. Returns false if there is no list at the // specified key or if the list has anything other than |T|. template <class T> |