add tests to EncodedData and fix hex conversion bug

...making sure the new changes to __proto__ are working correctly.
This commit is contained in:
Ryan X. Charles 2014-07-15 18:21:38 -07:00
parent cfa0c11983
commit 71f181efee
2 changed files with 44 additions and 9 deletions

View File

@ -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);
});
});
});

View File

@ -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;
}
};