This commit is contained in:
Ryan Dahl 2010-12-01 16:36:23 -08:00
parent a128451004
commit db78043d52
2 changed files with 87 additions and 84 deletions

View File

@ -15,7 +15,7 @@
// The above copyright notice and this permission notice shall be included in // The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software. // all copies or substantial portions of the Software.
// //
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN // AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
@ -33,10 +33,12 @@ var pSlice = Array.prototype.slice;
var assert = exports; var assert = exports;
// 2. The AssertionError is defined in assert. // 2. The AssertionError is defined in assert.
// new assert.AssertionError({message: message, actual: actual, expected: expected}) // new assert.AssertionError({ message: message,
// actual: actual,
// expected: expected })
assert.AssertionError = function AssertionError (options) { assert.AssertionError = function AssertionError(options) {
this.name = "AssertionError"; this.name = 'AssertionError';
this.message = options.message; this.message = options.message;
this.actual = options.actual; this.actual = options.actual;
this.expected = options.expected; this.expected = options.expected;
@ -51,13 +53,12 @@ util.inherits(assert.AssertionError, Error);
assert.AssertionError.prototype.toString = function() { assert.AssertionError.prototype.toString = function() {
if (this.message) { if (this.message) {
return [this.name+":", this.message].join(' '); return [this.name + ':', this.message].join(' ');
} else { } else {
return [ this.name+":" return [this.name + ':',
, JSON.stringify(this.expected ) JSON.stringify(this.expected),
, this.operator this.operator,
, JSON.stringify(this.actual) JSON.stringify(this.actual)].join(' ');
].join(" ");
} }
}; };
@ -97,7 +98,7 @@ assert.fail = fail;
// assert.strictEqual(true, guard, message_opt);. // assert.strictEqual(true, guard, message_opt);.
assert.ok = function ok(value, message) { assert.ok = function ok(value, message) {
if (!!!value) fail(value, true, message, "==", assert.ok); if (!!!value) fail(value, true, message, '==', assert.ok);
}; };
// 5. The equality assertion tests shallow, coercive equality with // 5. The equality assertion tests shallow, coercive equality with
@ -105,7 +106,7 @@ assert.ok = function ok(value, message) {
// assert.equal(actual, expected, message_opt); // assert.equal(actual, expected, message_opt);
assert.equal = function equal(actual, expected, message) { assert.equal = function equal(actual, expected, message) {
if (actual != expected) fail(actual, expected, message, "==", assert.equal); if (actual != expected) fail(actual, expected, message, '==', assert.equal);
}; };
// 6. The non-equality assertion tests for whether two objects are not equal // 6. The non-equality assertion tests for whether two objects are not equal
@ -113,7 +114,7 @@ assert.equal = function equal(actual, expected, message) {
assert.notEqual = function notEqual(actual, expected, message) { assert.notEqual = function notEqual(actual, expected, message) {
if (actual == expected) { if (actual == expected) {
fail(actual, expected, message, "!=", assert.notEqual); fail(actual, expected, message, '!=', assert.notEqual);
} }
}; };
@ -122,7 +123,7 @@ assert.notEqual = function notEqual(actual, expected, message) {
assert.deepEqual = function deepEqual(actual, expected, message) { assert.deepEqual = function deepEqual(actual, expected, message) {
if (!_deepEqual(actual, expected)) { if (!_deepEqual(actual, expected)) {
fail(actual, expected, message, "deepEqual", assert.deepEqual); fail(actual, expected, message, 'deepEqual', assert.deepEqual);
} }
}; };
@ -145,7 +146,7 @@ function _deepEqual(actual, expected) {
} else if (actual instanceof Date && expected instanceof Date) { } else if (actual instanceof Date && expected instanceof Date) {
return actual.getTime() === expected.getTime(); return actual.getTime() === expected.getTime();
// 7.3. Other pairs that do not both pass typeof value == "object", // 7.3. Other pairs that do not both pass typeof value == 'object',
// equivalence is determined by ==. // equivalence is determined by ==.
} else if (typeof actual != 'object' && typeof expected != 'object') { } else if (typeof actual != 'object' && typeof expected != 'object') {
return actual == expected; return actual == expected;
@ -154,25 +155,25 @@ function _deepEqual(actual, expected) {
// determined by having the same number of owned properties (as verified // determined by having the same number of owned properties (as verified
// with Object.prototype.hasOwnProperty.call), the same set of keys // with Object.prototype.hasOwnProperty.call), the same set of keys
// (although not necessarily the same order), equivalent values for every // (although not necessarily the same order), equivalent values for every
// corresponding key, and an identical "prototype" property. Note: this // corresponding key, and an identical 'prototype' property. Note: this
// accounts for both named and indexed properties on Arrays. // accounts for both named and indexed properties on Arrays.
} else { } else {
return objEquiv(actual, expected); return objEquiv(actual, expected);
} }
} }
function isUndefinedOrNull (value) { function isUndefinedOrNull(value) {
return value === null || value === undefined; return value === null || value === undefined;
} }
function isArguments (object) { function isArguments(object) {
return Object.prototype.toString.call(object) == '[object Arguments]'; return Object.prototype.toString.call(object) == '[object Arguments]';
} }
function objEquiv (a, b) { function objEquiv(a, b) {
if (isUndefinedOrNull(a) || isUndefinedOrNull(b)) if (isUndefinedOrNull(a) || isUndefinedOrNull(b))
return false; return false;
// an identical "prototype" property. // an identical 'prototype' property.
if (a.prototype !== b.prototype) return false; if (a.prototype !== b.prototype) return false;
//~~~I've managed to break Object.keys through screwy arguments passing. //~~~I've managed to break Object.keys through screwy arguments passing.
// Converting to array solves the problem. // Converting to array solves the problem.
@ -184,14 +185,15 @@ function objEquiv (a, b) {
b = pSlice.call(b); b = pSlice.call(b);
return _deepEqual(a, b); return _deepEqual(a, b);
} }
try{ try {
var ka = Object.keys(a), var ka = Object.keys(a),
kb = Object.keys(b), kb = Object.keys(b),
key, i; key, i;
} catch (e) {//happens when one is a string literal and the other isn't } catch (e) {//happens when one is a string literal and the other isn't
return false; return false;
} }
// having the same number of owned properties (keys incorporates hasOwnProperty) // having the same number of owned properties (keys incorporates
// hasOwnProperty)
if (ka.length != kb.length) if (ka.length != kb.length)
return false; return false;
//the same set of keys (although not necessarily the same order), //the same set of keys (although not necessarily the same order),
@ -206,8 +208,7 @@ function objEquiv (a, b) {
//~~~possibly expensive deep test //~~~possibly expensive deep test
for (i = ka.length - 1; i >= 0; i--) { for (i = ka.length - 1; i >= 0; i--) {
key = ka[i]; key = ka[i];
if (!_deepEqual(a[key], b[key] )) if (!_deepEqual(a[key], b[key])) return false;
return false;
} }
return true; return true;
} }
@ -217,7 +218,7 @@ function objEquiv (a, b) {
assert.notDeepEqual = function notDeepEqual(actual, expected, message) { assert.notDeepEqual = function notDeepEqual(actual, expected, message) {
if (_deepEqual(actual, expected)) { if (_deepEqual(actual, expected)) {
fail(actual, expected, message, "notDeepEqual", assert.notDeepEqual); fail(actual, expected, message, 'notDeepEqual', assert.notDeepEqual);
} }
}; };
@ -226,16 +227,16 @@ assert.notDeepEqual = function notDeepEqual(actual, expected, message) {
assert.strictEqual = function strictEqual(actual, expected, message) { assert.strictEqual = function strictEqual(actual, expected, message) {
if (actual !== expected) { if (actual !== expected) {
fail(actual, expected, message, "===", assert.strictEqual); fail(actual, expected, message, '===', assert.strictEqual);
} }
}; };
// 10. The strict non-equality assertion tests for strict inequality, as determined by !==. // 10. The strict non-equality assertion tests for strict inequality, as
// assert.notStrictEqual(actual, expected, message_opt); // determined by !==. assert.notStrictEqual(actual, expected, message_opt);
assert.notStrictEqual = function notStrictEqual(actual, expected, message) { assert.notStrictEqual = function notStrictEqual(actual, expected, message) {
if (actual === expected) { if (actual === expected) {
fail(actual, expected, message, "!==", assert.notStrictEqual); fail(actual, expected, message, '!==', assert.notStrictEqual);
} }
}; };
@ -248,15 +249,16 @@ function expectedException(actual, expected) {
if (expected.test(actual)) { if (expected.test(actual)) {
return true; return true;
} }
} else if (actual instanceof expected || expected.call({}, actual) !== false) { } else if (actual instanceof expected ||
expected.call({}, actual) !== false) {
return true; return true;
} }
} }
function _throws (shouldThrow, block, expected, message) { function _throws(shouldThrow, block, expected, message) {
var actual; var actual;
if (typeof expected === "string") { if (typeof expected === 'string') {
message = expected; message = expected;
expected = null; expected = null;
} }
@ -267,19 +269,19 @@ function _throws (shouldThrow, block, expected, message) {
actual = e; actual = e;
} }
message = (expected && expected.name ? " (" + expected.name + ")." : ".") message = (expected && expected.name ? ' (' + expected.name + ').' : '.') +
+ (message ? " " + message : "."); (message ? ' ' + message : '.');
if (shouldThrow && !actual) { if (shouldThrow && !actual) {
fail("Missing expected exception" + message); fail('Missing expected exception' + message);
} }
if (!shouldThrow && expectedException(actual, expected)) { if (!shouldThrow && expectedException(actual, expected)) {
fail("Got unwanted exception" + message); fail('Got unwanted exception' + message);
} }
if ((shouldThrow && actual && expected && !expectedException(actual, expected)) || if ((shouldThrow && actual && expected &&
(!shouldThrow && actual)) { !expectedException(actual, expected)) || (!shouldThrow && actual)) {
throw actual; throw actual;
} }
} }
@ -296,4 +298,4 @@ assert.doesNotThrow = function(block, /*optional*/error, /*optional*/message) {
_throws.apply(this, [false].concat(pSlice.call(arguments))); _throws.apply(this, [false].concat(pSlice.call(arguments)));
}; };
assert.ifError = function (err) { if (err) {throw err;}}; assert.ifError = function(err) { if (err) {throw err;}};

