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

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: Templates. 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..ab0be820e79dac712b79ccdced464ffff1957c7a
--- /dev/null
+++ b/tools/json_schema_compiler/test/error_generation_unittest.cc
@@ -0,0 +1,189 @@
+// 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;
+
+template <typename T>
+std::string GetPopulateError(const base::Value& value) {
+ std::string error;
+ T test_type;
+ T::Populate(value, &test_type, &error);
+ return error;
+}
+
+// GenerateTypePopulate errors
+
+TEST(JsonSchemaCompilerErrorTest, RequiredPropertyPopulate) {
+ {
+ scoped_ptr<DictionaryValue> value = Dictionary(
+ "string", new StringValue("bling"));
+ EXPECT_EQ(GetPopulateError<TestType>(*value), "");
+ }
+ {
+ scoped_ptr<base::BinaryValue> value(new base::BinaryValue());
+ EXPECT_EQ(GetPopulateError<TestType>(*value),
+ "expected dictionary, got binary");
not at google - send to devlin 2013/08/09 21:14:02 the usual idiom is to put the expected value first
+ }
+}
+
+TEST(JsonSchemaCompilerErrorTest, UnexpectedTypePopulation) {
+ {
+ scoped_ptr<base::ListValue> value(new base::ListValue());
+ EXPECT_EQ(GetPopulateError<ChoiceType::Integers>(*value), "");
+ }
+ {
+ scoped_ptr<base::BinaryValue> value(new base::BinaryValue());
+ EXPECT_EQ(GetPopulateError<ChoiceType::Integers>(*value),
+ "expected integers or integer, got binary");
+ }
+}
+
+// GenerateTypePopulateProperty errors
+
+TEST(JsonSchemaCompilerErrorTest, TypeIsRequired) {
+ {
+ scoped_ptr<DictionaryValue> value = Dictionary(
+ "integers", new FundamentalValue(5));
+ EXPECT_EQ(GetPopulateError<ChoiceType>(*value), "");
+ }
+ {
+ scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue());
+ EXPECT_EQ(GetPopulateError<ChoiceType>(*value), "'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_EQ(GetPopulateError<TestType>(*value), "");
+ }
+ {
+ scoped_ptr<DictionaryValue> value = Dictionary(
+ "string", new FundamentalValue(1.1));
+ EXPECT_EQ(GetPopulateError<TestType>(*value),
+ "'string': expected string, got number");
+ }
+}
+
+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_EQ(GetPopulateError<ObjectType>(*value), "");
+ }
+ {
+ scoped_ptr<DictionaryValue> value = Dictionary(
+ "otherType", new FundamentalValue(1.1));
+ EXPECT_EQ(GetPopulateError<ObjectType>(*value),
+ "'otherType': expected dictionary, got number");
+ }
+}
+
+TEST(JsonSchemaCompilerErrorTest, UnableToPopulateArray) {
+ {
+ scoped_ptr<base::ListValue> params_value = List(
+ new FundamentalValue(5));
+ EXPECT_EQ(GetPopulateError<ChoiceType::Integers>(*params_value), "");
+ }
+ {
+ scoped_ptr<base::ListValue> params_value = List(
+ new FundamentalValue(5),
+ new FundamentalValue(false));
+ EXPECT_EQ(GetPopulateError<ChoiceType::Integers>(*params_value),
+ "unable to populate array 'integers'");
+ }
+}
+
+TEST(JsonSchemaCompilerErrorTest, BinaryTypeExpected) {
+ {
+ scoped_ptr<DictionaryValue> value = Dictionary(
+ "data", new base::BinaryValue());
+ EXPECT_EQ(GetPopulateError<BinaryData>(*value), "");
+ }
+ {
+ scoped_ptr<DictionaryValue> value = Dictionary(
+ "data", new FundamentalValue(1.1));
+ EXPECT_EQ(GetPopulateError<BinaryData>(*value),
+ "'data': expected binary, got number");
+ }
+}
+
+// GenerateStringToEnumConversion errors
+
+TEST(JsonSchemaCompilerErrorTest, BadEnumValue) {
+ {
+ scoped_ptr<DictionaryValue> value = Dictionary(
+ "enumeration", new StringValue("one"));
+ EXPECT_EQ(GetPopulateError<HasEnumeration>(*value), "");
+ }
+ {
+ scoped_ptr<DictionaryValue> value = Dictionary(
+ "enumeration", new StringValue("bad sauce"));
+ EXPECT_EQ(GetPopulateError<HasEnumeration>(*value),
+ "'enumeration': expected \"one\" or \"two\" or \"three\", "
+ "got \"bad sauce\"");
+ }
+}
« 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