Browse Source

fixes to avoid relying on buffer and to work in older browsers

buffer-dep
Calvin Metcalf 9 years ago
parent
commit
45b19f6707
No known key found for this signature in database GPG Key ID: F617F2120633E5F2
  1. 3
      .zuul.yml
  2. 116
      assert.js
  3. 9
      package.json
  4. 15
      test.js

3
.zuul.yml

@ -1,4 +1,5 @@
ui: mocha-qunit
tunnel: ngrok
browsers:
- name: chrome
version: latest
@ -8,3 +9,5 @@ browsers:
version: latest
- name: ie
version: 9..latest
- name: microsoftedge
version: latest

116
assert.js

@ -1,3 +1,47 @@
'use strict';
// compare and isBuffer taken from https://github.com/feross/buffer/blob/680e9e5e488f22aac27599a57dc844a6315928dd/index.js
// original notice:
/*!
* The buffer module from node.js, for the browser.
*
* @author Feross Aboukhadijeh <feross@feross.org> <http://feross.org>
* @license MIT
*/
function compare(a, b) {
if (a === b) {
return 0;
}
var x = a.length;
var y = b.length;
for (var i = 0, len = Math.min(x, y); i < len; ++i) {
if (a[i] !== b[i]) {
x = a[i];
y = b[i];
break;
}
}
if (x < y) {
return -1;
}
if (y < x) {
return 1;
}
return 0;
}
function isBuffer(b) {
if (global.Buffer && typeof global.Buffer.isBuffer === 'function') {
return global.Buffer.isBuffer(b);
}
return !!(b != null && b._isBuffer);
}
// based on node assert, original notice:
// http://wiki.commonjs.org/wiki/Unit_Testing/1.0
//
// THIS IS NOT TESTED NOR LIKELY TO WORK OUTSIDE V8!
@ -22,30 +66,7 @@
// ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
'use strict';
// UTILITY
function compare(bufa, bufb) {
var cmpLen = Math.min(bufa, bufb);
if (cmpLen <= 0) {
return 0;
}
var i = -1;
var a,b;
while (++i < cmpLen) {
a = bufa[i];
b = bufb[i];
if (a < b) {
return -1;
} else if (a > b) {
return 1;
}
}
return 0;
}
var util = require('util/');
var Buffer = require('buffer').Buffer;
var BufferShim = require('buffer-shims');
var hasOwn = Object.prototype.hasOwnProperty;
var pSlice = Array.prototype.slice;
var functionsHaveNames = (function () {
@ -55,6 +76,9 @@ function pToString (obj) {
return Object.prototype.toString.call(obj);
}
function isView(arrbuf) {
if (isBuffer(arrbuf)) {
return false;
}
if (typeof global.ArrayBuffer !== 'function') {
return false;
}
@ -110,25 +134,25 @@ assert.AssertionError = function AssertionError(options) {
}
var stackStartFunction = options.stackStartFunction || fail;
if (Error.captureStackTrace) {
Error.captureStackTrace(this, stackStartFunction);
} else {
// non v8 browsers so we can have a stacktrace
var err = new Error();
if (err.stack) {
var out = err.stack;
// try to strip useless frames
var fn_name = getName(stackStartFunction);
var idx = out.indexOf('\n' + fn_name);
if (idx >= 0) {
// once we have located the function frame
// we need to strip out everything before it (and its line)
var next_line = out.indexOf('\n', idx + 1);
out = out.substring(next_line + 1);
}
this.stack = out;
}
Error.captureStackTrace(this, stackStartFunction);
} else {
// non v8 browsers so we can have a stacktrace
var err = new Error();
if (err.stack) {
var out = err.stack;
// try to strip useless frames
var fn_name = getName(stackStartFunction);
var idx = out.indexOf('\n' + fn_name);
if (idx >= 0) {
// once we have located the function frame
// we need to strip out everything before it (and its line)
var next_line = out.indexOf('\n', idx + 1);
out = out.substring(next_line + 1);
}
this.stack = out;
}
}
};
@ -228,7 +252,7 @@ function _deepEqual(actual, expected, strict, memos) {
// 7.1. All identical values are equivalent, as determined by ===.
if (actual === expected) {
return true;
} else if (Buffer.isBuffer(actual) && Buffer.isBuffer(expected)) {
} else if (isBuffer(actual) && isBuffer(expected)) {
return compare(actual, expected) === 0;
// 7.2. If the expected value is a Date object, the actual value is
@ -262,8 +286,8 @@ function _deepEqual(actual, expected, strict, memos) {
pToString(actual) === pToString(expected) &&
!(actual instanceof Float32Array ||
actual instanceof Float64Array)) {
return compare(BufferShim.from(actual.buffer),
BufferShim.from(expected.buffer)) === 0;
return compare(new Uint8Array(actual.buffer),
new Uint8Array(expected.buffer)) === 0;
// 7.5 For all other Object pairs, including Array objects, equivalence is
// determined by having the same number of owned properties (as verified
@ -271,6 +295,8 @@ function _deepEqual(actual, expected, strict, memos) {
// (although not necessarily the same order), equivalent values for every
// corresponding key, and an identical 'prototype' property. Note: this
// accounts for both named and indexed properties on Arrays.
} else if (isBuffer(actual) !== isBuffer(expected)) {
return false;
} else {
memos = memos || {actual: [], expected: []};

9
package.json

@ -12,18 +12,19 @@
},
"main": "./assert.js",
"dependencies": {
"buffer-shims": "1.0.0",
"util": "0.10.3"
},
"devDependencies": {
"zuul": "~3.9.0",
"mocha": "~1.21.4"
"mocha": "~1.21.4",
"zuul": "~3.10.0",
"zuul-ngrok": "^4.0.0"
},
"license": "MIT",
"scripts": {
"test-node": "mocha --ui qunit test.js",
"test-browser": "zuul -- test.js",
"test": "npm run test-node && npm run test-browser",
"test-native": "TEST_NATIVE=true mocha --ui qunit test.js"
"test-native": "TEST_NATIVE=true mocha --ui qunit test.js",
"browser-local": "zuul --no-coverage --local 8000 -- test.js"
}
}

15
test.js

@ -165,6 +165,16 @@ function tests (assert, what) {
assert.doesNotThrow(makeBlock(assert.deepEqual, new Boolean(true), {}));
});
test('assert.deepEqual - Buffers', function () {
assert.doesNotThrow(makeBlock(assert.deepEqual, new Buffer([1, 2, 3]), new Buffer([1, 2, 3])));
if (typeof global.Uint8Array === 'function') {
assert.throws(makeBlock(assert.deepEqual, new Buffer([1, 2, 3]), new Uint8Array([1, 2, 3])));
}
if (typeof global.Uint16Array === 'function') {
assert.doesNotThrow(makeBlock(assert.deepEqual, new Uint16Array([1, 2, 3]), new Uint16Array([1, 2, 3])));
}
});
function thrower(errorConstructor) {
throw new errorConstructor('test');
}
@ -294,6 +304,11 @@ function tests (assert, what) {
testAssertionMessage('foo', '\'foo\'');
testAssertionMessage([], '[]');
testAssertionMessage([1, 2, 3], '[ 1, 2, 3 ]');
testAssertionMessage(new Buffer([1, 2, 3]), '<Buffer 01 02 03>');
if (typeof global.Uint8Array === 'function' && Object.getOwnPropertyNames( new Uint8Array([])).length === 0) {
// todo fix util.inspect
testAssertionMessage(new Uint8Array([1, 2, 3]), '{ \'0\': 1, \'1\': 2, \'2\': 3 }');
}
testAssertionMessage(/a/, '/a/');
testAssertionMessage(function f() {}, '[Function: f]');
testAssertionMessage({}, '{}');

Loading…
Cancel
Save