add tests for getBalance
This commit is contained in:
parent
bb3e87d85e
commit
a5ecd9f1f4
|
@ -29,6 +29,9 @@ class AccountParser {
|
|||
return balanceString;
|
||||
}
|
||||
const match = balanceString.match(/([0-9]+) ?([a-zA-Z]*)/);
|
||||
if (!match) {
|
||||
throw new Error(__('Unrecognized balance string "%s"', balanceString));
|
||||
}
|
||||
if (!match[2]) {
|
||||
return web3.utils.toHex(parseInt(match[1], 10));
|
||||
}
|
||||
|
|
|
@ -3,6 +3,9 @@ const assert = require('assert');
|
|||
const sinon = require('sinon');
|
||||
const AccountParser = require('../lib/contracts/accountParser');
|
||||
let TestLogger = require('../lib/tests/test_logger.js');
|
||||
const Web3 = require('web3');
|
||||
const i18n = require('../lib/i18n/i18n.js');
|
||||
i18n.setOrDetectLocale('en');
|
||||
|
||||
describe('embark.AccountParser', function () {
|
||||
describe('#getAccount', function () {
|
||||
|
@ -25,7 +28,7 @@ describe('embark.AccountParser', function () {
|
|||
privateKey: 'myKey'
|
||||
}, web3, testLogger);
|
||||
|
||||
assert.deepEqual(account, {key: '0xmyKey'});
|
||||
assert.deepEqual(account, {key: '0xmyKey', hexBalance: null});
|
||||
});
|
||||
|
||||
it('should return two accounts from the keys in the file', function () {
|
||||
|
@ -34,8 +37,8 @@ describe('embark.AccountParser', function () {
|
|||
}, web3, testLogger);
|
||||
|
||||
assert.deepEqual(account, [
|
||||
{key: '0xkey1'},
|
||||
{key: '0xkey2'}
|
||||
{key: '0xkey1', hexBalance: null},
|
||||
{key: '0xkey2', hexBalance: null}
|
||||
]);
|
||||
});
|
||||
|
||||
|
@ -45,7 +48,7 @@ describe('embark.AccountParser', function () {
|
|||
}, web3, testLogger);
|
||||
|
||||
assert.deepEqual(account,
|
||||
[{key: "0xf942d5d524ec07158df4354402bfba8d928c99d0ab34d0799a6158d56156d986"}]);
|
||||
[{key: "0xf942d5d524ec07158df4354402bfba8d928c99d0ab34d0799a6158d56156d986", hexBalance: null}]);
|
||||
});
|
||||
|
||||
it('should return two accounts from the mnemonic using numAddresses', function () {
|
||||
|
@ -56,8 +59,8 @@ describe('embark.AccountParser', function () {
|
|||
|
||||
assert.deepEqual(account,
|
||||
[
|
||||
{key: "0xf942d5d524ec07158df4354402bfba8d928c99d0ab34d0799a6158d56156d986"},
|
||||
{key: "0x88f37cfbaed8c0c515c62a17a3a1ce2f397d08bbf20dcc788b69f11b5a5c9791"}
|
||||
{key: "0xf942d5d524ec07158df4354402bfba8d928c99d0ab34d0799a6158d56156d986", hexBalance: null},
|
||||
{key: "0x88f37cfbaed8c0c515c62a17a3a1ce2f397d08bbf20dcc788b69f11b5a5c9791", hexBalance: null}
|
||||
]);
|
||||
});
|
||||
|
||||
|
@ -68,6 +71,46 @@ describe('embark.AccountParser', function () {
|
|||
|
||||
assert.strictEqual(account, null);
|
||||
});
|
||||
});
|
||||
|
||||
describe('getHexBalance', () => {
|
||||
it('should return default if no balance', () => {
|
||||
const hexBalance = AccountParser.getHexBalance(null, Web3);
|
||||
|
||||
assert.strictEqual(hexBalance, 0xFFFFFFFFFFFFFFFFFF);
|
||||
});
|
||||
|
||||
it('should return the balance string if already hexadecimal', () => {
|
||||
const hexBalance = AccountParser.getHexBalance('0xFFF', Web3);
|
||||
|
||||
assert.strictEqual(hexBalance, '0xFFF');
|
||||
});
|
||||
|
||||
it('should convert to hex when decimal', () => {
|
||||
const hexBalance = AccountParser.getHexBalance('500', Web3);
|
||||
|
||||
assert.strictEqual(hexBalance, '0x1f4');
|
||||
});
|
||||
|
||||
it('should convert to hex with eth string', () => {
|
||||
const hexBalance = AccountParser.getHexBalance('4ether', Web3);
|
||||
|
||||
assert.strictEqual(hexBalance, '0x3782dace9d900000');
|
||||
});
|
||||
|
||||
it('should convert to hex with eth string with space', () => {
|
||||
const hexBalance = AccountParser.getHexBalance('673 shannon', Web3);
|
||||
|
||||
assert.strictEqual(hexBalance, '0x9cb1ed0a00');
|
||||
});
|
||||
|
||||
it('should fail when string is not good', () => {
|
||||
try {
|
||||
AccountParser.getHexBalance('nogood', Web3);
|
||||
assert.fail('Should have failed at getHexBalance');
|
||||
} catch (e) {
|
||||
// Ok
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Reference in New Issue