Comparing version 0.6.1 to 0.6.2
132
lib/typ.js
@@ -15,2 +15,3 @@ // Copyright 2012 The Obvious Corporation. | ||
var assert = require("assert"); | ||
var util = require("util"); | ||
@@ -152,33 +153,47 @@ | ||
/** | ||
* Somewhat more helpful failure message. | ||
* Somewhat more helpful failure message, or just whatever the client | ||
* specified. | ||
*/ | ||
function failType(value, expectedTypeName) { | ||
function failType(value, expectedTypeName, message) { | ||
var gotType = extendedTypeOf(value); | ||
var message = "Expected " + expectedTypeName + "; got " + gotType; | ||
switch (gotType) { | ||
case BOOLEAN: | ||
case DATE: | ||
case INT: | ||
case NUMBER: | ||
case REGEXP: | ||
case UINT: { | ||
message += " (" + value + ")"; | ||
break; | ||
} | ||
case ERROR: { | ||
if (value.message) { | ||
message += " (" + value.message + ")"; | ||
if (isUndefined(message)) { | ||
message = helpfulDetails(); | ||
} else { | ||
message = message.replace(/%[%s]/g, function(escape) { | ||
return (escape === '%%') ? '%' : helpfulDetails(); | ||
}); | ||
} | ||
assert.fail(gotType, expectedTypeName, message, "!=="); | ||
function helpfulDetails() { | ||
var details = "Expected " + expectedTypeName + "; got " + gotType; | ||
switch (gotType) { | ||
case BOOLEAN: | ||
case DATE: | ||
case INT: | ||
case NUMBER: | ||
case REGEXP: | ||
case UINT: { | ||
details += " (" + value + ")"; | ||
break; | ||
} | ||
break; | ||
} | ||
case FUNCTION: { | ||
if (value.name) { | ||
message += " (" + value.name + ")"; | ||
case ERROR: { | ||
if (value.message) { | ||
details += " (" + value.message + ")"; | ||
} | ||
break; | ||
} | ||
break; | ||
case FUNCTION: { | ||
if (value.name) { | ||
details += " (" + value.name + ")"; | ||
} | ||
break; | ||
} | ||
} | ||
return details + "."; | ||
} | ||
assert.fail(gotType, expectedTypeName, message, "!=="); | ||
} | ||
@@ -323,102 +338,101 @@ | ||
function assertArray(x) { | ||
function assertArray(x, message) { | ||
if (!isArray(x)) { | ||
failType(x, ARRAY); | ||
failType(x, ARRAY, message); | ||
} | ||
} | ||
function assertBoolean(x) { | ||
function assertBoolean(x, message) { | ||
if (!isBoolean(x)) { | ||
failType(x, BOOLEAN); | ||
failType(x, BOOLEAN, message); | ||
} | ||
} | ||
function assertBuffer(x) { | ||
function assertBuffer(x, message) { | ||
if (!isBuffer(x)) { | ||
failType(x, BUFFER); | ||
failType(x, BUFFER, message); | ||
} | ||
} | ||
function assertDate(x) { | ||
function assertDate(x, message) { | ||
if (!isDate(x)) { | ||
failType(x, DATE); | ||
failType(x, DATE, message); | ||
} | ||
} | ||
function assertDefined(x) { | ||
function assertDefined(x, message) { | ||
if (!isDefined(x)) { | ||
assert.fail("undefined", "(anything else)", | ||
"Expected defined value", "!=="); | ||
failType(x, "anything but undefined", message); | ||
} | ||
} | ||
function assertUndefined(x) { | ||
function assertUndefined(x, message) { | ||
if (!isUndefined(x)) { | ||
failType(x, UNDEFINED); | ||
failType(x, UNDEFINED, message); | ||
} | ||
} | ||
function assertError(x) { | ||
function assertError(x, message) { | ||
if (!isError(x)) { | ||
failType(x, ERROR); | ||
failType(x, ERROR, message); | ||
} | ||
} | ||
function assertInt(x) { | ||
function assertInt(x, message) { | ||
if (!isInt(x)) { | ||
failType(x, INT); | ||
failType(x, INT, message); | ||
} | ||
} | ||
function assertNull(x) { | ||
function assertNull(x, message) { | ||
if (!isNull(x)) { | ||
failType(x, NULL); | ||
failType(x, NULL, message); | ||
} | ||
} | ||
function assertNullish(x) { | ||
function assertNullish(x, message) { | ||
if (!isNullish(x)) { | ||
failType(x, NULLISH); | ||
failType(x, NULLISH, message); | ||
} | ||
} | ||
function assertNumber(x) { | ||
function assertNumber(x, message) { | ||
if (!isNumber(x)) { | ||
failType(x, NUMBER); | ||
failType(x, NUMBER, message); | ||
} | ||
} | ||
function assertMap(x) { | ||
function assertMap(x, message) { | ||
if (!isMap(x)) { | ||
failType(x, MAP); | ||
failType(x, MAP, message); | ||
} | ||
} | ||
function assertObject(x) { | ||
function assertObject(x, message) { | ||
if (!isObject(x)) { | ||
failType(x, OBJECT); | ||
failType(x, OBJECT, message); | ||
} | ||
} | ||
function assertRegExp(x) { | ||
function assertRegExp(x, message) { | ||
if (!isRegExp(x)) { | ||
failType(x, REGEXP); | ||
failType(x, REGEXP, message); | ||
} | ||
} | ||
function assertString(x) { | ||
function assertString(x, message) { | ||
if (!isString(x)) { | ||
failType(x, STRING); | ||
failType(x, STRING, message); | ||
} | ||
} | ||
function assertUInt(x) { | ||
function assertUInt(x, message) { | ||
if (!isUInt(x)) { | ||
failType(x, UINT); | ||
failType(x, UINT, message); | ||
} | ||
} | ||
function assertFunction(x) { | ||
function assertFunction(x, message) { | ||
if (!isFunction(x)) { | ||
failType(x, FUNCTION); | ||
failType(x, FUNCTION, message); | ||
} | ||
@@ -425,0 +439,0 @@ } |
{ | ||
"name": "typ", | ||
"version": "0.6.1", | ||
"version": "0.6.2", | ||
"keywords": | ||
@@ -5,0 +5,0 @@ ["type", "assert", "predicate"], |
@@ -97,2 +97,10 @@ typ: Type predicates and assertions for Node | ||
Predicates all take a single argument, `value`, the value to test. | ||
Assertions all take two arguments, `value` as with the predicate, and | ||
an optional `message` to replace the default message to report in an | ||
error in the case of failure. The `message` if specified may contain | ||
any number of instances of `%s`, which will be substituted with the | ||
default message. `%%` is substituted with a single `%`. No other `%` | ||
substitution is done. | ||
The following run-down indicates the meaning of the various types, as | ||
@@ -104,3 +112,3 @@ far as this module is concerned. If being a particular type implies | ||
### array: isArray(value), assertArray(value) | ||
### array: isArray(value), assertArray(value, message) | ||
@@ -112,3 +120,3 @@ Implies: object | ||
### boolean: isBoolean(value), assertBoolean(value) | ||
### boolean: isBoolean(value), assertBoolean(value, message) | ||
@@ -118,3 +126,3 @@ The only two booleans are `true` and `false`. Notably, `Boolean` objects | ||
### buffer: isBuffer(value), assertBuffer(value) | ||
### buffer: isBuffer(value), assertBuffer(value, message) | ||
@@ -127,3 +135,3 @@ Implies: object | ||
### date: isDate(value), assertDate(value) | ||
### date: isDate(value), assertDate(value, message) | ||
@@ -135,7 +143,7 @@ Implies: object | ||
### defined: isDefined(value), assertDefined(value) | ||
### defined: isDefined(value), assertDefined(value, message) | ||
All values other than `undefined` are `defined`. | ||
### error: isError(value), assertError(value) | ||
### error: isError(value), assertError(value, message) | ||
@@ -148,3 +156,3 @@ Implies: object | ||
### function: isFunction(value), assertFunction(value) | ||
### function: isFunction(value), assertFunction(value, message) | ||
@@ -157,3 +165,3 @@ Implies: object | ||
### int: isInt(value), assertInt(value) | ||
### int: isInt(value), assertInt(value, message) | ||
@@ -175,3 +183,3 @@ Implies: number | ||
### map: isMap(value), assertMap(value) | ||
### map: isMap(value), assertMap(value, message) | ||
@@ -186,7 +194,7 @@ Implies: object | ||
### null: isNull(value), assertNull(value) | ||
### null: isNull(value), assertNull(value, message) | ||
The only value that is null is `null` per se. | ||
### nullish: isNullish(value), assertNullish(value) | ||
### nullish: isNullish(value), assertNullish(value, message) | ||
@@ -196,3 +204,3 @@ The only two values that are considered to be "nullish" are `null` | ||
### number: isNumber(value), assertNumber(value) | ||
### number: isNumber(value), assertNumber(value, message) | ||
@@ -209,3 +217,3 @@ A number is, well, a numeric value. Numbers are what result from | ||
### object: isObject(value), assertObject(value) | ||
### object: isObject(value), assertObject(value, message) | ||
@@ -216,4 +224,7 @@ An object is an arbitrary mapping of string keys to values. They | ||
### regexp: isRegExp(value), assertRegExp(value) | ||
Notably, `null` is *not* considered to be an object (even though | ||
`typeof null == "object"`). | ||
### regexp: isRegExp(value), assertRegExp(value, message) | ||
Implies: object | ||
@@ -225,3 +236,3 @@ | ||
### string: isString(value), assertString(value) | ||
### string: isString(value), assertString(value, message) | ||
@@ -234,3 +245,3 @@ A string is an ordered sequence of characters. They can be created | ||
### uint: isUint(value), assertUint(value) | ||
### uint: isUInt(value), assertUInt(value, message) | ||
@@ -243,3 +254,3 @@ Implies: int, number | ||
### undefined: isUndefined(value), assertUndefined(value) | ||
### undefined: isUndefined(value), assertUndefined(value, message) | ||
@@ -246,0 +257,0 @@ The only value that is undefined is `undefined`. |
@@ -173,2 +173,13 @@ // Copyright 2012 The Obvious Corporation. | ||
} | ||
if (!passedAssert) { | ||
var message = "sample message " + i + " // " + value; | ||
try { | ||
assertFunc(value, message); | ||
var msg = assertFunc.name + "(" + util.inspect(value) + ", message)"; | ||
assert.fail(true, false, msg); | ||
} catch (ex) { | ||
assert.equal(ex.message, message); | ||
} | ||
} | ||
} | ||
@@ -179,2 +190,18 @@ } | ||
/** | ||
* Test `%` handling in failure messages. | ||
*/ | ||
function testFailureFormat() { | ||
try { | ||
typ.assertString(123, "Well %% howdy %%% [%s]"); | ||
assert.fail(true, false, "Failed to throw."); | ||
} catch (ex) { | ||
assert.equal(ex.message, | ||
"Well % howdy %% [Expected string; got uint (123).]"); | ||
} | ||
// We assume that all the substitution code is shared, and so we | ||
// don't bother testing all possible types. | ||
} | ||
/** | ||
* Test `hasDefaultPrototype()`. | ||
@@ -198,2 +225,3 @@ */ | ||
testAllPredicates(); | ||
testFailureFormat(); | ||
testHasDefaultPrototype(); | ||
@@ -200,0 +228,0 @@ testHasOwnProperty(); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
36554
594
301