OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/common/json_schema_validator_unittest_base.h" | 5 #include "chrome/common/json_schema_validator_unittest_base.h" |
6 | 6 |
7 #include <cfloat> | 7 #include <cfloat> |
8 #include <cmath> | 8 #include <cmath> |
9 #include <limits> | 9 #include <limits> |
10 | 10 |
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
132 void JSONSchemaValidatorTestBase::TestEnum() { | 132 void JSONSchemaValidatorTestBase::TestEnum() { |
133 scoped_ptr<DictionaryValue> schema(LoadDictionary("enum_schema.json")); | 133 scoped_ptr<DictionaryValue> schema(LoadDictionary("enum_schema.json")); |
134 | 134 |
135 ExpectValid(TEST_SOURCE, | 135 ExpectValid(TEST_SOURCE, |
136 scoped_ptr<Value>(Value::CreateStringValue("foo")).get(), | 136 scoped_ptr<Value>(Value::CreateStringValue("foo")).get(), |
137 schema.get(), NULL); | 137 schema.get(), NULL); |
138 ExpectValid(TEST_SOURCE, | 138 ExpectValid(TEST_SOURCE, |
139 scoped_ptr<Value>(Value::CreateIntegerValue(42)).get(), | 139 scoped_ptr<Value>(Value::CreateIntegerValue(42)).get(), |
140 schema.get(), NULL); | 140 schema.get(), NULL); |
141 ExpectValid(TEST_SOURCE, | 141 ExpectValid(TEST_SOURCE, |
142 scoped_ptr<Value>(Value::CreateBooleanValue(false)).get(), | 142 scoped_ptr<Value>(base::FalseValue()).get(), |
143 schema.get(), NULL); | 143 schema.get(), NULL); |
144 | 144 |
145 ExpectNotValid(TEST_SOURCE, | 145 ExpectNotValid(TEST_SOURCE, |
146 scoped_ptr<Value>(Value::CreateStringValue("42")).get(), | 146 scoped_ptr<Value>(Value::CreateStringValue("42")).get(), |
147 schema.get(), NULL, "", JSONSchemaValidator::kInvalidEnum); | 147 schema.get(), NULL, "", JSONSchemaValidator::kInvalidEnum); |
148 ExpectNotValid(TEST_SOURCE, | 148 ExpectNotValid(TEST_SOURCE, |
149 scoped_ptr<Value>(Value::CreateNullValue()).get(), | 149 scoped_ptr<Value>(base::NullValue()).get(), |
150 schema.get(), NULL, "", JSONSchemaValidator::kInvalidEnum); | 150 schema.get(), NULL, "", JSONSchemaValidator::kInvalidEnum); |
151 } | 151 } |
152 | 152 |
153 void JSONSchemaValidatorTestBase::TestChoices() { | 153 void JSONSchemaValidatorTestBase::TestChoices() { |
154 scoped_ptr<DictionaryValue> schema(LoadDictionary("choices_schema.json")); | 154 scoped_ptr<DictionaryValue> schema(LoadDictionary("choices_schema.json")); |
155 | 155 |
156 ExpectValid(TEST_SOURCE, | 156 ExpectValid(TEST_SOURCE, |
157 scoped_ptr<Value>(Value::CreateNullValue()).get(), | 157 scoped_ptr<Value>(base::NullValue()).get(), |
158 schema.get(), NULL); | 158 schema.get(), NULL); |
159 ExpectValid(TEST_SOURCE, | 159 ExpectValid(TEST_SOURCE, |
160 scoped_ptr<Value>(Value::CreateIntegerValue(42)).get(), | 160 scoped_ptr<Value>(Value::CreateIntegerValue(42)).get(), |
161 schema.get(), NULL); | 161 schema.get(), NULL); |
162 | 162 |
163 scoped_ptr<DictionaryValue> instance(new DictionaryValue()); | 163 scoped_ptr<DictionaryValue> instance(new DictionaryValue()); |
164 instance->SetString("foo", "bar"); | 164 instance->SetString("foo", "bar"); |
165 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL); | 165 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL); |
166 | 166 |
167 ExpectNotValid(TEST_SOURCE, | 167 ExpectNotValid(TEST_SOURCE, |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
229 DictionaryValue* properties = NULL; | 229 DictionaryValue* properties = NULL; |
230 DictionaryValue* bar_property = NULL; | 230 DictionaryValue* bar_property = NULL; |
231 ASSERT_TRUE(schema->GetDictionary("properties", &properties)); | 231 ASSERT_TRUE(schema->GetDictionary("properties", &properties)); |
232 ASSERT_TRUE(properties->GetDictionary("bar", &bar_property)); | 232 ASSERT_TRUE(properties->GetDictionary("bar", &bar_property)); |
233 | 233 |
234 bar_property->SetBoolean("optional", true); | 234 bar_property->SetBoolean("optional", true); |
235 instance->Remove("extra", NULL); | 235 instance->Remove("extra", NULL); |
236 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL); | 236 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL); |
237 instance->Remove("bar", NULL); | 237 instance->Remove("bar", NULL); |
238 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL); | 238 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL); |
239 instance->Set("bar", Value::CreateNullValue()); | 239 instance->Set("bar", base::NullValue()); |
240 ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), NULL, | 240 ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), NULL, |
241 "bar", JSONSchemaValidator::FormatErrorMessage( | 241 "bar", JSONSchemaValidator::FormatErrorMessage( |
242 JSONSchemaValidator::kInvalidType, "integer", "null")); | 242 JSONSchemaValidator::kInvalidType, "integer", "null")); |
243 instance->SetString("bar", "42"); | 243 instance->SetString("bar", "42"); |
244 ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), NULL, | 244 ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), NULL, |
245 "bar", JSONSchemaValidator::FormatErrorMessage( | 245 "bar", JSONSchemaValidator::FormatErrorMessage( |
246 JSONSchemaValidator::kInvalidType, "integer", "string")); | 246 JSONSchemaValidator::kInvalidType, "integer", "string")); |
247 } | 247 } |
248 | 248 |
249 void JSONSchemaValidatorTestBase::TestTypeReference() { | 249 void JSONSchemaValidatorTestBase::TestTypeReference() { |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
337 instance->Set(0, Value::CreateStringValue("42")); | 337 instance->Set(0, Value::CreateStringValue("42")); |
338 instance->Append(Value::CreateStringValue("anything")); | 338 instance->Append(Value::CreateStringValue("anything")); |
339 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL); | 339 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL); |
340 instance->Set(2, new ListValue()); | 340 instance->Set(2, new ListValue()); |
341 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL); | 341 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL); |
342 | 342 |
343 additional_properties->SetString("type", "boolean"); | 343 additional_properties->SetString("type", "boolean"); |
344 ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), NULL, "2", | 344 ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), NULL, "2", |
345 JSONSchemaValidator::FormatErrorMessage( | 345 JSONSchemaValidator::FormatErrorMessage( |
346 JSONSchemaValidator::kInvalidType, "boolean", "array")); | 346 JSONSchemaValidator::kInvalidType, "boolean", "array")); |
347 instance->Set(2, Value::CreateBooleanValue(false)); | 347 instance->Set(2, base::FalseValue()); |
348 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL); | 348 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL); |
349 | 349 |
350 ListValue* items_schema = NULL; | 350 ListValue* items_schema = NULL; |
351 DictionaryValue* item0_schema = NULL; | 351 DictionaryValue* item0_schema = NULL; |
352 ASSERT_TRUE(schema->GetList("items", &items_schema)); | 352 ASSERT_TRUE(schema->GetList("items", &items_schema)); |
353 ASSERT_TRUE(items_schema->GetDictionary(0, &item0_schema)); | 353 ASSERT_TRUE(items_schema->GetDictionary(0, &item0_schema)); |
354 item0_schema->SetBoolean("optional", true); | 354 item0_schema->SetBoolean("optional", true); |
355 instance->Remove(2, NULL); | 355 instance->Remove(2, NULL); |
356 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL); | 356 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL); |
357 // TODO(aa): I think this is inconsistent with the handling of NULL+optional | 357 // TODO(aa): I think this is inconsistent with the handling of NULL+optional |
358 // for objects. | 358 // for objects. |
359 instance->Set(0, Value::CreateNullValue()); | 359 instance->Set(0, base::NullValue()); |
360 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL); | 360 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL); |
361 instance->Set(0, Value::CreateIntegerValue(42)); | 361 instance->Set(0, Value::CreateIntegerValue(42)); |
362 ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), NULL, "0", | 362 ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), NULL, "0", |
363 JSONSchemaValidator::FormatErrorMessage( | 363 JSONSchemaValidator::FormatErrorMessage( |
364 JSONSchemaValidator::kInvalidType, "string", "integer")); | 364 JSONSchemaValidator::kInvalidType, "string", "integer")); |
365 } | 365 } |
366 | 366 |
367 void JSONSchemaValidatorTestBase::TestArrayNonTuple() { | 367 void JSONSchemaValidatorTestBase::TestArrayNonTuple() { |
368 scoped_ptr<DictionaryValue> schema(new DictionaryValue()); | 368 scoped_ptr<DictionaryValue> schema(new DictionaryValue()); |
369 schema->SetString("type", "array"); | 369 schema->SetString("type", "array"); |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
453 ExpectNotValid( | 453 ExpectNotValid( |
454 TEST_SOURCE, | 454 TEST_SOURCE, |
455 scoped_ptr<Value>(Value::CreateDoubleValue(100.1)).get(), | 455 scoped_ptr<Value>(Value::CreateDoubleValue(100.1)).get(), |
456 schema.get(), NULL, "", | 456 schema.get(), NULL, "", |
457 JSONSchemaValidator::FormatErrorMessage( | 457 JSONSchemaValidator::FormatErrorMessage( |
458 JSONSchemaValidator::kNumberMaximum, "100")); | 458 JSONSchemaValidator::kNumberMaximum, "100")); |
459 } | 459 } |
460 | 460 |
461 void JSONSchemaValidatorTestBase::TestTypeClassifier() { | 461 void JSONSchemaValidatorTestBase::TestTypeClassifier() { |
462 EXPECT_EQ("boolean", JSONSchemaValidator::GetJSONSchemaType( | 462 EXPECT_EQ("boolean", JSONSchemaValidator::GetJSONSchemaType( |
463 scoped_ptr<Value>(Value::CreateBooleanValue(true)).get())); | 463 scoped_ptr<Value>(base::TrueValue()).get())); |
464 EXPECT_EQ("boolean", JSONSchemaValidator::GetJSONSchemaType( | 464 EXPECT_EQ("boolean", JSONSchemaValidator::GetJSONSchemaType( |
465 scoped_ptr<Value>(Value::CreateBooleanValue(false)).get())); | 465 scoped_ptr<Value>(base::FalseValue()).get())); |
466 | 466 |
467 // It doesn't matter whether the C++ type is 'integer' or 'real'. If the | 467 // It doesn't matter whether the C++ type is 'integer' or 'real'. If the |
468 // number is integral and within the representable range of integers in | 468 // number is integral and within the representable range of integers in |
469 // double, it's classified as 'integer'. | 469 // double, it's classified as 'integer'. |
470 EXPECT_EQ("integer", JSONSchemaValidator::GetJSONSchemaType( | 470 EXPECT_EQ("integer", JSONSchemaValidator::GetJSONSchemaType( |
471 scoped_ptr<Value>(Value::CreateIntegerValue(42)).get())); | 471 scoped_ptr<Value>(Value::CreateIntegerValue(42)).get())); |
472 EXPECT_EQ("integer", JSONSchemaValidator::GetJSONSchemaType( | 472 EXPECT_EQ("integer", JSONSchemaValidator::GetJSONSchemaType( |
473 scoped_ptr<Value>(Value::CreateIntegerValue(0)).get())); | 473 scoped_ptr<Value>(Value::CreateIntegerValue(0)).get())); |
474 EXPECT_EQ("integer", JSONSchemaValidator::GetJSONSchemaType( | 474 EXPECT_EQ("integer", JSONSchemaValidator::GetJSONSchemaType( |
475 scoped_ptr<Value>(Value::CreateDoubleValue(42)).get())); | 475 scoped_ptr<Value>(Value::CreateDoubleValue(42)).get())); |
(...skipping 15 matching lines...) Expand all Loading... |
491 scoped_ptr<Value>(Value::CreateDoubleValue( | 491 scoped_ptr<Value>(Value::CreateDoubleValue( |
492 pow(-2.0, DBL_MANT_DIG) * 2)).get())); | 492 pow(-2.0, DBL_MANT_DIG) * 2)).get())); |
493 | 493 |
494 EXPECT_EQ("string", JSONSchemaValidator::GetJSONSchemaType( | 494 EXPECT_EQ("string", JSONSchemaValidator::GetJSONSchemaType( |
495 scoped_ptr<Value>(Value::CreateStringValue("foo")).get())); | 495 scoped_ptr<Value>(Value::CreateStringValue("foo")).get())); |
496 EXPECT_EQ("array", JSONSchemaValidator::GetJSONSchemaType( | 496 EXPECT_EQ("array", JSONSchemaValidator::GetJSONSchemaType( |
497 scoped_ptr<Value>(new ListValue()).get())); | 497 scoped_ptr<Value>(new ListValue()).get())); |
498 EXPECT_EQ("object", JSONSchemaValidator::GetJSONSchemaType( | 498 EXPECT_EQ("object", JSONSchemaValidator::GetJSONSchemaType( |
499 scoped_ptr<Value>(new DictionaryValue()).get())); | 499 scoped_ptr<Value>(new DictionaryValue()).get())); |
500 EXPECT_EQ("null", JSONSchemaValidator::GetJSONSchemaType( | 500 EXPECT_EQ("null", JSONSchemaValidator::GetJSONSchemaType( |
501 scoped_ptr<Value>(Value::CreateNullValue()).get())); | 501 scoped_ptr<Value>(base::NullValue()).get())); |
502 } | 502 } |
503 | 503 |
504 void JSONSchemaValidatorTestBase::TestTypes() { | 504 void JSONSchemaValidatorTestBase::TestTypes() { |
505 scoped_ptr<DictionaryValue> schema(new DictionaryValue()); | 505 scoped_ptr<DictionaryValue> schema(new DictionaryValue()); |
506 | 506 |
507 // valid | 507 // valid |
508 schema->SetString("type", "object"); | 508 schema->SetString("type", "object"); |
509 ExpectValid(TEST_SOURCE, scoped_ptr<Value>(new DictionaryValue()).get(), | 509 ExpectValid(TEST_SOURCE, scoped_ptr<Value>(new DictionaryValue()).get(), |
510 schema.get(), NULL); | 510 schema.get(), NULL); |
511 | 511 |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
546 scoped_ptr<Value>( | 546 scoped_ptr<Value>( |
547 Value::CreateDoubleValue(pow(2.0, DBL_MANT_DIG))).get(), | 547 Value::CreateDoubleValue(pow(2.0, DBL_MANT_DIG))).get(), |
548 schema.get(), NULL); | 548 schema.get(), NULL); |
549 ExpectValid(TEST_SOURCE, | 549 ExpectValid(TEST_SOURCE, |
550 scoped_ptr<Value>( | 550 scoped_ptr<Value>( |
551 Value::CreateDoubleValue(pow(-2.0, DBL_MANT_DIG))).get(), | 551 Value::CreateDoubleValue(pow(-2.0, DBL_MANT_DIG))).get(), |
552 schema.get(), NULL); | 552 schema.get(), NULL); |
553 | 553 |
554 schema->SetString("type", "boolean"); | 554 schema->SetString("type", "boolean"); |
555 ExpectValid(TEST_SOURCE, | 555 ExpectValid(TEST_SOURCE, |
556 scoped_ptr<Value>(Value::CreateBooleanValue(false)).get(), | 556 scoped_ptr<Value>(base::FalseValue()).get(), |
557 schema.get(), NULL); | 557 schema.get(), NULL); |
558 ExpectValid(TEST_SOURCE, | 558 ExpectValid(TEST_SOURCE, |
559 scoped_ptr<Value>(Value::CreateBooleanValue(true)).get(), | 559 scoped_ptr<Value>(base::TrueValue()).get(), |
560 schema.get(), NULL); | 560 schema.get(), NULL); |
561 | 561 |
562 schema->SetString("type", "null"); | 562 schema->SetString("type", "null"); |
563 ExpectValid(TEST_SOURCE, | 563 ExpectValid(TEST_SOURCE, |
564 scoped_ptr<Value>(Value::CreateNullValue()).get(), | 564 scoped_ptr<Value>(base::NullValue()).get(), |
565 schema.get(), NULL); | 565 schema.get(), NULL); |
566 | 566 |
567 // not valid | 567 // not valid |
568 schema->SetString("type", "object"); | 568 schema->SetString("type", "object"); |
569 ExpectNotValid(TEST_SOURCE, scoped_ptr<Value>(new ListValue()).get(), | 569 ExpectNotValid(TEST_SOURCE, scoped_ptr<Value>(new ListValue()).get(), |
570 schema.get(), NULL, "", | 570 schema.get(), NULL, "", |
571 JSONSchemaValidator::FormatErrorMessage( | 571 JSONSchemaValidator::FormatErrorMessage( |
572 JSONSchemaValidator::kInvalidType, "object", "array")); | 572 JSONSchemaValidator::kInvalidType, "object", "array")); |
573 | 573 |
574 schema->SetString("type", "object"); | 574 schema->SetString("type", "object"); |
575 ExpectNotValid(TEST_SOURCE, scoped_ptr<Value>(Value::CreateNullValue()).get(), | 575 ExpectNotValid(TEST_SOURCE, scoped_ptr<Value>(base::NullValue()).get(), |
576 schema.get(), NULL, "", | 576 schema.get(), NULL, "", |
577 JSONSchemaValidator::FormatErrorMessage( | 577 JSONSchemaValidator::FormatErrorMessage( |
578 JSONSchemaValidator::kInvalidType, "object", "null")); | 578 JSONSchemaValidator::kInvalidType, "object", "null")); |
579 | 579 |
580 schema->SetString("type", "array"); | 580 schema->SetString("type", "array"); |
581 ExpectNotValid(TEST_SOURCE, | 581 ExpectNotValid(TEST_SOURCE, |
582 scoped_ptr<Value>(Value::CreateIntegerValue(42)).get(), | 582 scoped_ptr<Value>(Value::CreateIntegerValue(42)).get(), |
583 schema.get(), NULL, "", | 583 schema.get(), NULL, "", |
584 JSONSchemaValidator::FormatErrorMessage( | 584 JSONSchemaValidator::FormatErrorMessage( |
585 JSONSchemaValidator::kInvalidType, "array", "integer")); | 585 JSONSchemaValidator::kInvalidType, "array", "integer")); |
(...skipping 28 matching lines...) Expand all Loading... |
614 | 614 |
615 schema->SetString("type", "boolean"); | 615 schema->SetString("type", "boolean"); |
616 ExpectNotValid(TEST_SOURCE, | 616 ExpectNotValid(TEST_SOURCE, |
617 scoped_ptr<Value>(Value::CreateIntegerValue(1)).get(), | 617 scoped_ptr<Value>(Value::CreateIntegerValue(1)).get(), |
618 schema.get(), NULL, "", | 618 schema.get(), NULL, "", |
619 JSONSchemaValidator::FormatErrorMessage( | 619 JSONSchemaValidator::FormatErrorMessage( |
620 JSONSchemaValidator::kInvalidType, "boolean", "integer")); | 620 JSONSchemaValidator::kInvalidType, "boolean", "integer")); |
621 | 621 |
622 schema->SetString("type", "null"); | 622 schema->SetString("type", "null"); |
623 ExpectNotValid(TEST_SOURCE, | 623 ExpectNotValid(TEST_SOURCE, |
624 scoped_ptr<Value>(Value::CreateBooleanValue(false)).get(), | 624 scoped_ptr<Value>(base::FalseValue()).get(), |
625 schema.get(), NULL, "", | 625 schema.get(), NULL, "", |
626 JSONSchemaValidator::FormatErrorMessage( | 626 JSONSchemaValidator::FormatErrorMessage( |
627 JSONSchemaValidator::kInvalidType, "null", "boolean")); | 627 JSONSchemaValidator::kInvalidType, "null", "boolean")); |
628 } | 628 } |
OLD | NEW |