Skip to content

Commit

Permalink
Fix buffer diffs (closes mochajs#1132, closes mochajs#1433)
Browse files Browse the repository at this point in the history
  • Loading branch information
a8m authored and tandrewnichols committed Dec 15, 2014
1 parent 525dd09 commit 267aabe
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 6 deletions.
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 @@ -83,4 +83,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
@@ -1,19 +1,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);
})
})
})
});
});

0 comments on commit 267aabe

Please sign in to comment.