Skip to content

armgit / mocha

Public Imported from GitHub
0

☕️ Classic, reliable, trusted test framework for Node.js and the browser

Fix buffer diffs (closes #1132, closes #1433)

Ariel Mashraki7057f7b3324e
diff --git a/lib/utils.js b/lib/utils.js
index ecc0a142..166d2481 100644
--- a/lib/utils.js
+++ b/lib/utils.js
@@ -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
@@ -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);
diff --git a/test/acceptance/diffs.js b/test/acceptance/diffs.js
index 3b3155f7..c87ffe0c 100644
--- a/test/acceptance/diffs.js
+++ b/test/acceptance/diffs.js
@@ -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);
+ });
});
diff --git a/test/utils.js b/test/utils.js
index f8d6dcfb..3810c777 100644
--- a/test/utils.js
+++ b/test/utils.js
@@ -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);
})
- })
-})
+ });
+});