2015-03-23 13:08:59 +00:00
|
|
|
var chai = require('chai');
|
|
|
|
var assert = chai.assert;
|
|
|
|
var web3 = require('../index');
|
2015-03-23 14:57:36 +00:00
|
|
|
var FakeHttpProvider = require('./helpers/FakeHttpProvider');
|
2015-03-23 13:08:59 +00:00
|
|
|
|
|
|
|
var method = 'getBalance';
|
|
|
|
|
|
|
|
var tests = [{
|
|
|
|
args: ['0x12d', '0x1'],
|
|
|
|
formattedArgs: ['0x12d', '0x1'],
|
|
|
|
result: '0x31981',
|
|
|
|
formattedResult: '0x31981',
|
|
|
|
call: 'eth_getBalance'
|
2015-03-23 13:23:45 +00:00
|
|
|
}, {
|
|
|
|
args: [0x12d, 0x1],
|
|
|
|
formattedArgs: ['0x12d', '0x1'],
|
|
|
|
result: '0x31981',
|
|
|
|
formattedResult: '0x31981',
|
|
|
|
call: 'eth_getBalance'
|
|
|
|
}, {
|
|
|
|
args: [0x12d],
|
2015-03-23 15:42:09 +00:00
|
|
|
formattedArgs: ['0x12d', 'latest'], // here we might want to get current defaultBlock
|
2015-03-23 13:23:45 +00:00
|
|
|
result: '0x31981',
|
|
|
|
formattedResult: '0x31981',
|
|
|
|
call: 'eth_getBalance'
|
2015-03-23 13:08:59 +00:00
|
|
|
}];
|
|
|
|
|
|
|
|
describe('eth', function () {
|
|
|
|
describe(method, function () {
|
|
|
|
tests.forEach(function (test, index) {
|
2015-03-23 13:23:45 +00:00
|
|
|
it('sync test: ' + index, function () {
|
2015-03-23 13:08:59 +00:00
|
|
|
|
|
|
|
// given
|
|
|
|
var provider = new FakeHttpProvider();
|
|
|
|
web3.setProvider(provider);
|
|
|
|
provider.injectResult(test.result);
|
|
|
|
var validation = function (payload) {
|
|
|
|
assert.equal(payload.jsonrpc, '2.0');
|
|
|
|
assert.equal(payload.method, test.call);
|
|
|
|
assert.deepEqual(payload.params, test.formattedArgs);
|
|
|
|
};
|
|
|
|
provider.injectValidation(validation);
|
|
|
|
|
|
|
|
// when
|
|
|
|
var result = web3.eth[method].apply(null, test.args.slice(0));
|
|
|
|
|
|
|
|
// then
|
2015-03-23 15:42:09 +00:00
|
|
|
assert.equal(+test.formattedResult, result.toNumber());
|
2015-03-23 13:08:59 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('async test: ' + index, function (done) {
|
|
|
|
|
|
|
|
// given
|
|
|
|
var provider = new FakeHttpProvider();
|
|
|
|
web3.setProvider(provider);
|
|
|
|
provider.injectResult(test.result);
|
|
|
|
var validation = function (payload) {
|
|
|
|
assert.equal(payload.jsonrpc, '2.0');
|
|
|
|
assert.equal(payload.method, test.call);
|
|
|
|
assert.deepEqual(payload.params, test.formattedArgs);
|
|
|
|
};
|
|
|
|
provider.injectValidation(validation);
|
|
|
|
var callback = function (err, result) {
|
2015-03-23 15:42:09 +00:00
|
|
|
assert.equal(+test.formattedResult, result.toNumber());
|
2015-03-23 13:08:59 +00:00
|
|
|
done();
|
|
|
|
};
|
|
|
|
|
|
|
|
var args = test.args.slice(0);
|
|
|
|
args.push(callback);
|
|
|
|
|
|
|
|
// when
|
|
|
|
web3.eth[method].apply(null, args);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|