From 4241bdac68d919470bac0b5ba402ee5e7dc8e849 Mon Sep 17 00:00:00 2001 From: Matias Alejo Garcia Date: Tue, 29 Jul 2014 13:33:54 -0300 Subject: [PATCH] all test pass! --- test/test.WalletFactory.js | 88 +++++++++++++++++++++++++++++++++++++- 1 file changed, 86 insertions(+), 2 deletions(-) diff --git a/test/test.WalletFactory.js b/test/test.WalletFactory.js index 9dc19f16b..00cad01ce 100644 --- a/test/test.WalletFactory.js +++ b/test/test.WalletFactory.js @@ -13,6 +13,70 @@ var WalletFactory = require('../js/models/core/WalletFactory'); var Passphrase = require('../js/models/core/Passphrase'); +/** + * A better way to compare two objects in Javascript + **/ +function getKeys(obj) { + var keys; + if(obj.keys) { + keys = obj.keys(); + } else { + keys = []; + + for(var k in obj) { + if(Object.prototype.hasOwnProperty.call(obj, k)) { + keys.push(k); + } + } + } + + return keys; +} + +/** + * Create a new object so the keys appear in the provided order. + * @param {Object} obj The object to be the base for the new object + * @param {Array} keys The order in which properties of the new object should appear + **/ +function reconstructObject(obj, keys) { + var result = {}; + for (var i = 0, l = keys.length; i < l; i++) { + if (Object.prototype.hasOwnProperty.call(obj, keys[i])) { + result[keys[i]] = obj[keys[i]]; + } + } + + return result; +} + +function assertObjectEqual(a, b, msg) { + msg = msg || ''; + if( Object.prototype.toString.call( a ) === '[object Array]' && Object.prototype.toString.call( b ) === '[object Array]') { + // special case: array of objects + if (a.filter(function(e) { return Object.prototype.toString.call( e ) === '[object Object]' }).length > 0 || + b.filter(function(e) { return Object.prototype.toString.call( e ) === '[object Object]' }).length > 0 ){ + + if (a.length !== b.length) { + JSON.stringify(a).should.equal(JSON.stringify(b), msg); + } else { + for(var i = 0, l = a.length; i < l; i++) { + assertObjectEqual(a[i], b[i], msg + '[elements at index ' + i + ' should be equal]'); + } + } + // simple array of primitives + } else { + JSON.stringify(a).should.equal(JSON.stringify(b), msg); + } + } else { + var orderedA = reconstructObject(a, getKeys(a).sort()), + orderedB = reconstructObject(b, getKeys(b).sort()); + + // compare as strings for diff tolls to show us the difference + JSON.stringify(orderedA).should.equal(JSON.stringify(orderedB), msg) + } +} + + describe('WalletFactory model', function() { var config = { Network: FakeNetwork, @@ -83,7 +147,23 @@ describe('WalletFactory model', function() { w2.id.should.equal(w.id); }); + it('#fromObj #toObj round trip', function() { + var wf = new WalletFactory(config, '0.0.5'); + var o2 = o.replace(/cosigner/g,'copayerIndex'); + var w = wf.fromObj(JSON.parse(o2)); + + should.exist(w); + w.id.should.equal("dbfe10c3fae71cea"); + should.exist(w.publicKeyRing.getCopayerId); + should.exist(w.txProposals.toObj()); + should.exist(w.privateKey.toObj()); + assertObjectEqual(w.toObj(), JSON.parse(o2)); + }); + + + + it('#fromObj #toObj round trip, using old cosigner', function() { var wf = new WalletFactory(config, '0.0.5'); var w = wf.fromObj(JSON.parse(o)); @@ -92,8 +172,8 @@ describe('WalletFactory model', function() { should.exist(w.publicKeyRing.getCopayerId); should.exist(w.txProposals.toObj()); should.exist(w.privateKey.toObj()); - - JSON.stringify(w.toObj()).should.equal(o); + var expected = JSON.parse(o.replace(/cosigner/g,'copayerIndex')); + assertObjectEqual(w.toObj(), expected); }); it('support old index schema: #fromObj #toObj round trip', function() { @@ -108,6 +188,10 @@ describe('WalletFactory model', function() { should.exist(w.publicKeyRing.getCopayerId); should.exist(w.txProposals.toObj); should.exist(w.privateKey.toObj); + + // + var expected = JSON.parse(o2.replace(/cosigner/g,'copayerIndex')); + assertObjectEqual(w.toObj(), expected); }); it('should create wallet from encrypted object', function() {