From 6a5aa76b76fd5f799d4d7f88ea6e4c32a6366e88 Mon Sep 17 00:00:00 2001 From: Manuel Araoz Date: Thu, 10 Apr 2014 18:19:13 -0300 Subject: [PATCH 1/3] Buffers.monkey now at 100% coverage --- test/test.misc.js | 38 +++++++++++++++++++++++++++++++++++--- 1 file changed, 35 insertions(+), 3 deletions(-) diff --git a/test/test.misc.js b/test/test.misc.js index b3ecda63b..2354efdd9 100644 --- a/test/test.misc.js +++ b/test/test.misc.js @@ -3,6 +3,7 @@ var chai = chai || require('chai'); var bitcore = bitcore || require('../bitcore'); var buffertools = require('buffertools'); +buffertools.extend(); var should = chai.should(); @@ -15,6 +16,7 @@ var base58Check = base58.base58Check; var Address = bitcore.Address; var networks = bitcore.networks; var WalletKey = bitcore.WalletKey; +var Buffers = require('buffers'); describe('Miscelaneous stuff', function() { it('should initialze the config object', function() { @@ -26,7 +28,7 @@ describe('Miscelaneous stuff', function() { it('should initialze the network object', function() { should.exist(networks); var nets = [networks.livenet, networks.testnet]; - for (var i=0; i<2; i++) { + for (var i = 0; i < 2; i++) { var net = nets[i]; should.exist(net.addressVersion); should.exist(net.privKeyVersion); @@ -42,8 +44,36 @@ describe('Miscelaneous stuff', function() { should.exist(bitcore.Deserialize); should.exist(bitcore.Deserialize.intFromCompact); }); + it('should initialze the Buffer class', function() { + should.exist(bitcore.Buffer); + }); + describe('Buffers monkey patch', function() { + var bufs; + beforeEach(function() { + bufs = new Buffers(); + bufs.push(new Buffer([1, 2, 3])); + bufs.push(new Buffer([4, 5, 6, 7])); + bufs.push(new Buffer([8, 9, 10])); + }); + it('should monkey patch the Buffers class', function() { + require('../Buffers.monkey').patch(Buffers); + should.exist(bufs.skip); + }); + it('should work for 0', function() { + bufs.skip(0); + bufs.toBuffer().toHex().should.equal('0102030405060708090a'); + }); + it('should work for length', function() { + bufs.skip(bufs.length); + bufs.toBuffer().toHex().should.equal(''); + }); + it('should work for middle values', function() { + bufs.skip(5); + bufs.toBuffer().toHex().should.equal('060708090a'); + }); + }); // bignum it('should initialze the bignum object', function() { should.exist(bitcore.bignum); @@ -165,8 +195,10 @@ describe('Miscelaneous stuff', function() { priv: b58 }); }; - createLivenet.should.throw(); - createTestnet.should.throw(); + createLivenet.should. + throw (); + createTestnet.should. + throw (); }); }); From b58d5c508414720e7cec7ae9fb697318a9430e4f Mon Sep 17 00:00:00 2001 From: Manuel Araoz Date: Thu, 10 Apr 2014 18:24:20 -0300 Subject: [PATCH 2/3] fix formatting --- test/test.misc.js | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/test/test.misc.js b/test/test.misc.js index 2354efdd9..fd212396d 100644 --- a/test/test.misc.js +++ b/test/test.misc.js @@ -195,10 +195,8 @@ describe('Miscelaneous stuff', function() { priv: b58 }); }; - createLivenet.should. - throw (); - createTestnet.should. - throw (); + createLivenet.should.throw(); + createTestnet.should.throw(); }); }); From 3cbcbd54cb7a4da9f1be3a263c8c328830ba04d8 Mon Sep 17 00:00:00 2001 From: Manuel Araoz Date: Thu, 10 Apr 2014 18:52:13 -0300 Subject: [PATCH 3/3] fix Buffers.skip in the browser --- Buffers.monkey.js | 3 +-- bitcore.js | 1 + browser/build.js | 3 +++ test/test.misc.js | 19 ++++++++++++------- 4 files changed, 17 insertions(+), 9 deletions(-) diff --git a/Buffers.monkey.js b/Buffers.monkey.js index ef7c73d26..5cf677085 100644 --- a/Buffers.monkey.js +++ b/Buffers.monkey.js @@ -9,8 +9,7 @@ exports.patch = function(Buffers) { } var pos = this.pos(i); this.buffers = this.buffers.slice(pos.buf); - this.buffers[0].length -= pos.offset; - this.buffers[0].offset += pos.offset; + this.buffers[0] = new Buffer(this.buffers[0].slice(pos.offset)); this.length -= i; }; }; diff --git a/bitcore.js b/bitcore.js index 93e1a8dd4..66eb47cfb 100644 --- a/bitcore.js +++ b/bitcore.js @@ -14,6 +14,7 @@ requireWhenAccessed('bignum', 'bignum'); requireWhenAccessed('base58', 'base58-native'); requireWhenAccessed('bufferput', 'bufferput'); requireWhenAccessed('buffertools', 'buffertools'); +requireWhenAccessed('Buffers.monkey', './Buffers.monkey'); requireWhenAccessed('config', './config'); requireWhenAccessed('const', './const'); requireWhenAccessed('Deserialize', './Deserialize'); diff --git a/browser/build.js b/browser/build.js index 941d52078..41280cf10 100644 --- a/browser/build.js +++ b/browser/build.js @@ -101,6 +101,9 @@ var createBitcore = function(opts) { b.require(opts.dir + 'base58-native', { expose: 'base58-native' }); + b.require(opts.dir + 'buffers', { + expose: 'buffers' + }); b.require('./' + opts.dir + 'bitcore', { expose: 'bitcore' }); diff --git a/test/test.misc.js b/test/test.misc.js index fd212396d..f3da0b852 100644 --- a/test/test.misc.js +++ b/test/test.misc.js @@ -17,6 +17,8 @@ var Address = bitcore.Address; var networks = bitcore.networks; var WalletKey = bitcore.WalletKey; var Buffers = require('buffers'); +var m = bitcore['Buffers.monkey'] || require('../Buffers.monkey'); +m.patch(Buffers); describe('Miscelaneous stuff', function() { it('should initialze the config object', function() { @@ -53,25 +55,28 @@ describe('Miscelaneous stuff', function() { var bufs; beforeEach(function() { bufs = new Buffers(); - bufs.push(new Buffer([1, 2, 3])); - bufs.push(new Buffer([4, 5, 6, 7])); - bufs.push(new Buffer([8, 9, 10])); + bufs.push(new Buffer('aaaaaa', 'hex')); + bufs.push(new Buffer('bbbb', 'hex')); + bufs.push(new Buffer('cc', 'hex')); }); it('should monkey patch the Buffers class', function() { - require('../Buffers.monkey').patch(Buffers); should.exist(bufs.skip); }); it('should work for 0', function() { bufs.skip(0); - bufs.toBuffer().toHex().should.equal('0102030405060708090a'); + bufs.toBuffer().toHex().should.equal('aaaaaabbbbcc'); }); it('should work for length', function() { bufs.skip(bufs.length); bufs.toBuffer().toHex().should.equal(''); }); it('should work for middle values', function() { - bufs.skip(5); - bufs.toBuffer().toHex().should.equal('060708090a'); + bufs.skip(4); + bufs.toBuffer().toHex().should.equal('bbcc'); + bufs.skip(1); + bufs.toBuffer().toHex().should.equal('cc'); + bufs.skip(1); + bufs.toBuffer().toHex().should.equal(''); }); }); // bignum