diff --git a/.zuul.yml b/.zuul.yml index c43e74d54..7fca42909 100644 --- a/.zuul.yml +++ b/.zuul.yml @@ -22,3 +22,8 @@ scripts: tunnel: type: ngrok proto: tcp +browserify: + - external: long + - require: ./lib/tape-adapter + expose: intimidate + entry: true diff --git a/lib/deep-equal/LICENSE b/lib/deep-equal/LICENSE new file mode 100644 index 000000000..30a59ef86 --- /dev/null +++ b/lib/deep-equal/LICENSE @@ -0,0 +1,18 @@ +This software is released under the MIT license: + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of +the Software, and to permit persons to whom the Software is furnished to do so, +subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR +COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER +IN AN 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. \ No newline at end of file diff --git a/lib/deep-equal/README.md b/lib/deep-equal/README.md new file mode 100644 index 000000000..c37ebd91e --- /dev/null +++ b/lib/deep-equal/README.md @@ -0,0 +1,3 @@ +Forked [node-deep-equal](https://github.com/substack/node-deep-equal)@1.0.1 with monkey-patched buffer equality. + +License: MIT. Derived largely from node's assert module. diff --git a/lib/deep-equal/index.js b/lib/deep-equal/index.js new file mode 100644 index 000000000..a2fba8b81 --- /dev/null +++ b/lib/deep-equal/index.js @@ -0,0 +1,95 @@ +var pSlice = Array.prototype.slice; +var objectKeys = require('./lib/keys.js'); +var isArguments = require('./lib/is_arguments.js'); + +var deepEqual = module.exports = function (actual, expected, opts) { + if (!opts) opts = {}; + // 7.1. All identical values are equivalent, as determined by ===. + if (actual === expected) { + return true; + + } else if (actual instanceof Date && expected instanceof Date) { + return actual.getTime() === expected.getTime(); + + // 7.3. Other pairs that do not both pass typeof value == 'object', + // equivalence is determined by ==. + } else if (!actual || !expected || typeof actual != 'object' && typeof expected != 'object') { + return opts.strict ? actual === expected : actual == expected; + + // 7.4. For all other Object pairs, including Array objects, equivalence is + // determined by having the same number of owned properties (as verified + // with Object.prototype.hasOwnProperty.call), the same set of keys + // (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 { + return objEquiv(actual, expected, opts); + } +} + +function isUndefinedOrNull(value) { + return value === null || value === undefined; +} + +function isBuffer (x) { + if (!x || typeof x !== 'object' || typeof x.length !== 'number') return false; + // MONKEY PATCH: Support buffer, Uint8Array and Array alike + /* if (typeof x.copy !== 'function' || typeof x.slice !== 'function') { + return false; + } */ + if (x.length > 0 && typeof x[0] !== 'number') return false; + return true; +} + +function objEquiv(a, b, opts) { + var i, key; + if (isUndefinedOrNull(a) || isUndefinedOrNull(b)) + return false; + // an identical 'prototype' property. + if (a.prototype !== b.prototype) return false; + //~~~I've managed to break Object.keys through screwy arguments passing. + // Converting to array solves the problem. + if (isArguments(a)) { + if (!isArguments(b)) { + return false; + } + a = pSlice.call(a); + b = pSlice.call(b); + return deepEqual(a, b, opts); + } + if (isBuffer(a)) { + if (!isBuffer(b)) { + return false; + } + if (a.length !== b.length) return false; + for (i = 0; i < a.length; i++) { + if (a[i] !== b[i]) return false; + } + return true; + } + try { + var ka = objectKeys(a), + kb = objectKeys(b); + } catch (e) {//happens when one is a string literal and the other isn't + return false; + } + // having the same number of owned properties (keys incorporates + // hasOwnProperty) + if (ka.length != kb.length) + return false; + //the same set of keys (although not necessarily the same order), + ka.sort(); + kb.sort(); + //~~~cheap key test + for (i = ka.length - 1; i >= 0; i--) { + if (ka[i] != kb[i]) + return false; + } + //equivalent values for every corresponding key, and + //~~~possibly expensive deep test + for (i = ka.length - 1; i >= 0; i--) { + key = ka[i]; + if (!deepEqual(a[key], b[key], opts)) return false; + } + return typeof a === typeof b; +} \ No newline at end of file diff --git a/lib/deep-equal/lib/is_arguments.js b/lib/deep-equal/lib/is_arguments.js new file mode 100644 index 000000000..e3fbc1fc5 --- /dev/null +++ b/lib/deep-equal/lib/is_arguments.js @@ -0,0 +1,20 @@ +var supportsArgumentsClass = (function(){ + return Object.prototype.toString.call(arguments) +})() == '[object Arguments]'; + +exports = module.exports = supportsArgumentsClass ? supported : unsupported; + +exports.supported = supported; +function supported(object) { + return Object.prototype.toString.call(object) == '[object Arguments]'; +}; + +exports.unsupported = unsupported; +function unsupported(object){ + return object && + typeof object == 'object' && + typeof object.length == 'number' && + Object.prototype.hasOwnProperty.call(object, 'callee') && + !Object.prototype.propertyIsEnumerable.call(object, 'callee') || + false; +}; \ No newline at end of file diff --git a/lib/deep-equal/lib/keys.js b/lib/deep-equal/lib/keys.js new file mode 100644 index 000000000..6f04fcdbd --- /dev/null +++ b/lib/deep-equal/lib/keys.js @@ -0,0 +1,9 @@ +exports = module.exports = typeof Object.keys === 'function' + ? Object.keys : shim; + +exports.shim = shim; +function shim (obj) { + var keys = []; + for (var key in obj) keys.push(key); + return keys; +} \ No newline at end of file diff --git a/lib/tape-adapter.js b/lib/tape-adapter.js new file mode 100644 index 000000000..c3744ff62 --- /dev/null +++ b/lib/tape-adapter.js @@ -0,0 +1,22 @@ +var tape = require("tape"); + +var Test = tape.Test; + +// some ancient environments have invalid own properties on buffers so that deepEqual doesn't work. +// the following uses a monkey-patched deepEqual implementation for all kinds of number arrays. + +var deepEqual = require("./deep-equal"); + +Test.prototype.deepEqual += Test.prototype.deepEquals += Test.prototype.isEquivalent += Test.prototype.same += function (a, b, msg, extra) { + this._assert(deepEqual(a, b, { strict: true }), { + message : msg || 'should be equivalent', + operator : 'deepEqual', + actual : a, + expected : b, + extra : extra + }); +}; diff --git a/package.json b/package.json index 1ba75489d..4d0aa697c 100644 --- a/package.json +++ b/package.json @@ -34,7 +34,7 @@ "pages": "node scripts/pages", "prepublish": "node scripts/prepublish", "prof": "node bench/prof", - "test": "tape tests/*.js | tap-spec", + "test": "tape -r ./lib/tape-adapter tests/*.js | tap-spec", "types": "node bin/pbts --main --global protobuf --out index.d.ts src && tsc tests/typescript.ts --lib es2015 --noEmit && tsc tests/data/test.ts --lib es2015 --noEmit", "zuul": "zuul --ui tape --no-coverage --concurrency 4 -- tests/*.js", "zuul-local": "zuul --ui tape --concurrency 1 --local 8080 --disable-tunnel -- tests/*.js", diff --git a/tests/basics.js b/tests/basics.js index 2b6fb7fc4..994b6311a 100644 --- a/tests/basics.js +++ b/tests/basics.js @@ -30,8 +30,7 @@ tape.test("google.protobuf.Any type", function(test) { }, "should be populated with the contents we provided"); var writer = Any.encode(any), - buf, - msg; + buf; function verifyEncode(test, buf) { test.equal(buf[0] , 1 << 3 | 2, "a tag with id 1, wire type 2"); @@ -53,7 +52,7 @@ tape.test("google.protobuf.Any type", function(test) { test.test("should decode", function(test) { - msg = Any.decode(buf); + var msg = Any.decode(buf); test.deepEqual(msg, any, "an equal message"); @@ -77,7 +76,7 @@ tape.test("google.protobuf.Any type", function(test) { test.test("should decodeDelimited", function(test) { - msg = Any.decodeDelimited(buf); + var msg = Any.decodeDelimited(buf); test.deepEqual(msg, any, "an equal message"); test.end(); diff --git a/tests/index.js b/tests/index.js new file mode 100644 index 000000000..e69de29bb