By moving the call to Load() up in SearchProvider::Start(), we are giving a chance...
[chromium-blink-merge.git] / chrome / common / json_schema_validator_unittest_base.cc
blobb3cc89dd59a63fa41e9c19324e739a9c146ad060
1 // Copyright (c) 2012 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 "chrome/common/json_schema_validator_unittest_base.h"
7 #include <cfloat>
8 #include <cmath>
9 #include <limits>
11 #include "base/file_util.h"
12 #include "base/json/json_file_value_serializer.h"
13 #include "base/logging.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/path_service.h"
16 #include "base/stringprintf.h"
17 #include "base/values.h"
18 #include "chrome/common/chrome_paths.h"
19 #include "chrome/common/json_schema_constants.h"
20 #include "chrome/common/json_schema_validator.h"
22 namespace schema = json_schema_constants;
24 namespace {
26 #define TEST_SOURCE base::StringPrintf("%s:%i", __FILE__, __LINE__)
28 Value* LoadValue(const std::string& filename) {
29 FilePath path;
30 PathService::Get(chrome::DIR_TEST_DATA, &path);
31 path = path.AppendASCII("json_schema_validator").AppendASCII(filename);
32 EXPECT_TRUE(file_util::PathExists(path));
34 std::string error_message;
35 JSONFileValueSerializer serializer(path);
36 Value* result = serializer.Deserialize(NULL, &error_message);
37 if (!result)
38 ADD_FAILURE() << "Could not parse JSON: " << error_message;
39 return result;
42 Value* LoadValue(const std::string& filename, base::Value::Type type) {
43 scoped_ptr<Value> result(LoadValue(filename));
44 if (!result.get())
45 return NULL;
46 if (!result->IsType(type)) {
47 ADD_FAILURE() << "Expected type " << type << ", got: " << result->GetType();
48 return NULL;
50 return result.release();
53 ListValue* LoadList(const std::string& filename) {
54 return static_cast<ListValue*>(
55 LoadValue(filename, Value::TYPE_LIST));
58 DictionaryValue* LoadDictionary(const std::string& filename) {
59 return static_cast<DictionaryValue*>(
60 LoadValue(filename, Value::TYPE_DICTIONARY));
63 } // namespace
66 JSONSchemaValidatorTestBase::JSONSchemaValidatorTestBase(
67 JSONSchemaValidatorTestBase::ValidatorType type)
68 : type_(type) {
71 void JSONSchemaValidatorTestBase::RunTests() {
72 TestComplex();
73 TestStringPattern();
74 TestEnum();
75 TestChoices();
76 TestExtends();
77 TestObject();
78 TestTypeReference();
79 TestArrayTuple();
80 TestArrayNonTuple();
81 TestString();
82 TestNumber();
83 TestTypeClassifier();
84 TestTypes();
87 void JSONSchemaValidatorTestBase::TestComplex() {
88 scoped_ptr<DictionaryValue> schema(LoadDictionary("complex_schema.json"));
89 scoped_ptr<ListValue> instance(LoadList("complex_instance.json"));
91 ASSERT_TRUE(schema.get());
92 ASSERT_TRUE(instance.get());
94 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL);
95 instance->Remove(instance->GetSize() - 1, NULL);
96 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL);
97 instance->Append(new DictionaryValue());
98 ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), NULL, "1",
99 JSONSchemaValidator::FormatErrorMessage(
100 JSONSchemaValidator::kInvalidType,
101 schema::kNumber,
102 schema::kObject));
103 instance->Remove(instance->GetSize() - 1, NULL);
105 DictionaryValue* item = NULL;
106 ASSERT_TRUE(instance->GetDictionary(0, &item));
107 item->SetString("url", "xxxxxxxxxxx");
109 ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), NULL,
110 "0.url",
111 JSONSchemaValidator::FormatErrorMessage(
112 JSONSchemaValidator::kStringMaxLength, "10"));
115 void JSONSchemaValidatorTestBase::TestStringPattern() {
116 // Regex patterns not supported in CPP validator.
117 if (type_ == CPP)
118 return;
120 scoped_ptr<DictionaryValue> schema(new DictionaryValue());
121 schema->SetString(schema::kType, schema::kString);
122 schema->SetString(schema::kPattern, "foo+");
124 ExpectValid(TEST_SOURCE,
125 scoped_ptr<Value>(Value::CreateStringValue("foo")).get(),
126 schema.get(), NULL);
127 ExpectValid(TEST_SOURCE,
128 scoped_ptr<Value>(Value::CreateStringValue("foooooo")).get(),
129 schema.get(), NULL);
130 ExpectNotValid(TEST_SOURCE,
131 scoped_ptr<Value>(Value::CreateStringValue("bar")).get(),
132 schema.get(), NULL, "",
133 JSONSchemaValidator::FormatErrorMessage(
134 JSONSchemaValidator::kStringPattern, "foo+"));
137 void JSONSchemaValidatorTestBase::TestEnum() {
138 scoped_ptr<DictionaryValue> schema(LoadDictionary("enum_schema.json"));
140 ExpectValid(TEST_SOURCE,
141 scoped_ptr<Value>(Value::CreateStringValue("foo")).get(),
142 schema.get(), NULL);
143 ExpectValid(TEST_SOURCE,
144 scoped_ptr<Value>(Value::CreateIntegerValue(42)).get(),
145 schema.get(), NULL);
146 ExpectValid(TEST_SOURCE,
147 scoped_ptr<Value>(Value::CreateBooleanValue(false)).get(),
148 schema.get(), NULL);
150 ExpectNotValid(TEST_SOURCE,
151 scoped_ptr<Value>(Value::CreateStringValue("42")).get(),
152 schema.get(), NULL, "", JSONSchemaValidator::kInvalidEnum);
153 ExpectNotValid(TEST_SOURCE,
154 scoped_ptr<Value>(Value::CreateNullValue()).get(),
155 schema.get(), NULL, "", JSONSchemaValidator::kInvalidEnum);
158 void JSONSchemaValidatorTestBase::TestChoices() {
159 scoped_ptr<DictionaryValue> schema(LoadDictionary("choices_schema.json"));
161 ExpectValid(TEST_SOURCE,
162 scoped_ptr<Value>(Value::CreateNullValue()).get(),
163 schema.get(), NULL);
164 ExpectValid(TEST_SOURCE,
165 scoped_ptr<Value>(Value::CreateIntegerValue(42)).get(),
166 schema.get(), NULL);
168 scoped_ptr<DictionaryValue> instance(new DictionaryValue());
169 instance->SetString("foo", "bar");
170 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL);
172 ExpectNotValid(TEST_SOURCE,
173 scoped_ptr<Value>(Value::CreateStringValue("foo")).get(),
174 schema.get(), NULL, "", JSONSchemaValidator::kInvalidChoice);
175 ExpectNotValid(TEST_SOURCE,
176 scoped_ptr<Value>(new ListValue()).get(),
177 schema.get(), NULL, "", JSONSchemaValidator::kInvalidChoice);
179 instance->SetInteger("foo", 42);
180 ExpectNotValid(TEST_SOURCE, instance.get(),
181 schema.get(), NULL, "", JSONSchemaValidator::kInvalidChoice);
184 void JSONSchemaValidatorTestBase::TestExtends() {
185 // TODO(aa): JS only
188 void JSONSchemaValidatorTestBase::TestObject() {
189 scoped_ptr<DictionaryValue> schema(new DictionaryValue());
190 schema->SetString(schema::kType, schema::kObject);
191 schema->SetString("properties.foo.type", schema::kString);
192 schema->SetString("properties.bar.type", schema::kInteger);
194 scoped_ptr<DictionaryValue> instance(new DictionaryValue());
195 instance->SetString("foo", "foo");
196 instance->SetInteger("bar", 42);
198 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL);
200 instance->SetBoolean("extra", true);
201 ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), NULL,
202 "extra", JSONSchemaValidator::kUnexpectedProperty);
204 instance->Remove("extra", NULL);
205 instance->Remove("bar", NULL);
206 ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), NULL, "bar",
207 JSONSchemaValidator::kObjectPropertyIsRequired);
209 instance->SetString("bar", "42");
210 ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), NULL, "bar",
211 JSONSchemaValidator::FormatErrorMessage(
212 JSONSchemaValidator::kInvalidType,
213 schema::kInteger,
214 schema::kString));
216 DictionaryValue* additional_properties = new DictionaryValue();
217 additional_properties->SetString(schema::kType, schema::kAny);
218 schema->Set(schema::kAdditionalProperties, additional_properties);
220 instance->SetInteger("bar", 42);
221 instance->SetBoolean("extra", true);
222 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL);
224 instance->SetString("extra", "foo");
225 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL);
227 additional_properties->SetString(schema::kType, schema::kBoolean);
228 instance->SetBoolean("extra", true);
229 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL);
231 instance->SetString("extra", "foo");
232 ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), NULL,
233 "extra", JSONSchemaValidator::FormatErrorMessage(
234 JSONSchemaValidator::kInvalidType,
235 schema::kBoolean,
236 schema::kString));
238 DictionaryValue* properties = NULL;
239 DictionaryValue* bar_property = NULL;
240 ASSERT_TRUE(schema->GetDictionary(schema::kProperties, &properties));
241 ASSERT_TRUE(properties->GetDictionary("bar", &bar_property));
243 bar_property->SetBoolean(schema::kOptional, true);
244 instance->Remove("extra", NULL);
245 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL);
246 instance->Remove("bar", NULL);
247 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL);
248 instance->Set("bar", Value::CreateNullValue());
249 ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), NULL,
250 "bar", JSONSchemaValidator::FormatErrorMessage(
251 JSONSchemaValidator::kInvalidType,
252 schema::kInteger,
253 schema::kNull));
254 instance->SetString("bar", "42");
255 ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), NULL,
256 "bar", JSONSchemaValidator::FormatErrorMessage(
257 JSONSchemaValidator::kInvalidType,
258 schema::kInteger,
259 schema::kString));
262 void JSONSchemaValidatorTestBase::TestTypeReference() {
263 scoped_ptr<ListValue> types(LoadList("reference_types.json"));
264 ASSERT_TRUE(types.get());
266 scoped_ptr<DictionaryValue> schema(new DictionaryValue());
267 schema->SetString(schema::kType, schema::kObject);
268 schema->SetString("properties.foo.type", schema::kString);
269 schema->SetString("properties.bar.$ref", "Max10Int");
270 schema->SetString("properties.baz.$ref", "MinLengthString");
272 scoped_ptr<DictionaryValue> schema_inline(new DictionaryValue());
273 schema_inline->SetString(schema::kType, schema::kObject);
274 schema_inline->SetString("properties.foo.type", schema::kString);
275 schema_inline->SetString("properties.bar.id", "NegativeInt");
276 schema_inline->SetString("properties.bar.type", schema::kInteger);
277 schema_inline->SetInteger("properties.bar.maximum", 0);
278 schema_inline->SetString("properties.baz.$ref", "NegativeInt");
280 scoped_ptr<DictionaryValue> instance(new DictionaryValue());
281 instance->SetString("foo", "foo");
282 instance->SetInteger("bar", 4);
283 instance->SetString("baz", "ab");
285 scoped_ptr<DictionaryValue> instance_inline(new DictionaryValue());
286 instance_inline->SetString("foo", "foo");
287 instance_inline->SetInteger("bar", -4);
288 instance_inline->SetInteger("baz", -2);
290 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), types.get());
291 ExpectValid(TEST_SOURCE, instance_inline.get(), schema_inline.get(), NULL);
293 // Validation failure, but successful schema reference.
294 instance->SetString("baz", "a");
295 ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), types.get(),
296 "baz", JSONSchemaValidator::FormatErrorMessage(
297 JSONSchemaValidator::kStringMinLength, "2"));
299 instance_inline->SetInteger("bar", 20);
300 ExpectNotValid(TEST_SOURCE, instance_inline.get(), schema_inline.get(), NULL,
301 "bar", JSONSchemaValidator::FormatErrorMessage(
302 JSONSchemaValidator::kNumberMaximum, "0"));
304 // Remove MinLengthString type.
305 types->Remove(types->GetSize() - 1, NULL);
306 instance->SetString("baz", "ab");
307 ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), types.get(),
308 "bar", JSONSchemaValidator::FormatErrorMessage(
309 JSONSchemaValidator::kUnknownTypeReference,
310 "Max10Int"));
312 // Remove internal type "NegativeInt".
313 schema_inline->Remove("properties.bar", NULL);
314 instance_inline->Remove("bar", NULL);
315 ExpectNotValid(TEST_SOURCE, instance_inline.get(), schema_inline.get(), NULL,
316 "baz", JSONSchemaValidator::FormatErrorMessage(
317 JSONSchemaValidator::kUnknownTypeReference,
318 "NegativeInt"));
321 void JSONSchemaValidatorTestBase::TestArrayTuple() {
322 scoped_ptr<DictionaryValue> schema(LoadDictionary("array_tuple_schema.json"));
323 ASSERT_TRUE(schema.get());
325 scoped_ptr<ListValue> instance(new ListValue());
326 instance->Append(Value::CreateStringValue("42"));
327 instance->Append(Value::CreateIntegerValue(42));
329 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL);
331 instance->Append(Value::CreateStringValue("anything"));
332 ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), NULL, "",
333 JSONSchemaValidator::FormatErrorMessage(
334 JSONSchemaValidator::kArrayMaxItems, "2"));
336 instance->Remove(1, NULL);
337 instance->Remove(1, NULL);
338 ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), NULL, "1",
339 JSONSchemaValidator::kArrayItemRequired);
341 instance->Set(0, Value::CreateIntegerValue(42));
342 instance->Append(Value::CreateIntegerValue(42));
343 ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), NULL, "0",
344 JSONSchemaValidator::FormatErrorMessage(
345 JSONSchemaValidator::kInvalidType,
346 schema::kString,
347 schema::kInteger));
349 DictionaryValue* additional_properties = new DictionaryValue();
350 additional_properties->SetString(schema::kType, schema::kAny);
351 schema->Set(schema::kAdditionalProperties, additional_properties);
352 instance->Set(0, Value::CreateStringValue("42"));
353 instance->Append(Value::CreateStringValue("anything"));
354 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL);
355 instance->Set(2, new ListValue());
356 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL);
358 additional_properties->SetString(schema::kType, schema::kBoolean);
359 ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), NULL, "2",
360 JSONSchemaValidator::FormatErrorMessage(
361 JSONSchemaValidator::kInvalidType,
362 schema::kBoolean,
363 schema::kArray));
364 instance->Set(2, Value::CreateBooleanValue(false));
365 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL);
367 ListValue* items_schema = NULL;
368 DictionaryValue* item0_schema = NULL;
369 ASSERT_TRUE(schema->GetList(schema::kItems, &items_schema));
370 ASSERT_TRUE(items_schema->GetDictionary(0, &item0_schema));
371 item0_schema->SetBoolean(schema::kOptional, true);
372 instance->Remove(2, NULL);
373 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL);
374 // TODO(aa): I think this is inconsistent with the handling of NULL+optional
375 // for objects.
376 instance->Set(0, Value::CreateNullValue());
377 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL);
378 instance->Set(0, Value::CreateIntegerValue(42));
379 ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), NULL, "0",
380 JSONSchemaValidator::FormatErrorMessage(
381 JSONSchemaValidator::kInvalidType,
382 schema::kString,
383 schema::kInteger));
386 void JSONSchemaValidatorTestBase::TestArrayNonTuple() {
387 scoped_ptr<DictionaryValue> schema(new DictionaryValue());
388 schema->SetString(schema::kType, schema::kArray);
389 schema->SetString("items.type", schema::kString);
390 schema->SetInteger(schema::kMinItems, 2);
391 schema->SetInteger(schema::kMaxItems, 3);
393 scoped_ptr<ListValue> instance(new ListValue());
394 instance->Append(Value::CreateStringValue("x"));
395 instance->Append(Value::CreateStringValue("x"));
397 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL);
398 instance->Append(Value::CreateStringValue("x"));
399 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL);
401 instance->Append(Value::CreateStringValue("x"));
402 ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), NULL, "",
403 JSONSchemaValidator::FormatErrorMessage(
404 JSONSchemaValidator::kArrayMaxItems, "3"));
405 instance->Remove(1, NULL);
406 instance->Remove(1, NULL);
407 instance->Remove(1, NULL);
408 ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), NULL, "",
409 JSONSchemaValidator::FormatErrorMessage(
410 JSONSchemaValidator::kArrayMinItems, "2"));
412 instance->Remove(1, NULL);
413 instance->Append(Value::CreateIntegerValue(42));
414 ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), NULL, "1",
415 JSONSchemaValidator::FormatErrorMessage(
416 JSONSchemaValidator::kInvalidType,
417 schema::kString,
418 schema::kInteger));
421 void JSONSchemaValidatorTestBase::TestString() {
422 scoped_ptr<DictionaryValue> schema(new DictionaryValue());
423 schema->SetString(schema::kType, schema::kString);
424 schema->SetInteger(schema::kMinLength, 1);
425 schema->SetInteger(schema::kMaxLength, 10);
427 ExpectValid(TEST_SOURCE,
428 scoped_ptr<Value>(Value::CreateStringValue("x")).get(),
429 schema.get(), NULL);
430 ExpectValid(TEST_SOURCE,
431 scoped_ptr<Value>(Value::CreateStringValue("xxxxxxxxxx")).get(),
432 schema.get(), NULL);
434 ExpectNotValid(TEST_SOURCE,
435 scoped_ptr<Value>(Value::CreateStringValue("")).get(),
436 schema.get(), NULL, "",
437 JSONSchemaValidator::FormatErrorMessage(
438 JSONSchemaValidator::kStringMinLength, "1"));
439 ExpectNotValid(
440 TEST_SOURCE,
441 scoped_ptr<Value>(Value::CreateStringValue("xxxxxxxxxxx")).get(),
442 schema.get(), NULL, "",
443 JSONSchemaValidator::FormatErrorMessage(
444 JSONSchemaValidator::kStringMaxLength, "10"));
448 void JSONSchemaValidatorTestBase::TestNumber() {
449 scoped_ptr<DictionaryValue> schema(new DictionaryValue());
450 schema->SetString(schema::kType, schema::kNumber);
451 schema->SetInteger(schema::kMinimum, 1);
452 schema->SetInteger(schema::kMaximum, 100);
453 schema->SetInteger("maxDecimal", 2);
455 ExpectValid(TEST_SOURCE,
456 scoped_ptr<Value>(Value::CreateIntegerValue(1)).get(),
457 schema.get(), NULL);
458 ExpectValid(TEST_SOURCE,
459 scoped_ptr<Value>(Value::CreateIntegerValue(50)).get(),
460 schema.get(), NULL);
461 ExpectValid(TEST_SOURCE,
462 scoped_ptr<Value>(Value::CreateIntegerValue(100)).get(),
463 schema.get(), NULL);
464 ExpectValid(TEST_SOURCE,
465 scoped_ptr<Value>(Value::CreateDoubleValue(88.88)).get(),
466 schema.get(), NULL);
468 ExpectNotValid(
469 TEST_SOURCE,
470 scoped_ptr<Value>(Value::CreateDoubleValue(0.5)).get(),
471 schema.get(), NULL, "",
472 JSONSchemaValidator::FormatErrorMessage(
473 JSONSchemaValidator::kNumberMinimum, "1"));
474 ExpectNotValid(
475 TEST_SOURCE,
476 scoped_ptr<Value>(Value::CreateDoubleValue(100.1)).get(),
477 schema.get(), NULL, "",
478 JSONSchemaValidator::FormatErrorMessage(
479 JSONSchemaValidator::kNumberMaximum, "100"));
482 void JSONSchemaValidatorTestBase::TestTypeClassifier() {
483 EXPECT_EQ(std::string(schema::kBoolean),
484 JSONSchemaValidator::GetJSONSchemaType(
485 scoped_ptr<Value>(Value::CreateBooleanValue(true)).get()));
486 EXPECT_EQ(std::string(schema::kBoolean),
487 JSONSchemaValidator::GetJSONSchemaType(
488 scoped_ptr<Value>(Value::CreateBooleanValue(false)).get()));
490 // It doesn't matter whether the C++ type is 'integer' or 'real'. If the
491 // number is integral and within the representable range of integers in
492 // double, it's classified as 'integer'.
493 EXPECT_EQ(std::string(schema::kInteger),
494 JSONSchemaValidator::GetJSONSchemaType(
495 scoped_ptr<Value>(Value::CreateIntegerValue(42)).get()));
496 EXPECT_EQ(std::string(schema::kInteger),
497 JSONSchemaValidator::GetJSONSchemaType(
498 scoped_ptr<Value>(Value::CreateIntegerValue(0)).get()));
499 EXPECT_EQ(std::string(schema::kInteger),
500 JSONSchemaValidator::GetJSONSchemaType(
501 scoped_ptr<Value>(Value::CreateDoubleValue(42)).get()));
502 EXPECT_EQ(std::string(schema::kInteger),
503 JSONSchemaValidator::GetJSONSchemaType(scoped_ptr<Value>(
504 Value::CreateDoubleValue(pow(2.0, DBL_MANT_DIG))).get()));
505 EXPECT_EQ(std::string(schema::kInteger),
506 JSONSchemaValidator::GetJSONSchemaType(scoped_ptr<Value>(
507 Value::CreateDoubleValue(pow(-2.0, DBL_MANT_DIG))).get()));
509 // "number" is only used for non-integral numbers, or numbers beyond what
510 // double can accurately represent.
511 EXPECT_EQ(std::string(schema::kNumber),
512 JSONSchemaValidator::GetJSONSchemaType(
513 scoped_ptr<Value>(Value::CreateDoubleValue(88.8)).get()));
514 EXPECT_EQ(std::string(schema::kNumber),
515 JSONSchemaValidator::GetJSONSchemaType(scoped_ptr<Value>(
516 Value::CreateDoubleValue(pow(2.0, DBL_MANT_DIG) * 2)).get()));
517 EXPECT_EQ(std::string(schema::kNumber),
518 JSONSchemaValidator::GetJSONSchemaType(scoped_ptr<Value>(
519 Value::CreateDoubleValue(pow(-2.0, DBL_MANT_DIG) * 2)).get()));
521 EXPECT_EQ(std::string(schema::kString),
522 JSONSchemaValidator::GetJSONSchemaType(
523 scoped_ptr<Value>(Value::CreateStringValue("foo")).get()));
524 EXPECT_EQ(std::string(schema::kArray),
525 JSONSchemaValidator::GetJSONSchemaType(
526 scoped_ptr<Value>(new ListValue()).get()));
527 EXPECT_EQ(std::string(schema::kObject),
528 JSONSchemaValidator::GetJSONSchemaType(
529 scoped_ptr<Value>(new DictionaryValue()).get()));
530 EXPECT_EQ(std::string(schema::kNull),
531 JSONSchemaValidator::GetJSONSchemaType(
532 scoped_ptr<Value>(Value::CreateNullValue()).get()));
535 void JSONSchemaValidatorTestBase::TestTypes() {
536 scoped_ptr<DictionaryValue> schema(new DictionaryValue());
538 // valid
539 schema->SetString(schema::kType, schema::kObject);
540 ExpectValid(TEST_SOURCE, scoped_ptr<Value>(new DictionaryValue()).get(),
541 schema.get(), NULL);
543 schema->SetString(schema::kType, schema::kArray);
544 ExpectValid(TEST_SOURCE, scoped_ptr<Value>(new ListValue()).get(),
545 schema.get(), NULL);
547 schema->SetString(schema::kType, schema::kString);
548 ExpectValid(TEST_SOURCE,
549 scoped_ptr<Value>(Value::CreateStringValue("foobar")).get(),
550 schema.get(), NULL);
552 schema->SetString(schema::kType, schema::kNumber);
553 ExpectValid(TEST_SOURCE,
554 scoped_ptr<Value>(Value::CreateDoubleValue(88.8)).get(),
555 schema.get(), NULL);
556 ExpectValid(TEST_SOURCE,
557 scoped_ptr<Value>(Value::CreateDoubleValue(42)).get(),
558 schema.get(), NULL);
559 ExpectValid(TEST_SOURCE,
560 scoped_ptr<Value>(Value::CreateIntegerValue(42)).get(),
561 schema.get(), NULL);
562 ExpectValid(TEST_SOURCE,
563 scoped_ptr<Value>(Value::CreateIntegerValue(0)).get(),
564 schema.get(), NULL);
566 schema->SetString(schema::kType, schema::kInteger);
567 ExpectValid(TEST_SOURCE,
568 scoped_ptr<Value>(Value::CreateIntegerValue(42)).get(),
569 schema.get(), NULL);
570 ExpectValid(TEST_SOURCE,
571 scoped_ptr<Value>(Value::CreateDoubleValue(42)).get(),
572 schema.get(), NULL);
573 ExpectValid(TEST_SOURCE,
574 scoped_ptr<Value>(Value::CreateIntegerValue(0)).get(),
575 schema.get(), NULL);
576 ExpectValid(TEST_SOURCE,
577 scoped_ptr<Value>(
578 Value::CreateDoubleValue(pow(2.0, DBL_MANT_DIG))).get(),
579 schema.get(), NULL);
580 ExpectValid(TEST_SOURCE,
581 scoped_ptr<Value>(
582 Value::CreateDoubleValue(pow(-2.0, DBL_MANT_DIG))).get(),
583 schema.get(), NULL);
585 schema->SetString(schema::kType, schema::kBoolean);
586 ExpectValid(TEST_SOURCE,
587 scoped_ptr<Value>(Value::CreateBooleanValue(false)).get(),
588 schema.get(), NULL);
589 ExpectValid(TEST_SOURCE,
590 scoped_ptr<Value>(Value::CreateBooleanValue(true)).get(),
591 schema.get(), NULL);
593 schema->SetString(schema::kType, schema::kNull);
594 ExpectValid(TEST_SOURCE,
595 scoped_ptr<Value>(Value::CreateNullValue()).get(),
596 schema.get(), NULL);
598 // not valid
599 schema->SetString(schema::kType, schema::kObject);
600 ExpectNotValid(TEST_SOURCE, scoped_ptr<Value>(new ListValue()).get(),
601 schema.get(), NULL, "",
602 JSONSchemaValidator::FormatErrorMessage(
603 JSONSchemaValidator::kInvalidType,
604 schema::kObject,
605 schema::kArray));
607 schema->SetString(schema::kType, schema::kObject);
608 ExpectNotValid(TEST_SOURCE, scoped_ptr<Value>(Value::CreateNullValue()).get(),
609 schema.get(), NULL, "",
610 JSONSchemaValidator::FormatErrorMessage(
611 JSONSchemaValidator::kInvalidType,
612 schema::kObject,
613 schema::kNull));
615 schema->SetString(schema::kType, schema::kArray);
616 ExpectNotValid(TEST_SOURCE,
617 scoped_ptr<Value>(Value::CreateIntegerValue(42)).get(),
618 schema.get(), NULL, "",
619 JSONSchemaValidator::FormatErrorMessage(
620 JSONSchemaValidator::kInvalidType,
621 schema::kArray,
622 schema::kInteger));
624 schema->SetString(schema::kType, schema::kString);
625 ExpectNotValid(TEST_SOURCE,
626 scoped_ptr<Value>(Value::CreateIntegerValue(42)).get(),
627 schema.get(), NULL, "",
628 JSONSchemaValidator::FormatErrorMessage(
629 JSONSchemaValidator::kInvalidType,
630 schema::kString,
631 schema::kInteger));
633 schema->SetString(schema::kType, schema::kNumber);
634 ExpectNotValid(TEST_SOURCE,
635 scoped_ptr<Value>(Value::CreateStringValue("42")).get(),
636 schema.get(), NULL, "",
637 JSONSchemaValidator::FormatErrorMessage(
638 JSONSchemaValidator::kInvalidType,
639 schema::kNumber,
640 schema::kString));
642 schema->SetString(schema::kType, schema::kInteger);
643 ExpectNotValid(TEST_SOURCE,
644 scoped_ptr<Value>(Value::CreateDoubleValue(88.8)).get(),
645 schema.get(), NULL, "",
646 JSONSchemaValidator::FormatErrorMessage(
647 JSONSchemaValidator::kInvalidType,
648 schema::kInteger,
649 schema::kNumber));
651 schema->SetString(schema::kType, schema::kInteger);
652 ExpectNotValid(TEST_SOURCE,
653 scoped_ptr<Value>(Value::CreateDoubleValue(88.8)).get(),
654 schema.get(), NULL, "",
655 JSONSchemaValidator::FormatErrorMessage(
656 JSONSchemaValidator::kInvalidType,
657 schema::kInteger,
658 schema::kNumber));
660 schema->SetString(schema::kType, schema::kBoolean);
661 ExpectNotValid(TEST_SOURCE,
662 scoped_ptr<Value>(Value::CreateIntegerValue(1)).get(),
663 schema.get(), NULL, "",
664 JSONSchemaValidator::FormatErrorMessage(
665 JSONSchemaValidator::kInvalidType,
666 schema::kBoolean,
667 schema::kInteger));
669 schema->SetString(schema::kType, schema::kNull);
670 ExpectNotValid(TEST_SOURCE,
671 scoped_ptr<Value>(Value::CreateBooleanValue(false)).get(),
672 schema.get(), NULL, "",
673 JSONSchemaValidator::FormatErrorMessage(
674 JSONSchemaValidator::kInvalidType,
675 schema::kNull,
676 schema::kBoolean));