OLD | NEW |
1 // Copyright (c) 2009 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) { |
11 var validator = new chromeHidden.JSONSchemaValidator(); | 11 var validator = new chromeHidden.JSONSchemaValidator(); |
(...skipping 402 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
414 [formatError("numberFiniteNotNan", ["Infinity"]), | 414 [formatError("numberFiniteNotNan", ["Infinity"]), |
415 formatError("numberMaxValue", [schema.maximum]) | 415 formatError("numberMaxValue", [schema.maximum]) |
416 ]); | 416 ]); |
417 | 417 |
418 assertNotValid("Number", Number.NEGATIVE_INFINITY, schema, | 418 assertNotValid("Number", Number.NEGATIVE_INFINITY, schema, |
419 [formatError("numberFiniteNotNan", ["-Infinity"]), | 419 [formatError("numberFiniteNotNan", ["-Infinity"]), |
420 formatError("numberMinValue", [schema.minimum]) | 420 formatError("numberMinValue", [schema.minimum]) |
421 ]); | 421 ]); |
422 } | 422 } |
423 | 423 |
| 424 function testIntegerBounds() { |
| 425 assertValid("Number", 0, {type:"integer"}); |
| 426 assertValid("Number", -1, {type:"integer"}); |
| 427 assertValid("Number", 2147483647, {type:"integer"}); |
| 428 assertValid("Number", -2147483648, {type:"integer"}); |
| 429 assertNotValid("Number", 0.5, {type:"integer"}, |
| 430 [formatError("numberIntValue", [])]); |
| 431 assertNotValid("Number", 10000000000, {type:"integer"}, |
| 432 [formatError("numberIntValue", [])]); |
| 433 assertNotValid("Number", 2147483647.5, {type:"integer"}, |
| 434 [formatError("numberIntValue", [])]); |
| 435 assertNotValid("Number", 2147483648, {type:"integer"}, |
| 436 [formatError("numberIntValue", [])]); |
| 437 assertNotValid("Number", 2147483649, {type:"integer"}, |
| 438 [formatError("numberIntValue", [])]); |
| 439 assertNotValid("Number", -2147483649, {type:"integer"}, |
| 440 [formatError("numberIntValue", [])]); |
| 441 } |
| 442 |
424 function testType() { | 443 function testType() { |
425 // valid | 444 // valid |
426 assertValid("Type", {}, {type:"object"}); | 445 assertValid("Type", {}, {type:"object"}); |
427 assertValid("Type", [], {type:"array"}); | 446 assertValid("Type", [], {type:"array"}); |
428 assertValid("Type", function(){}, {type:"function"}); | 447 assertValid("Type", function(){}, {type:"function"}); |
429 assertValid("Type", "foobar", {type:"string"}); | 448 assertValid("Type", "foobar", {type:"string"}); |
430 assertValid("Type", "", {type:"string"}); | 449 assertValid("Type", "", {type:"string"}); |
431 assertValid("Type", 88.8, {type:"number"}); | 450 assertValid("Type", 88.8, {type:"number"}); |
432 assertValid("Type", 42, {type:"number"}); | 451 assertValid("Type", 42, {type:"number"}); |
433 assertValid("Type", 0, {type:"number"}); | 452 assertValid("Type", 0, {type:"number"}); |
(...skipping 21 matching lines...) Expand all Loading... |
455 [formatError("invalidType", ["integer", "number"])]); | 474 [formatError("invalidType", ["integer", "number"])]); |
456 assertNotValid("Type", 1, {type: "boolean"}, | 475 assertNotValid("Type", 1, {type: "boolean"}, |
457 [formatError("invalidType", ["boolean", "integer"])]); | 476 [formatError("invalidType", ["boolean", "integer"])]); |
458 assertNotValid("Type", false, {type: "null"}, | 477 assertNotValid("Type", false, {type: "null"}, |
459 [formatError("invalidType", ["null", "boolean"])]); | 478 [formatError("invalidType", ["null", "boolean"])]); |
460 assertNotValid("Type", undefined, {type: "null"}, | 479 assertNotValid("Type", undefined, {type: "null"}, |
461 [formatError("invalidType", ["null", "undefined"])]); | 480 [formatError("invalidType", ["null", "undefined"])]); |
462 assertNotValid("Type", {}, {type: "function"}, | 481 assertNotValid("Type", {}, {type: "function"}, |
463 [formatError("invalidType", ["function", "object"])]); | 482 [formatError("invalidType", ["function", "object"])]); |
464 } | 483 } |
OLD | NEW |