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

fix(utils.js): diffs for buffers, issue #1132 #1368

Closed
wants to merge 3 commits into from
Closed
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
14 changes: 13 additions & 1 deletion lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,16 @@ exports.stringify = function(obj) {
return JSON.stringify(exports.canonicalize(obj), null, 2).replace(/,(\n|$)/g, '$1');
};

/**
* Return if obj is a Buffer
* @param {Object} arg
* @return {Boolean}
* @api private
*/
exports.isBuffer = function (arg) {
return typeof Buffer !== 'undefined' && arg instanceof Buffer;
};

/**
* Return a new object that has the keys in sorted order.
* @param {Object} obj
Expand All @@ -321,7 +331,9 @@ exports.canonicalize = function(obj, stack) {

var canonicalizedObj;

if ({}.toString.call(obj) === '[object Array]') {
if(exports.isBuffer(obj)) {
return obj;
} else if ({}.toString.call(obj) === '[object Array]') {
stack.push(obj);
canonicalizedObj = exports.map(obj, function (item) {
return exports.canonicalize(item, stack);
Expand Down
7 changes: 7 additions & 0 deletions test/acceptance/diffs.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,11 @@ describe('diffs', function(){

// obj1.should.equal(obj2);
});

it('should display diff by data and not like an objects', function(){
var buf1 = new Buffer([0x01]);
var buf2 = new Buffer([0x02]);

// buf1.should.equal(buf2);
});
});
17 changes: 12 additions & 5 deletions test/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,26 @@
var mocha = require('..');
var utils = mocha.utils;
var clean = utils.clean;
var isBuffer = utils.isBuffer;

describe('utils', function(){
describe('utils', function() {
describe('.clean()', function(){
it('should remove the wrapping function declaration', function(){
clean('function (one, two, three) {\n//code\n}').should.equal('//code');
})
});

it('should remove space character indentation from the function body', function(){
clean(' //line1\n //line2').should.equal('//line1\n //line2');
})
});

it('should remove tab character indentation from the function body', function(){
clean('\t//line1\n\t\t//line2').should.equal('//line1\n\t//line2');
});
});
describe('.isBuffer()', function(){
it('should test if object is a Buffer', function() {
isBuffer(new Buffer([0x01])).should.equal(true);
isBuffer({}).should.equal(false);
})
})
})
});
});