add more tests

This commit is contained in:
Fabian Vogelsteller 2015-02-26 15:54:49 +01:00
parent 2e9a41113e
commit 545de254f9
11 changed files with 160 additions and 85 deletions

74
dist/ethereum.js vendored
View File

@ -1113,7 +1113,7 @@ var inputTransactionFormatter = function(options){
// format the following options
/*jshint maxcomplexity:5 */
['gasPrice', 'value'].forEach(function(key){
['gasPrice', 'gas', 'value'].forEach(function(key){
// if hex or string integer
if(typeof options[key] === 'string') {
@ -1132,9 +1132,6 @@ var inputTransactionFormatter = function(options){
}
});
// format gas to number
options.gas = Number(options.gas);
return options;
};
@ -1170,7 +1167,6 @@ Formats the output of a block to its proper values
@returns object
*/
var outputBlockFormatter = function(block){
/*jshint maxcomplexity:7 */
// transform to number
block.gasLimit = Number(block.gasLimit);
@ -1178,32 +1174,9 @@ var outputBlockFormatter = function(block){
block.size = Number(block.size);
block.timestamp = Number(block.timestamp);
block.number = Number(block.number);
// minGasPrice to bignumber
if(block.minGasPrice) {
if(typeof block.minGasPrice === 'string' && block.minGasPrice.indexOf('0x') === 0)
block.minGasPrice = new BigNumber(block.minGasPrice, 16);
else
block.minGasPrice = new BigNumber(block.minGasPrice.toString(10), 10);
}
// difficulty to bignumber
if(block.difficulty) {
if(typeof block.difficulty === 'string' && block.difficulty.indexOf('0x') === 0)
block.difficulty = new BigNumber(block.difficulty, 16);
else
block.difficulty = new BigNumber(block.difficulty.toString(10), 10);
}
// difficulty to bignumber
if(block.totalDifficulty) {
if(typeof block.totalDifficulty === 'string' && block.totalDifficulty.indexOf('0x') === 0)
block.totalDifficulty = new BigNumber(block.totalDifficulty, 16);
else
block.totalDifficulty = new BigNumber(block.totalDifficulty.toString(10), 10);
}
block.minGasPrice = utils.toBigNumber(block.minGasPrice);
block.difficulty = utils.toBigNumber(block.difficulty);
block.totalDifficulty = utils.toBigNumber(block.totalDifficulty);
return block;
};
@ -1756,11 +1729,10 @@ var filterEvents = function (json) {
/// used to transform value/string to eth string
/// TODO: use BigNumber.js to parse int
/// TODO: add tests for it!
// DEPRECATED
var toEth = function (str) {
console.warn('This method is deprecated please use eth.fromWei(BigNumberOrNumber, unit) instead.');
/*jshint maxcomplexity:7 */
var val = typeof str === "string" ? str.indexOf('0x') === 0 ? parseInt(str.substr(2), 16) : parseInt(str) : str;
var unit = 0;
var units = c.ETH_UNITS;
@ -1914,6 +1886,28 @@ var isAddress = function(address) {
};
/**
Takes an input and transforms it into an bignumber
@method toBigNumber
@param {Number|String|BigNumber} a number, string, HEX string or BigNumber
@return {Object} BigNumber
*/
var toBigNumber = function(number) {
if(number instanceof BigNumber)
return number;
if(number) {
if(typeof number === 'string' && number.indexOf('0x') === 0)
number = new BigNumber(number, 16);
else
number = new BigNumber(number.toString(10), 10);
}
return number;
};
module.exports = {
findIndex: findIndex,
toDecimal: toDecimal,
@ -1927,6 +1921,7 @@ module.exports = {
toEth: toEth,
toWei: toWei,
fromWei: fromWei,
toBigNumber: toBigNumber,
isAddress: isAddress
};
@ -2152,8 +2147,17 @@ var web3 = {
/// @returns hex representation (prefixed by 0x) of decimal value
fromDecimal: utils.fromDecimal,
/// @returns a BigNumber object
toBigNumber: utils.toBigNumber,
// DEPRECATED
/// used to transform value/string to eth string
toEth: utils.toEth,
toEth: function(str) {
console.warn('This method is deprecated please use eth.fromWei(BigNumberOrNumber, unit) instead.');
return utils.toEth(str);
},
toWei: utils.toWei,
fromWei: utils.fromWei,

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -177,7 +177,7 @@ var inputTransactionFormatter = function(options){
// format the following options
/*jshint maxcomplexity:5 */
['gasPrice', 'value'].forEach(function(key){
['gasPrice', 'gas', 'value'].forEach(function(key){
// if hex or string integer
if(typeof options[key] === 'string') {
@ -196,9 +196,6 @@ var inputTransactionFormatter = function(options){
}
});
// format gas to number
options.gas = Number(options.gas);
return options;
};
@ -234,40 +231,17 @@ Formats the output of a block to its proper values
@returns object
*/
var outputBlockFormatter = function(block){
/*jshint maxcomplexity:7 */
// transform to number
block.gasLimit = Number(block.gasLimit);
block.gasUsed = Number(block.gasUsed);
block.size = Number(block.size);
block.timestamp = Number(block.timestamp);
block.number = Number(block.number);
// minGasPrice to bignumber
if(block.minGasPrice) {
if(typeof block.minGasPrice === 'string' && block.minGasPrice.indexOf('0x') === 0)
block.minGasPrice = new BigNumber(block.minGasPrice, 16);
else
block.minGasPrice = new BigNumber(block.minGasPrice.toString(10), 10);
}
// difficulty to bignumber
if(block.difficulty) {
if(typeof block.difficulty === 'string' && block.difficulty.indexOf('0x') === 0)
block.difficulty = new BigNumber(block.difficulty, 16);
else
block.difficulty = new BigNumber(block.difficulty.toString(10), 10);
}
// difficulty to bignumber
if(block.totalDifficulty) {
if(typeof block.totalDifficulty === 'string' && block.totalDifficulty.indexOf('0x') === 0)
block.totalDifficulty = new BigNumber(block.totalDifficulty, 16);
else
block.totalDifficulty = new BigNumber(block.totalDifficulty.toString(10), 10);
}
block.gasLimit = utils.toDecimal(block.gasLimit);
block.gasUsed = utils.toDecimal(block.gasUsed);
block.size = utils.toDecimal(block.size);
block.timestamp = utils.toDecimal(block.timestamp);
block.number = utils.toDecimal(block.number);
block.minGasPrice = utils.toBigNumber(block.minGasPrice);
block.difficulty = utils.toBigNumber(block.difficulty);
block.totalDifficulty = utils.toBigNumber(block.totalDifficulty);
return block;
};

View File

@ -130,11 +130,10 @@ var filterEvents = function (json) {
/// used to transform value/string to eth string
/// TODO: use BigNumber.js to parse int
/// TODO: add tests for it!
// DEPRECATED
var toEth = function (str) {
console.warn('This method is deprecated please use eth.fromWei(BigNumberOrNumber, unit) instead.');
/*jshint maxcomplexity:7 */
var val = typeof str === "string" ? str.indexOf('0x') === 0 ? parseInt(str.substr(2), 16) : parseInt(str) : str;
var unit = 0;
var units = c.ETH_UNITS;
@ -159,6 +158,11 @@ var toEth = function (str) {
var toDecimal = function (val) {
// pass it through is its already a number
if(typeof val === 'number' || (typeof val === 'string' && val.indexOf('0x') === -1))
return val;
// remove 0x and place 0, if it's required
val = val.length > 2 ? val.substring(2) : "0";
return (new BigNumber(val, 16).toString(10));
@ -288,6 +292,28 @@ var isAddress = function(address) {
};
/**
Takes an input and transforms it into an bignumber
@method toBigNumber
@param {Number|String|BigNumber} a number, string, HEX string or BigNumber
@return {Object} BigNumber
*/
var toBigNumber = function(number) {
if(number instanceof BigNumber)
return number;
if(number) {
if(typeof number === 'string' && number.indexOf('0x') === 0)
number = new BigNumber(number, 16);
else
number = new BigNumber(number.toString(10), 10);
}
return number;
};
module.exports = {
findIndex: findIndex,
toDecimal: toDecimal,
@ -301,6 +327,7 @@ module.exports = {
toEth: toEth,
toWei: toWei,
fromWei: fromWei,
toBigNumber: toBigNumber,
isAddress: isAddress
};

View File

@ -23,10 +23,6 @@
* @date 2014
*/
// if (process.env.NODE_ENV !== 'build') {
// var BigNumber = require('bignumber.js');
// }
var eth = require('./eth');
var db = require('./db');
var shh = require('./shh');
@ -167,8 +163,17 @@ var web3 = {
/// @returns hex representation (prefixed by 0x) of decimal value
fromDecimal: utils.fromDecimal,
/// @returns a BigNumber object
toBigNumber: utils.toBigNumber,
// DEPRECATED
/// used to transform value/string to eth string
toEth: utils.toEth,
toEth: function(str) {
console.warn('This method is deprecated please use eth.fromWei(BigNumberOrNumber, unit) instead.');
return utils.toEth(str);
},
toWei: utils.toWei,
fromWei: utils.fromWei,

View File

@ -0,0 +1,26 @@
var assert = require('assert');
var formatters = require('../lib/formatters.js');
var BigNumber = require('bignumber.js');
describe('formatters', function () {
describe('inputTransactionFormatter', function () {
it('should return the correct value', function () {
assert.deepEqual(formatters.inputTransactionFormatter({
data: '0x34234kjh23kj4234',
value: new BigNumber(100),
from: '0x00000',
to: '0x00000',
gas: 1000,
gasPrice: new BigNumber(1000),
}), {
data: '0x34234kjh23kj4234',
value: '0x64',
from: '0x00000',
to: '0x00000',
gas: '0x3e8',
gasPrice: '0x3e8',
});
});
});
});

12
test/utils.fromDecimal.js Normal file
View File

@ -0,0 +1,12 @@
var assert = require('assert');
var utils = require('../lib/utils.js');
describe('utils', function () {
describe('fromDecimal', function () {
it('should return the correct value', function () {
assert.equal(utils.fromDecimal(1000), "0x3e8");
assert.equal(utils.fromDecimal('1000'), "0x3e8");
});
});
});

13
test/utils.toBigNumber.js Normal file
View File

@ -0,0 +1,13 @@
var assert = require('assert');
var utils = require('../lib/utils.js');
var BigNumber = require('bignumber.js');
describe('utils', function () {
describe('toBigNumber', function () {
it('should return the correct value', function () {
assert.equal(utils.toBigNumber(100000) instanceof BigNumber, true);
assert.equal(utils.toBigNumber(100000).toString(10), '100000');
});
});
});

14
test/utils.toDecimal.js Normal file
View File

@ -0,0 +1,14 @@
var assert = require('assert');
var utils = require('../lib/utils.js');
describe('utils', function () {
describe('toDecimal', function () {
it('should return the correct value', function () {
assert.equal(utils.toDecimal("0x3e8"), '1000');
// allow compatiblity
assert.equal(utils.toDecimal(100000), '100000');
assert.equal(utils.toDecimal('100000'), '100000');
});
});
});

View File

@ -8,9 +8,9 @@ describe('web3', function() {
u.methodExists(web3, 'fromAscii');
u.methodExists(web3, 'toDecimal');
u.methodExists(web3, 'fromDecimal');
// u.methodExists(web3, 'toEth');
u.methodExists(web3, 'fromWei');
u.methodExists(web3, 'toWei');
u.methodExists(web3, 'toBigNumber');
u.methodExists(web3, 'isAddress');
u.methodExists(web3, 'setProvider');
u.methodExists(web3, 'reset');