OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 function assert(truth) { | 5 function assert(truth) { |
6 if (!truth) | 6 if (!truth) |
7 throw new Error("Assertion failed."); | 7 throw new Error("Assertion failed."); |
8 } | 8 } |
9 | 9 |
10 function assertValid(type, instance, schema, types) { | 10 function assertValid(type, instance, schema, types) { |
(...skipping 21 matching lines...) Expand all Loading... |
32 log("Got expected error: " + validator.errors[i].message + " for path: " + | 32 log("Got expected error: " + validator.errors[i].message + " for path: " + |
33 validator.errors[i].path); | 33 validator.errors[i].path); |
34 } else { | 34 } else { |
35 log("Missed expected error: " + errors[i] + ". Got: " + | 35 log("Missed expected error: " + errors[i] + ". Got: " + |
36 validator.errors[i].message + " instead."); | 36 validator.errors[i].message + " instead."); |
37 assert(false); | 37 assert(false); |
38 } | 38 } |
39 } | 39 } |
40 } | 40 } |
41 | 41 |
| 42 function assertListConsistsOfElements(list, elements) { |
| 43 for (var li = 0; li < list.length; li++) { |
| 44 for (var ei = 0; ei < elements.length && list[li] != elements[ei]; ei++) { } |
| 45 if (ei == elements.length) { |
| 46 log("Expected type not found: " + list[li]); |
| 47 assert(false); |
| 48 } |
| 49 } |
| 50 } |
| 51 |
| 52 function assertEqualSets(set1, set2) { |
| 53 assertListConsistsOfElements(set1, set2); |
| 54 assertListConsistsOfElements(set2, set1); |
| 55 } |
| 56 |
42 function formatError(key, replacements) { | 57 function formatError(key, replacements) { |
43 return chromeHidden.JSONSchemaValidator.formatError(key, replacements); | 58 return chromeHidden.JSONSchemaValidator.formatError(key, replacements); |
44 } | 59 } |
45 | 60 |
46 function testFormatError() { | 61 function testFormatError() { |
47 assert(formatError("propertyRequired") == "Property is required."); | 62 assert(formatError("propertyRequired") == "Property is required."); |
48 assert(formatError("invalidEnum", ["foo, bar"]) == | 63 assert(formatError("invalidEnum", ["foo, bar"]) == |
49 "Value must be one of: [foo, bar]."); | 64 "Value must be one of: [foo, bar]."); |
50 assert(formatError("invalidType", ["foo", "bar"]) == | 65 assert(formatError("invalidType", ["foo", "bar"]) == |
51 "Expected 'foo' but got 'bar'."); | 66 "Expected 'foo' but got 'bar'."); |
(...skipping 390 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
442 [formatError("invalidType", ["integer", "number"])]); | 457 [formatError("invalidType", ["integer", "number"])]); |
443 assertNotValid("Type", 1, {type: "boolean"}, | 458 assertNotValid("Type", 1, {type: "boolean"}, |
444 [formatError("invalidType", ["boolean", "integer"])]); | 459 [formatError("invalidType", ["boolean", "integer"])]); |
445 assertNotValid("Type", false, {type: "null"}, | 460 assertNotValid("Type", false, {type: "null"}, |
446 [formatError("invalidType", ["null", "boolean"])]); | 461 [formatError("invalidType", ["null", "boolean"])]); |
447 assertNotValid("Type", undefined, {type: "null"}, | 462 assertNotValid("Type", undefined, {type: "null"}, |
448 [formatError("invalidType", ["null", "undefined"])]); | 463 [formatError("invalidType", ["null", "undefined"])]); |
449 assertNotValid("Type", {}, {type: "function"}, | 464 assertNotValid("Type", {}, {type: "function"}, |
450 [formatError("invalidType", ["function", "object"])]); | 465 [formatError("invalidType", ["function", "object"])]); |
451 } | 466 } |
| 467 |
| 468 function testGetAllTypesForSchema() { |
| 469 var referencedTypes = [ |
| 470 { |
| 471 id: "ChoicesRef", |
| 472 choices: [ |
| 473 { type: "integer" }, |
| 474 { type: "string" } |
| 475 ] |
| 476 }, |
| 477 { |
| 478 id: "ObjectRef", |
| 479 type: "object", |
| 480 } |
| 481 ]; |
| 482 |
| 483 var arraySchema = { |
| 484 type: "array" |
| 485 }; |
| 486 |
| 487 var choicesSchema = { |
| 488 choices: [ |
| 489 { type: "object" }, |
| 490 { type: "function" } |
| 491 ] |
| 492 }; |
| 493 |
| 494 var objectRefSchema = { |
| 495 $ref: "ObjectRef" |
| 496 }; |
| 497 |
| 498 var complexSchema = { |
| 499 choices: [ |
| 500 { $ref: "ChoicesRef" }, |
| 501 { type: "function" }, |
| 502 { $ref: "ObjectRef" } |
| 503 ] |
| 504 }; |
| 505 |
| 506 var validator = new chromeHidden.JSONSchemaValidator(); |
| 507 validator.addTypes(referencedTypes); |
| 508 |
| 509 var arraySchemaTypes = validator.getAllTypesForSchema(arraySchema); |
| 510 assertEqualSets(arraySchemaTypes, ["array"]); |
| 511 |
| 512 var choicesSchemaTypes = validator.getAllTypesForSchema(choicesSchema); |
| 513 assertEqualSets(choicesSchemaTypes, ["object", "function"]); |
| 514 |
| 515 var objectRefSchemaTypes = validator.getAllTypesForSchema(objectRefSchema); |
| 516 assertEqualSets(objectRefSchemaTypes, ["object"]); |
| 517 |
| 518 var complexSchemaTypes = validator.getAllTypesForSchema(complexSchema); |
| 519 assertEqualSets(complexSchemaTypes, |
| 520 ["integer", "string", "function", "object"]); |
| 521 } |
| 522 |
| 523 function testIsValidSchemaType() { |
| 524 var referencedTypes = [ |
| 525 { |
| 526 id: "ChoicesRef", |
| 527 choices: [ |
| 528 { type: "integer" }, |
| 529 { type: "string" } |
| 530 ] |
| 531 } |
| 532 ]; |
| 533 |
| 534 var objectSchema = { |
| 535 type: "object", |
| 536 optional: true |
| 537 }; |
| 538 |
| 539 var complexSchema = { |
| 540 choices: [ |
| 541 { $ref: "ChoicesRef" }, |
| 542 { type: "function" }, |
| 543 ] |
| 544 }; |
| 545 |
| 546 var validator = new chromeHidden.JSONSchemaValidator(); |
| 547 validator.addTypes(referencedTypes); |
| 548 |
| 549 assert(validator.isValidSchemaType("object", objectSchema)); |
| 550 assert(!validator.isValidSchemaType("integer", objectSchema)); |
| 551 assert(!validator.isValidSchemaType("array", objectSchema)); |
| 552 assert(validator.isValidSchemaType("null", objectSchema)); |
| 553 assert(validator.isValidSchemaType("undefined", objectSchema)); |
| 554 |
| 555 assert(validator.isValidSchemaType("integer", complexSchema)); |
| 556 assert(validator.isValidSchemaType("function", complexSchema)); |
| 557 assert(validator.isValidSchemaType("string", complexSchema)); |
| 558 assert(!validator.isValidSchemaType("object", complexSchema)); |
| 559 assert(!validator.isValidSchemaType("null", complexSchema)); |
| 560 assert(!validator.isValidSchemaType("undefined", complexSchema)); |
| 561 } |
| 562 |
| 563 function testCheckSchemaOverlap() { |
| 564 var referencedTypes = [ |
| 565 { |
| 566 id: "ChoicesRef", |
| 567 choices: [ |
| 568 { type: "integer" }, |
| 569 { type: "string" } |
| 570 ] |
| 571 }, |
| 572 { |
| 573 id: "ObjectRef", |
| 574 type: "object", |
| 575 } |
| 576 ]; |
| 577 |
| 578 var arraySchema = { |
| 579 type: "array" |
| 580 }; |
| 581 |
| 582 var choicesSchema = { |
| 583 choices: [ |
| 584 { type: "object" }, |
| 585 { type: "function" } |
| 586 ] |
| 587 }; |
| 588 |
| 589 var objectRefSchema = { |
| 590 $ref: "ObjectRef" |
| 591 }; |
| 592 |
| 593 var complexSchema = { |
| 594 choices: [ |
| 595 { $ref: "ChoicesRef" }, |
| 596 { type: "function" }, |
| 597 { $ref: "ObjectRef" } |
| 598 ] |
| 599 }; |
| 600 |
| 601 var validator = new chromeHidden.JSONSchemaValidator(); |
| 602 validator.addTypes(referencedTypes); |
| 603 |
| 604 assert(!validator.checkSchemaOverlap(arraySchema, choicesSchema)); |
| 605 assert(!validator.checkSchemaOverlap(arraySchema, objectRefSchema)); |
| 606 assert(!validator.checkSchemaOverlap(arraySchema, complexSchema)); |
| 607 assert(validator.checkSchemaOverlap(choicesSchema, objectRefSchema)); |
| 608 assert(validator.checkSchemaOverlap(choicesSchema, complexSchema)); |
| 609 assert(validator.checkSchemaOverlap(objectRefSchema, complexSchema)); |
| 610 } |
OLD | NEW |