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"
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
;
26 #define TEST_SOURCE base::StringPrintf("%s:%i", __FILE__, __LINE__)
28 Value
* LoadValue(const std::string
& filename
) {
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
);
38 ADD_FAILURE() << "Could not parse JSON: " << error_message
;
42 Value
* LoadValue(const std::string
& filename
, base::Value::Type type
) {
43 scoped_ptr
<Value
> result(LoadValue(filename
));
46 if (!result
->IsType(type
)) {
47 ADD_FAILURE() << "Expected type " << type
<< ", got: " << result
->GetType();
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
));
66 JSONSchemaValidatorTestBase::JSONSchemaValidatorTestBase(
67 JSONSchemaValidatorTestBase::ValidatorType type
)
71 void JSONSchemaValidatorTestBase::RunTests() {
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
,
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
,
111 JSONSchemaValidator::FormatErrorMessage(
112 JSONSchemaValidator::kStringMaxLength
, "10"));
115 void JSONSchemaValidatorTestBase::TestStringPattern() {
116 // Regex patterns not supported in CPP validator.
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(),
127 ExpectValid(TEST_SOURCE
,
128 scoped_ptr
<Value
>(Value::CreateStringValue("foooooo")).get(),
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(),
143 ExpectValid(TEST_SOURCE
,
144 scoped_ptr
<Value
>(Value::CreateIntegerValue(42)).get(),
146 ExpectValid(TEST_SOURCE
,
147 scoped_ptr
<Value
>(Value::CreateBooleanValue(false)).get(),
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(),
164 ExpectValid(TEST_SOURCE
,
165 scoped_ptr
<Value
>(Value::CreateIntegerValue(42)).get(),
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() {
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
,
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
,
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
,
254 instance
->SetString("bar", "42");
255 ExpectNotValid(TEST_SOURCE
, instance
.get(), schema
.get(), NULL
,
256 "bar", JSONSchemaValidator::FormatErrorMessage(
257 JSONSchemaValidator::kInvalidType
,
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
,
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
,
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
,
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
,
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
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
,
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
,
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(),
430 ExpectValid(TEST_SOURCE
,
431 scoped_ptr
<Value
>(Value::CreateStringValue("xxxxxxxxxx")).get(),
434 ExpectNotValid(TEST_SOURCE
,
435 scoped_ptr
<Value
>(Value::CreateStringValue("")).get(),
436 schema
.get(), NULL
, "",
437 JSONSchemaValidator::FormatErrorMessage(
438 JSONSchemaValidator::kStringMinLength
, "1"));
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(),
458 ExpectValid(TEST_SOURCE
,
459 scoped_ptr
<Value
>(Value::CreateIntegerValue(50)).get(),
461 ExpectValid(TEST_SOURCE
,
462 scoped_ptr
<Value
>(Value::CreateIntegerValue(100)).get(),
464 ExpectValid(TEST_SOURCE
,
465 scoped_ptr
<Value
>(Value::CreateDoubleValue(88.88)).get(),
470 scoped_ptr
<Value
>(Value::CreateDoubleValue(0.5)).get(),
471 schema
.get(), NULL
, "",
472 JSONSchemaValidator::FormatErrorMessage(
473 JSONSchemaValidator::kNumberMinimum
, "1"));
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());
539 schema
->SetString(schema::kType
, schema::kObject
);
540 ExpectValid(TEST_SOURCE
, scoped_ptr
<Value
>(new DictionaryValue()).get(),
543 schema
->SetString(schema::kType
, schema::kArray
);
544 ExpectValid(TEST_SOURCE
, scoped_ptr
<Value
>(new ListValue()).get(),
547 schema
->SetString(schema::kType
, schema::kString
);
548 ExpectValid(TEST_SOURCE
,
549 scoped_ptr
<Value
>(Value::CreateStringValue("foobar")).get(),
552 schema
->SetString(schema::kType
, schema::kNumber
);
553 ExpectValid(TEST_SOURCE
,
554 scoped_ptr
<Value
>(Value::CreateDoubleValue(88.8)).get(),
556 ExpectValid(TEST_SOURCE
,
557 scoped_ptr
<Value
>(Value::CreateDoubleValue(42)).get(),
559 ExpectValid(TEST_SOURCE
,
560 scoped_ptr
<Value
>(Value::CreateIntegerValue(42)).get(),
562 ExpectValid(TEST_SOURCE
,
563 scoped_ptr
<Value
>(Value::CreateIntegerValue(0)).get(),
566 schema
->SetString(schema::kType
, schema::kInteger
);
567 ExpectValid(TEST_SOURCE
,
568 scoped_ptr
<Value
>(Value::CreateIntegerValue(42)).get(),
570 ExpectValid(TEST_SOURCE
,
571 scoped_ptr
<Value
>(Value::CreateDoubleValue(42)).get(),
573 ExpectValid(TEST_SOURCE
,
574 scoped_ptr
<Value
>(Value::CreateIntegerValue(0)).get(),
576 ExpectValid(TEST_SOURCE
,
578 Value::CreateDoubleValue(pow(2.0, DBL_MANT_DIG
))).get(),
580 ExpectValid(TEST_SOURCE
,
582 Value::CreateDoubleValue(pow(-2.0, DBL_MANT_DIG
))).get(),
585 schema
->SetString(schema::kType
, schema::kBoolean
);
586 ExpectValid(TEST_SOURCE
,
587 scoped_ptr
<Value
>(Value::CreateBooleanValue(false)).get(),
589 ExpectValid(TEST_SOURCE
,
590 scoped_ptr
<Value
>(Value::CreateBooleanValue(true)).get(),
593 schema
->SetString(schema::kType
, schema::kNull
);
594 ExpectValid(TEST_SOURCE
,
595 scoped_ptr
<Value
>(Value::CreateNullValue()).get(),
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
,
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
,
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
,
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
,
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
,
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
,
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
,
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
,
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
,