1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "tools/json_schema_compiler/test/error_generation.h"
7 #include "base/json/json_writer.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "testing/gtest/include/gtest/gtest.h"
10 #include "tools/json_schema_compiler/test/test_util.h"
12 using namespace test::api::error_generation;
13 using base::FundamentalValue;
14 using json_schema_compiler::test_util::Dictionary;
15 using json_schema_compiler::test_util::List;
18 base::string16 GetPopulateError(const base::Value& value) {
21 T::Populate(value, &test_type, &error);
25 testing::AssertionResult EqualsUtf16(const std::string& expected,
26 const base::string16& actual) {
27 if (ASCIIToUTF16(expected) != actual)
28 return testing::AssertionFailure() << expected << " != " << actual;
29 return testing::AssertionSuccess();
32 // GenerateTypePopulate errors
34 TEST(JsonSchemaCompilerErrorTest, RequiredPropertyPopulate) {
36 scoped_ptr<DictionaryValue> value = Dictionary(
37 "string", new StringValue("bling"));
38 EXPECT_TRUE(EqualsUtf16("", GetPopulateError<TestType>(*value)));
41 scoped_ptr<base::BinaryValue> value(new base::BinaryValue());
42 EXPECT_TRUE(EqualsUtf16("expected dictionary, got binary",
43 GetPopulateError<TestType>(*value)));
47 TEST(JsonSchemaCompilerErrorTest, UnexpectedTypePopulation) {
49 scoped_ptr<base::ListValue> value(new base::ListValue());
50 EXPECT_TRUE(EqualsUtf16("",
51 GetPopulateError<ChoiceType::Integers>(*value)));
54 scoped_ptr<base::BinaryValue> value(new base::BinaryValue());
55 EXPECT_TRUE(EqualsUtf16("expected integers or integer, got binary",
56 GetPopulateError<ChoiceType::Integers>(*value)));
60 // GenerateTypePopulateProperty errors
62 TEST(JsonSchemaCompilerErrorTest, TypeIsRequired) {
64 scoped_ptr<DictionaryValue> value = Dictionary(
65 "integers", new FundamentalValue(5));
66 EXPECT_TRUE(EqualsUtf16("", GetPopulateError<ChoiceType>(*value)));
69 scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue());
70 EXPECT_TRUE(EqualsUtf16("'integers' is required",
71 GetPopulateError<ChoiceType>(*value)));
75 // GenerateParamsCheck errors
77 TEST(JsonSchemaCompilerErrorTest, TooManyParameters) {
79 scoped_ptr<base::ListValue> params_value = List(
80 new FundamentalValue(5));
81 EXPECT_TRUE(TestFunction::Params::Create(*params_value));
84 scoped_ptr<base::ListValue> params_value = List(
85 new FundamentalValue(5),
86 new FundamentalValue(5));
88 EXPECT_FALSE(TestFunction::Params::Create(*params_value, &error));
89 EXPECT_TRUE(EqualsUtf16("expected 1 arguments, got 2", error));
93 // GenerateFunctionParamsCreate errors
95 TEST(JsonSchemaCompilerErrorTest, ParamIsRequired) {
97 scoped_ptr<base::ListValue> params_value = List(
98 new FundamentalValue(5));
99 EXPECT_TRUE(TestFunction::Params::Create(*params_value));
102 scoped_ptr<base::ListValue> params_value = List(
103 Value::CreateNullValue());
104 base::string16 error;
105 EXPECT_FALSE(TestFunction::Params::Create(*params_value, &error));
106 EXPECT_TRUE(EqualsUtf16("'num' is required", error));
110 // GeneratePopulateVariableFromValue errors
112 TEST(JsonSchemaCompilerErrorTest, WrongPropertyValueType) {
114 scoped_ptr<DictionaryValue> value = Dictionary(
115 "string", new StringValue("yes"));
116 EXPECT_TRUE(EqualsUtf16("", GetPopulateError<TestType>(*value)));
119 scoped_ptr<DictionaryValue> value = Dictionary(
120 "string", new FundamentalValue(1.1));
121 EXPECT_TRUE(EqualsUtf16("'string': expected string, got number",
122 GetPopulateError<TestType>(*value)));
126 TEST(JsonSchemaCompilerErrorTest, WrongParameterCreationType) {
128 scoped_ptr<base::ListValue> params_value = List(
129 new StringValue("Yeah!"));
130 EXPECT_TRUE(TestString::Params::Create(*params_value));
133 scoped_ptr<base::ListValue> params_value = List(
134 new FundamentalValue(5));
135 base::string16 error;
136 EXPECT_FALSE(TestTypeInObject::Params::Create(*params_value, &error));
137 EXPECT_TRUE(EqualsUtf16("'paramObject': expected dictionary, got integer",
142 TEST(JsonSchemaCompilerErrorTest, WrongTypeValueType) {
144 scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue());
145 EXPECT_TRUE(EqualsUtf16("", GetPopulateError<ObjectType>(*value)));
148 scoped_ptr<DictionaryValue> value = Dictionary(
149 "otherType", new FundamentalValue(1.1));
150 EXPECT_TRUE(EqualsUtf16("'otherType': expected dictionary, got number",
151 GetPopulateError<ObjectType>(*value)));
155 TEST(JsonSchemaCompilerErrorTest, UnableToPopulateArray) {
157 scoped_ptr<base::ListValue> params_value = List(
158 new FundamentalValue(5));
159 EXPECT_TRUE(EqualsUtf16("",
160 GetPopulateError<ChoiceType::Integers>(*params_value)));
163 scoped_ptr<base::ListValue> params_value = List(
164 new FundamentalValue(5),
165 new FundamentalValue(false));
166 EXPECT_TRUE(EqualsUtf16("unable to populate array 'integers'",
167 GetPopulateError<ChoiceType::Integers>(*params_value)));
171 TEST(JsonSchemaCompilerErrorTest, BinaryTypeExpected) {
173 scoped_ptr<DictionaryValue> value = Dictionary(
174 "data", new base::BinaryValue());
175 EXPECT_TRUE(EqualsUtf16("", GetPopulateError<BinaryData>(*value)));
178 scoped_ptr<DictionaryValue> value = Dictionary(
179 "data", new FundamentalValue(1.1));
180 EXPECT_TRUE(EqualsUtf16("'data': expected binary, got number",
181 GetPopulateError<BinaryData>(*value)));
185 TEST(JsonSchemaCompilerErrorTest, ListExpected) {
187 scoped_ptr<DictionaryValue> value = Dictionary(
188 "TheArray", new base::ListValue());
189 EXPECT_TRUE(EqualsUtf16("", GetPopulateError<ArrayObject>(*value)));
192 scoped_ptr<DictionaryValue> value = Dictionary(
193 "TheArray", new FundamentalValue(5));
194 EXPECT_TRUE(EqualsUtf16("'TheArray': expected list, got integer",
195 GetPopulateError<ArrayObject>(*value)));
199 // GenerateStringToEnumConversion errors
201 TEST(JsonSchemaCompilerErrorTest, BadEnumValue) {
203 scoped_ptr<DictionaryValue> value = Dictionary(
204 "enumeration", new StringValue("one"));
205 EXPECT_TRUE(EqualsUtf16("", GetPopulateError<HasEnumeration>(*value)));
208 scoped_ptr<DictionaryValue> value = Dictionary(
209 "enumeration", new StringValue("bad sauce"));
210 EXPECT_TRUE(EqualsUtf16("'enumeration': expected \"one\" or \"two\" "
211 "or \"three\", got \"bad sauce\"",
212 GetPopulateError<HasEnumeration>(*value)));