bitcore/test/merkleblock.js

135 lines
3.7 KiB
JavaScript
Raw Normal View History

2015-02-19 22:38:47 -08:00
'use strict';
var bitcore = require('..'),
MerkleBlock = bitcore.MerkleBlock,
BufferReader = bitcore.encoding.BufferReader,
BufferWriter = bitcore.encoding.BufferWriter,
data = require('./data/merkleblocks.js');
describe('MerkleBlock', function() {
var blockhex = data.HEX[0];
var blockbuf = new Buffer(blockhex,'hex');
var blockJSON = JSON.stringify(data.JSON[0]);
var blockObject = JSON.parse(JSON.stringify(data.JSON[0]));
describe('#constructor', function() {
it('should make a new merkleblock from buffer', function() {
var b = MerkleBlock(blockbuf);
b.toBuffer().toString('hex').should.equal(blockhex);
});
it('should make a new merkleblock from object', function() {
var b = MerkleBlock(blockObject);
b.toObject().should.deep.equal(blockObject);
});
it('should make a new merkleblock from JSON', function() {
var b = MerkleBlock(blockJSON);
b.toJSON().should.equal(blockJSON);
});
it('should not make an empty block', function() {
(function() {
return new MerkleBlock();
}).should.throw('Unrecognized argument for Block');
});
});
describe('#fromJSON', function() {
it('should set these known values', function() {
var block = MerkleBlock.fromJSON(blockJSON);
should.exist(block.header);
should.exist(block.numTransactions);
should.exist(block.hashes);
should.exist(block.flags);
});
it('should set these known values', function() {
var block = MerkleBlock(blockJSON);
should.exist(block.header);
should.exist(block.numTransactions);
should.exist(block.hashes);
should.exist(block.flags);
});
it('accepts an object as argument', function() {
var block = MerkleBlock(blockbuf);
MerkleBlock.fromJSON(block.toObject()).should.exist();
});
});
describe('#toJSON', function() {
it('should recover these known values', function() {
var block = MerkleBlock.fromJSON(blockJSON);
var b = JSON.parse(block.toJSON());
should.exist(block.header);
should.exist(block.numTransactions);
should.exist(block.hashes);
should.exist(block.flags);
2015-02-20 09:07:50 -08:00
should.exist(b.header);
should.exist(b.numTransactions);
should.exist(b.hashes);
should.exist(b.flags);
2015-02-19 22:38:47 -08:00
});
});
// TODO
//describe('#fromString/#toString', function() {
//it('should output/input a block hex string', function() {
//var b = MerkleBlock.fromString(blockhex);
//b.toString().should.equal(blockhex);
//});
//});
describe('#fromBuffer', function() {
it('should make a block from this known buffer', function() {
var block = MerkleBlock.fromBuffer(blockbuf);
block.toBuffer().toString('hex').should.equal(blockhex);
});
});
describe('#fromBufferReader', function() {
it('should make a block from this known buffer', function() {
var block = MerkleBlock.fromBufferReader(BufferReader(blockbuf));
block.toBuffer().toString('hex').should.equal(blockhex);
});
});
describe('#toBuffer', function() {
it('should recover a block from this known buffer', function() {
var block = MerkleBlock.fromBuffer(blockbuf);
block.toBuffer().toString('hex').should.equal(blockhex);
});
});
describe('#toBufferWriter', function() {
it('should recover a block from this known buffer', function() {
var block = MerkleBlock.fromBuffer(blockbuf);
block.toBufferWriter().concat().toString('hex').should.equal(blockhex);
});
it('doesn\'t create a bufferWriter if one provided', function() {
var writer = new BufferWriter();
var block = MerkleBlock.fromBuffer(blockbuf);
block.toBufferWriter(writer).should.equal(writer);
});
});
});