From c1f5ab2cdc4bcd8517e4022fa5032601bd180da3 Mon Sep 17 00:00:00 2001 From: Marek Kotewicz Date: Tue, 24 Mar 2015 06:50:42 +0100 Subject: [PATCH] test.method.js in helpers --- test/eth.getBalance.js | 61 +++++-------------------------------- test/eth.getBlock.js | 52 ++----------------------------- test/helpers/test.method.js | 61 +++++++++++++++++++++++++++++++++++++ 3 files changed, 70 insertions(+), 104 deletions(-) create mode 100644 test/helpers/test.method.js diff --git a/test/eth.getBalance.js b/test/eth.getBalance.js index 0c798bb..e9b2b15 100644 --- a/test/eth.getBalance.js +++ b/test/eth.getBalance.js @@ -1,7 +1,6 @@ -var chai = require('chai'); -var assert = chai.assert; +var BigNumber = require('bignumber.js'); var web3 = require('../index'); -var FakeHttpProvider = require('./helpers/FakeHttpProvider'); +var testMethod = require('./helpers/test.method.js'); var method = 'getBalance'; @@ -9,73 +8,27 @@ var tests = [{ args: [301, 2], formattedArgs: ['0x12d', '0x2'], result: '0x31981', - formattedResult: '0x31981', + formattedResult: new BigNumber('0x31981', 16), call: 'eth_'+ method },{ args: ['0x12d', '0x1'], formattedArgs: ['0x12d', '0x1'], result: '0x31981', - formattedResult: '0x31981', + formattedResult: new BigNumber('0x31981', 16), call: 'eth_'+ method }, { args: [0x12d, 0x1], formattedArgs: ['0x12d', '0x1'], result: '0x31981', - formattedResult: '0x31981', + formattedResult: new BigNumber('0x31981', 16), call: 'eth_'+ method }, { args: [0x12d], formattedArgs: ['0x12d', web3.eth.defaultBlock], result: '0x31981', - formattedResult: '0x31981', + formattedResult: new BigNumber('0x31981', 16), call: 'eth_'+ method }]; -describe('eth', function () { - describe(method, function () { - tests.forEach(function (test, index) { - it('sync test: ' + index, function () { - - // given - var provider = new FakeHttpProvider(); - web3.setProvider(provider); - provider.injectResult(test.result); - provider.injectValidation(function (payload) { - assert.equal(payload.jsonrpc, '2.0'); - assert.equal(payload.method, test.call); - assert.deepEqual(payload.params, test.formattedArgs); - }); - - // when - var result = web3.eth[method].apply(null, test.args.slice(0)); - - // then - assert.equal(+test.formattedResult, result.toNumber()); - }); - - it('async test: ' + index, function (done) { - - // given - var provider = new FakeHttpProvider(); - web3.setProvider(provider); - provider.injectResult(test.result); - provider.injectValidation(function (payload) { - assert.equal(payload.jsonrpc, '2.0'); - assert.equal(payload.method, test.call); - assert.deepEqual(payload.params, test.formattedArgs); - }); - var callback = function (err, result) { - assert.equal(+test.formattedResult, result.toNumber()); - done(); - }; - - var args = test.args.slice(0); - args.push(callback); - - // when - web3.eth[method].apply(null, args); - }); - }); - }); -}); +testMethod.runTests(method, tests); diff --git a/test/eth.getBlock.js b/test/eth.getBlock.js index 883d213..f836c12 100644 --- a/test/eth.getBlock.js +++ b/test/eth.getBlock.js @@ -1,8 +1,6 @@ -var chai = require('chai'); -var assert = chai.assert; var web3 = require('../index'); var BigNumber = require('bignumber.js'); -var FakeHttpProvider = require('./helpers/FakeHttpProvider'); +var testMethod = require('./helpers/test.method.js'); var method = 'getBlock'; @@ -137,51 +135,5 @@ var tests = [{ call: 'eth_'+ method + 'ByNumber' }]; -describe('eth', function () { - describe(method, function () { - tests.forEach(function (test, index) { - it('sync test: ' + index, function () { - - // given - var provider = new FakeHttpProvider(); - web3.setProvider(provider); - provider.injectResult(test.result); - provider.injectValidation(function (payload) { - assert.equal(payload.jsonrpc, '2.0'); - assert.equal(payload.method, test.call); - assert.deepEqual(payload.params, test.formattedArgs); - }); - - // when - var result = web3.eth[method].apply(null, test.args.slice(0)); - - // then - assert.deepEqual(test.formattedResult, result); - }); - - it('async test: ' + index, function (done) { - - // given - var provider = new FakeHttpProvider(); - web3.setProvider(provider); - provider.injectResult(test.result); - provider.injectValidation(function (payload) { - assert.equal(payload.jsonrpc, '2.0'); - assert.equal(payload.method, test.call); - assert.deepEqual(payload.params, test.formattedArgs); - }); - var callback = function (err, result) { - assert.deepEqual(test.formattedResult, result); - done(); - }; - - var args = test.args.slice(0); - args.push(callback); - - // when - web3.eth[method].apply(null, args); - }); - }); - }); -}); +testMethod.runTests(method, tests); diff --git a/test/helpers/test.method.js b/test/helpers/test.method.js new file mode 100644 index 0000000..21a1def --- /dev/null +++ b/test/helpers/test.method.js @@ -0,0 +1,61 @@ +var chai = require('chai'); +var assert = chai.assert; +var web3 = require('../../index'); +var FakeHttpProvider = require('./FakeHttpProvider'); + +var runTests = function (method, tests) { + + describe('eth', function () { + describe(method, function () { + tests.forEach(function (test, index) { + it('sync test: ' + index, function () { + + // given + var provider = new FakeHttpProvider(); + web3.setProvider(provider); + provider.injectResult(test.result); + provider.injectValidation(function (payload) { + assert.equal(payload.jsonrpc, '2.0'); + assert.equal(payload.method, test.call); + assert.deepEqual(payload.params, test.formattedArgs); + }); + + // when + var result = web3.eth[method].apply(null, test.args.slice(0)); + + // then + assert.deepEqual(test.formattedResult, result); + }); + + it('async test: ' + index, function (done) { + + // given + var provider = new FakeHttpProvider(); + web3.setProvider(provider); + provider.injectResult(test.result); + provider.injectValidation(function (payload) { + assert.equal(payload.jsonrpc, '2.0'); + assert.equal(payload.method, test.call); + assert.deepEqual(payload.params, test.formattedArgs); + }); + var callback = function (err, result) { + assert.deepEqual(test.formattedResult, result); + done(); + }; + + var args = test.args.slice(0); + args.push(callback); + + // when + web3.eth[method].apply(null, args); + }); + }); + }); + }); + +}; + +module.exports = { + runTests: runTests +} +