Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(160)

Side by Side Diff: chrome/test/data/extensions/json_schema_test.js

Issue 9317072: Allow omitting optional parameters for Extensions API functions (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: null and undefined arguments now handled. Comments added and variables renamed. Created 8 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 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
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 422 matching lines...) Expand 10 before | Expand all | Expand 10 after
474 [formatError("invalidType", ["integer", "number"])]); 489 [formatError("invalidType", ["integer", "number"])]);
475 assertNotValid("Type", 1, {type: "boolean"}, 490 assertNotValid("Type", 1, {type: "boolean"},
476 [formatError("invalidType", ["boolean", "integer"])]); 491 [formatError("invalidType", ["boolean", "integer"])]);
477 assertNotValid("Type", false, {type: "null"}, 492 assertNotValid("Type", false, {type: "null"},
478 [formatError("invalidType", ["null", "boolean"])]); 493 [formatError("invalidType", ["null", "boolean"])]);
479 assertNotValid("Type", undefined, {type: "null"}, 494 assertNotValid("Type", undefined, {type: "null"},
480 [formatError("invalidType", ["null", "undefined"])]); 495 [formatError("invalidType", ["null", "undefined"])]);
481 assertNotValid("Type", {}, {type: "function"}, 496 assertNotValid("Type", {}, {type: "function"},
482 [formatError("invalidType", ["function", "object"])]); 497 [formatError("invalidType", ["function", "object"])]);
483 } 498 }
499
500 function testGetAllTypesForSchema() {
501 var referencedTypes = [
502 {
503 id: "ChoicesRef",
504 choices: [
505 { type: "integer" },
506 { type: "string" }
507 ]
508 },
509 {
510 id: "ObjectRef",
511 type: "object",
512 }
513 ];
514
515 var arraySchema = {
516 type: "array"
517 };
518
519 var choicesSchema = {
520 choices: [
521 { type: "object" },
522 { type: "function" }
523 ]
524 };
525
526 var objectRefSchema = {
527 $ref: "ObjectRef"
528 };
529
530 var complexSchema = {
531 choices: [
532 { $ref: "ChoicesRef" },
533 { type: "function" },
534 { $ref: "ObjectRef" }
535 ]
536 };
537
538 var validator = new chromeHidden.JSONSchemaValidator();
539 validator.addTypes(referencedTypes);
540
541 var arraySchemaTypes = validator.getAllTypesForSchema(arraySchema);
542 assertEqualSets(arraySchemaTypes, ["array"]);
543
544 var choicesSchemaTypes = validator.getAllTypesForSchema(choicesSchema);
545 assertEqualSets(choicesSchemaTypes, ["object", "function"]);
546
547 var objectRefSchemaTypes = validator.getAllTypesForSchema(objectRefSchema);
548 assertEqualSets(objectRefSchemaTypes, ["object"]);
549
550 var complexSchemaTypes = validator.getAllTypesForSchema(complexSchema);
551 assertEqualSets(complexSchemaTypes,
552 ["integer", "string", "function", "object"]);
553 }
554
555 function testIsValidSchemaType() {
556 var referencedTypes = [
557 {
558 id: "ChoicesRef",
559 choices: [
560 { type: "integer" },
561 { type: "string" }
562 ]
563 }
564 ];
565
566 var objectSchema = {
567 type: "object",
568 optional: true
569 };
570
571 var complexSchema = {
572 choices: [
573 { $ref: "ChoicesRef" },
574 { type: "function" },
575 ]
576 };
577
578 var validator = new chromeHidden.JSONSchemaValidator();
579 validator.addTypes(referencedTypes);
580
581 assert(validator.isValidSchemaType("object", objectSchema));
582 assert(!validator.isValidSchemaType("integer", objectSchema));
583 assert(!validator.isValidSchemaType("array", objectSchema));
584 assert(validator.isValidSchemaType("null", objectSchema));
585 assert(validator.isValidSchemaType("undefined", objectSchema));
586
587 assert(validator.isValidSchemaType("integer", complexSchema));
588 assert(validator.isValidSchemaType("function", complexSchema));
589 assert(validator.isValidSchemaType("string", complexSchema));
590 assert(!validator.isValidSchemaType("object", complexSchema));
591 assert(!validator.isValidSchemaType("null", complexSchema));
592 assert(!validator.isValidSchemaType("undefined", complexSchema));
593 }
594
595 function testCheckSchemaOverlap() {
596 var referencedTypes = [
597 {
598 id: "ChoicesRef",
599 choices: [
600 { type: "integer" },
601 { type: "string" }
602 ]
603 },
604 {
605 id: "ObjectRef",
606 type: "object",
607 }
608 ];
609
610 var arraySchema = {
611 type: "array"
612 };
613
614 var choicesSchema = {
615 choices: [
616 { type: "object" },
617 { type: "function" }
618 ]
619 };
620
621 var objectRefSchema = {
622 $ref: "ObjectRef"
623 };
624
625 var complexSchema = {
626 choices: [
627 { $ref: "ChoicesRef" },
628 { type: "function" },
629 { $ref: "ObjectRef" }
630 ]
631 };
632
633 var validator = new chromeHidden.JSONSchemaValidator();
634 validator.addTypes(referencedTypes);
635
636 assert(!validator.checkSchemaOverlap(arraySchema, choicesSchema));
637 assert(!validator.checkSchemaOverlap(arraySchema, objectRefSchema));
638 assert(!validator.checkSchemaOverlap(arraySchema, complexSchema));
639 assert(validator.checkSchemaOverlap(choicesSchema, objectRefSchema));
640 assert(validator.checkSchemaOverlap(choicesSchema, complexSchema));
641 assert(validator.checkSchemaOverlap(objectRefSchema, complexSchema));
642 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698