Roll src/third_party/WebKit 6f84130:7353389 (svn 184386:184391)
[chromium-blink-merge.git] / tools / json_schema_compiler / test / error_generation_unittest.cc
blob42f95760a64825d13a79b4477ca6191bda622335
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;
17 template <typename T>
18 base::string16 GetPopulateError(const base::Value& value) {
19 base::string16 error;
20 T test_type;
21 T::Populate(value, &test_type, &error);
22 return error;
25 testing::AssertionResult EqualsUtf16(const std::string& expected,
26 const base::string16& actual) {
27 if (base::ASCIIToUTF16(expected) != actual)
28 return testing::AssertionFailure() << expected << " != " << actual;
29 return testing::AssertionSuccess();
32 // GenerateTypePopulate errors
34 TEST(JsonSchemaCompilerErrorTest, RequiredPropertyPopulate) {
36 scoped_ptr<base::DictionaryValue> value = Dictionary(
37 "string", new base::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<base::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 base::string16 error;
82 EXPECT_TRUE(TestFunction::Params::Create(*params_value, &error));
85 scoped_ptr<base::ListValue> params_value = List(
86 new FundamentalValue(5),
87 new FundamentalValue(5));
88 base::string16 error;
89 EXPECT_FALSE(TestFunction::Params::Create(*params_value, &error));
90 EXPECT_TRUE(EqualsUtf16("expected 1 arguments, got 2", error));
94 // GenerateFunctionParamsCreate errors
96 TEST(JsonSchemaCompilerErrorTest, ParamIsRequired) {
98 scoped_ptr<base::ListValue> params_value = List(
99 new FundamentalValue(5));
100 base::string16 error;
101 EXPECT_TRUE(TestFunction::Params::Create(*params_value, &error));
104 scoped_ptr<base::ListValue> params_value = List(
105 base::Value::CreateNullValue());
106 base::string16 error;
107 EXPECT_FALSE(TestFunction::Params::Create(*params_value, &error));
108 EXPECT_TRUE(EqualsUtf16("'num' is required", error));
112 // GeneratePopulateVariableFromValue errors
114 TEST(JsonSchemaCompilerErrorTest, WrongPropertyValueType) {
116 scoped_ptr<base::DictionaryValue> value = Dictionary(
117 "string", new base::StringValue("yes"));
118 EXPECT_TRUE(EqualsUtf16("", GetPopulateError<TestType>(*value)));
121 scoped_ptr<base::DictionaryValue> value = Dictionary(
122 "string", new FundamentalValue(1.1));
123 EXPECT_TRUE(EqualsUtf16("'string': expected string, got number",
124 GetPopulateError<TestType>(*value)));
128 TEST(JsonSchemaCompilerErrorTest, WrongParameterCreationType) {
130 base::string16 error;
131 scoped_ptr<base::ListValue> params_value = List(
132 new base::StringValue("Yeah!"));
133 EXPECT_TRUE(TestString::Params::Create(*params_value, &error));
136 scoped_ptr<base::ListValue> params_value = List(
137 new FundamentalValue(5));
138 base::string16 error;
139 EXPECT_FALSE(TestTypeInObject::Params::Create(*params_value, &error));
140 EXPECT_TRUE(EqualsUtf16("'paramObject': expected dictionary, got integer",
141 error));
145 TEST(JsonSchemaCompilerErrorTest, WrongTypeValueType) {
147 scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue());
148 EXPECT_TRUE(EqualsUtf16("", GetPopulateError<ObjectType>(*value)));
151 scoped_ptr<base::DictionaryValue> value = Dictionary(
152 "otherType", new FundamentalValue(1.1));
153 ObjectType out;
154 base::string16 error;
155 EXPECT_TRUE(ObjectType::Populate(*value, &out, &error));
156 EXPECT_TRUE(EqualsUtf16("'otherType': expected dictionary, got number",
157 error));
158 EXPECT_EQ(NULL, out.other_type.get());
162 TEST(JsonSchemaCompilerErrorTest, UnableToPopulateArray) {
164 scoped_ptr<base::ListValue> params_value = List(
165 new FundamentalValue(5));
166 EXPECT_TRUE(EqualsUtf16("",
167 GetPopulateError<ChoiceType::Integers>(*params_value)));
170 scoped_ptr<base::ListValue> params_value = List(
171 new FundamentalValue(5),
172 new FundamentalValue(false));
173 EXPECT_TRUE(EqualsUtf16("unable to populate array 'integers'",
174 GetPopulateError<ChoiceType::Integers>(*params_value)));
178 TEST(JsonSchemaCompilerErrorTest, BinaryTypeExpected) {
180 scoped_ptr<base::DictionaryValue> value = Dictionary(
181 "data", new base::BinaryValue());
182 EXPECT_TRUE(EqualsUtf16("", GetPopulateError<BinaryData>(*value)));
185 scoped_ptr<base::DictionaryValue> value = Dictionary(
186 "data", new FundamentalValue(1.1));
187 EXPECT_TRUE(EqualsUtf16("'data': expected binary, got number",
188 GetPopulateError<BinaryData>(*value)));
192 TEST(JsonSchemaCompilerErrorTest, ListExpected) {
194 scoped_ptr<base::DictionaryValue> value = Dictionary(
195 "TheArray", new base::ListValue());
196 EXPECT_TRUE(EqualsUtf16("", GetPopulateError<ArrayObject>(*value)));
199 scoped_ptr<base::DictionaryValue> value = Dictionary(
200 "TheArray", new FundamentalValue(5));
201 EXPECT_TRUE(EqualsUtf16("'TheArray': expected list, got integer",
202 GetPopulateError<ArrayObject>(*value)));
206 // GenerateStringToEnumConversion errors
208 TEST(JsonSchemaCompilerErrorTest, BadEnumValue) {
210 scoped_ptr<base::DictionaryValue> value = Dictionary(
211 "enumeration", new base::StringValue("one"));
212 EXPECT_TRUE(EqualsUtf16("", GetPopulateError<HasEnumeration>(*value)));
215 scoped_ptr<base::DictionaryValue> value = Dictionary(
216 "enumeration", new base::StringValue("bad sauce"));
217 EXPECT_TRUE(EqualsUtf16("'Enumeration': expected \"one\" or \"two\" "
218 "or \"three\", got \"bad sauce\"",
219 GetPopulateError<HasEnumeration>(*value)));
223 // Warn but don't fail out errors
225 TEST(JsonSchemaCompilerErrorTest, WarnOnOptionalFailure) {
227 scoped_ptr<base::DictionaryValue> value = Dictionary(
228 "string", new base::StringValue("bling"));
229 EXPECT_TRUE(EqualsUtf16("", GetPopulateError<OptionalTestType>(*value)));
232 scoped_ptr<base::DictionaryValue> value = Dictionary(
233 "string", new base::FundamentalValue(1));
235 OptionalTestType out;
236 base::string16 error;
237 EXPECT_TRUE(OptionalTestType::Populate(*value, &out, &error));
238 EXPECT_TRUE(EqualsUtf16("'string': expected string, got integer",
239 error));
240 EXPECT_EQ(NULL, out.string.get());
244 TEST(JsonSchemaCompilerErrorTest, OptionalBinaryTypeFailure) {
246 scoped_ptr<base::DictionaryValue> value = Dictionary(
247 "data", new base::BinaryValue());
248 EXPECT_TRUE(EqualsUtf16("", GetPopulateError<OptionalBinaryData>(*value)));
251 // There's a bug with silent failures if the key doesn't exist.
252 scoped_ptr<base::DictionaryValue> value = Dictionary("data",
253 new base::FundamentalValue(1));
255 OptionalBinaryData out;
256 base::string16 error;
257 EXPECT_TRUE(OptionalBinaryData::Populate(*value, &out, &error));
258 EXPECT_TRUE(EqualsUtf16("'data': expected binary, got integer",
259 error));
260 EXPECT_EQ(NULL, out.data.get());
264 TEST(JsonSchemaCompilerErrorTest, OptionalArrayTypeFailure) {
266 scoped_ptr<base::DictionaryValue> value = Dictionary(
267 "TheArray", new base::ListValue());
268 EXPECT_TRUE(EqualsUtf16("", GetPopulateError<ArrayObject>(*value)));
271 scoped_ptr<base::DictionaryValue> value = Dictionary(
272 "TheArray", new FundamentalValue(5));
273 ArrayObject out;
274 base::string16 error;
275 EXPECT_TRUE(ArrayObject::Populate(*value, &out, &error));
276 EXPECT_TRUE(EqualsUtf16("'TheArray': expected list, got integer",
277 error));
278 EXPECT_EQ(NULL, out.the_array.get());
282 TEST(JsonSchemaCompilerErrorTest, OptionalUnableToPopulateArray) {
284 scoped_ptr<base::ListValue> params_value = List(
285 new FundamentalValue(5));
286 EXPECT_TRUE(EqualsUtf16("",
287 GetPopulateError<OptionalChoiceType::Integers>(*params_value)));
290 scoped_ptr<base::ListValue> params_value = List(
291 new FundamentalValue(5),
292 new FundamentalValue(false));
293 OptionalChoiceType::Integers out;
294 base::string16 error;
295 EXPECT_TRUE(OptionalChoiceType::Integers::Populate(*params_value, &out,
296 &error));
297 EXPECT_TRUE(EqualsUtf16("unable to populate array 'integers'",
298 error));
299 EXPECT_EQ(NULL, out.as_integer.get());
303 TEST(JsonSchemaCompilerErrorTest, MultiplePopulationErrors) {
306 scoped_ptr<base::DictionaryValue> value = Dictionary(
307 "TheArray", new FundamentalValue(5));
308 ArrayObject out;
309 base::string16 error;
310 EXPECT_TRUE(ArrayObject::Populate(*value, &out, &error));
311 EXPECT_TRUE(EqualsUtf16("'TheArray': expected list, got integer",
312 error));
313 EXPECT_EQ(NULL, out.the_array.get());
315 EXPECT_TRUE(ArrayObject::Populate(*value, &out, &error));
316 EXPECT_TRUE(EqualsUtf16("'TheArray': expected list, got integer; "
317 "'TheArray': expected list, got integer",
318 error));
319 EXPECT_EQ(NULL, out.the_array.get());
323 TEST(JsonSchemaCompilerErrorTest, TooManyKeys) {
325 scoped_ptr<base::DictionaryValue> value = Dictionary(
326 "string", new base::StringValue("yes"));
327 EXPECT_TRUE(EqualsUtf16("", GetPopulateError<TestType>(*value)));
330 scoped_ptr<base::DictionaryValue> value = Dictionary(
331 "string", new base::StringValue("yes"),
332 "ohno", new base::StringValue("many values"));
333 EXPECT_TRUE(EqualsUtf16("found unexpected key 'ohno'",
334 GetPopulateError<TestType>(*value)));