diff options
author | calamity@chromium.org <calamity@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-02-21 01:13:07 +0000 |
---|---|---|
committer | calamity@chromium.org <calamity@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-02-21 01:13:07 +0000 |
commit | 712eca0f458b38d5fe47c14932302ad3f14e292d (patch) | |
tree | 905236d46f0a4c471745ead8ad371910b89b5bd0 /tools/json_schema_compiler/cpp_util.py | |
parent | 17f5abff8f422bbfd63bfbb809a86464fd20f7ce (diff) | |
download | chromium_src-712eca0f458b38d5fe47c14932302ad3f14e292d.zip chromium_src-712eca0f458b38d5fe47c14932302ad3f14e292d.tar.gz chromium_src-712eca0f458b38d5fe47c14932302ad3f14e292d.tar.bz2 |
Add tests to tools/json_schema_compiler
Add tests for different json cases by compiling test jsons and running C++
tests against them. Also fixed bugs where tests failed, removed a dead flag and refactored for readability.
BUG=
TEST=
Review URL: http://codereview.chromium.org/9415001
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@122781 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'tools/json_schema_compiler/cpp_util.py')
-rw-r--r-- | tools/json_schema_compiler/cpp_util.py | 79 |
1 files changed, 29 insertions, 50 deletions
diff --git a/tools/json_schema_compiler/cpp_util.py b/tools/json_schema_compiler/cpp_util.py index 8762fa6..1c4575a 100644 --- a/tools/json_schema_compiler/cpp_util.py +++ b/tools/json_schema_compiler/cpp_util.py @@ -26,21 +26,6 @@ def Classname(s): """ return '_'.join([x[0].upper() + x[1:] for x in s.split('.')]) -def CreateFundamentalValue(prop, var): - """Returns the C++ code for creating a value of the given property type - using the given variable. - - var: Fundamental or Fundamental* - """ - if prop.optional: - var = '*' + var - return { - PropertyType.STRING: 'Value::CreateStringValue(%s)', - PropertyType.BOOLEAN: 'Value::CreateBooleanValue(%s)', - PropertyType.INTEGER: 'Value::CreateIntegerValue(%s)', - PropertyType.DOUBLE: 'Value::CreateDoubleValue(%s)', - }[prop.type_] % var - def GetAsFundamentalValue(prop, src, dst): """Returns the C++ code for retrieving a fundamental type from a Value into a variable. @@ -55,60 +40,54 @@ def GetAsFundamentalValue(prop, src, dst): PropertyType.DOUBLE: '%s->GetAsDouble(%s)', }[prop.type_] % (src, dst) -def GetFundamentalValue(prop, src, name, dst): - """Returns the C++ code for retrieving a fundamental type from a - DictionaryValue into a variable. - - src: DictionaryValue* - name: key - dst: Property* +def GetValueType(prop): + """Returns the Value::Type corresponding to the model.PropertyType. """ return { - PropertyType.STRING: '%s->GetString("%s", %s)', - PropertyType.BOOLEAN: '%s->GetBoolean("%s", %s)', - PropertyType.INTEGER: '%s->GetInteger("%s", %s)', - PropertyType.DOUBLE: '%s->GetDouble("%s", %s)', - }[prop.type_] % (src, name, dst) + PropertyType.STRING: 'Value::TYPE_STRING', + PropertyType.INTEGER: 'Value::TYPE_INTEGER', + PropertyType.BOOLEAN: 'Value::TYPE_BOOLEAN', + PropertyType.DOUBLE: 'Value::TYPE_DOUBLE', + PropertyType.REF: 'Value::TYPE_DICTIONARY', + PropertyType.OBJECT: 'Value::TYPE_DICTIONARY', + PropertyType.ARRAY: 'Value::TYPE_LIST' + }[prop.type_] + def CreateValueFromSingleProperty(prop, var): """Creates a Value given a single property. Use for everything except PropertyType.ARRAY. - var: raw value + var: variable or variable* """ - if prop.type_ == PropertyType.REF or prop.type_ == PropertyType.OBJECT: + if prop.type_ in (PropertyType.REF, PropertyType.OBJECT): if prop.optional: return '%s->ToValue().release()' % var else: return '%s.ToValue().release()' % var elif prop.type_.is_fundamental: - return CreateFundamentalValue(prop, var) + if prop.optional: + var = '*' + var + return { + PropertyType.STRING: 'Value::CreateStringValue(%s)', + PropertyType.BOOLEAN: 'Value::CreateBooleanValue(%s)', + PropertyType.INTEGER: 'Value::CreateIntegerValue(%s)', + PropertyType.DOUBLE: 'Value::CreateDoubleValue(%s)', + }[prop.type_] % var else: raise NotImplementedError('Conversion of single %s to Value not implemented' % repr(prop.type_)) -def GetValueFromList(prop, src, index, dst): - """Returns the C++ code for retrieving a fundamental type from a - DictionaryValue into a variable. - - src: ListValue& - index: int - dst: Property* +def GetParameterDeclaration(param, type_): + """Gets a const parameter declaration of a given model.Property and its C++ + type. """ - return { - PropertyType.REF: '%s.GetDictionary(%d, %s)', - PropertyType.STRING: '%s.GetString(%d, %s)', - PropertyType.BOOLEAN: '%s.GetBoolean(%d, %s)', - PropertyType.INTEGER: '%s.GetInteger(%d, %s)', - PropertyType.DOUBLE: '%s.GetDouble(%d, %s)', - }[prop.type_] % (src, index, dst) - -def GetConstParameterDeclaration(param, type_generator): - if param.type_.is_fundamental: - arg = 'const %(type)s %(name)s' + if param.type_ in (PropertyType.REF, PropertyType.OBJECT, PropertyType.ARRAY, + PropertyType.STRING): + arg = '%(type)s& %(name)s' else: - arg = 'const %(type)s& %(name)s' + arg = '%(type)s %(name)s' return arg % { - 'type': type_generator.GetType(param, wrap_optional=True), + 'type': type_, 'name': param.unix_name, } |