Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(21)

Unified Diff: tools/json_schema_compiler/test/error_generation_unittest.cc

Issue 22228002: Add optional schema compiler error messages (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Unit tests! Created 7 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..1cde953a1f72599bd6a464d93010cffdbea6f552
--- /dev/null
+++ b/tools/json_schema_compiler/test/error_generation_unittest.cc
@@ -0,0 +1,233 @@
+// 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"
+
+using namespace test::api::error_generation;
+
+// GenerateTypePopulate errors
+
+TEST(JsonSchemaCompilerErrorTest, RequiredPropertyPopulate) {
+ {
+ scoped_ptr<DictionaryValue> value(new DictionaryValue());
+ value->SetWithoutPathExpansion("string", Value::CreateStringValue("bling"));
not at google - send to devlin 2013/08/09 00:08:49 there is a utility in test_util.h for this sort of
dhnishi (use Chromium) 2013/08/09 17:59:50 You are correct. Just found bug 160596 which is al
+ scoped_ptr<TestType> testType(new TestType());
not at google - send to devlin 2013/08/09 00:08:49 test_type, and also, just declare on stack.
dhnishi (use Chromium) 2013/08/09 17:59:50 Done.
+ std::string error;
+ EXPECT_TRUE(TestType::Populate(*value, testType.get(), &error));
+ }
+ {
+ scoped_ptr<base::BinaryValue> value(new base::BinaryValue());
+ scoped_ptr<TestType> testType(new TestType());
+ std::string error;
+ EXPECT_FALSE(TestType::Populate(*value, testType.get(), &error));
+ EXPECT_FALSE(error.compare("expected dictionary, got binary"));
not at google - send to devlin 2013/08/09 00:08:49 EXPECT_EQ is better, gives you nice test messages.
dhnishi (use Chromium) 2013/08/09 17:59:50 Done.
+ }
+}
+
+TEST(JsonSchemaCompilerErrorTest, UnexpectedTypePopulation) {
+ {
+ scoped_ptr<base::ListValue> good_value(new base::ListValue());
+ scoped_ptr<ChoiceType::Integers> choice_type(new ChoiceType::Integers());
+ std::string error;
+ EXPECT_TRUE(ChoiceType::Integers::Populate(*good_value,
+ choice_type.get(),
+ &error));
+ }
+ {
+ scoped_ptr<base::BinaryValue> bad_value(new base::BinaryValue());
+ scoped_ptr<ChoiceType::Integers> choice_type(new ChoiceType::Integers());
+ std::string error;
+ EXPECT_FALSE(ChoiceType::Integers::Populate(*bad_value,
+ choice_type.get(),
+ &error));
+ EXPECT_FALSE(error.compare("unexpected type, got binary"));
not at google - send to devlin 2013/08/09 00:08:49 this does look a bit weird, and we should be able
dhnishi (use Chromium) 2013/08/09 17:59:50 It formats arrays by just pluralizing it, but I've
+ }
+}
+
+// GenerateTypePopulateProperty errors
+
+TEST(JsonSchemaCompilerErrorTest, TypeIsRequired) {
+ {
+ scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue());
+ value->SetWithoutPathExpansion("integers", Value::CreateIntegerValue(5));
+ scoped_ptr<ChoiceType> choice_type(new ChoiceType());
+ std::string error;
+ EXPECT_TRUE(ChoiceType::Populate(*value, choice_type.get(), &error));
+ }
+ {
+ scoped_ptr<base::DictionaryValue> bad_value(new base::DictionaryValue());
+ scoped_ptr<ChoiceType> choice_type(new ChoiceType());
+ std::string error;
+ EXPECT_FALSE(ChoiceType::Populate(*bad_value, choice_type.get(), &error));
+ EXPECT_FALSE(error.compare("'integers' is required"));
+ }
+}
+
+// GenerateParamsCheck errors
+
+TEST(JsonSchemaCompilerErrorTest, TooManyParameters) {
+ {
+ scoped_ptr<ListValue> params_value(new ListValue());
+ params_value->Append(Value::CreateIntegerValue(5));
+ std::string error;
+ EXPECT_TRUE(TestFunction::Params::Create(*params_value, &error));
+ }
+ {
+ scoped_ptr<ListValue> params_value(new ListValue());
+ params_value->Append(Value::CreateIntegerValue(5));
+ params_value->Append(Value::CreateIntegerValue(5));
not at google - send to devlin 2013/08/09 00:08:49 and here (and above, etc): scoped_ptr<ListValue>
dhnishi (use Chromium) 2013/08/09 17:59:50 Done.
+ std::string error;
+ EXPECT_FALSE(TestFunction::Params::Create(*params_value, &error));
+ EXPECT_FALSE(error.compare("expected 1 arguments, got 2"));
+ }
+}
+
+// GenerateFunctionParamsCreate errors
+
+TEST(JsonSchemaCompilerErrorTest, ParamIsRequired) {
+ {
+ scoped_ptr<ListValue> params_value(new ListValue());
+ params_value->Append(Value::CreateIntegerValue(5));
+ std::string error;
+ EXPECT_TRUE(TestFunction::Params::Create(*params_value, &error));
+ }
+ {
+ scoped_ptr<ListValue> params_value(new ListValue());
+ params_value->Append(Value::CreateNullValue());
+ std::string error;
+ EXPECT_FALSE(TestFunction::Params::Create(*params_value, &error));
+ EXPECT_FALSE(error.compare("'num' is required"));
+ }
+}
+
+// GeneratePopulateVariableFromValue errors
+
+TEST(JsonSchemaCompilerErrorTest, WrongPropertyValueType) {
+ {
+ scoped_ptr<DictionaryValue> value(new DictionaryValue());
+ value->SetWithoutPathExpansion("string", Value::CreateStringValue("Yes"));
+ scoped_ptr<TestType> testType(new TestType());
+ std::string error;
+ EXPECT_TRUE(TestType::Populate(*value, testType.get(), &error));
+ }
+ {
+ scoped_ptr<DictionaryValue> value(new DictionaryValue());
+ value->SetWithoutPathExpansion("string", Value::CreateDoubleValue(1.1));
+ scoped_ptr<TestType> testType(new TestType());
+ std::string error;
+ EXPECT_FALSE(TestType::Populate(*value, testType.get(), &error));
+ EXPECT_FALSE(error.compare("'string': expected std::string, got number"));
+ }
+}
+
+TEST(JsonSchemaCompilerErrorTest, WrongParameterCreationType) {
+ {
+ scoped_ptr<ListValue> params_value(new ListValue());
+ params_value->Append(Value::CreateIntegerValue(5));
+ std::string error;
+ EXPECT_FALSE(TestString::Params::Create(*params_value, &error));
+ EXPECT_FALSE(error.compare("'str': expected std::string, got integer"));
+ }
+ {
+ scoped_ptr<ListValue> params_value(new ListValue());
+ params_value->Append(Value::CreateIntegerValue(5));
+ std::string error;
+ EXPECT_FALSE(TestTypeInObject::Params::Create(*params_value, &error));
+ EXPECT_FALSE(error.compare(
+ "'paramObject': expected dictionary, got integer"));
+ }
+}
+
+TEST(JsonSchemaCompilerErrorTest, WrongTypeValueType) {
+ {
+ scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue());
+ scoped_ptr<ObjectType> object_type(new ObjectType());
+ std::string error;
+ EXPECT_TRUE(ObjectType::Populate(*value, object_type.get(), &error));
+ }
+ {
+ scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue());
+ scoped_ptr<ObjectType> object_type(new ObjectType());
+ std::string error;
+ value->SetWithoutPathExpansion("otherType", Value::CreateDoubleValue(1.1));
+ EXPECT_FALSE(ObjectType::Populate(*value, object_type.get(), &error));
+ EXPECT_FALSE(error.compare(
+ "'otherType': expected dictionary, got number"));
+ }
+}
+
+TEST(JsonSchemaCompilerErrorTest, UnableToPopulateArray) {
+ {
+ scoped_ptr<ListValue> params_value(new ListValue());
+ params_value->Append(Value::CreateIntegerValue(5));
+ params_value->Append(Value::CreateBooleanValue(6));
not at google - send to devlin 2013/08/09 00:08:49 6? that's just going to get coerced into true. Doe
dhnishi (use Chromium) 2013/08/09 17:59:50 Whoops. I originally was adding an Integer value t
+ scoped_ptr<ChoiceType::Integers> choice_type(new ChoiceType::Integers());
+ std::string error;
+ EXPECT_FALSE(ChoiceType::Integers::Populate(*params_value,
+ choice_type.get(),
+ &error));
+ EXPECT_FALSE(error.compare("unable to populate array 'integers'"));
not at google - send to devlin 2013/08/09 00:08:49 how hard would it be for this to say like "'intege
dhnishi (use Chromium) 2013/08/09 17:59:50 The generated code itself doesn't have enough info
+ }
+ {
+ scoped_ptr<ListValue> params_value(new ListValue());
+ params_value->Append(Value::CreateIntegerValue(5));
+ params_value->Append(Value::CreateBooleanValue(false));
+ scoped_ptr<ChoiceType::Integers> choice_type(new ChoiceType::Integers());
+ std::string error;
+ EXPECT_FALSE(ChoiceType::Integers::Populate(*params_value,
+ choice_type.get(),
+ &error));
+ EXPECT_FALSE(error.compare("unable to populate array 'integers'"));
+ }
+}
+
+TEST(JsonSchemaCompilerErrorTest, BinaryTypeExpected) {
+ {
+ base::DictionaryValue* value = new base::DictionaryValue();
+ scoped_ptr<BinaryData> binary_data(new BinaryData());
+ scoped_ptr<base::BinaryValue> test_value(new base::BinaryValue());
+ std::string error;
+ value->SetWithoutPathExpansion("data", test_value.get());
+ EXPECT_TRUE(BinaryData::Populate(*value,
+ binary_data.get(),
+ &error));
+ }
+ {
+ base::DictionaryValue* value = new base::DictionaryValue();
+ scoped_ptr<BinaryData> binary_data(new BinaryData());
+ std::string error;
+ value->SetWithoutPathExpansion("data", Value::CreateDoubleValue(1.1));
+ EXPECT_FALSE(BinaryData::Populate(*value, binary_data.get(), &error));
+ EXPECT_FALSE(error.compare("'data': expected BinaryValue, got number"));
not at google - send to devlin 2013/08/09 00:08:49 s/BinaryValue/binary/
dhnishi (use Chromium) 2013/08/09 17:59:50 Done.
+ }
+}
+
+// GenerateStringToEnumConversion errors
+
+TEST(JsonSchemaCompilerErrorTest, BadEnumValue) {
+ {
+ scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue());
+ scoped_ptr<HasEnumeration> hasEnumeration(new HasEnumeration());
not at google - send to devlin 2013/08/09 00:08:49 has_enumeration
dhnishi (use Chromium) 2013/08/09 17:59:50 In-lined.
+ std::string error;
+ value->SetWithoutPathExpansion("enumeration",
+ Value::CreateStringValue("one"));
+ EXPECT_TRUE(HasEnumeration::Populate(*value,
+ hasEnumeration.get(),
+ &error));
+ }
+ {
+ scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue());
+ scoped_ptr<HasEnumeration> hasEnumeration(new HasEnumeration());
+ std::string error;
+ value->SetWithoutPathExpansion("enumeration",
+ Value::CreateStringValue("bad_sauce"));
+ EXPECT_FALSE(HasEnumeration::Populate(*value,
+ hasEnumeration.get(),
+ &error));
+ EXPECT_FALSE(error.compare("got bad enum value from 'enumeration'"));
not at google - send to devlin 2013/08/09 00:08:49 how about 'enumeration': expected "one" or "two"
dhnishi (use Chromium) 2013/08/09 17:59:50 Done.
+ }
+}
« no previous file with comments | « tools/json_schema_compiler/test/error_generation.json ('k') | tools/json_schema_compiler/test/json_schema_compiler_tests.gyp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698