Skip to content

Commit 00a7456

Browse files
teppeisbnoordhuis
authored andcommitted
assert: fix deepEqual regression
Change of Object.keys in ES6 breaks assert.deepEqual about primitive values. V8: https://code.google.com/p/v8/issues/detail?id=3443 Previously deepEqual depends on Object.key that throws an error for a primitive value, but now Object.key does not throw. PR-URL: #193 Reviewed-By: Ben Noordhuis <[email protected]>
1 parent ef10827 commit 00a7456

File tree

2 files changed

+22
-11
lines changed

2 files changed

+22
-11
lines changed

lib/assert.js

+6-9
Original file line numberDiff line numberDiff line change
@@ -201,8 +201,9 @@ function objEquiv(a, b) {
201201
return false;
202202
// an identical 'prototype' property.
203203
if (a.prototype !== b.prototype) return false;
204-
//~~~I've managed to break Object.keys through screwy arguments passing.
205-
// Converting to array solves the problem.
204+
// if one is a primitive, the other must be same
205+
if (util.isPrimitive(a) || util.isPrimitive(b))
206+
return a === b;
206207
var aIsArgs = isArguments(a),
207208
bIsArgs = isArguments(b);
208209
if ((aIsArgs && !bIsArgs) || (!aIsArgs && bIsArgs))
@@ -212,13 +213,9 @@ function objEquiv(a, b) {
212213
b = pSlice.call(b);
213214
return _deepEqual(a, b);
214215
}
215-
try {
216-
var ka = Object.keys(a),
217-
kb = Object.keys(b),
218-
key, i;
219-
} catch (e) {//happens when one is a string literal and the other isn't
220-
return false;
221-
}
216+
var ka = Object.keys(a),
217+
kb = Object.keys(b),
218+
key, i;
222219
// having the same number of owned properties (keys incorporates
223220
// hasOwnProperty)
224221
if (ka.length != kb.length)

test/parallel/test-assert.js

+16-2
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,22 @@ nameBuilder2.prototype = Object;
153153
nb2 = new nameBuilder2('Ryan', 'Dahl');
154154
assert.throws(makeBlock(a.deepEqual, nb1, nb2), a.AssertionError);
155155

156-
// String literal + object blew up my implementation...
157-
assert.throws(makeBlock(a.deepEqual, 'a', {}), a.AssertionError);
156+
// primitives and object
157+
assert.throws(makeBlock(a.deepEqual, null, {}), a.AssertionError);
158+
assert.throws(makeBlock(a.deepEqual, undefined, {}), a.AssertionError);
159+
assert.throws(makeBlock(a.deepEqual, 'a', ['a']), a.AssertionError);
160+
assert.throws(makeBlock(a.deepEqual, 'a', {0: 'a'}), a.AssertionError);
161+
assert.throws(makeBlock(a.deepEqual, 1, {}), a.AssertionError);
162+
assert.throws(makeBlock(a.deepEqual, true, {}), a.AssertionError);
163+
if (typeof Symbol === 'symbol') {
164+
assert.throws(makeBlock(assert.deepEqual, Symbol(), {}), a.AssertionError);
165+
}
166+
167+
// primitive wrappers and object
168+
assert.doesNotThrow(makeBlock(a.deepEqual, new String('a'), ['a']), a.AssertionError);
169+
assert.doesNotThrow(makeBlock(a.deepEqual, new String('a'), {0: 'a'}), a.AssertionError);
170+
assert.doesNotThrow(makeBlock(a.deepEqual, new Number(1), {}), a.AssertionError);
171+
assert.doesNotThrow(makeBlock(a.deepEqual, new Boolean(true), {}), a.AssertionError);
158172

159173
// Testing the throwing
160174
function thrower(errorConstructor) {

0 commit comments

Comments
 (0)