View File

@ -1,26 +1,26 @@
var SlowBuffer = process.binding('buffer').SlowBuffer; var SlowBuffer = process.binding('buffer').SlowBuffer;
function toHex (n) { function toHex(n) {
if (n < 16) return "0" + n.toString(16); if (n < 16) return '0' + n.toString(16);
return n.toString(16); return n.toString(16);
} }
SlowBuffer.prototype.inspect = function () { SlowBuffer.prototype.inspect = function() {
var out = [], var out = [],
len = this.length; len = this.length;
for (var i = 0; i < len; i++) { for (var i = 0; i < len; i++) {
out[i] = toHex(this[i]); out[i] = toHex(this[i]);
} }
return "<SlowBuffer " + out.join(" ") + ">"; return '<SlowBuffer ' + out.join(' ') + '>';
}; };
SlowBuffer.prototype.toString = function (encoding, start, end) { SlowBuffer.prototype.toString = function(encoding, start, end) {
encoding = String(encoding || 'utf8').toLowerCase(); encoding = String(encoding || 'utf8').toLowerCase();
start = +start || 0; start = +start || 0;
if (typeof end == "undefined") end = this.length; if (typeof end == 'undefined') end = this.length;
// Fastpath empty strings // Fastpath empty strings
if (+end == start) { if (+end == start) {
@ -47,7 +47,7 @@ SlowBuffer.prototype.toString = function (encoding, start, end) {
}; };
SlowBuffer.prototype.write = function (string, offset, encoding) { SlowBuffer.prototype.write = function(string, offset, encoding) {
// Support both (string, offset, encoding) // Support both (string, offset, encoding)
// and the legacy (string, encoding, offset) // and the legacy (string, encoding, offset)
if (!isFinite(offset)) { if (!isFinite(offset)) {
@ -80,12 +80,12 @@ SlowBuffer.prototype.write = function (string, offset, encoding) {
// slice(start, end) // slice(start, end)
SlowBuffer.prototype.slice = function (start, end) { SlowBuffer.prototype.slice = function(start, end) {
if (end > this.length) { if (end > this.length) {
throw new Error("oob"); throw new Error('oob');
} }
if (start > end) { if (start > end) {
throw new Error("oob"); throw new Error('oob');
} }
return new Buffer(this, end - start, +start); return new Buffer(this, end - start, +start);
@ -94,7 +94,7 @@ SlowBuffer.prototype.slice = function (start, end) {
// Buffer // Buffer
function Buffer (subject, encoding, offset) { function Buffer(subject, encoding, offset) {
if (!(this instanceof Buffer)) { if (!(this instanceof Buffer)) {
return new Buffer(subject, encoding, offset); return new Buffer(subject, encoding, offset);
} }
@ -122,7 +122,8 @@ function Buffer (subject, encoding, offset) {
break; break;
default: default:
throw new Error("First argument need to be an number, array or string."); throw new Error('First argument need to be an number,' +
'array or string.');
} }
if (this.length > Buffer.poolSize) { if (this.length > Buffer.poolSize) {
@ -155,10 +156,10 @@ function Buffer (subject, encoding, offset) {
exports.SlowBuffer = SlowBuffer; exports.SlowBuffer = SlowBuffer;
exports.Buffer = Buffer; exports.Buffer = Buffer;
Buffer.poolSize = 8*1024; Buffer.poolSize = 8 * 1024;
var pool; var pool;
function allocPool () { function allocPool() {
pool = new SlowBuffer(Buffer.poolSize); pool = new SlowBuffer(Buffer.poolSize);
pool.used = 0; pool.used = 0;
} }
@ -177,24 +178,24 @@ Buffer.prototype.inspect = function inspect() {
for (var i = 0; i < len; i++) { for (var i = 0; i < len; i++) {
out[i] = toHex(this.parent[i + this.offset]); out[i] = toHex(this.parent[i + this.offset]);
} }
return "<Buffer " + out.join(" ") + ">"; return '<Buffer ' + out.join(' ') + '>';
}; };
Buffer.prototype.get = function get (i) { Buffer.prototype.get = function get(i) {
if (i < 0 || i >= this.length) throw new Error("oob"); if (i < 0 || i >= this.length) throw new Error('oob');
return this.parent[this.offset + i]; return this.parent[this.offset + i];
}; };
Buffer.prototype.set = function set (i, v) { Buffer.prototype.set = function set(i, v) {
if (i < 0 || i >= this.length) throw new Error("oob"); if (i < 0 || i >= this.length) throw new Error('oob');
return this.parent[this.offset + i] = v; return this.parent[this.offset + i] = v;
}; };
// write(string, offset = 0, encoding = 'utf8') // write(string, offset = 0, encoding = 'utf8')
Buffer.prototype.write = function write (string, offset, encoding) { Buffer.prototype.write = function(string, offset, encoding) {
if (!isFinite(offset)) { if (!isFinite(offset)) {
var swap = encoding; var swap = encoding;
encoding = offset; encoding = offset;
@ -238,7 +239,7 @@ Buffer.prototype.write = function write (string, offset, encoding) {
// toString(encoding, start=0, end=buffer.length) // toString(encoding, start=0, end=buffer.length)
Buffer.prototype.toString = function (encoding, start, end) { Buffer.prototype.toString = function(encoding, start, end) {
encoding = String(encoding || 'utf8').toLowerCase(); encoding = String(encoding || 'utf8').toLowerCase();
if (typeof start == 'undefined' || start < 0) { if (typeof start == 'undefined' || start < 0) {
@ -247,7 +248,7 @@ Buffer.prototype.toString = function (encoding, start, end) {
start = this.length; start = this.length;
} }
if (typeof end == "undefined" || end > this.length) { if (typeof end == 'undefined' || end > this.length) {
end = this.length; end = this.length;
} else if (end < 0) { } else if (end < 0) {
end = 0; end = 0;
@ -281,28 +282,28 @@ Buffer.byteLength = SlowBuffer.byteLength;
// copy(targetBuffer, targetStart=0, sourceStart=0, sourceEnd=buffer.length) // copy(targetBuffer, targetStart=0, sourceStart=0, sourceEnd=buffer.length)
Buffer.prototype.copy = function copy (target, target_start, start, end) { Buffer.prototype.copy = function(target, target_start, start, end) {
var source = this; var source = this;
start || (start = 0); start || (start = 0);
end || (end = this.length); end || (end = this.length);
target_start || (target_start = 0); target_start || (target_start = 0);
if (end < start) throw new Error("sourceEnd < sourceStart"); if (end < start) throw new Error('sourceEnd < sourceStart');
// Copy 0 bytes; we're done // Copy 0 bytes; we're done
if (end === start) return 0; if (end === start) return 0;
if (target.length == 0 || source.length == 0) return 0; if (target.length == 0 || source.length == 0) return 0;
if (target_start < 0 || target_start >= target.length) { if (target_start < 0 || target_start >= target.length) {
throw new Error("targetStart out of bounds"); throw new Error('targetStart out of bounds');
} }
if (start < 0 || start >= source.length) { if (start < 0 || start >= source.length) {
throw new Error("sourceStart out of bounds"); throw new Error('sourceStart out of bounds');
} }
if (end < 0 || end > source.length) { if (end < 0 || end > source.length) {
throw new Error("sourceEnd out of bounds"); throw new Error('sourceEnd out of bounds');
} }
// Are we oob? // Are we oob?
@ -322,10 +323,10 @@ Buffer.prototype.copy = function copy (target, target_start, start, end) {
// slice(start, end) // slice(start, end)
Buffer.prototype.slice = function (start, end) { Buffer.prototype.slice = function(start, end) {
if (end === undefined) end = this.length; if (end === undefined) end = this.length;
if (end > this.length) throw new Error("oob"); if (end > this.length) throw new Error('oob');
if (start > end) throw new Error("oob"); if (start > end) throw new Error('oob');
return new Buffer(this.parent, end - start, +start + this.offset); return new Buffer(this.parent, end - start, +start + this.offset);
}; };
@ -333,27 +334,27 @@ Buffer.prototype.slice = function (start, end) {
// Legacy methods for backwards compatibility. // Legacy methods for backwards compatibility.
Buffer.prototype.utf8Slice = function (start, end) { Buffer.prototype.utf8Slice = function(start, end) {
return this.toString("utf8", start, end); return this.toString('utf8', start, end);
}; };
Buffer.prototype.binarySlice = function (start, end) { Buffer.prototype.binarySlice = function(start, end) {
return this.toString("binary", start, end); return this.toString('binary', start, end);
}; };
Buffer.prototype.asciiSlice = function (start, end) { Buffer.prototype.asciiSlice = function(start, end) {
return this.toString("ascii", start, end); return this.toString('ascii', start, end);
}; };
Buffer.prototype.utf8Write = function (string, offset) { Buffer.prototype.utf8Write = function(string, offset) {
return this.write(string, offset, "utf8"); return this.write(string, offset, 'utf8');
}; };
Buffer.prototype.binaryWrite = function (string, offset) { Buffer.prototype.binaryWrite = function(string, offset) {
return this.write(string, offset, "binary"); return this.write(string, offset, 'binary');
}; };
Buffer.prototype.asciiWrite = function (string, offset) { Buffer.prototype.asciiWrite = function(string, offset) {
return this.write(string, offset, "ascii"); return this.write(string, offset, 'ascii');
}; };