Chromium Code Reviews| Index: tools/json_schema_compiler/test/error_generation_unittest.cc |
| diff --git a/tools/json_schema_compiler/test/error_generation_unittest.cc b/tools/json_schema_compiler/test/error_generation_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..0848d47d40c56fda04a4331780f0b7281a889c23 |
| --- /dev/null |
| +++ b/tools/json_schema_compiler/test/error_generation_unittest.cc |
| @@ -0,0 +1,202 @@ |
| +// Copyright 2013 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. |
| + |
| +#include "tools/json_schema_compiler/test/error_generation.h" |
| + |
| +#include "base/json/json_writer.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| +#include "tools/json_schema_compiler/test/test_util.h" |
| + |
| +using namespace test::api::error_generation; |
| +using base::FundamentalValue; |
| +using json_schema_compiler::test_util::Dictionary; |
| +using json_schema_compiler::test_util::List; |
| + |
| +// GenerateTypePopulate errors |
| + |
| +TEST(JsonSchemaCompilerErrorTest, RequiredPropertyPopulate) { |
| + { |
| + scoped_ptr<DictionaryValue> value = Dictionary( |
| + "string", new StringValue("bling")); |
| + EXPECT_TRUE(TestType::Populate(*value, new TestType())); |
| + } |
| + { |
| + scoped_ptr<base::BinaryValue> value(new base::BinaryValue()); |
| + std::string error; |
| + EXPECT_FALSE(TestType::Populate(*value, new TestType(), &error)); |
|
not at google - send to devlin
2013/08/09 18:20:58
(new TestType()) looks leaky... other tests like t
dhnishi (use Chromium)
2013/08/09 20:50:48
Valgrind suggests an 4-16 byte leak for all of tho
|
| + EXPECT_EQ(error, "expected dictionary, got binary"); |
| + } |
| +} |
| + |
| +TEST(JsonSchemaCompilerErrorTest, UnexpectedTypePopulation) { |
| + { |
| + scoped_ptr<base::ListValue> good_value(new base::ListValue()); |
| + EXPECT_TRUE(ChoiceType::Integers::Populate(*good_value, |
| + new ChoiceType::Integers())); |
| + } |
| + { |
| + scoped_ptr<base::BinaryValue> bad_value(new base::BinaryValue()); |
| + std::string error; |
| + EXPECT_FALSE(ChoiceType::Integers::Populate(*bad_value, |
| + new ChoiceType::Integers(), |
| + &error)); |
| + EXPECT_EQ(error, "expected integers or integer, got binary"); |
| + } |
| +} |
| + |
| +// GenerateTypePopulateProperty errors |
| + |
| +TEST(JsonSchemaCompilerErrorTest, TypeIsRequired) { |
| + { |
| + scoped_ptr<DictionaryValue> value = Dictionary( |
| + "integers", new FundamentalValue(5)); |
| + EXPECT_TRUE(ChoiceType::Populate(*value, new ChoiceType())); |
| + } |
| + { |
| + scoped_ptr<base::DictionaryValue> bad_value(new base::DictionaryValue()); |
| + std::string error; |
| + EXPECT_FALSE(ChoiceType::Populate(*bad_value, new ChoiceType(), &error)); |
| + EXPECT_EQ(error, "'integers' is required"); |
| + } |
| +} |
| + |
| +// GenerateParamsCheck errors |
| + |
| +TEST(JsonSchemaCompilerErrorTest, TooManyParameters) { |
| + { |
| + scoped_ptr<base::ListValue> params_value = List( |
| + new FundamentalValue(5)); |
| + EXPECT_TRUE(TestFunction::Params::Create(*params_value)); |
| + } |
| + { |
| + scoped_ptr<base::ListValue> params_value = List( |
| + new FundamentalValue(5), |
| + new FundamentalValue(5)); |
| + std::string error; |
| + EXPECT_FALSE(TestFunction::Params::Create(*params_value, &error)); |
| + EXPECT_EQ(error, "expected 1 arguments, got 2"); |
| + } |
| +} |
| + |
| +// GenerateFunctionParamsCreate errors |
| + |
| +TEST(JsonSchemaCompilerErrorTest, ParamIsRequired) { |
| + { |
| + scoped_ptr<base::ListValue> params_value = List( |
| + new FundamentalValue(5)); |
| + EXPECT_TRUE(TestFunction::Params::Create(*params_value)); |
| + } |
| + { |
| + scoped_ptr<base::ListValue> params_value = List( |
| + Value::CreateNullValue()); |
| + std::string error; |
| + EXPECT_FALSE(TestFunction::Params::Create(*params_value, &error)); |
| + EXPECT_EQ(error, "'num' is required"); |
| + } |
| +} |
| + |
| +// GeneratePopulateVariableFromValue errors |
| + |
| +TEST(JsonSchemaCompilerErrorTest, WrongPropertyValueType) { |
| + { |
| + scoped_ptr<DictionaryValue> value = Dictionary( |
| + "string", new StringValue("yes"); |
| + EXPECT_TRUE(TestType::Populate(*value, new TestType())); |
| + } |
| + { |
| + scoped_ptr<DictionaryValue> value = Dictionary( |
| + "string", new FundamentalValue(1.1)); |
| + std::string error; |
| + EXPECT_FALSE(TestType::Populate(*value, new TestType(), &error)); |
| + EXPECT_EQ(error, "'string': expected std::string, got number"); |
|
not at google - send to devlin
2013/08/09 18:20:58
s/std::string/string/
dhnishi (use Chromium)
2013/08/09 20:50:48
Done. Using the underlying_type now, rather than t
not at google - send to devlin
2013/08/09 21:13:01
Yeah these errors are for displaying to extension
|
| + } |
| +} |
| + |
| +TEST(JsonSchemaCompilerErrorTest, WrongParameterCreationType) { |
| + { |
| + scoped_ptr<base::ListValue> params_value = List( |
| + new StringValue("Yeah!")); |
| + EXPECT_TRUE(TestString::Params::Create(*params_value)); |
| + } |
| + { |
| + scoped_ptr<base::ListValue> params_value = List( |
| + new FundamentalValue(5)); |
| + std::string error; |
| + EXPECT_FALSE(TestTypeInObject::Params::Create(*params_value, &error)); |
| + EXPECT_EQ(error, |
| + "'paramObject': expected dictionary, got integer"); |
| + } |
| +} |
| + |
| +TEST(JsonSchemaCompilerErrorTest, WrongTypeValueType) { |
| + { |
| + scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue()); |
| + EXPECT_TRUE(ObjectType::Populate(*value, new ObjectType())); |
| + } |
| + { |
| + scoped_ptr<DictionaryValue> value = Dictionary( |
| + "otherType", new FundamentalValue(1.1)); |
| + std::string error; |
| + EXPECT_FALSE(ObjectType::Populate(*value, new ObjectType(), &error)); |
| + EXPECT_EQ(error, |
| + "'otherType': expected dictionary, got number"); |
| + } |
| +} |
| + |
| +TEST(JsonSchemaCompilerErrorTest, UnableToPopulateArray) { |
| + { |
| + scoped_ptr<base::ListValue> params_value = List( |
| + new FundamentalValue(5)); |
| + EXPECT_TRUE(ChoiceType::Integers::Populate(*params_value, |
| + new ChoiceType::Integers())); |
| + } |
| + { |
| + scoped_ptr<base::ListValue> params_value = List( |
| + new FundamentalValue(5), |
| + new FundamentalValue(false)); |
| + std::string error; |
| + EXPECT_FALSE(ChoiceType::Integers::Populate(*params_value, |
| + new ChoiceType::Integers(), |
| + &error)); |
| + EXPECT_EQ(error, "unable to populate array 'integers'"); |
| + } |
| +} |
| + |
| +TEST(JsonSchemaCompilerErrorTest, BinaryTypeExpected) { |
| + { |
| + scoped_ptr<DictionaryValue> value = Dictionary( |
| + "data", new base::BinaryValue()); |
| + EXPECT_TRUE(BinaryData::Populate(*value, |
| + new BinaryData())); |
| + } |
| + { |
| + scoped_ptr<DictionaryValue> value = Dictionary( |
| + "data", new FundamentalValue(1.1)); |
| + std::string error; |
| + EXPECT_FALSE(BinaryData::Populate(*value, new BinaryData(), &error)); |
| + EXPECT_EQ(error, "'data': expected binary, got number"); |
| + } |
| +} |
| + |
| +// GenerateStringToEnumConversion errors |
| + |
| +TEST(JsonSchemaCompilerErrorTest, BadEnumValue) { |
| + { |
| + scoped_ptr<DictionaryValue> value = Dictionary( |
| + "enumeration", new StringValue("one")); |
| + EXPECT_TRUE(HasEnumeration::Populate(*value, |
| + new HasEnumeration())); |
| + } |
| + { |
| + scoped_ptr<DictionaryValue> value = Dictionary( |
| + "enumeration", new StringValue("bad sauce")); |
| + std::string error; |
| + EXPECT_FALSE(HasEnumeration::Populate(*value, |
| + new HasEnumeration(), |
| + &error)); |
| + EXPECT_EQ(error, |
| + "'enumeration': expected \"one\" or \"two\" or \"three\", " |
| + "got \"bad sauce\""); |
| + } |
| +} |