[Groonga-commit] groonga/express-kotoumi [master] Compare expected and actual JSON except the order of hash keys

Back to archive index

YUKI Hiroshi null+****@clear*****
Wed Jan 16 12:10:00 JST 2013


YUKI Hiroshi	2013-01-16 12:10:00 +0900 (Wed, 16 Jan 2013)

  New Revision: 254f7209a654615aee075956b42de541e3091691
  https://github.com/groonga/express-kotoumi/commit/254f7209a654615aee075956b42de541e3091691

  Log:
    Compare expected and actual JSON except the order of hash keys

  Modified files:
    test/builders-rest-request.test.js
    test/test-utils.js

  Modified: test/builders-rest-request.test.js (+4 -2)
===================================================================
--- test/builders-rest-request.test.js    2013-01-11 19:09:54 +0900 (2b5b047)
+++ test/builders-rest-request.test.js    2013-01-16 12:10:00 +0900 (923e1a2)
@@ -1,5 +1,7 @@
 var assert = require('chai').assert;
 
+var utils = require('./test-utils');
+
 var builders = require('../lib/builders/rest-request');
 
 suite('building message from REST API request', function() {
@@ -27,7 +29,7 @@ suite('building message from REST API request', function() {
         }
       };
       var actualBody = builders.search({ params: params });
-      assert.deepEqual(actualBody, expectedBody);
+      assert.equalJSON(actualBody, expectedBody);
     });
 
     test('with options', function() {
@@ -55,7 +57,7 @@ suite('building message from REST API request', function() {
         }
       };
       var actualBody = builders.search({ params: params });
-      assert.deepEqual(actualBody, expectedBody);
+      assert.equalJSON(actualBody, expectedBody);
     });
   });
 });

  Modified: test/test-utils.js (+21 -0)
===================================================================
--- test/test-utils.js    2013-01-11 19:09:54 +0900 (294f534)
+++ test/test-utils.js    2013-01-16 12:10:00 +0900 (a8a84ca)
@@ -109,3 +109,24 @@ function assertEnvelopeEqual(actual, expected) {
   });
 }
 assert.envelopeEqual = assertEnvelopeEqual;
+
+function sortKeys(original) {
+  if (!original || typeof original != 'object')
+    return original;
+
+  if (Array.isArray(original))
+    return original.map(sortKeys);
+
+  var sorted = {};
+  Object.keys(original).sort().forEach(key) {
+    sorted[key] = sortKeys(original[key]);
+  });
+  return sorted;
+}
+
+// assert.deepEqual fails when the order of hash keys are different,
+// even if they are "eaual" as JSON objects.
+function assertEqualJSON(actual, expected) {
+  this.deepEqual(sortKeys(actual), sortKeys(expected));
+}
+assert.equalJSON = assertEqualJSON;
-------------- next part --------------
HTML����������������������������...
Download 



More information about the Groonga-commit mailing list
Back to archive index