Skip to content

Commit ea3de89

Browse files
committed
update to latest assert api and util module
1 parent 34a780c commit ea3de89

File tree

4 files changed

+394
-40
lines changed

4 files changed

+394
-40
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

assert.js

Lines changed: 64 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
(function () {
2-
"use strict"
3-
41
// http://wiki.commonjs.org/wiki/Unit_Testing/1.0
52
//
63
// THIS IS NOT TESTED NOR LIKELY TO WORK OUTSIDE V8!
@@ -25,15 +22,18 @@
2522
// ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
2623
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2724

28-
// UTILITY
29-
var util = require('util');
25+
// when used in node, this will actually load the util module we depend on
26+
// versus loading the builtin util module as happens otherwise
27+
// this is a bug in node module loading as far as I am concerned
28+
var util = require('./node_modules/util');
29+
3030
var pSlice = Array.prototype.slice;
3131

3232
// 1. The assert module provides functions that throw
3333
// AssertionError's when particular conditions are not met. The
3434
// assert module must conform to the following interface.
3535

36-
var assert = exports;
36+
var assert = module.exports = ok;
3737

3838
// 2. The AssertionError is defined in assert.
3939
// new assert.AssertionError({ message: message,
@@ -42,32 +42,52 @@ var assert = exports;
4242

4343
assert.AssertionError = function AssertionError(options) {
4444
this.name = 'AssertionError';
45-
this.message = options.message;
4645
this.actual = options.actual;
4746
this.expected = options.expected;
4847
this.operator = options.operator;
48+
if (options.message) {
49+
this.message = options.message;
50+
this.generatedMessage = false;
51+
} else {
52+
this.message = getMessage(this);
53+
this.generatedMessage = true;
54+
}
4955
var stackStartFunction = options.stackStartFunction || fail;
5056

5157
if (Error.captureStackTrace) {
5258
Error.captureStackTrace(this, stackStartFunction);
5359
}
5460
};
61+
62+
// assert.AssertionError instanceof Error
5563
util.inherits(assert.AssertionError, Error);
5664

57-
assert.AssertionError.prototype.toString = function() {
58-
if (this.message) {
59-
return [this.name + ':', this.message].join(' ');
60-
} else {
61-
return [this.name + ':',
62-
JSON.stringify(this.expected),
63-
this.operator,
64-
JSON.stringify(this.actual)].join(' ');
65+
function replacer(key, value) {
66+
if (util.isUndefined(value)) {
67+
return '' + value;
6568
}
66-
};
69+
if (util.isNumber(value) && (isNaN(value) || !isFinite(value))) {
70+
return value.toString();
71+
}
72+
if (util.isFunction(value) || util.isRegExp(value)) {
73+
return value.toString();
74+
}
75+
return value;
76+
}
6777

68-
// assert.AssertionError instanceof Error
78+
function truncate(s, n) {
79+
if (util.isString(s)) {
80+
return s.length < n ? s : s.slice(0, n);
81+
} else {
82+
return s;
83+
}
84+
}
6985

70-
assert.AssertionError.__proto__ = Error.prototype;
86+
function getMessage(self) {
87+
return truncate(JSON.stringify(self.actual, replacer), 128) + ' ' +
88+
self.operator + ' ' +
89+
truncate(JSON.stringify(self.expected, replacer), 128);
90+
}
7191

7292
// At present only the three keys mentioned above are used and
7393
// understood by the spec. Implementations or sub modules can pass
@@ -96,13 +116,14 @@ assert.fail = fail;
96116
// 4. Pure assertion tests whether a value is truthy, as determined
97117
// by !!guard.
98118
// assert.ok(guard, message_opt);
99-
// This statement is equivalent to assert.equal(true, guard,
119+
// This statement is equivalent to assert.equal(true, !!guard,
100120
// message_opt);. To test strictly for the value true, use
101121
// assert.strictEqual(true, guard, message_opt);.
102122

103-
assert.ok = function ok(value, message) {
104-
if (!!!value) fail(value, true, message, '==', assert.ok);
105-
};
123+
function ok(value, message) {
124+
if (!value) fail(value, true, message, '==', assert.ok);
125+
}
126+
assert.ok = ok;
106127

107128
// 5. The equality assertion tests shallow, coercive equality with
108129
// ==.
@@ -135,7 +156,7 @@ function _deepEqual(actual, expected) {
135156
if (actual === expected) {
136157
return true;
137158

138-
} else if (Buffer.isBuffer(actual) && Buffer.isBuffer(expected)) {
159+
} else if (util.isBuffer(actual) && util.isBuffer(expected)) {
139160
if (actual.length != expected.length) return false;
140161

141162
for (var i = 0; i < actual.length; i++) {
@@ -146,15 +167,25 @@ function _deepEqual(actual, expected) {
146167

147168
// 7.2. If the expected value is a Date object, the actual value is
148169
// equivalent if it is also a Date object that refers to the same time.
149-
} else if (actual instanceof Date && expected instanceof Date) {
170+
} else if (util.isDate(actual) && util.isDate(expected)) {
150171
return actual.getTime() === expected.getTime();
151172

152-
// 7.3. Other pairs that do not both pass typeof value == 'object',
173+
// 7.3 If the expected value is a RegExp object, the actual value is
174+
// equivalent if it is also a RegExp object with the same source and
175+
// properties (`global`, `multiline`, `lastIndex`, `ignoreCase`).
176+
} else if (util.isRegExp(actual) && util.isRegExp(expected)) {
177+
return actual.source === expected.source &&
178+
actual.global === expected.global &&
179+
actual.multiline === expected.multiline &&
180+
actual.lastIndex === expected.lastIndex &&
181+
actual.ignoreCase === expected.ignoreCase;
182+
183+
// 7.4. Other pairs that do not both pass typeof value == 'object',
153184
// equivalence is determined by ==.
154-
} else if (typeof actual != 'object' && typeof expected != 'object') {
185+
} else if (!util.isObject(actual) && !util.isObject(expected)) {
155186
return actual == expected;
156187

157-
// 7.4. For all other Object pairs, including Array objects, equivalence is
188+
// 7.5 For all other Object pairs, including Array objects, equivalence is
158189
// determined by having the same number of owned properties (as verified
159190
// with Object.prototype.hasOwnProperty.call), the same set of keys
160191
// (although not necessarily the same order), equivalent values for every
@@ -165,16 +196,12 @@ function _deepEqual(actual, expected) {
165196
}
166197
}
167198

168-
function isUndefinedOrNull(value) {
169-
return value === null || value === undefined;
170-
}
171-
172199
function isArguments(object) {
173200
return Object.prototype.toString.call(object) == '[object Arguments]';
174201
}
175202

176203
function objEquiv(a, b) {
177-
if (isUndefinedOrNull(a) || isUndefinedOrNull(b))
204+
if (util.isNullOrUndefined(a) || util.isNullOrUndefined(b))
178205
return false;
179206
// an identical 'prototype' property.
180207
if (a.prototype !== b.prototype) return false;
@@ -248,7 +275,7 @@ function expectedException(actual, expected) {
248275
return false;
249276
}
250277

251-
if (expected instanceof RegExp) {
278+
if (Object.prototype.toString.call(expected) == '[object RegExp]') {
252279
return expected.test(actual);
253280
} else if (actual instanceof expected) {
254281
return true;
@@ -262,7 +289,7 @@ function expectedException(actual, expected) {
262289
function _throws(shouldThrow, block, expected, message) {
263290
var actual;
264291

265-
if (typeof expected === 'string') {
292+
if (util.isString(expected)) {
266293
message = expected;
267294
expected = null;
268295
}
@@ -277,11 +304,11 @@ function _throws(shouldThrow, block, expected, message) {
277304
(message ? ' ' + message : '.');
278305

279306
if (shouldThrow && !actual) {
280-
fail('Missing expected exception' + message);
307+
fail(actual, expected, 'Missing expected exception' + message);
281308
}
282309

283310
if (!shouldThrow && expectedException(actual, expected)) {
284-
fail('Got unwanted exception' + message);
311+
fail(actual, expected, 'Got unwanted exception' + message);
285312
}
286313

287314
if ((shouldThrow && actual && expected &&
@@ -298,10 +325,8 @@ assert.throws = function(block, /*optional*/error, /*optional*/message) {
298325
};
299326

300327
// EXTENSION! This is annoying to write outside this module.
301-
assert.doesNotThrow = function(block, /*optional*/error, /*optional*/message) {
328+
assert.doesNotThrow = function(block, /*optional*/message) {
302329
_throws.apply(this, [false].concat(pSlice.call(arguments)));
303330
};
304331

305332
assert.ifError = function(err) { if (err) {throw err;}};
306-
307-
}());

package.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@
1313
},
1414
"main": "./assert.js",
1515
"dependencies": {
16-
"util": ">= 0.4.9"
16+
"util": "0.10.0"
17+
},
18+
"devDependencies": {
19+
"mocha": "1.14.0"
20+
},
21+
"scripts": {
22+
"test": "mocha --ui qunit test.js"
1723
}
1824
}

0 commit comments

Comments
 (0)