1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
|
// 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 "base/strings/utf_string_conversions.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>
base::string16 GetPopulateError(const base::Value& value) {
base::string16 error;
T test_type;
T::Populate(value, &test_type, &error);
return error;
}
testing::AssertionResult EqualsUtf16(const std::string& expected,
const base::string16& actual) {
if (base::ASCIIToUTF16(expected) != actual)
return testing::AssertionFailure() << expected << " != " << actual;
return testing::AssertionSuccess();
}
// GenerateTypePopulate errors
TEST(JsonSchemaCompilerErrorTest, RequiredPropertyPopulate) {
{
scoped_ptr<base::DictionaryValue> value = Dictionary(
"string", new base::StringValue("bling"));
EXPECT_TRUE(EqualsUtf16("", GetPopulateError<TestType>(*value)));
}
{
scoped_ptr<base::BinaryValue> value(new base::BinaryValue());
EXPECT_TRUE(EqualsUtf16("expected dictionary, got binary",
GetPopulateError<TestType>(*value)));
}
}
TEST(JsonSchemaCompilerErrorTest, UnexpectedTypePopulation) {
{
scoped_ptr<base::ListValue> value(new base::ListValue());
EXPECT_TRUE(EqualsUtf16("",
GetPopulateError<ChoiceType::Integers>(*value)));
}
{
scoped_ptr<base::BinaryValue> value(new base::BinaryValue());
EXPECT_TRUE(EqualsUtf16("expected integers or integer, got binary",
GetPopulateError<ChoiceType::Integers>(*value)));
}
}
// GenerateTypePopulateProperty errors
TEST(JsonSchemaCompilerErrorTest, TypeIsRequired) {
{
scoped_ptr<base::DictionaryValue> value = Dictionary(
"integers", new FundamentalValue(5));
EXPECT_TRUE(EqualsUtf16("", GetPopulateError<ChoiceType>(*value)));
}
{
scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue());
EXPECT_TRUE(EqualsUtf16("'integers' is required",
GetPopulateError<ChoiceType>(*value)));
}
}
// GenerateParamsCheck errors
TEST(JsonSchemaCompilerErrorTest, TooManyParameters) {
{
scoped_ptr<base::ListValue> params_value = List(
new FundamentalValue(5));
base::string16 error;
EXPECT_TRUE(TestFunction::Params::Create(*params_value, &error));
}
{
scoped_ptr<base::ListValue> params_value = List(
new FundamentalValue(5),
new FundamentalValue(5));
base::string16 error;
EXPECT_FALSE(TestFunction::Params::Create(*params_value, &error));
EXPECT_TRUE(EqualsUtf16("expected 1 arguments, got 2", error));
}
}
// GenerateFunctionParamsCreate errors
TEST(JsonSchemaCompilerErrorTest, ParamIsRequired) {
{
scoped_ptr<base::ListValue> params_value = List(
new FundamentalValue(5));
base::string16 error;
EXPECT_TRUE(TestFunction::Params::Create(*params_value, &error));
}
{
scoped_ptr<base::ListValue> params_value = List(
base::Value::CreateNullValue());
base::string16 error;
EXPECT_FALSE(TestFunction::Params::Create(*params_value, &error));
EXPECT_TRUE(EqualsUtf16("'num' is required", error));
}
}
// GeneratePopulateVariableFromValue errors
TEST(JsonSchemaCompilerErrorTest, WrongPropertyValueType) {
{
scoped_ptr<base::DictionaryValue> value = Dictionary(
"string", new base::StringValue("yes"));
EXPECT_TRUE(EqualsUtf16("", GetPopulateError<TestType>(*value)));
}
{
scoped_ptr<base::DictionaryValue> value = Dictionary(
"string", new FundamentalValue(1.1));
EXPECT_TRUE(EqualsUtf16("'string': expected string, got number",
GetPopulateError<TestType>(*value)));
}
}
TEST(JsonSchemaCompilerErrorTest, WrongParameterCreationType) {
{
base::string16 error;
scoped_ptr<base::ListValue> params_value = List(
new base::StringValue("Yeah!"));
EXPECT_TRUE(TestString::Params::Create(*params_value, &error));
}
{
scoped_ptr<base::ListValue> params_value = List(
new FundamentalValue(5));
base::string16 error;
EXPECT_FALSE(TestTypeInObject::Params::Create(*params_value, &error));
EXPECT_TRUE(EqualsUtf16("'paramObject': expected dictionary, got integer",
error));
}
}
TEST(JsonSchemaCompilerErrorTest, WrongTypeValueType) {
{
scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue());
EXPECT_TRUE(EqualsUtf16("", GetPopulateError<ObjectType>(*value)));
}
{
scoped_ptr<base::DictionaryValue> value = Dictionary(
"otherType", new FundamentalValue(1.1));
ObjectType out;
base::string16 error;
EXPECT_TRUE(ObjectType::Populate(*value, &out, &error));
EXPECT_TRUE(EqualsUtf16("'otherType': expected dictionary, got number",
error));
EXPECT_EQ(NULL, out.other_type.get());
}
}
TEST(JsonSchemaCompilerErrorTest, UnableToPopulateArray) {
{
scoped_ptr<base::ListValue> params_value = List(
new FundamentalValue(5));
EXPECT_TRUE(EqualsUtf16("",
GetPopulateError<ChoiceType::Integers>(*params_value)));
}
{
scoped_ptr<base::ListValue> params_value = List(
new FundamentalValue(5),
new FundamentalValue(false));
EXPECT_TRUE(EqualsUtf16(
"expected integer, got boolean; unable to populate array 'integers'",
GetPopulateError<ChoiceType::Integers>(*params_value)));
}
}
TEST(JsonSchemaCompilerErrorTest, BinaryTypeExpected) {
{
scoped_ptr<base::DictionaryValue> value = Dictionary(
"data", new base::BinaryValue());
EXPECT_TRUE(EqualsUtf16("", GetPopulateError<BinaryData>(*value)));
}
{
scoped_ptr<base::DictionaryValue> value = Dictionary(
"data", new FundamentalValue(1.1));
EXPECT_TRUE(EqualsUtf16("'data': expected binary, got number",
GetPopulateError<BinaryData>(*value)));
}
}
TEST(JsonSchemaCompilerErrorTest, ListExpected) {
{
scoped_ptr<base::DictionaryValue> value = Dictionary(
"TheArray", new base::ListValue());
EXPECT_TRUE(EqualsUtf16("", GetPopulateError<ArrayObject>(*value)));
}
{
scoped_ptr<base::DictionaryValue> value = Dictionary(
"TheArray", new FundamentalValue(5));
EXPECT_TRUE(EqualsUtf16("'TheArray': expected list, got integer",
GetPopulateError<ArrayObject>(*value)));
}
}
// GenerateStringToEnumConversion errors
TEST(JsonSchemaCompilerErrorTest, BadEnumValue) {
{
scoped_ptr<base::DictionaryValue> value = Dictionary(
"enumeration", new base::StringValue("one"));
EXPECT_TRUE(EqualsUtf16("", GetPopulateError<HasEnumeration>(*value)));
}
{
scoped_ptr<base::DictionaryValue> value = Dictionary(
"enumeration", new base::StringValue("bad sauce"));
EXPECT_TRUE(EqualsUtf16("'Enumeration': expected \"one\" or \"two\" "
"or \"three\", got \"bad sauce\"",
GetPopulateError<HasEnumeration>(*value)));
}
}
// Warn but don't fail out errors
TEST(JsonSchemaCompilerErrorTest, WarnOnOptionalFailure) {
{
scoped_ptr<base::DictionaryValue> value = Dictionary(
"string", new base::StringValue("bling"));
EXPECT_TRUE(EqualsUtf16("", GetPopulateError<OptionalTestType>(*value)));
}
{
scoped_ptr<base::DictionaryValue> value = Dictionary(
"string", new base::FundamentalValue(1));
OptionalTestType out;
base::string16 error;
EXPECT_TRUE(OptionalTestType::Populate(*value, &out, &error));
EXPECT_TRUE(EqualsUtf16("'string': expected string, got integer",
error));
EXPECT_EQ(NULL, out.string.get());
}
}
TEST(JsonSchemaCompilerErrorTest, OptionalBinaryTypeFailure) {
{
scoped_ptr<base::DictionaryValue> value = Dictionary(
"data", new base::BinaryValue());
EXPECT_TRUE(EqualsUtf16("", GetPopulateError<OptionalBinaryData>(*value)));
}
{
// There's a bug with silent failures if the key doesn't exist.
scoped_ptr<base::DictionaryValue> value = Dictionary("data",
new base::FundamentalValue(1));
OptionalBinaryData out;
base::string16 error;
EXPECT_TRUE(OptionalBinaryData::Populate(*value, &out, &error));
EXPECT_TRUE(EqualsUtf16("'data': expected binary, got integer",
error));
EXPECT_EQ(NULL, out.data.get());
}
}
TEST(JsonSchemaCompilerErrorTest, OptionalArrayTypeFailure) {
{
scoped_ptr<base::DictionaryValue> value = Dictionary(
"TheArray", new base::ListValue());
EXPECT_TRUE(EqualsUtf16("", GetPopulateError<ArrayObject>(*value)));
}
{
scoped_ptr<base::DictionaryValue> value = Dictionary(
"TheArray", new FundamentalValue(5));
ArrayObject out;
base::string16 error;
EXPECT_TRUE(ArrayObject::Populate(*value, &out, &error));
EXPECT_TRUE(EqualsUtf16("'TheArray': expected list, got integer",
error));
EXPECT_EQ(NULL, out.the_array.get());
}
}
TEST(JsonSchemaCompilerErrorTest, OptionalUnableToPopulateArray) {
{
scoped_ptr<base::ListValue> params_value = List(
new FundamentalValue(5));
EXPECT_TRUE(EqualsUtf16("",
GetPopulateError<OptionalChoiceType::Integers>(*params_value)));
}
{
scoped_ptr<base::ListValue> params_value = List(
new FundamentalValue(5),
new FundamentalValue(false));
OptionalChoiceType::Integers out;
base::string16 error;
EXPECT_TRUE(OptionalChoiceType::Integers::Populate(*params_value, &out,
&error));
EXPECT_TRUE(EqualsUtf16(
"expected integer, got boolean; unable to populate array 'integers'",
error));
EXPECT_EQ(NULL, out.as_integer.get());
}
}
TEST(JsonSchemaCompilerErrorTest, MultiplePopulationErrors) {
{
scoped_ptr<base::DictionaryValue> value = Dictionary(
"TheArray", new FundamentalValue(5));
ArrayObject out;
base::string16 error;
EXPECT_TRUE(ArrayObject::Populate(*value, &out, &error));
EXPECT_TRUE(EqualsUtf16("'TheArray': expected list, got integer",
error));
EXPECT_EQ(NULL, out.the_array.get());
EXPECT_TRUE(ArrayObject::Populate(*value, &out, &error));
EXPECT_TRUE(EqualsUtf16("'TheArray': expected list, got integer; "
"'TheArray': expected list, got integer",
error));
EXPECT_EQ(NULL, out.the_array.get());
}
}
TEST(JsonSchemaCompilerErrorTest, TooManyKeys) {
{
scoped_ptr<base::DictionaryValue> value = Dictionary(
"string", new base::StringValue("yes"));
EXPECT_TRUE(EqualsUtf16("", GetPopulateError<TestType>(*value)));
}
{
scoped_ptr<base::DictionaryValue> value = Dictionary(
"string", new base::StringValue("yes"),
"ohno", new base::StringValue("many values"));
EXPECT_TRUE(EqualsUtf16("found unexpected key 'ohno'",
GetPopulateError<TestType>(*value)));
}
}
|