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 (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));
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));
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
=
105 List(base::Value::CreateNullValue().release());
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",
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));
154 base::string16 error
;
155 EXPECT_TRUE(ObjectType::Populate(*value
, &out
, &error
));
156 EXPECT_TRUE(EqualsUtf16("'otherType': expected dictionary, got number",
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(
174 "expected integer, got boolean; unable to populate array 'integers'",
175 GetPopulateError
<ChoiceType::Integers
>(*params_value
)));
179 TEST(JsonSchemaCompilerErrorTest
, BinaryTypeExpected
) {
181 scoped_ptr
<base::DictionaryValue
> value
= Dictionary(
182 "data", new base::BinaryValue());
183 EXPECT_TRUE(EqualsUtf16("", GetPopulateError
<BinaryData
>(*value
)));
186 scoped_ptr
<base::DictionaryValue
> value
= Dictionary(
187 "data", new FundamentalValue(1.1));
188 EXPECT_TRUE(EqualsUtf16("'data': expected binary, got number",
189 GetPopulateError
<BinaryData
>(*value
)));
193 TEST(JsonSchemaCompilerErrorTest
, ListExpected
) {
195 scoped_ptr
<base::DictionaryValue
> value
= Dictionary(
196 "TheArray", new base::ListValue());
197 EXPECT_TRUE(EqualsUtf16("", GetPopulateError
<ArrayObject
>(*value
)));
200 scoped_ptr
<base::DictionaryValue
> value
= Dictionary(
201 "TheArray", new FundamentalValue(5));
202 EXPECT_TRUE(EqualsUtf16("'TheArray': expected list, got integer",
203 GetPopulateError
<ArrayObject
>(*value
)));
207 // GenerateStringToEnumConversion errors
209 TEST(JsonSchemaCompilerErrorTest
, BadEnumValue
) {
211 scoped_ptr
<base::DictionaryValue
> value
= Dictionary(
212 "enumeration", new base::StringValue("one"));
213 EXPECT_TRUE(EqualsUtf16("", GetPopulateError
<HasEnumeration
>(*value
)));
216 scoped_ptr
<base::DictionaryValue
> value
= Dictionary(
217 "enumeration", new base::StringValue("bad sauce"));
218 EXPECT_TRUE(EqualsUtf16("'Enumeration': expected \"one\" or \"two\" "
219 "or \"three\", got \"bad sauce\"",
220 GetPopulateError
<HasEnumeration
>(*value
)));
224 // Warn but don't fail out errors
226 TEST(JsonSchemaCompilerErrorTest
, WarnOnOptionalFailure
) {
228 scoped_ptr
<base::DictionaryValue
> value
= Dictionary(
229 "string", new base::StringValue("bling"));
230 EXPECT_TRUE(EqualsUtf16("", GetPopulateError
<OptionalTestType
>(*value
)));
233 scoped_ptr
<base::DictionaryValue
> value
= Dictionary(
234 "string", new base::FundamentalValue(1));
236 OptionalTestType out
;
237 base::string16 error
;
238 EXPECT_TRUE(OptionalTestType::Populate(*value
, &out
, &error
));
239 EXPECT_TRUE(EqualsUtf16("'string': expected string, got integer",
241 EXPECT_EQ(NULL
, out
.string
.get());
245 TEST(JsonSchemaCompilerErrorTest
, OptionalBinaryTypeFailure
) {
247 scoped_ptr
<base::DictionaryValue
> value
= Dictionary(
248 "data", new base::BinaryValue());
249 EXPECT_TRUE(EqualsUtf16("", GetPopulateError
<OptionalBinaryData
>(*value
)));
252 // There's a bug with silent failures if the key doesn't exist.
253 scoped_ptr
<base::DictionaryValue
> value
= Dictionary("data",
254 new base::FundamentalValue(1));
256 OptionalBinaryData out
;
257 base::string16 error
;
258 EXPECT_TRUE(OptionalBinaryData::Populate(*value
, &out
, &error
));
259 EXPECT_TRUE(EqualsUtf16("'data': expected binary, got integer",
261 EXPECT_EQ(NULL
, out
.data
.get());
265 TEST(JsonSchemaCompilerErrorTest
, OptionalArrayTypeFailure
) {
267 scoped_ptr
<base::DictionaryValue
> value
= Dictionary(
268 "TheArray", new base::ListValue());
269 EXPECT_TRUE(EqualsUtf16("", GetPopulateError
<ArrayObject
>(*value
)));
272 scoped_ptr
<base::DictionaryValue
> value
= Dictionary(
273 "TheArray", new FundamentalValue(5));
275 base::string16 error
;
276 EXPECT_TRUE(ArrayObject::Populate(*value
, &out
, &error
));
277 EXPECT_TRUE(EqualsUtf16("'TheArray': expected list, got integer",
279 EXPECT_EQ(NULL
, out
.the_array
.get());
283 TEST(JsonSchemaCompilerErrorTest
, OptionalUnableToPopulateArray
) {
285 scoped_ptr
<base::ListValue
> params_value
= List(
286 new FundamentalValue(5));
287 EXPECT_TRUE(EqualsUtf16("",
288 GetPopulateError
<OptionalChoiceType::Integers
>(*params_value
)));
291 scoped_ptr
<base::ListValue
> params_value
= List(
292 new FundamentalValue(5),
293 new FundamentalValue(false));
294 OptionalChoiceType::Integers out
;
295 base::string16 error
;
296 EXPECT_TRUE(OptionalChoiceType::Integers::Populate(*params_value
, &out
,
298 EXPECT_TRUE(EqualsUtf16(
299 "expected integer, got boolean; unable to populate array 'integers'",
301 EXPECT_EQ(NULL
, out
.as_integer
.get());
305 TEST(JsonSchemaCompilerErrorTest
, MultiplePopulationErrors
) {
308 scoped_ptr
<base::DictionaryValue
> value
= Dictionary(
309 "TheArray", new FundamentalValue(5));
311 base::string16 error
;
312 EXPECT_TRUE(ArrayObject::Populate(*value
, &out
, &error
));
313 EXPECT_TRUE(EqualsUtf16("'TheArray': expected list, got integer",
315 EXPECT_EQ(NULL
, out
.the_array
.get());
317 EXPECT_TRUE(ArrayObject::Populate(*value
, &out
, &error
));
318 EXPECT_TRUE(EqualsUtf16("'TheArray': expected list, got integer; "
319 "'TheArray': expected list, got integer",
321 EXPECT_EQ(NULL
, out
.the_array
.get());
325 TEST(JsonSchemaCompilerErrorTest
, TooManyKeys
) {
327 scoped_ptr
<base::DictionaryValue
> value
= Dictionary(
328 "string", new base::StringValue("yes"));
329 EXPECT_TRUE(EqualsUtf16("", GetPopulateError
<TestType
>(*value
)));
332 scoped_ptr
<base::DictionaryValue
> value
= Dictionary(
333 "string", new base::StringValue("yes"),
334 "ohno", new base::StringValue("many values"));
335 EXPECT_TRUE(EqualsUtf16("found unexpected key 'ohno'",
336 GetPopulateError
<TestType
>(*value
)));