Skip to content

Commit

Permalink
Fix false positive of eql when some keys set undefined.
Browse files Browse the repository at this point in the history
  • Loading branch information
moll committed Jun 13, 2015
1 parent 9f7ccbd commit b0bacb9
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
demand(undefined).must.be.undefined().
```

- Fixes a false positive in [`eql`][] when an object had some keys set to
`undefined`.

[`must`]: https://github.com/moll/js-must/blob/master/doc/API.md#Must.prototype.must

## 0.12.0 (May 28, 2014)
Expand Down
4 changes: 4 additions & 0 deletions must.js
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,10 @@ function eql(a, b, aStack, bStack) {
if (aKeys.length != bKeys.length) return false
if (aKeys.length == 0) return true

aKeys.sort()
bKeys.sort()
for (i = 0; i < aKeys.length; ++i) if (aKeys[i] !== bKeys[i]) return false

for (var key in a) if (!eql(a[key], b[key], aStack, bStack)) return false
return true
}
Expand Down
9 changes: 9 additions & 0 deletions test/must/eql_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,15 @@ describe("Must.prototype.eql", function() {
assert.fail(function() { Must(a).be.eql(b) })
})

// This was a bug I discovered on Jun 12, 2015 related to not comparing
// keys equivalence before comparing their values.
it("must return false given equal amount of keys undefined keys",
function() {
var obj = {name: undefined}
assert.fail(function() { Must(obj).be.eql({age: undefined}) })
assert.fail(function() { Must(obj).be.eql({age: 13}) })
})

describe("with circular references", function() {
it("must pass if equal", function() {
var a = create({life: {love: 69}})
Expand Down

0 comments on commit b0bacb9

Please sign in to comment.