Skip to content

Commit 751a90f

Browse files
committed
Other: Added a tape adapter to assert message equality accross browsers
1 parent 9681854 commit 751a90f

File tree

10 files changed

+176
-5
lines changed

10 files changed

+176
-5
lines changed

.zuul.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,8 @@ scripts:
2222
tunnel:
2323
type: ngrok
2424
proto: tcp
25+
browserify:
26+
- external: long
27+
- require: ./lib/tape-adapter
28+
expose: intimidate
29+
entry: true

lib/deep-equal/LICENSE

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
This software is released under the MIT license:
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy of
4+
this software and associated documentation files (the "Software"), to deal in
5+
the Software without restriction, including without limitation the rights to
6+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
7+
the Software, and to permit persons to whom the Software is furnished to do so,
8+
subject to the following conditions:
9+
10+
The above copyright notice and this permission notice shall be included in all
11+
copies or substantial portions of the Software.
12+
13+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
15+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
16+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
17+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
18+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

lib/deep-equal/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Forked [node-deep-equal](https://github.com/substack/node-deep-equal)@1.0.1 with monkey-patched buffer equality.
2+
3+
License: MIT. Derived largely from node's assert module.

lib/deep-equal/index.js

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
var pSlice = Array.prototype.slice;
2+
var objectKeys = require('./lib/keys.js');
3+
var isArguments = require('./lib/is_arguments.js');
4+
5+
var deepEqual = module.exports = function (actual, expected, opts) {
6+
if (!opts) opts = {};
7+
// 7.1. All identical values are equivalent, as determined by ===.
8+
if (actual === expected) {
9+
return true;
10+
11+
} else if (actual instanceof Date && expected instanceof Date) {
12+
return actual.getTime() === expected.getTime();
13+
14+
// 7.3. Other pairs that do not both pass typeof value == 'object',
15+
// equivalence is determined by ==.
16+
} else if (!actual || !expected || typeof actual != 'object' && typeof expected != 'object') {
17+
return opts.strict ? actual === expected : actual == expected;
18+
19+
// 7.4. For all other Object pairs, including Array objects, equivalence is
20+
// determined by having the same number of owned properties (as verified
21+
// with Object.prototype.hasOwnProperty.call), the same set of keys
22+
// (although not necessarily the same order), equivalent values for every
23+
// corresponding key, and an identical 'prototype' property. Note: this
24+
// accounts for both named and indexed properties on Arrays.
25+
} else {
26+
return objEquiv(actual, expected, opts);
27+
}
28+
}
29+
30+
function isUndefinedOrNull(value) {
31+
return value === null || value === undefined;
32+
}
33+
34+
function isBuffer (x) {
35+
if (!x || typeof x !== 'object' || typeof x.length !== 'number') return false;
36+
// MONKEY PATCH: Support buffer, Uint8Array and Array alike
37+
/* if (typeof x.copy !== 'function' || typeof x.slice !== 'function') {
38+
return false;
39+
} */
40+
if (x.length > 0 && typeof x[0] !== 'number') return false;
41+
return true;
42+
}
43+
44+
function objEquiv(a, b, opts) {
45+
var i, key;
46+
if (isUndefinedOrNull(a) || isUndefinedOrNull(b))
47+
return false;
48+
// an identical 'prototype' property.
49+
if (a.prototype !== b.prototype) return false;
50+
//~~~I've managed to break Object.keys through screwy arguments passing.
51+
// Converting to array solves the problem.
52+
if (isArguments(a)) {
53+
if (!isArguments(b)) {
54+
return false;
55+
}
56+
a = pSlice.call(a);
57+
b = pSlice.call(b);
58+
return deepEqual(a, b, opts);
59+
}
60+
if (isBuffer(a)) {
61+
if (!isBuffer(b)) {
62+
return false;
63+
}
64+
if (a.length !== b.length) return false;
65+
for (i = 0; i < a.length; i++) {
66+
if (a[i] !== b[i]) return false;
67+
}
68+
return true;
69+
}
70+
try {
71+
var ka = objectKeys(a),
72+
kb = objectKeys(b);
73+
} catch (e) {//happens when one is a string literal and the other isn't
74+
return false;
75+
}
76+
// having the same number of owned properties (keys incorporates
77+
// hasOwnProperty)
78+
if (ka.length != kb.length)
79+
return false;
80+
//the same set of keys (although not necessarily the same order),
81+
ka.sort();
82+
kb.sort();
83+
//~~~cheap key test
84+
for (i = ka.length - 1; i >= 0; i--) {
85+
if (ka[i] != kb[i])
86+
return false;
87+
}
88+
//equivalent values for every corresponding key, and
89+
//~~~possibly expensive deep test
90+
for (i = ka.length - 1; i >= 0; i--) {
91+
key = ka[i];
92+
if (!deepEqual(a[key], b[key], opts)) return false;
93+
}
94+
return typeof a === typeof b;
95+
}

lib/deep-equal/lib/is_arguments.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
var supportsArgumentsClass = (function(){
2+
return Object.prototype.toString.call(arguments)
3+
})() == '[object Arguments]';
4+
5+
exports = module.exports = supportsArgumentsClass ? supported : unsupported;
6+
7+
exports.supported = supported;
8+
function supported(object) {
9+
return Object.prototype.toString.call(object) == '[object Arguments]';
10+
};
11+
12+
exports.unsupported = unsupported;
13+
function unsupported(object){
14+
return object &&
15+
typeof object == 'object' &&
16+
typeof object.length == 'number' &&
17+
Object.prototype.hasOwnProperty.call(object, 'callee') &&
18+
!Object.prototype.propertyIsEnumerable.call(object, 'callee') ||
19+
false;
20+
};

lib/deep-equal/lib/keys.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
exports = module.exports = typeof Object.keys === 'function'
2+
? Object.keys : shim;
3+
4+
exports.shim = shim;
5+
function shim (obj) {
6+
var keys = [];
7+
for (var key in obj) keys.push(key);
8+
return keys;
9+
}

lib/tape-adapter.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
var tape = require("tape");
2+
3+
var Test = tape.Test;
4+
5+
// some ancient environments have invalid own properties on buffers so that deepEqual doesn't work.
6+
// the following uses a monkey-patched deepEqual implementation for all kinds of number arrays.
7+
8+
var deepEqual = require("./deep-equal");
9+
10+
Test.prototype.deepEqual
11+
= Test.prototype.deepEquals
12+
= Test.prototype.isEquivalent
13+
= Test.prototype.same
14+
= function (a, b, msg, extra) {
15+
this._assert(deepEqual(a, b, { strict: true }), {
16+
message : msg || 'should be equivalent',
17+
operator : 'deepEqual',
18+
actual : a,
19+
expected : b,
20+
extra : extra
21+
});
22+
};

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
"pages": "node scripts/pages",
3535
"prepublish": "node scripts/prepublish",
3636
"prof": "node bench/prof",
37-
"test": "tape tests/*.js | tap-spec",
37+
"test": "tape -r ./lib/tape-adapter tests/*.js | tap-spec",
3838
"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",
3939
"zuul": "zuul --ui tape --no-coverage --concurrency 4 -- tests/*.js",
4040
"zuul-local": "zuul --ui tape --concurrency 1 --local 8080 --disable-tunnel -- tests/*.js",

tests/basics.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,7 @@ tape.test("google.protobuf.Any type", function(test) {
3030
}, "should be populated with the contents we provided");
3131

3232
var writer = Any.encode(any),
33-
buf,
34-
msg;
33+
buf;
3534

3635
function verifyEncode(test, buf) {
3736
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) {
5352

5453
test.test("should decode", function(test) {
5554

56-
msg = Any.decode(buf);
55+
var msg = Any.decode(buf);
5756

5857
test.deepEqual(msg, any, "an equal message");
5958

@@ -77,7 +76,7 @@ tape.test("google.protobuf.Any type", function(test) {
7776

7877
test.test("should decodeDelimited", function(test) {
7978

80-
msg = Any.decodeDelimited(buf);
79+
var msg = Any.decodeDelimited(buf);
8180
test.deepEqual(msg, any, "an equal message");
8281

8382
test.end();

tests/index.js

Whitespace-only changes.

0 commit comments

Comments
 (0)