Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use inherited properties in deep equal and inspect #125

Merged
merged 3 commits into from
Dec 30, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions lib/chai/utils/eql.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

module.exports = _deepEqual;

var getEnumerableProperties = require('./getEnumerableProperties');

// for the browser
var Buffer;
try {
Expand Down Expand Up @@ -87,8 +89,8 @@ function objEquiv(a, b, memos) {
return _deepEqual(a, b, memos);
}
try {
var ka = Object.keys(a),
kb = Object.keys(b),
var ka = getEnumerableProperties(a),
kb = getEnumerableProperties(b),
key;
} catch (e) {//happens when one is a string literal and the other isn't
return false;
Expand Down
25 changes: 25 additions & 0 deletions lib/chai/utils/getEnumerableProperties.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*!
* Chai - getEnumerableProperties utility
* Copyright(c) 2012 Jake Luer <jake@alogicalparadox.com>
* MIT Licensed
*/

/**
* ### .getEnumerableProperties(object)
*
* This allows the retrieval of enumerable property names of an object,
* inherited or not.
*
* @param {Object} object
* @returns {Array}
* @name getEnumerableProperties
* @api public
*/

module.exports = function getEnumerableProperties(object) {
var result = [];
for (var name in object) {
result.push(name);
}
return result;
};
35 changes: 35 additions & 0 deletions lib/chai/utils/getProperties.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*!
* Chai - getProperties utility
* Copyright(c) 2012 Jake Luer <jake@alogicalparadox.com>
* MIT Licensed
*/

/**
* ### .getProperties(object)
*
* This allows the retrieval of property names of an object, enumerable or not,
* inherited or not.
*
* @param {Object} object
* @returns {Array}
* @name getProperties
* @api public
*/

module.exports = function getProperties(object) {
var result = Object.getOwnPropertyNames(subject);

function addProperty(property) {
if (result.indexOf(property) === -1) {
result.push(property);
}
}

var proto = Object.getPrototypeOf(subject);
while (proto !== null) {
Object.getOwnPropertyNames(proto).forEach(addProperty);
proto = Object.getPrototypeOf(proto);
}

return result;
};
8 changes: 5 additions & 3 deletions lib/chai/utils/inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// https://github.com/joyent/node/blob/f8c335d0caf47f16d31413f89aa28eda3878e3aa/lib/util.js

var getName = require('./getName');
var getProperties = require('./getProperties');
var getEnumerableProperties = require('./getEnumerableProperties');

module.exports = inspect;

Expand Down Expand Up @@ -42,7 +44,7 @@ var getOuterHTML = function(element) {
return html;
}
};

// Returns true if object is a DOM element.
var isDOMElement = function (object) {
if (typeof HTMLElement === 'object') {
Expand Down Expand Up @@ -78,8 +80,8 @@ function formatValue(ctx, value, recurseTimes) {
}

// Look up the keys of the object.
var visibleKeys = Object.keys(value);
var keys = ctx.showHidden ? Object.getOwnPropertyNames(value) : visibleKeys;
var visibleKeys = getEnumerableProperties(value);
var keys = ctx.showHidden ? getProperties(value) : visibleKeys;

// Some type of object without properties can be shortcutted.
// In IE, errors have a single `stack` property, or if they are vanilla `Error`,
Expand Down
7 changes: 7 additions & 0 deletions test/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,9 +164,16 @@ suite('assert', function () {
test('deepEqual', function() {
assert.deepEqual({tea: 'chai'}, {tea: 'chai'});


err(function () {
assert.deepEqual({tea: 'chai'}, {tea: 'black'});
}, "expected { tea: \'chai\' } to deeply equal { tea: \'black\' }");

var obj1 = Object.create({tea: 'chai'});
var obj2 = Object.create({tea: 'black'});
err(function () {
assert.deepEqual(obj1, obj2);
}, "expected { tea: \'chai\' } to deeply equal { tea: \'black\' }");
});

test('deepEqual (circular)', function() {
Expand Down
2 changes: 1 addition & 1 deletion test/expect.js
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ suite('expect', function () {

err(function(){
expect(new FakeArgs).not.to.be.empty;
}, "expected {} not to be empty");
}, "expected { length: 0 } not to be empty");

err(function(){
expect({arguments: 0}).to.be.empty;
Expand Down
2 changes: 1 addition & 1 deletion test/should.js
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ suite('should', function() {

err(function(){
(new FakeArgs).should.not.be.empty;
}, "expected {} not to be empty");
}, "expected { length: 0 } not to be empty");

err(function(){
({arguments: 0}).should.be.empty;
Expand Down