summaryrefslogtreecommitdiffstats
path: root/tools/json_schema_compiler
diff options
context:
space:
mode:
authorhebert.christopherj@chromium.org <hebert.christopherj@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-08-29 04:07:13 +0000
committerhebert.christopherj@chromium.org <hebert.christopherj@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-08-29 04:07:13 +0000
commite7ae7017cb69c66e43e08dedede8f9f5376dbe02 (patch)
tree0dbfecc0a86884aa44c7d5cca2c9d69746e1e5e8 /tools/json_schema_compiler
parent6c56fde5bf676be3cee74fc3587163f067a28e04 (diff)
downloadchromium_src-e7ae7017cb69c66e43e08dedede8f9f5376dbe02.zip
chromium_src-e7ae7017cb69c66e43e08dedede8f9f5376dbe02.tar.gz
chromium_src-e7ae7017cb69c66e43e08dedede8f9f5376dbe02.tar.bz2
Changes:
1) IsEnumOrEnumRef is refactored to be in the cpp_type_generator. 2) Refactored GeneratePopulatePropertyFromValue to call private methods (it was getting to be unwieldy.) 3) Refactored most of the unittests to put values on the stack instead of scoped_ptrs. 4) Implemented Enums as types. Enums all have the ENUMERATION_NONE. BUG=141940 Review URL: https://chromiumcodereview.appspot.com/10828407 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@153849 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'tools/json_schema_compiler')
-rw-r--r--tools/json_schema_compiler/cc_generator.py313
-rw-r--r--tools/json_schema_compiler/cpp_type_generator.py24
-rw-r--r--tools/json_schema_compiler/h_generator.py11
-rw-r--r--tools/json_schema_compiler/model.py5
-rw-r--r--tools/json_schema_compiler/test/enums.json50
-rw-r--r--tools/json_schema_compiler/test/enums_unittest.cc132
6 files changed, 354 insertions, 181 deletions
diff --git a/tools/json_schema_compiler/cc_generator.py b/tools/json_schema_compiler/cc_generator.py
index d09d325..f01479a 100644
--- a/tools/json_schema_compiler/cc_generator.py
+++ b/tools/json_schema_compiler/cc_generator.py
@@ -127,6 +127,9 @@ class CCGenerator(object):
(c.Concat(self._GenerateTypeToValue(cpp_namespace, type_))
.Append()
)
+ elif self._cpp_type_generator.IsEnumOrEnumRef(type_):
+ c.Concat(self._GenerateCreateEnumTypeValue(cpp_namespace, type_))
+ c.Append()
c.Substitute({'classname': classname, 'namespace': cpp_namespace})
return c
@@ -252,7 +255,7 @@ class CCGenerator(object):
c.Append('value->MergeDictionary(&%s);' % prop.unix_name)
else:
if prop.optional:
- if prop.type_ == PropertyType.ENUM:
+ if self._cpp_type_generator.IsEnumOrEnumRef(prop):
c.Sblock('if (%s != %s) {' %
(prop.unix_name,
self._cpp_type_generator.GetEnumNoneValue(prop)))
@@ -339,7 +342,7 @@ class CCGenerator(object):
else:
vardot = var + '.'
return '%sDeepCopy()' % vardot
- elif prop.type_ == PropertyType.ENUM:
+ elif self._cpp_type_generator.IsEnumOrEnumRef(prop):
return 'CreateEnumValue(%s).release()' % var
elif prop.type_ == PropertyType.BINARY:
if prop.optional:
@@ -460,127 +463,21 @@ class CCGenerator(object):
c.Sblock('{')
if self._IsFundamentalOrFundamentalRef(prop):
- if prop.optional:
- (c.Append('%(ctype)s temp;')
- .Append('if (!%s)' %
- cpp_util.GetAsFundamentalValue(
- self._cpp_type_generator.GetReferencedProperty(prop),
- value_var,
- '&temp'))
- .Append(' return %(failure_value)s;')
- )
- if prop.type_ != prop.compiled_type:
- (c.Append('%(compiled_ctype)s temp2;')
- .Append('if (!%s)' %
- cpp_util.GenerateTypeToCompiledTypeConversion(
- self._cpp_type_generator.GetReferencedProperty(prop),
- 'temp',
- 'temp2'))
- .Append(' return %(failure_value)s;')
- .Append('%(dst)s->%(name)s.reset(new %(compiled_ctype)s(temp2));')
- )
- else:
- c.Append('%(dst)s->%(name)s.reset(new %(ctype)s(temp));')
-
- else:
- if prop.type_ == prop.compiled_type:
- assignment_target = '&%s->%s' % (dst, prop.unix_name)
- else:
- c.Append('%(ctype)s temp;')
- assignment_target = '&temp'
- (c.Append('if (!%s)' %
- cpp_util.GetAsFundamentalValue(
- self._cpp_type_generator.GetReferencedProperty(prop),
- value_var,
- assignment_target))
- .Append(' return %(failure_value)s;')
- )
- if prop.type_ != prop.compiled_type:
- (c.Append('if (!%s)' %
- cpp_util.GenerateTypeToCompiledTypeConversion(
- self._cpp_type_generator.GetReferencedProperty(prop),
- 'temp',
- '%s->%s' % (dst, prop.unix_name)))
- .Append(' return %(failure_value)s;')
- )
-
+ self._GenerateFundamentalOrFundamentalRefPopulate(c, prop, value_var, dst)
elif self._IsObjectOrObjectRef(prop):
- if prop.optional:
- (c.Append('const base::DictionaryValue* dictionary = NULL;')
- .Append('if (!%(value_var)s->GetAsDictionary(&dictionary))')
- .Append(' return %(failure_value)s;')
- .Append('scoped_ptr<%(ctype)s> temp(new %(ctype)s());')
- .Append('if (!%(ctype)s::Populate(*dictionary, temp.get()))')
- .Append(' return %(failure_value)s;')
- .Append('%(dst)s->%(name)s = temp.Pass();')
- )
- else:
- (c.Append('const base::DictionaryValue* dictionary = NULL;')
- .Append('if (!%(value_var)s->GetAsDictionary(&dictionary))')
- .Append(' return %(failure_value)s;')
- .Append(
- 'if (!%(ctype)s::Populate(*dictionary, &%(dst)s->%(name)s))')
- .Append(' return %(failure_value)s;')
- )
+ self._GenerateObjectOrObjectRefPopulate(c, prop)
elif prop.type_ == PropertyType.FUNCTION:
- if prop.optional:
- c.Append('%(dst)s->%(name)s.reset(new base::DictionaryValue());')
+ self._GenerateFunctionPopulate(c, prop)
elif prop.type_ == PropertyType.ANY:
- if prop.optional:
- c.Append('%(dst)s->%(name)s.reset(new ' + any_helper.ANY_CLASS + '());')
- c.Append(self._any_helper.Init(prop, value_var, dst) + ';')
+ self._GenerateAnyPopulate(c, prop, value_var, dst)
elif self._IsArrayOrArrayRef(prop):
- # util_cc_helper deals with optional and required arrays
- (c.Append('const base::ListValue* list = NULL;')
- .Append('if (!%(value_var)s->GetAsList(&list))')
- .Append(' return %(failure_value)s;'))
- if prop.item_type.type_ == PropertyType.ENUM:
- self._GenerateListValueToEnumArrayConversion(c, prop)
- else:
- (c.Append('if (!%s)' % self._util_cc_helper.PopulateArrayFromList(
- self._cpp_type_generator.GetReferencedProperty(prop), 'list',
- dst + '->' + prop.unix_name, prop.optional))
- .Append(' return %(failure_value)s;')
- )
+ self._GenerateArrayOrArrayRefPopulate(c, prop, dst)
elif prop.type_ == PropertyType.CHOICES:
- type_var = '%(dst)s->%(name)s_type'
- c.Sblock('switch (%(value_var)s->GetType()) {')
- for choice in self._cpp_type_generator.ExpandParams([prop]):
- (c.Sblock('case %s: {' % cpp_util.GetValueType(
- self._cpp_type_generator.GetReferencedProperty(choice).type_))
- .Concat(self._GeneratePopulatePropertyFromValue(
- choice, value_var, dst, failure_value, check_type=False))
- .Append('%s = %s;' %
- (type_var,
- self._cpp_type_generator.GetEnumValue(
- prop, choice.type_.name)))
- .Append('break;')
- .Eblock('}')
- )
- (c.Append('default:')
- .Append(' return %(failure_value)s;')
- )
- c.Eblock('}')
- elif prop.type_ == PropertyType.ENUM:
- c.Sblock('{')
- self._GenerateStringToEnumConversion(c, prop, value_var, 'enum_temp')
- c.Append('%(dst)s->%(name)s = enum_temp;')
- c.Eblock('}')
+ self._GenerateChoicePopulate(c, prop, value_var, dst, failure_value)
+ elif self._cpp_type_generator.IsEnumOrEnumRef(prop):
+ self._GenerateEnumPopulate(c, prop, value_var)
elif prop.type_ == PropertyType.BINARY:
- (c.Append('if (!%(value_var)s->IsType(%(value_type)s))')
- .Append(' return %(failure_value)s;')
- .Append('const base::BinaryValue* binary_value =')
- .Append(' static_cast<const base::BinaryValue*>(%(value_var)s);')
- )
- if prop.optional:
- (c.Append('%(dst)s->%(name)s.reset(')
- .Append(' new std::string(binary_value->GetBuffer(),')
- .Append(' binary_value->GetSize()));')
- )
- else:
- (c.Append('%(dst)s->%(name)s.assign(binary_value->GetBuffer(),')
- .Append(' binary_value->GetSize());')
- )
+ self._GenerateBinaryPopulate(c, prop)
else:
raise NotImplementedError(prop.type_)
c.Eblock('}')
@@ -598,6 +495,139 @@ class CCGenerator(object):
c.Substitute(sub)
return c
+ def _GenerateFundamentalOrFundamentalRefPopulate(self,
+ c,
+ prop,
+ value_var,
+ dst):
+ if prop.optional:
+ (c.Append('%(ctype)s temp;')
+ .Append('if (!%s)' %
+ cpp_util.GetAsFundamentalValue(
+ self._cpp_type_generator.GetReferencedProperty(prop),
+ value_var,
+ '&temp'))
+ .Append(' return %(failure_value)s;')
+ )
+ if prop.type_ != prop.compiled_type:
+ (c.Append('%(compiled_ctype)s temp2;')
+ .Append('if (!%s)' %
+ cpp_util.GenerateTypeToCompiledTypeConversion(
+ self._cpp_type_generator.GetReferencedProperty(prop),
+ 'temp',
+ 'temp2'))
+ .Append(' return %(failure_value)s;')
+ .Append('%(dst)s->%(name)s.reset(new %(compiled_ctype)s(temp2));')
+ )
+ else:
+ c.Append('%(dst)s->%(name)s.reset(new %(ctype)s(temp));')
+
+ else:
+ if prop.type_ == prop.compiled_type:
+ assignment_target = '&%s->%s' % (dst, prop.unix_name)
+ else:
+ c.Append('%(ctype)s temp;')
+ assignment_target = '&temp'
+ (c.Append('if (!%s)' %
+ cpp_util.GetAsFundamentalValue(
+ self._cpp_type_generator.GetReferencedProperty(prop),
+ value_var,
+ assignment_target))
+ .Append(' return %(failure_value)s;')
+ )
+ if prop.type_ != prop.compiled_type:
+ (c.Append('if (!%s)' %
+ cpp_util.GenerateTypeToCompiledTypeConversion(
+ self._cpp_type_generator.GetReferencedProperty(prop),
+ 'temp',
+ '%s->%s' % (dst, prop.unix_name)))
+ .Append(' return %(failure_value)s;')
+ )
+
+ def _GenerateObjectOrObjectRefPopulate(self, c, prop):
+ if prop.optional:
+ (c.Append('const base::DictionaryValue* dictionary = NULL;')
+ .Append('if (!%(value_var)s->GetAsDictionary(&dictionary))')
+ .Append(' return %(failure_value)s;')
+ .Append('scoped_ptr<%(ctype)s> temp(new %(ctype)s());')
+ .Append('if (!%(ctype)s::Populate(*dictionary, temp.get()))')
+ .Append(' return %(failure_value)s;')
+ .Append('%(dst)s->%(name)s = temp.Pass();')
+ )
+ else:
+ (c.Append('const base::DictionaryValue* dictionary = NULL;')
+ .Append('if (!%(value_var)s->GetAsDictionary(&dictionary))')
+ .Append(' return %(failure_value)s;')
+ .Append(
+ 'if (!%(ctype)s::Populate(*dictionary, &%(dst)s->%(name)s))')
+ .Append(' return %(failure_value)s;')
+ )
+
+ def _GenerateFunctionPopulate(self, c, prop):
+ if prop.optional:
+ c.Append('%(dst)s->%(name)s.reset(new base::DictionaryValue());')
+
+ def _GenerateAnyPopulate(self, c, prop, value_var, dst):
+ if prop.optional:
+ c.Append('%(dst)s->%(name)s.reset(new ' + any_helper.ANY_CLASS + '());')
+ c.Append(self._any_helper.Init(prop, value_var, dst) + ';')
+
+ def _GenerateArrayOrArrayRefPopulate(self, c, prop, dst):
+ # util_cc_helper deals with optional and required arrays
+ (c.Append('const base::ListValue* list = NULL;')
+ .Append('if (!%(value_var)s->GetAsList(&list))')
+ .Append(' return %(failure_value)s;'))
+ if prop.item_type.type_ == PropertyType.ENUM:
+ self._GenerateListValueToEnumArrayConversion(c, prop)
+ else:
+ (c.Append('if (!%s)' % self._util_cc_helper.PopulateArrayFromList(
+ self._cpp_type_generator.GetReferencedProperty(prop), 'list',
+ dst + '->' + prop.unix_name, prop.optional))
+ .Append(' return %(failure_value)s;')
+ )
+
+ def _GenerateChoicePopulate(self, c, prop, value_var, dst, failure_value):
+ type_var = '%(dst)s->%(name)s_type'
+ c.Sblock('switch (%(value_var)s->GetType()) {')
+ for choice in self._cpp_type_generator.ExpandParams([prop]):
+ (c.Sblock('case %s: {' % cpp_util.GetValueType(
+ self._cpp_type_generator.GetReferencedProperty(choice).type_))
+ .Concat(self._GeneratePopulatePropertyFromValue(
+ choice, value_var, dst, failure_value, check_type=False))
+ .Append('%s = %s;' %
+ (type_var,
+ self._cpp_type_generator.GetEnumValue(
+ prop, choice.type_.name)))
+ .Append('break;')
+ .Eblock('}')
+ )
+ (c.Append('default:')
+ .Append(' return %(failure_value)s;')
+ )
+ c.Eblock('}')
+
+ def _GenerateEnumPopulate(self, c, prop, value_var):
+ c.Sblock('{')
+ self._GenerateStringToEnumConversion(c, prop, value_var, 'enum_temp')
+ c.Append('%(dst)s->%(name)s = enum_temp;')
+ c.Eblock('}')
+
+ def _GenerateBinaryPopulate(self, c, prop):
+ (c.Append('if (!%(value_var)s->IsType(%(value_type)s))')
+ .Append(' return %(failure_value)s;')
+ .Append('const base::BinaryValue* binary_value =')
+ .Append(' static_cast<const base::BinaryValue*>(%(value_var)s);')
+ )
+ if prop.optional:
+ (c.Append('%(dst)s->%(name)s.reset(')
+ .Append(' new std::string(binary_value->GetBuffer(),')
+ .Append(' binary_value->GetSize()));')
+ )
+ else:
+ (c.Append('%(dst)s->%(name)s.assign(binary_value->GetBuffer(),')
+ .Append(' binary_value->GetSize());')
+ )
+
def _GenerateListValueToEnumArrayConversion(self, c, prop):
"""Appends code that converts a ListValue of string contstants to
an array of enums in dst.
@@ -613,31 +643,37 @@ class CCGenerator(object):
accessor = '->'
c.Sblock('for (ListValue::const_iterator it = list->begin(); '
'it != list->end(); ++it) {')
- self._GenerateStringToEnumConversion(c, prop.item_type,
- '(*it)', 'enum_temp')
+ self._GenerateStringToEnumConversion(
+ c, prop.item_type, '(*it)', 'enum_temp')
c.Append('%(dst)s->%(name)s' + accessor + 'push_back(enum_temp);')
c.Eblock('}')
- def _GenerateStringToEnumConversion(self, c, prop, value_var, enum_temp):
+ def _GenerateStringToEnumConversion(self,
+ c,
+ prop,
+ value_var,
+ enum_temp):
"""Appends code that converts a string to an enum.
- Leaves failure_value unsubstituded.
+ Leaves failure_value unsubstituted.
c: the code that is appended to.
prop: the property that the code is populating.
value_var: the string value that is being converted.
enum_temp: the name used to store the temporary enum value.
"""
- (c.Append('%s %s;' % (self._cpp_type_generator.GetType(prop), enum_temp))
+ (c.Append('%s %s;' % (self._cpp_type_generator.GetCompiledType(prop),
+ enum_temp))
.Append('std::string enum_as_string;')
.Append('if (!%s->GetAsString(&enum_as_string))' % value_var)
.Append(' return %(failure_value)s;')
)
- for i, enum_value in enumerate(prop.enum_values):
+ for i, enum_value in enumerate(
+ self._cpp_type_generator.GetReferencedProperty(prop).enum_values):
(c.Append(
('if' if i == 0 else 'else if') +
'(enum_as_string == "%s")' % enum_value)
.Append(' ' + enum_temp + ' = %s;' % (
- self._cpp_type_generator.GetEnumValue(prop, enum_value)))
+ self._cpp_type_generator.GetEnumValue(prop, enum_value)))
)
(c.Append('else')
.Append(' return %(failure_value)s;')
@@ -697,6 +733,32 @@ class CCGenerator(object):
)
return c
+ def _GenerateCreateEnumTypeValue(self, cpp_namespace, prop):
+ """Generates CreateEnumValue() that returns the base::StringValue
+ representation of an enum type.
+ """
+ c = Code()
+ classname = cpp_util.Classname(schema_util.StripSchemaNamespace(prop.name))
+ c.Sblock('scoped_ptr<base::Value> CreateEnumValue(%s %s) {' % (
+ classname, classname.lower()))
+ c.Sblock('switch (%s) {' % classname.lower())
+
+ enum_prop = self._cpp_type_generator.GetReferencedProperty(prop)
+ for enum_value in enum_prop.enum_values:
+ c.Concat(self._GenerateReturnCase(
+ '%s_%s' % (classname.upper(), enum_value.upper()),
+ 'scoped_ptr<base::Value>(base::Value::CreateStringValue("%s"))' %
+ enum_value))
+ (c.Eblock('}')
+ .Append('NOTREACHED();')
+ .Append('return scoped_ptr<base::Value>();')
+ .Eblock('}')
+ )
+ return c
+
+ # TODO(chebert): This is basically the same as GenerateCreateEnumTypeValue().
+ # The plan is to phase out the old-style enums, and make all enums into REF
+ # types.
def _GenerateCreateEnumValue(self, cpp_namespace, prop):
"""Generates CreateEnumValue() that returns the base::StringValue
representation of an enum.
@@ -813,7 +875,8 @@ class CCGenerator(object):
dst: Type*
"""
c = Code()
- if prop.type_ in (PropertyType.ENUM, PropertyType.CHOICES):
+ if (self._cpp_type_generator.IsEnumOrEnumRef(prop) or
+ prop.type_ == PropertyType.CHOICES):
if prop.optional:
prop_name = prop.unix_name
if prop.type_ == PropertyType.CHOICES:
diff --git a/tools/json_schema_compiler/cpp_type_generator.py b/tools/json_schema_compiler/cpp_type_generator.py
index acaa52b..95329c3 100644
--- a/tools/json_schema_compiler/cpp_type_generator.py
+++ b/tools/json_schema_compiler/cpp_type_generator.py
@@ -98,15 +98,20 @@ class CppTypeGenerator(object):
"""Gets the enum value in the given model.Property indicating no value has
been set.
"""
- return '%s_NONE' % prop.unix_name.upper()
+ prop_name = prop.unix_name
+ if (self.IsEnumRef(prop)):
+ prop_name = self.GetCompiledType(prop).upper()
+ return '%s_NONE' % prop_name.upper()
def GetEnumValue(self, prop, enum_value):
"""Gets the enum value of the given model.Property of the given type.
e.g VAR_STRING
"""
- return '%s_%s' % (
- prop.unix_name.upper(), cpp_util.Classname(enum_value.upper()))
+ prop_name = prop.unix_name.upper()
+ if (self.IsEnumRef(prop)):
+ prop_name = self.GetCompiledType(prop).upper()
+ return '%s_%s' % (prop_name, cpp_util.Classname(enum_value.upper()))
def GetChoicesEnumType(self, prop):
"""Gets the type of the enum for the given model.Property.
@@ -189,7 +194,7 @@ class CppTypeGenerator(object):
# Enums aren't wrapped because C++ won't allow it. Optional enums have a
# NONE value generated instead.
- if wrap_optional and prop.optional and type_ != PropertyType.ENUM:
+ if wrap_optional and prop.optional and not self.IsEnumOrEnumRef(prop):
cpp_type = 'scoped_ptr<%s> ' % cpp_type
if pad_for_generics:
return cpp_type
@@ -263,6 +268,17 @@ class CppTypeGenerator(object):
return self._ResolveTypeNamespace(prop.ref_type).types.get(prop.ref_type,
None)
+ def IsEnumOrEnumRef(self, prop):
+ """Returns true if the property is an ENUM or a reference to an ENUM.
+ """
+ return self.GetReferencedProperty(prop).type_ == PropertyType.ENUM
+
+ def IsEnumRef(self, prop):
+ """Returns true if the property is a reference to an ENUM.
+ """
+ return (prop.type_ == PropertyType.REF and
+ self.GetReferencedProperty(prop).type_ == PropertyType.ENUM)
+
def _NamespaceTypeDependencies(self):
"""Returns a dict containing a mapping of model.Namespace to the C++ type
of type dependencies for self._namespace.
diff --git a/tools/json_schema_compiler/h_generator.py b/tools/json_schema_compiler/h_generator.py
index e1d9f48..c1df7ba 100644
--- a/tools/json_schema_compiler/h_generator.py
+++ b/tools/json_schema_compiler/h_generator.py
@@ -189,6 +189,17 @@ class HGenerator(object):
if type_.description:
c.Comment(type_.description)
c.Append('typedef std::string %(classname)s;')
+ elif type_.type_ == PropertyType.ENUM:
+ if type_.description:
+ c.Comment(type_.description)
+ c.Sblock('enum %(classname)s {')
+ for value in type_.enum_values:
+ c.Append('%s_%s,' % (classname.upper(), value.upper()))
+ (c.Eblock('};')
+ .Append()
+ .Append('scoped_ptr<base::Value> CreateEnumValue(%s %s);' %
+ (classname, classname.lower()))
+ )
else:
if type_.description:
c.Comment(type_.description)
diff --git a/tools/json_schema_compiler/model.py b/tools/json_schema_compiler/model.py
index e377295..753274f 100644
--- a/tools/json_schema_compiler/model.py
+++ b/tools/json_schema_compiler/model.py
@@ -81,6 +81,11 @@ class Type(object):
from_client=True)
elif json.get('type') == 'string':
self.type_ = PropertyType.STRING
+ elif 'enum' in json:
+ self.enum_values = []
+ for value in json['enum']:
+ self.enum_values.append(value)
+ self.type_ = PropertyType.ENUM
else:
if not (
'properties' in json or
diff --git a/tools/json_schema_compiler/test/enums.json b/tools/json_schema_compiler/test/enums.json
index ae6be004..48c2c980 100644
--- a/tools/json_schema_compiler/test/enums.json
+++ b/tools/json_schema_compiler/test/enums.json
@@ -3,6 +3,10 @@
"namespace": "enums",
"types": [
{
+ "id": "Enumeration",
+ "enum": ["none", "one", "two", "three"]
+ },
+ {
"id": "EnumType",
"type": "object",
"properties": {
@@ -13,6 +17,19 @@
}
},
{
+ "id": "HasEnumeration",
+ "type": "object",
+ "properties": {
+ "enumeration": {
+ "$ref": "Enumeration"
+ },
+ "optional_enumeration": {
+ "$ref": "Enumeration",
+ "optional": true
+ }
+ }
+ },
+ {
"id": "OptionalEnumType",
"type": "object",
"properties": {
@@ -43,6 +60,22 @@
]
},
{
+ "name": "takesEnumAsType",
+ "type": "function",
+ "description": "Takes an enum type as its parameter.",
+ "parameters": [
+ {
+ "name": "enumeration",
+ "$ref": "Enumeration"
+ },
+ {
+ "name": "callback",
+ "type": "function",
+ "parameters": []
+ }
+ ]
+ },
+ {
"name": "returnsEnum",
"type": "function",
"description": "Returns an enum through the callback",
@@ -61,6 +94,23 @@
]
},
{
+ "name": "returnsEnumAsType",
+ "type": "function",
+ "description": "Returns an enum through the callback",
+ "parameters": [
+ {
+ "name": "callback",
+ "type": "function",
+ "parameters": [
+ {
+ "name": "enumeration",
+ "$ref": "Enumeration"
+ }
+ ]
+ }
+ ]
+ },
+ {
"name": "returnsTwoEnums",
"type": "function",
"description": "Returns two enums through the callback",
diff --git a/tools/json_schema_compiler/test/enums_unittest.cc b/tools/json_schema_compiler/test/enums_unittest.cc
index 6037874..b9fec70 100644
--- a/tools/json_schema_compiler/test/enums_unittest.cc
+++ b/tools/json_schema_compiler/test/enums_unittest.cc
@@ -10,18 +10,46 @@ using namespace test::api::enums;
TEST(JsonSchemaCompilerEnumsTest, EnumTypePopulate) {
{
- scoped_ptr<EnumType> enum_type(new EnumType());
- scoped_ptr<DictionaryValue> value(new DictionaryValue());
- value->Set("type", Value::CreateStringValue("one"));
- EXPECT_TRUE(EnumType::Populate(*value, enum_type.get()));
- EXPECT_EQ(EnumType::TYPE_ONE, enum_type->type);
- EXPECT_TRUE(value->Equals(enum_type->ToValue().get()));
+ EnumType enum_type;
+ DictionaryValue value;
+ value.Set("type", Value::CreateStringValue("one"));
+ EXPECT_TRUE(EnumType::Populate(value, &enum_type));
+ EXPECT_EQ(EnumType::TYPE_ONE, enum_type.type);
+ EXPECT_TRUE(value.Equals(enum_type.ToValue().get()));
}
{
- scoped_ptr<EnumType> enum_type(new EnumType());
- scoped_ptr<DictionaryValue> value(new DictionaryValue());
- value->Set("type", Value::CreateStringValue("invalid"));
- EXPECT_FALSE(EnumType::Populate(*value, enum_type.get()));
+ EnumType enum_type;
+ DictionaryValue value;
+ value.Set("type", Value::CreateStringValue("invalid"));
+ EXPECT_FALSE(EnumType::Populate(value, &enum_type));
+ }
+}
+
+TEST(JsonSchemaCompilerEnumsTest, EnumsAsTypes) {
+ {
+ ListValue args;
+ args.Append(Value::CreateStringValue("one"));
+
+ scoped_ptr<TakesEnumAsType::Params> params(
+ TakesEnumAsType::Params::Create(args));
+ ASSERT_TRUE(params.get());
+ EXPECT_EQ(ENUMERATION_ONE, params->enumeration);
+
+ EXPECT_TRUE(args.Equals(ReturnsEnumAsType::Results::Create(
+ ENUMERATION_ONE).get()));
+ }
+ {
+ HasEnumeration enumeration;
+ DictionaryValue value;
+ ASSERT_FALSE(HasEnumeration::Populate(value, &enumeration));
+
+ value.Set("enumeration", Value::CreateStringValue("one"));
+ ASSERT_TRUE(HasEnumeration::Populate(value, &enumeration));
+ EXPECT_TRUE(value.Equals(enumeration.ToValue().get()));
+
+ value.Set("optional_enumeration", Value::CreateStringValue("two"));
+ ASSERT_TRUE(HasEnumeration::Populate(value, &enumeration));
+ EXPECT_TRUE(value.Equals(enumeration.ToValue().get()));
}
}
@@ -55,105 +83,105 @@ TEST(JsonSchemaCompilerEnumsTest, ReturnsTwoEnumsCreate) {
TEST(JsonSchemaCompilerEnumsTest, OptionalEnumTypePopulate) {
{
- scoped_ptr<OptionalEnumType> enum_type(new OptionalEnumType());
- scoped_ptr<DictionaryValue> value(new DictionaryValue());
- value->Set("type", Value::CreateStringValue("two"));
- EXPECT_TRUE(OptionalEnumType::Populate(*value, enum_type.get()));
- EXPECT_EQ(OptionalEnumType::TYPE_TWO, enum_type->type);
- EXPECT_TRUE(value->Equals(enum_type->ToValue().get()));
+ OptionalEnumType enum_type;
+ DictionaryValue value;
+ value.Set("type", Value::CreateStringValue("two"));
+ EXPECT_TRUE(OptionalEnumType::Populate(value, &enum_type));
+ EXPECT_EQ(OptionalEnumType::TYPE_TWO, enum_type.type);
+ EXPECT_TRUE(value.Equals(enum_type.ToValue().get()));
}
{
- scoped_ptr<OptionalEnumType> enum_type(new OptionalEnumType());
- scoped_ptr<DictionaryValue> value(new DictionaryValue());
- EXPECT_TRUE(OptionalEnumType::Populate(*value, enum_type.get()));
- EXPECT_EQ(OptionalEnumType::TYPE_NONE, enum_type->type);
- EXPECT_TRUE(value->Equals(enum_type->ToValue().get()));
+ OptionalEnumType enum_type;
+ DictionaryValue value;
+ EXPECT_TRUE(OptionalEnumType::Populate(value, &enum_type));
+ EXPECT_EQ(OptionalEnumType::TYPE_NONE, enum_type.type);
+ EXPECT_TRUE(value.Equals(enum_type.ToValue().get()));
}
{
- scoped_ptr<OptionalEnumType> enum_type(new OptionalEnumType());
- scoped_ptr<DictionaryValue> value(new DictionaryValue());
- value->Set("type", Value::CreateStringValue("invalid"));
- EXPECT_FALSE(OptionalEnumType::Populate(*value, enum_type.get()));
+ OptionalEnumType enum_type;
+ DictionaryValue value;
+ value.Set("type", Value::CreateStringValue("invalid"));
+ EXPECT_FALSE(OptionalEnumType::Populate(value, &enum_type));
}
}
TEST(JsonSchemaCompilerEnumsTest, TakesEnumParamsCreate) {
{
- scoped_ptr<ListValue> params_value(new ListValue());
- params_value->Append(Value::CreateStringValue("baz"));
+ ListValue params_value;
+ params_value.Append(Value::CreateStringValue("baz"));
scoped_ptr<TakesEnum::Params> params(
- TakesEnum::Params::Create(*params_value));
+ TakesEnum::Params::Create(params_value));
EXPECT_TRUE(params.get());
EXPECT_EQ(TakesEnum::Params::STATE_BAZ, params->state);
}
{
- scoped_ptr<ListValue> params_value(new ListValue());
- params_value->Append(Value::CreateStringValue("invalid"));
+ ListValue params_value;
+ params_value.Append(Value::CreateStringValue("invalid"));
scoped_ptr<TakesEnum::Params> params(
- TakesEnum::Params::Create(*params_value));
+ TakesEnum::Params::Create(params_value));
EXPECT_FALSE(params.get());
}
}
TEST(JsonSchemaCompilerEnumsTest, TakesOptionalEnumParamsCreate) {
{
- scoped_ptr<ListValue> params_value(new ListValue());
- params_value->Append(Value::CreateStringValue("baz"));
+ ListValue params_value;
+ params_value.Append(Value::CreateStringValue("baz"));
scoped_ptr<TakesOptionalEnum::Params> params(
- TakesOptionalEnum::Params::Create(*params_value));
+ TakesOptionalEnum::Params::Create(params_value));
EXPECT_TRUE(params.get());
EXPECT_EQ(TakesOptionalEnum::Params::STATE_BAZ, params->state);
}
{
- scoped_ptr<ListValue> params_value(new ListValue());
+ ListValue params_value;
scoped_ptr<TakesOptionalEnum::Params> params(
- TakesOptionalEnum::Params::Create(*params_value));
+ TakesOptionalEnum::Params::Create(params_value));
EXPECT_TRUE(params.get());
EXPECT_EQ(TakesOptionalEnum::Params::STATE_NONE, params->state);
}
{
- scoped_ptr<ListValue> params_value(new ListValue());
- params_value->Append(Value::CreateStringValue("invalid"));
+ ListValue params_value;
+ params_value.Append(Value::CreateStringValue("invalid"));
scoped_ptr<TakesOptionalEnum::Params> params(
- TakesOptionalEnum::Params::Create(*params_value));
+ TakesOptionalEnum::Params::Create(params_value));
EXPECT_FALSE(params.get());
}
}
TEST(JsonSchemaCompilerEnumsTest, TakesMultipleOptionalEnumsParamsCreate) {
{
- scoped_ptr<ListValue> params_value(new ListValue());
- params_value->Append(Value::CreateStringValue("foo"));
- params_value->Append(Value::CreateStringValue("foo"));
+ ListValue params_value;
+ params_value.Append(Value::CreateStringValue("foo"));
+ params_value.Append(Value::CreateStringValue("foo"));
scoped_ptr<TakesMultipleOptionalEnums::Params> params(
- TakesMultipleOptionalEnums::Params::Create(*params_value));
+ TakesMultipleOptionalEnums::Params::Create(params_value));
EXPECT_TRUE(params.get());
EXPECT_EQ(TakesMultipleOptionalEnums::Params::STATE_FOO, params->state);
EXPECT_EQ(TakesMultipleOptionalEnums::Params::TYPE_FOO, params->type);
}
{
- scoped_ptr<ListValue> params_value(new ListValue());
- params_value->Append(Value::CreateStringValue("foo"));
+ ListValue params_value;
+ params_value.Append(Value::CreateStringValue("foo"));
scoped_ptr<TakesMultipleOptionalEnums::Params> params(
- TakesMultipleOptionalEnums::Params::Create(*params_value));
+ TakesMultipleOptionalEnums::Params::Create(params_value));
EXPECT_TRUE(params.get());
EXPECT_EQ(TakesMultipleOptionalEnums::Params::STATE_FOO, params->state);
EXPECT_EQ(TakesMultipleOptionalEnums::Params::TYPE_NONE, params->type);
}
{
- scoped_ptr<ListValue> params_value(new ListValue());
+ ListValue params_value;
scoped_ptr<TakesMultipleOptionalEnums::Params> params(
- TakesMultipleOptionalEnums::Params::Create(*params_value));
+ TakesMultipleOptionalEnums::Params::Create(params_value));
EXPECT_TRUE(params.get());
EXPECT_EQ(TakesMultipleOptionalEnums::Params::STATE_NONE, params->state);
EXPECT_EQ(TakesMultipleOptionalEnums::Params::TYPE_NONE, params->type);
}
{
- scoped_ptr<ListValue> params_value(new ListValue());
- params_value->Append(Value::CreateStringValue("baz"));
- params_value->Append(Value::CreateStringValue("invalid"));
+ ListValue params_value;
+ params_value.Append(Value::CreateStringValue("baz"));
+ params_value.Append(Value::CreateStringValue("invalid"));
scoped_ptr<TakesMultipleOptionalEnums::Params> params(
- TakesMultipleOptionalEnums::Params::Create(*params_value));
+ TakesMultipleOptionalEnums::Params::Create(params_value));
EXPECT_FALSE(params.get());
}
}