2015-06-04 12:25:46 +02:00
|
|
|
var chai = require('chai');
|
|
|
|
var assert = chai.assert;
|
|
|
|
var FakeHttpProvider = require('./helpers/FakeHttpProvider');
|
2017-01-26 10:24:14 +01:00
|
|
|
var Web3 = require('../src/index');
|
2015-10-08 04:09:51 +02:00
|
|
|
var web3 = new Web3();
|
2015-06-04 12:25:46 +02:00
|
|
|
|
|
|
|
|
|
|
|
var tests = [{
|
2017-01-26 10:24:14 +01:00
|
|
|
methods: [{
|
|
|
|
name: 'getGasPrice',
|
|
|
|
call: 'eth_gasPrice',
|
|
|
|
outputFormatter: web3.extend.formatters.outputBigNumberFormatter
|
|
|
|
},{
|
2015-06-04 12:25:46 +02:00
|
|
|
name: 'getBalance',
|
|
|
|
call: 'eth_getBalance',
|
|
|
|
params: 2,
|
2017-01-26 10:24:14 +01:00
|
|
|
inputFormatter: [web3.extend.utils.toAddress, web3.extend.formatters.inputDefaultBlockNumberFormatter],
|
|
|
|
outputFormatter: web3.extend.formatters.outputBigNumberFormatter
|
|
|
|
}]
|
2015-06-04 12:25:46 +02:00
|
|
|
},{
|
|
|
|
property: 'admin',
|
2017-01-26 10:24:14 +01:00
|
|
|
methods: [{
|
|
|
|
name: 'getGasPrice',
|
|
|
|
call: 'eth_gasPrice',
|
|
|
|
outputFormatter: web3.extend.formatters.outputBigNumberFormatter
|
|
|
|
},{
|
2015-06-04 12:25:46 +02:00
|
|
|
name: 'getBalance',
|
|
|
|
call: 'eth_getBalance',
|
|
|
|
params: 2,
|
2017-01-26 10:24:14 +01:00
|
|
|
inputFormatter: [web3.extend.utils.toAddress, web3.extend.formatters.inputDefaultBlockNumberFormatter],
|
|
|
|
outputFormatter: web3.extend.formatters.outputBigNumberFormatter
|
|
|
|
}]
|
|
|
|
},{
|
|
|
|
error: true,
|
|
|
|
methods: [{
|
|
|
|
name: 'getGasPrice',
|
|
|
|
outputFormatter: web3.extend.formatters.outputBigNumberFormatter
|
|
|
|
}]
|
|
|
|
},{
|
|
|
|
error: true,
|
|
|
|
methods: [{
|
|
|
|
call: 'eth_gasPrice',
|
|
|
|
outputFormatter: web3.extend.formatters.outputBigNumberFormatter
|
|
|
|
}]
|
2015-06-04 12:25:46 +02:00
|
|
|
}];
|
|
|
|
|
|
|
|
describe('web3', function () {
|
2017-01-26 10:24:14 +01:00
|
|
|
describe('extend', function () {
|
2015-06-04 12:25:46 +02:00
|
|
|
tests.forEach(function (test, index) {
|
2017-01-26 10:24:14 +01:00
|
|
|
it('test no: ' + index, function (done) {
|
|
|
|
var count = 1;
|
|
|
|
|
|
|
|
var provider = new FakeHttpProvider();
|
|
|
|
web3.setProvider(provider);
|
|
|
|
|
|
|
|
if(test.error) {
|
|
|
|
assert.throws(web3.extend.bind(web3,test));
|
2015-06-04 12:25:46 +02:00
|
|
|
|
2017-01-26 10:24:14 +01:00
|
|
|
return done();
|
2015-06-04 12:25:46 +02:00
|
|
|
|
2017-01-26 10:24:14 +01:00
|
|
|
} else {
|
|
|
|
web3.extend(test);
|
|
|
|
}
|
2015-06-04 12:25:46 +02:00
|
|
|
|
2017-01-26 10:24:14 +01:00
|
|
|
if(test.methods)
|
|
|
|
test.methods.forEach(function(property){
|
|
|
|
|
|
|
|
|
|
|
|
provider.injectResult('0x1234');
|
2015-06-04 12:25:46 +02:00
|
|
|
provider.injectValidation(function (payload) {
|
|
|
|
assert.equal(payload.jsonrpc, '2.0');
|
2017-01-26 10:24:14 +01:00
|
|
|
assert.equal(payload.method, property.call);
|
|
|
|
|
|
|
|
if(test.methods.length === count)
|
|
|
|
done();
|
|
|
|
else
|
|
|
|
count++;
|
2015-06-04 12:25:46 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
if(test.property) {
|
|
|
|
assert.isFunction(web3[test.property][property.name]);
|
2017-01-26 10:24:14 +01:00
|
|
|
web3[test.property][property.name]();
|
|
|
|
} else {
|
2015-06-04 12:25:46 +02:00
|
|
|
assert.isFunction(web3[property.name]);
|
2017-01-26 10:24:14 +01:00
|
|
|
web3[property.name]();
|
|
|
|
}
|
2015-06-04 12:25:46 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|