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 "components/json_schema/json_schema_validator_unittest_base.h"
11 #include "base/base_paths.h"
12 #include "base/files/file_util.h"
13 #include "base/json/json_file_value_serializer.h"
14 #include "base/logging.h"
15 #include "base/memory/scoped_ptr.h"
16 #include "base/path_service.h"
17 #include "base/strings/stringprintf.h"
18 #include "base/values.h"
19 #include "components/json_schema/json_schema_constants.h"
20 #include "components/json_schema/json_schema_validator.h"
22 namespace schema
= json_schema_constants
;
26 #define TEST_SOURCE base::StringPrintf("%s:%i", __FILE__, __LINE__)
28 base::Value
* LoadValue(const std::string
& filename
) {
30 PathService::Get(base::DIR_SOURCE_ROOT
, &path
);
31 path
= path
.AppendASCII("components")
34 .AppendASCII("json_schema")
35 .AppendASCII(filename
);
36 EXPECT_TRUE(base::PathExists(path
));
38 std::string error_message
;
39 JSONFileValueDeserializer
deserializer(path
);
40 base::Value
* result
= deserializer
.Deserialize(NULL
, &error_message
);
42 ADD_FAILURE() << "Could not parse JSON: " << error_message
;
46 base::Value
* LoadValue(const std::string
& filename
, base::Value::Type type
) {
47 scoped_ptr
<base::Value
> result(LoadValue(filename
));
50 if (!result
->IsType(type
)) {
51 ADD_FAILURE() << "Expected type " << type
<< ", got: " << result
->GetType();
54 return result
.release();
57 base::ListValue
* LoadList(const std::string
& filename
) {
58 return static_cast<base::ListValue
*>(
59 LoadValue(filename
, base::Value::TYPE_LIST
));
62 base::DictionaryValue
* LoadDictionary(const std::string
& filename
) {
63 return static_cast<base::DictionaryValue
*>(
64 LoadValue(filename
, base::Value::TYPE_DICTIONARY
));
70 JSONSchemaValidatorTestBase::JSONSchemaValidatorTestBase() {
73 void JSONSchemaValidatorTestBase::RunTests() {
89 void JSONSchemaValidatorTestBase::TestComplex() {
90 scoped_ptr
<base::DictionaryValue
> schema(
91 LoadDictionary("complex_schema.json"));
92 scoped_ptr
<base::ListValue
> instance(LoadList("complex_instance.json"));
94 ASSERT_TRUE(schema
.get());
95 ASSERT_TRUE(instance
.get());
97 ExpectValid(TEST_SOURCE
, instance
.get(), schema
.get(), NULL
);
98 instance
->Remove(instance
->GetSize() - 1, NULL
);
99 ExpectValid(TEST_SOURCE
, instance
.get(), schema
.get(), NULL
);
100 instance
->Append(new base::DictionaryValue());
101 ExpectNotValid(TEST_SOURCE
, instance
.get(), schema
.get(), NULL
, "1",
102 JSONSchemaValidator::FormatErrorMessage(
103 JSONSchemaValidator::kInvalidType
,
106 instance
->Remove(instance
->GetSize() - 1, NULL
);
108 base::DictionaryValue
* item
= NULL
;
109 ASSERT_TRUE(instance
->GetDictionary(0, &item
));
110 item
->SetString("url", "xxxxxxxxxxx");
112 ExpectNotValid(TEST_SOURCE
, instance
.get(), schema
.get(), NULL
,
114 JSONSchemaValidator::FormatErrorMessage(
115 JSONSchemaValidator::kStringMaxLength
, "10"));
118 void JSONSchemaValidatorTestBase::TestStringPattern() {
119 scoped_ptr
<base::DictionaryValue
> schema(new base::DictionaryValue());
120 schema
->SetString(schema::kType
, schema::kString
);
121 schema
->SetString(schema::kPattern
, "foo+");
123 ExpectValid(TEST_SOURCE
,
124 scoped_ptr
<base::Value
>(new base::StringValue("foo")).get(),
126 ExpectValid(TEST_SOURCE
,
127 scoped_ptr
<base::Value
>(new base::StringValue("foooooo")).get(),
129 ExpectNotValid(TEST_SOURCE
,
130 scoped_ptr
<base::Value
>(new base::StringValue("bar")).get(),
134 JSONSchemaValidator::FormatErrorMessage(
135 JSONSchemaValidator::kStringPattern
, "foo+"));
138 void JSONSchemaValidatorTestBase::TestEnum() {
139 scoped_ptr
<base::DictionaryValue
> schema(LoadDictionary("enum_schema.json"));
141 ExpectValid(TEST_SOURCE
,
142 scoped_ptr
<base::Value
>(new base::StringValue("foo")).get(),
144 ExpectValid(TEST_SOURCE
,
145 scoped_ptr
<base::Value
>(new base::FundamentalValue(42)).get(),
147 ExpectValid(TEST_SOURCE
,
148 scoped_ptr
<base::Value
>(new base::FundamentalValue(false)).get(),
151 ExpectNotValid(TEST_SOURCE
,
152 scoped_ptr
<base::Value
>(new base::StringValue("42")).get(),
156 JSONSchemaValidator::kInvalidEnum
);
157 ExpectNotValid(TEST_SOURCE
, base::Value::CreateNullValue().get(),
158 schema
.get(), NULL
, std::string(),
159 JSONSchemaValidator::kInvalidEnum
);
162 void JSONSchemaValidatorTestBase::TestChoices() {
163 scoped_ptr
<base::DictionaryValue
> schema(
164 LoadDictionary("choices_schema.json"));
166 ExpectValid(TEST_SOURCE
, base::Value::CreateNullValue().get(), schema
.get(),
168 ExpectValid(TEST_SOURCE
,
169 scoped_ptr
<base::Value
>(new base::FundamentalValue(42)).get(),
172 scoped_ptr
<base::DictionaryValue
> instance(new base::DictionaryValue());
173 instance
->SetString("foo", "bar");
174 ExpectValid(TEST_SOURCE
, instance
.get(), schema
.get(), NULL
);
176 ExpectNotValid(TEST_SOURCE
,
177 scoped_ptr
<base::Value
>(new base::StringValue("foo")).get(),
181 JSONSchemaValidator::kInvalidChoice
);
182 ExpectNotValid(TEST_SOURCE
,
183 scoped_ptr
<base::Value
>(new base::ListValue()).get(),
187 JSONSchemaValidator::kInvalidChoice
);
189 instance
->SetInteger("foo", 42);
190 ExpectNotValid(TEST_SOURCE
,
195 JSONSchemaValidator::kInvalidChoice
);
198 void JSONSchemaValidatorTestBase::TestExtends() {
202 void JSONSchemaValidatorTestBase::TestObject() {
203 scoped_ptr
<base::DictionaryValue
> schema(new base::DictionaryValue());
204 schema
->SetString(schema::kType
, schema::kObject
);
205 schema
->SetString("properties.foo.type", schema::kString
);
206 schema
->SetString("properties.bar.type", schema::kInteger
);
208 scoped_ptr
<base::DictionaryValue
> instance(new base::DictionaryValue());
209 instance
->SetString("foo", "foo");
210 instance
->SetInteger("bar", 42);
212 ExpectValid(TEST_SOURCE
, instance
.get(), schema
.get(), NULL
);
214 instance
->SetBoolean("extra", true);
215 ExpectNotValid(TEST_SOURCE
, instance
.get(), schema
.get(), NULL
,
216 "extra", JSONSchemaValidator::kUnexpectedProperty
);
217 instance
->Remove("extra", NULL
);
219 instance
->Remove("bar", NULL
);
220 ExpectNotValid(TEST_SOURCE
, instance
.get(), schema
.get(), NULL
, "bar",
221 JSONSchemaValidator::kObjectPropertyIsRequired
);
223 instance
->SetString("bar", "42");
224 ExpectNotValid(TEST_SOURCE
, instance
.get(), schema
.get(), NULL
, "bar",
225 JSONSchemaValidator::FormatErrorMessage(
226 JSONSchemaValidator::kInvalidType
,
229 instance
->SetInteger("bar", 42);
231 // Test "patternProperties".
232 instance
->SetInteger("extra", 42);
233 ExpectNotValid(TEST_SOURCE
, instance
.get(), schema
.get(), NULL
,
234 "extra", JSONSchemaValidator::kUnexpectedProperty
);
235 schema
->SetString("patternProperties.extra+.type",
237 ExpectValid(TEST_SOURCE
, instance
.get(), schema
.get(), NULL
);
238 instance
->Remove("extra", NULL
);
239 instance
->SetInteger("extraaa", 42);
240 ExpectValid(TEST_SOURCE
, instance
.get(), schema
.get(), NULL
);
241 instance
->Remove("extraaa", NULL
);
242 instance
->SetInteger("extr", 42);
243 ExpectNotValid(TEST_SOURCE
, instance
.get(), schema
.get(), NULL
,
244 "extr", JSONSchemaValidator::kUnexpectedProperty
);
245 instance
->Remove("extr", NULL
);
246 schema
->Remove(schema::kPatternProperties
, NULL
);
248 // Test "patternProperties" and "properties" schemas are both checked if
250 schema
->SetString("patternProperties.fo+.type", schema::kInteger
);
251 ExpectNotValid(TEST_SOURCE
, instance
.get(), schema
.get(), NULL
, "foo",
252 JSONSchemaValidator::FormatErrorMessage(
253 JSONSchemaValidator::kInvalidType
,
256 instance
->SetInteger("foo", 123);
257 ExpectNotValid(TEST_SOURCE
, instance
.get(), schema
.get(), NULL
, "foo",
258 JSONSchemaValidator::FormatErrorMessage(
259 JSONSchemaValidator::kInvalidType
,
262 instance
->SetString("foo", "foo");
263 schema
->Remove(schema::kPatternProperties
, NULL
);
265 // Test additional properties.
266 base::DictionaryValue
* additional_properties
= new base::DictionaryValue();
267 additional_properties
->SetString(schema::kType
, schema::kAny
);
268 schema
->Set(schema::kAdditionalProperties
, additional_properties
);
270 instance
->SetBoolean("extra", true);
271 ExpectValid(TEST_SOURCE
, instance
.get(), schema
.get(), NULL
);
273 instance
->SetString("extra", "foo");
274 ExpectValid(TEST_SOURCE
, instance
.get(), schema
.get(), NULL
);
276 additional_properties
->SetString(schema::kType
, schema::kBoolean
);
277 instance
->SetBoolean("extra", true);
278 ExpectValid(TEST_SOURCE
, instance
.get(), schema
.get(), NULL
);
280 instance
->SetString("extra", "foo");
281 ExpectNotValid(TEST_SOURCE
, instance
.get(), schema
.get(), NULL
,
282 "extra", JSONSchemaValidator::FormatErrorMessage(
283 JSONSchemaValidator::kInvalidType
,
286 instance
->Remove("extra", NULL
);
288 base::DictionaryValue
* properties
= NULL
;
289 base::DictionaryValue
* bar_property
= NULL
;
290 ASSERT_TRUE(schema
->GetDictionary(schema::kProperties
, &properties
));
291 ASSERT_TRUE(properties
->GetDictionary("bar", &bar_property
));
293 bar_property
->SetBoolean(schema::kOptional
, true);
294 ExpectValid(TEST_SOURCE
, instance
.get(), schema
.get(), NULL
);
295 instance
->Remove("bar", NULL
);
296 ExpectValid(TEST_SOURCE
, instance
.get(), schema
.get(), NULL
);
297 instance
->Set("bar", base::Value::CreateNullValue());
298 ExpectNotValid(TEST_SOURCE
, instance
.get(), schema
.get(), NULL
,
299 "bar", JSONSchemaValidator::FormatErrorMessage(
300 JSONSchemaValidator::kInvalidType
,
303 instance
->SetString("bar", "42");
304 ExpectNotValid(TEST_SOURCE
, instance
.get(), schema
.get(), NULL
,
305 "bar", JSONSchemaValidator::FormatErrorMessage(
306 JSONSchemaValidator::kInvalidType
,
310 // Verify that JSON parser handles dot in "patternProperties" well.
311 schema
.reset(LoadDictionary("pattern_properties_dot.json"));
312 ASSERT_TRUE(schema
->GetDictionary(schema::kPatternProperties
, &properties
));
313 ASSERT_TRUE(properties
->HasKey("^.$"));
315 instance
.reset(new base::DictionaryValue());
316 instance
->SetString("a", "whatever");
317 ExpectValid(TEST_SOURCE
, instance
.get(), schema
.get(), NULL
);
318 instance
->SetString("foo", "bar");
319 ExpectNotValid(TEST_SOURCE
, instance
.get(), schema
.get(), NULL
,
320 "foo", JSONSchemaValidator::kUnexpectedProperty
);
323 void JSONSchemaValidatorTestBase::TestTypeReference() {
324 scoped_ptr
<base::ListValue
> types(LoadList("reference_types.json"));
325 ASSERT_TRUE(types
.get());
327 scoped_ptr
<base::DictionaryValue
> schema(new base::DictionaryValue());
328 schema
->SetString(schema::kType
, schema::kObject
);
329 schema
->SetString("properties.foo.type", schema::kString
);
330 schema
->SetString("properties.bar.$ref", "Max10Int");
331 schema
->SetString("properties.baz.$ref", "MinLengthString");
333 scoped_ptr
<base::DictionaryValue
> schema_inline(new base::DictionaryValue());
334 schema_inline
->SetString(schema::kType
, schema::kObject
);
335 schema_inline
->SetString("properties.foo.type", schema::kString
);
336 schema_inline
->SetString("properties.bar.id", "NegativeInt");
337 schema_inline
->SetString("properties.bar.type", schema::kInteger
);
338 schema_inline
->SetInteger("properties.bar.maximum", 0);
339 schema_inline
->SetString("properties.baz.$ref", "NegativeInt");
341 scoped_ptr
<base::DictionaryValue
> instance(new base::DictionaryValue());
342 instance
->SetString("foo", "foo");
343 instance
->SetInteger("bar", 4);
344 instance
->SetString("baz", "ab");
346 scoped_ptr
<base::DictionaryValue
> instance_inline(
347 new base::DictionaryValue());
348 instance_inline
->SetString("foo", "foo");
349 instance_inline
->SetInteger("bar", -4);
350 instance_inline
->SetInteger("baz", -2);
352 ExpectValid(TEST_SOURCE
, instance
.get(), schema
.get(), types
.get());
353 ExpectValid(TEST_SOURCE
, instance_inline
.get(), schema_inline
.get(), NULL
);
355 // Validation failure, but successful schema reference.
356 instance
->SetString("baz", "a");
357 ExpectNotValid(TEST_SOURCE
, instance
.get(), schema
.get(), types
.get(),
358 "baz", JSONSchemaValidator::FormatErrorMessage(
359 JSONSchemaValidator::kStringMinLength
, "2"));
361 instance_inline
->SetInteger("bar", 20);
362 ExpectNotValid(TEST_SOURCE
, instance_inline
.get(), schema_inline
.get(), NULL
,
363 "bar", JSONSchemaValidator::FormatErrorMessage(
364 JSONSchemaValidator::kNumberMaximum
, "0"));
366 // Remove MinLengthString type.
367 types
->Remove(types
->GetSize() - 1, NULL
);
368 instance
->SetString("baz", "ab");
369 ExpectNotValid(TEST_SOURCE
, instance
.get(), schema
.get(), types
.get(),
370 "bar", JSONSchemaValidator::FormatErrorMessage(
371 JSONSchemaValidator::kUnknownTypeReference
,
374 // Remove internal type "NegativeInt".
375 schema_inline
->Remove("properties.bar", NULL
);
376 instance_inline
->Remove("bar", NULL
);
377 ExpectNotValid(TEST_SOURCE
, instance_inline
.get(), schema_inline
.get(), NULL
,
378 "baz", JSONSchemaValidator::FormatErrorMessage(
379 JSONSchemaValidator::kUnknownTypeReference
,
383 void JSONSchemaValidatorTestBase::TestArrayTuple() {
384 scoped_ptr
<base::DictionaryValue
> schema(
385 LoadDictionary("array_tuple_schema.json"));
386 ASSERT_TRUE(schema
.get());
388 scoped_ptr
<base::ListValue
> instance(new base::ListValue());
389 instance
->Append(new base::StringValue("42"));
390 instance
->Append(new base::FundamentalValue(42));
392 ExpectValid(TEST_SOURCE
, instance
.get(), schema
.get(), NULL
);
394 instance
->Append(new base::StringValue("anything"));
395 ExpectNotValid(TEST_SOURCE
,
400 JSONSchemaValidator::FormatErrorMessage(
401 JSONSchemaValidator::kArrayMaxItems
, "2"));
403 instance
->Remove(1, NULL
);
404 instance
->Remove(1, NULL
);
405 ExpectNotValid(TEST_SOURCE
, instance
.get(), schema
.get(), NULL
, "1",
406 JSONSchemaValidator::kArrayItemRequired
);
408 instance
->Set(0, new base::FundamentalValue(42));
409 instance
->Append(new base::FundamentalValue(42));
410 ExpectNotValid(TEST_SOURCE
, instance
.get(), schema
.get(), NULL
, "0",
411 JSONSchemaValidator::FormatErrorMessage(
412 JSONSchemaValidator::kInvalidType
,
416 base::DictionaryValue
* additional_properties
= new base::DictionaryValue();
417 additional_properties
->SetString(schema::kType
, schema::kAny
);
418 schema
->Set(schema::kAdditionalProperties
, additional_properties
);
419 instance
->Set(0, new base::StringValue("42"));
420 instance
->Append(new base::StringValue("anything"));
421 ExpectValid(TEST_SOURCE
, instance
.get(), schema
.get(), NULL
);
422 instance
->Set(2, new base::ListValue());
423 ExpectValid(TEST_SOURCE
, instance
.get(), schema
.get(), NULL
);
425 additional_properties
->SetString(schema::kType
, schema::kBoolean
);
426 ExpectNotValid(TEST_SOURCE
, instance
.get(), schema
.get(), NULL
, "2",
427 JSONSchemaValidator::FormatErrorMessage(
428 JSONSchemaValidator::kInvalidType
,
431 instance
->Set(2, new base::FundamentalValue(false));
432 ExpectValid(TEST_SOURCE
, instance
.get(), schema
.get(), NULL
);
434 base::ListValue
* items_schema
= NULL
;
435 base::DictionaryValue
* item0_schema
= NULL
;
436 ASSERT_TRUE(schema
->GetList(schema::kItems
, &items_schema
));
437 ASSERT_TRUE(items_schema
->GetDictionary(0, &item0_schema
));
438 item0_schema
->SetBoolean(schema::kOptional
, true);
439 instance
->Remove(2, NULL
);
440 ExpectValid(TEST_SOURCE
, instance
.get(), schema
.get(), NULL
);
441 // TODO(aa): I think this is inconsistent with the handling of NULL+optional
443 instance
->Set(0, base::Value::CreateNullValue());
444 ExpectValid(TEST_SOURCE
, instance
.get(), schema
.get(), NULL
);
445 instance
->Set(0, new base::FundamentalValue(42));
446 ExpectNotValid(TEST_SOURCE
, instance
.get(), schema
.get(), NULL
, "0",
447 JSONSchemaValidator::FormatErrorMessage(
448 JSONSchemaValidator::kInvalidType
,
453 void JSONSchemaValidatorTestBase::TestArrayNonTuple() {
454 scoped_ptr
<base::DictionaryValue
> schema(new base::DictionaryValue());
455 schema
->SetString(schema::kType
, schema::kArray
);
456 schema
->SetString("items.type", schema::kString
);
457 schema
->SetInteger(schema::kMinItems
, 2);
458 schema
->SetInteger(schema::kMaxItems
, 3);
460 scoped_ptr
<base::ListValue
> instance(new base::ListValue());
461 instance
->Append(new base::StringValue("x"));
462 instance
->Append(new base::StringValue("x"));
464 ExpectValid(TEST_SOURCE
, instance
.get(), schema
.get(), NULL
);
465 instance
->Append(new base::StringValue("x"));
466 ExpectValid(TEST_SOURCE
, instance
.get(), schema
.get(), NULL
);
468 instance
->Append(new base::StringValue("x"));
469 ExpectNotValid(TEST_SOURCE
,
474 JSONSchemaValidator::FormatErrorMessage(
475 JSONSchemaValidator::kArrayMaxItems
, "3"));
476 instance
->Remove(1, NULL
);
477 instance
->Remove(1, NULL
);
478 instance
->Remove(1, NULL
);
479 ExpectNotValid(TEST_SOURCE
,
484 JSONSchemaValidator::FormatErrorMessage(
485 JSONSchemaValidator::kArrayMinItems
, "2"));
487 instance
->Remove(1, NULL
);
488 instance
->Append(new base::FundamentalValue(42));
489 ExpectNotValid(TEST_SOURCE
, instance
.get(), schema
.get(), NULL
, "1",
490 JSONSchemaValidator::FormatErrorMessage(
491 JSONSchemaValidator::kInvalidType
,
496 void JSONSchemaValidatorTestBase::TestString() {
497 scoped_ptr
<base::DictionaryValue
> schema(new base::DictionaryValue());
498 schema
->SetString(schema::kType
, schema::kString
);
499 schema
->SetInteger(schema::kMinLength
, 1);
500 schema
->SetInteger(schema::kMaxLength
, 10);
502 ExpectValid(TEST_SOURCE
,
503 scoped_ptr
<base::Value
>(new base::StringValue("x")).get(),
505 ExpectValid(TEST_SOURCE
,
506 scoped_ptr
<base::Value
>(
507 new base::StringValue("xxxxxxxxxx")).get(),
512 scoped_ptr
<base::Value
>(new base::StringValue(std::string())).get(),
516 JSONSchemaValidator::FormatErrorMessage(
517 JSONSchemaValidator::kStringMinLength
, "1"));
520 scoped_ptr
<base::Value
>(new base::StringValue("xxxxxxxxxxx")).get(),
524 JSONSchemaValidator::FormatErrorMessage(
525 JSONSchemaValidator::kStringMaxLength
, "10"));
528 void JSONSchemaValidatorTestBase::TestNumber() {
529 scoped_ptr
<base::DictionaryValue
> schema(new base::DictionaryValue());
530 schema
->SetString(schema::kType
, schema::kNumber
);
531 schema
->SetInteger(schema::kMinimum
, 1);
532 schema
->SetInteger(schema::kMaximum
, 100);
533 schema
->SetInteger("maxDecimal", 2);
535 ExpectValid(TEST_SOURCE
,
536 scoped_ptr
<base::Value
>(new base::FundamentalValue(1)).get(),
538 ExpectValid(TEST_SOURCE
,
539 scoped_ptr
<base::Value
>(new base::FundamentalValue(50)).get(),
541 ExpectValid(TEST_SOURCE
,
542 scoped_ptr
<base::Value
>(new base::FundamentalValue(100)).get(),
544 ExpectValid(TEST_SOURCE
,
545 scoped_ptr
<base::Value
>(new base::FundamentalValue(88.88)).get(),
548 ExpectNotValid(TEST_SOURCE
,
549 scoped_ptr
<base::Value
>(new base::FundamentalValue(0.5)).get(),
553 JSONSchemaValidator::FormatErrorMessage(
554 JSONSchemaValidator::kNumberMinimum
, "1"));
557 scoped_ptr
<base::Value
>(new base::FundamentalValue(100.1)).get(),
561 JSONSchemaValidator::FormatErrorMessage(
562 JSONSchemaValidator::kNumberMaximum
, "100"));
565 void JSONSchemaValidatorTestBase::TestTypeClassifier() {
566 EXPECT_EQ(std::string(schema::kBoolean
),
567 JSONSchemaValidator::GetJSONSchemaType(
568 scoped_ptr
<base::Value
>(
569 new base::FundamentalValue(true)).get()));
570 EXPECT_EQ(std::string(schema::kBoolean
),
571 JSONSchemaValidator::GetJSONSchemaType(
572 scoped_ptr
<base::Value
>(
573 new base::FundamentalValue(false)).get()));
575 // It doesn't matter whether the C++ type is 'integer' or 'real'. If the
576 // number is integral and within the representable range of integers in
577 // double, it's classified as 'integer'.
578 EXPECT_EQ(std::string(schema::kInteger
),
579 JSONSchemaValidator::GetJSONSchemaType(
580 scoped_ptr
<base::Value
>(new base::FundamentalValue(42)).get()));
581 EXPECT_EQ(std::string(schema::kInteger
),
582 JSONSchemaValidator::GetJSONSchemaType(
583 scoped_ptr
<base::Value
>(new base::FundamentalValue(0)).get()));
584 EXPECT_EQ(std::string(schema::kInteger
),
585 JSONSchemaValidator::GetJSONSchemaType(
586 scoped_ptr
<base::Value
>(new base::FundamentalValue(42)).get()));
587 EXPECT_EQ(std::string(schema::kInteger
),
588 JSONSchemaValidator::GetJSONSchemaType(scoped_ptr
<base::Value
>(
589 new base::FundamentalValue(pow(2.0, DBL_MANT_DIG
))).get()));
590 EXPECT_EQ(std::string(schema::kInteger
),
591 JSONSchemaValidator::GetJSONSchemaType(scoped_ptr
<base::Value
>(
592 new base::FundamentalValue(pow(-2.0, DBL_MANT_DIG
))).get()));
594 // "number" is only used for non-integral numbers, or numbers beyond what
595 // double can accurately represent.
596 EXPECT_EQ(std::string(schema::kNumber
),
597 JSONSchemaValidator::GetJSONSchemaType(
598 scoped_ptr
<base::Value
>(
599 new base::FundamentalValue(88.8)).get()));
600 EXPECT_EQ(std::string(schema::kNumber
),
601 JSONSchemaValidator::GetJSONSchemaType(scoped_ptr
<base::Value
>(
602 new base::FundamentalValue(pow(2.0, DBL_MANT_DIG
) * 2)).get()));
603 EXPECT_EQ(std::string(schema::kNumber
),
604 JSONSchemaValidator::GetJSONSchemaType(scoped_ptr
<base::Value
>(
605 new base::FundamentalValue(
606 pow(-2.0, DBL_MANT_DIG
) * 2)).get()));
608 EXPECT_EQ(std::string(schema::kString
),
609 JSONSchemaValidator::GetJSONSchemaType(
610 scoped_ptr
<base::Value
>(new base::StringValue("foo")).get()));
611 EXPECT_EQ(std::string(schema::kArray
),
612 JSONSchemaValidator::GetJSONSchemaType(
613 scoped_ptr
<base::Value
>(new base::ListValue()).get()));
614 EXPECT_EQ(std::string(schema::kObject
),
615 JSONSchemaValidator::GetJSONSchemaType(
616 scoped_ptr
<base::Value
>(new base::DictionaryValue()).get()));
617 EXPECT_EQ(std::string(schema::kNull
),
618 JSONSchemaValidator::GetJSONSchemaType(
619 base::Value::CreateNullValue().get()));
622 void JSONSchemaValidatorTestBase::TestTypes() {
623 scoped_ptr
<base::DictionaryValue
> schema(new base::DictionaryValue());
626 schema
->SetString(schema::kType
, schema::kObject
);
627 ExpectValid(TEST_SOURCE
,
628 scoped_ptr
<base::Value
>(new base::DictionaryValue()).get(),
631 schema
->SetString(schema::kType
, schema::kArray
);
632 ExpectValid(TEST_SOURCE
, scoped_ptr
<base::Value
>(new base::ListValue()).get(),
635 schema
->SetString(schema::kType
, schema::kString
);
636 ExpectValid(TEST_SOURCE
,
637 scoped_ptr
<base::Value
>(new base::StringValue("foobar")).get(),
640 schema
->SetString(schema::kType
, schema::kNumber
);
641 ExpectValid(TEST_SOURCE
,
642 scoped_ptr
<base::Value
>(new base::FundamentalValue(88.8)).get(),
644 ExpectValid(TEST_SOURCE
,
645 scoped_ptr
<base::Value
>(new base::FundamentalValue(42)).get(),
647 ExpectValid(TEST_SOURCE
,
648 scoped_ptr
<base::Value
>(new base::FundamentalValue(42)).get(),
650 ExpectValid(TEST_SOURCE
,
651 scoped_ptr
<base::Value
>(new base::FundamentalValue(0)).get(),
654 schema
->SetString(schema::kType
, schema::kInteger
);
655 ExpectValid(TEST_SOURCE
,
656 scoped_ptr
<base::Value
>(new base::FundamentalValue(42)).get(),
658 ExpectValid(TEST_SOURCE
,
659 scoped_ptr
<base::Value
>(new base::FundamentalValue(42)).get(),
661 ExpectValid(TEST_SOURCE
,
662 scoped_ptr
<base::Value
>(new base::FundamentalValue(0)).get(),
664 ExpectValid(TEST_SOURCE
,
665 scoped_ptr
<base::Value
>(
666 new base::FundamentalValue(pow(2.0, DBL_MANT_DIG
))).get(),
668 ExpectValid(TEST_SOURCE
,
669 scoped_ptr
<base::Value
>(
670 new base::FundamentalValue(pow(-2.0, DBL_MANT_DIG
))).get(),
673 schema
->SetString(schema::kType
, schema::kBoolean
);
674 ExpectValid(TEST_SOURCE
,
675 scoped_ptr
<base::Value
>(new base::FundamentalValue(false)).get(),
677 ExpectValid(TEST_SOURCE
,
678 scoped_ptr
<base::Value
>(new base::FundamentalValue(true)).get(),
681 schema
->SetString(schema::kType
, schema::kNull
);
682 ExpectValid(TEST_SOURCE
, base::Value::CreateNullValue().get(), schema
.get(),
686 schema
->SetString(schema::kType
, schema::kObject
);
689 scoped_ptr
<base::Value
>(new base::ListValue()).get(),
693 JSONSchemaValidator::FormatErrorMessage(
694 JSONSchemaValidator::kInvalidType
, schema::kObject
, schema::kArray
));
696 schema
->SetString(schema::kType
, schema::kObject
);
698 TEST_SOURCE
, base::Value::CreateNullValue().get(), schema
.get(), NULL
,
700 JSONSchemaValidator::FormatErrorMessage(JSONSchemaValidator::kInvalidType
,
701 schema::kObject
, schema::kNull
));
703 schema
->SetString(schema::kType
, schema::kArray
);
706 scoped_ptr
<base::Value
>(new base::FundamentalValue(42)).get(),
710 JSONSchemaValidator::FormatErrorMessage(
711 JSONSchemaValidator::kInvalidType
, schema::kArray
, schema::kInteger
));
713 schema
->SetString(schema::kType
, schema::kString
);
716 scoped_ptr
<base::Value
>(new base::FundamentalValue(42)).get(),
720 JSONSchemaValidator::FormatErrorMessage(JSONSchemaValidator::kInvalidType
,
724 schema
->SetString(schema::kType
, schema::kNumber
);
727 scoped_ptr
<base::Value
>(new base::StringValue("42")).get(),
731 JSONSchemaValidator::FormatErrorMessage(
732 JSONSchemaValidator::kInvalidType
, schema::kNumber
, schema::kString
));
734 schema
->SetString(schema::kType
, schema::kInteger
);
737 scoped_ptr
<base::Value
>(new base::FundamentalValue(88.8)).get(),
741 JSONSchemaValidator::kInvalidTypeIntegerNumber
);
743 schema
->SetString(schema::kType
, schema::kBoolean
);
746 scoped_ptr
<base::Value
>(new base::FundamentalValue(1)).get(),
750 JSONSchemaValidator::FormatErrorMessage(JSONSchemaValidator::kInvalidType
,
754 schema
->SetString(schema::kType
, schema::kNull
);
757 scoped_ptr
<base::Value
>(new base::FundamentalValue(false)).get(),
761 JSONSchemaValidator::FormatErrorMessage(
762 JSONSchemaValidator::kInvalidType
, schema::kNull
, schema::kBoolean
));