From 71f181efee636e942c9dce26b2a70df6d3d3e4f4 Mon Sep 17 00:00:00 2001 From: "Ryan X. Charles" Date: Tue, 15 Jul 2014 18:21:38 -0700 Subject: [PATCH] add tests to EncodedData and fix hex conversion bug ...making sure the new changes to __proto__ are working correctly. --- test/test.EncodedData.js | 49 ++++++++++++++++++++++++++++++++++------ util/EncodedData.js | 4 ++-- 2 files changed, 44 insertions(+), 9 deletions(-) diff --git a/test/test.EncodedData.js b/test/test.EncodedData.js index e269661..3573972 100644 --- a/test/test.EncodedData.js +++ b/test/test.EncodedData.js @@ -5,21 +5,56 @@ var bitcore = bitcore || require('../bitcore'); var should = chai.should(); -var EncodedDataModule = bitcore.EncodedData; -var EncodedData; +var EncodedData = bitcore.EncodedData; describe('EncodedData', function() { - it('should initialze the main object', function() { - should.exist(EncodedDataModule); - }); - it('should be able to create class', function() { - EncodedData = EncodedDataModule; + + it('should initialize the main object', function() { should.exist(EncodedData); }); + it('should be able to create an instance', function() { var ed = new EncodedData('1GMx4HdDmN78xzGvdQYkwrVqkmLDG1aMNT'); should.exist(ed); }); + + describe('#as', function() { + var buf = bitcore.util.sha256('test'); + var hex = buf.toString('hex'); + var b58 = '2DFtpKRbW2nfrzgAgE25onW3vwCQwM7S1iHk34LW9cwH1kzmHp'; + + it('should convert from binary -> base58', function() { + var ed = new EncodedData(buf); + ed.as('base58').should.equal(bitcore.Base58.base58Check.encode(buf)); + }); + + it('should convert from binary -> hex', function() { + var ed = new EncodedData(buf); + ed.as('hex').should.equal(hex); + }); + + it('should convert from base58 -> binary', function() { + var ed = new EncodedData(b58); + ed.as('binary').toString('hex').should.equal(hex); + }); + + it('should convert from base58 -> hex', function() { + var ed = new EncodedData(b58); + ed.as('hex').should.equal(hex); + }); + + it('should convert from hex -> binary', function() { + var ed = new EncodedData(hex, 'hex'); + ed.as('binary').toString('hex').should.equal(hex); + }); + + it('should convert from hex -> base58', function() { + var ed = new EncodedData(hex, 'hex'); + ed.as('base58').should.equal(b58); + }); + + }); + }); diff --git a/util/EncodedData.js b/util/EncodedData.js index 87e25d2..fbd45dd 100644 --- a/util/EncodedData.js +++ b/util/EncodedData.js @@ -14,8 +14,8 @@ function EncodedData(data, encoding) { } else { if (typeof this.encodings[encoding] === 'undefined') encoding = 'binary'; - this.converters = this.encodings['binary'].converters; - this._encoding = this.encodings['binary']._encoding; + this.converters = this.encodings[encoding].converters; + this._encoding = this.encodings[encoding]._encoding; } };