[Groonga-commit] groonga/gcs [master] Show diff when assertion failure of assert.deepEqual

Back to archive index

null+****@clear***** null+****@clear*****
2012年 7月 13日 (金) 16:57:02 JST


SHIMODA Hiroshi	2012-07-13 16:57:02 +0900 (Fri, 13 Jul 2012)

  New Revision: 83ca64b52b98576b6f551eb3a552d5f99c33dd90
  https://github.com/groonga/gcs/commit/83ca64b52b98576b6f551eb3a552d5f99c33dd90

  Log:
    Show diff when assertion failure of assert.deepEqual

  Modified files:
    test/test-utils.js

  Modified: test/test-utils.js (+38 -0)
===================================================================
--- test/test-utils.js    2012-07-13 16:24:20 +0900 (b3235c6)
+++ test/test-utils.js    2012-07-13 16:57:02 +0900 (a8e89e5)
@@ -129,3 +129,41 @@ function rmRSync(directoryPath) {
   fs.rmdirSync(directoryPath);
 }
 exports.rmRSync = rmRSync;
+
+
+// activate diff for chai.assert.deepEqual
+
+var chai_utils = require('chai/lib/chai/utils');
+
+function sortAndStringify(object) {
+  switch (typeof object) {
+    case 'string':
+    case 'number':
+    case 'boolean':
+      return JSON.stringify(object);
+    default:
+      if (Array.isArray(object)) {
+        return '[' + object.map(function(item) { return sortAndStringify(item); }).sort().join(', ') + ']';
+      } else if (!object) {
+        return JSON.stringify(object);
+      } else {
+        var sorted = {};
+        Object.keys(object).sort().forEach(function(key) {
+          if (object.hasOwnProperty(key))
+            sorted[key] = object[key];
+        });
+        return JSON.stringify(sorted);
+      }
+  }
+}
+
+require('chai').Assertion.prototype.eql = function(obj) {
+  var expected = chai_utils.flag(this, 'object');
+  this.assert(
+      chai_utils.eql(obj, expected)
+    , 'expected #{this} to deeply equal #{exp}'
+    , 'expected #{this} to not deeply equal #{exp}'
+    , sortAndStringify(expected)
+    , sortAndStringify(obj)
+  );
+};
-------------- next part --------------
HTML$B$NE:IU%U%!%$%k$rJ]4I$7$^$7$?(B...
下載 



Groonga-commit メーリングリストの案内
Back to archive index