From cf83d92cc1505884dbfe1fc9156192f854894562 Mon Sep 17 00:00:00 2001 From: ricmoo Date: Wed, 1 Mar 2017 15:56:49 -0500 Subject: [PATCH] Fixing up browser test cases. --- tests/{browser.js => make-browser.js} | 5 +- tests/run-providers.js | 26 +++++-- tests/test-contracts.js | 92 ---------------------- tests/test-providers.js | 105 -------------------------- 4 files changed, 23 insertions(+), 205 deletions(-) rename tests/{browser.js => make-browser.js} (70%) delete mode 100644 tests/test-contracts.js delete mode 100644 tests/test-providers.js diff --git a/tests/browser.js b/tests/make-browser.js similarity index 70% rename from tests/browser.js rename to tests/make-browser.js index 089960772..e13d1af48 100644 --- a/tests/browser.js +++ b/tests/make-browser.js @@ -1,8 +1,11 @@ 'use strict'; -module.exports = { +var Tests = { "contract-interface": require('./run-contract-interface.js'), "hdnode": require('./run-hdnode.js'), + "providers": require('./run-providers.js'), "utils": require('./run-utils.js'), "wallet": require('./run-wallet.js'), }; + +module.exports = Tests; diff --git a/tests/run-providers.js b/tests/run-providers.js index d75718701..4c5b07321 100644 --- a/tests/run-providers.js +++ b/tests/run-providers.js @@ -23,13 +23,25 @@ var callFallback = (function() { })(); var privateKey = null; -try { - privateKey = fs.readFileSync('.test-account.key').toString(); - console.log('Found privateKey!'); -} catch (error) { - console.log('Creating new private key!'); - privateKey = utils.hexlify(utils.randomBytes(32)); - fs.writeFileSync('.test-account.key', privateKey); +if (fs && fs.readFileSync) { + try { + privateKey = fs.readFileSync('.test-account.key').toString(); + console.log('Found privateKey!'); + } catch (error) { + console.log('Creating new private key!'); + privateKey = utils.hexlify(utils.randomBytes(32)); + fs.writeFileSync('.test-account.key', privateKey); + } + +} else { + privateKey = global.localStorage.getItem('ethers-tests-privateKey'); + if (privateKey) { + console.log('Found privateKey'); + } else { + console.log('Creating new private key!'); + privateKey = utils.hexlify(utils.randomBytes(32)); + global.localStorage.setItem('ethers-tests-privateKey', privateKey); + } } var provider = providers.getDefaultProvider(true); diff --git a/tests/test-contracts.js b/tests/test-contracts.js deleted file mode 100644 index b940f5570..000000000 --- a/tests/test-contracts.js +++ /dev/null @@ -1,92 +0,0 @@ -'use strict'; - -var contracts = require('../contracts/index.js'); -//var providers = require('../providers/index.js'); - -var provider = new providers.EtherscanProvider({testnet: true}); - -module.exports = function(test) { - var contractAddress = '0xdfaf84077cF4bCECA4F79d167F47041Ed3006D5b'; - var contractABI = { - "SimpleStorage": [ - { - "constant":true, - "inputs":[], - "name":"getValue", - "outputs":[{"name":"value","type":"string"}], - "type":"function" - }, { - "constant":false, - "inputs":[{"name":"value","type":"string"}], - "name":"setValue", - "outputs":[], - "type":"function" - }, { - "anonymous":false, - "inputs":[ - {"indexed":false,"name":"oldValue","type":"string"}, - {"indexed":false,"name":"newValue","type":"string"} - ], - "name":"valueChanged", - "type":"event" - } - ] - } - - var contractInterface = new contracts.Interface(contractABI.SimpleStorage); - var getValue = contractInterface.getValue() - var setValue = contractInterface.setValue("foobar"); - var valueChanged = contractInterface.valueChanged() - - test.equal(getValue.data, '0x20965255', "wrong call data"); - test.equal(setValue.data, '0x93a0935200000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000006666f6f6261720000000000000000000000000000000000000000000000000000', "wrong transaction data"); - test.ok( - (valueChanged.topics.length === 1 && valueChanged.topics[0] === '0x68ad6719a0070b3bb2f866fa0d46c8123b18cefe9b387ddb4feb6647ca418435'), - "wrong call data" - ); - - // @TODO - test decode - - var privateKey = new Buffer(32); - privateKey.fill(0x42); - - var wallet = new Wallet(privateKey, provider); - var contract = wallet.getContract(contractAddress, contractABI.SimpleStorage); - - function testCall() { - return new Promise(function(resolve, reject) { - contract.getValue().then(function(result) { - test.equal(result[0], 'test888888', 'failed to call getVaue (positional)'); - test.equal(result.value, 'test888888', 'failed to call getVaue (keyword)'); - resolve(result); - }, function(error) { - test.ok(false, 'failed to call getValue (is parity running on this host?)'); - reject(error); - }); - }); - } - - function testEstimate() { - return new Promise(function(resolve, reject) { - contract.estimate.setValue('foo').then(function(result) { - test.equal(result.toString(16), '8f5a', 'failed to estimate setVaue'); - resolve(result); - }, function(error) { - test.ok(false, 'failed to call getValue (is parity running on this host?)'); - reject(error); - }); - }); - } - - Promise.all([ - testCall(), - testEstimate(), - ]).then(function(results) { - test.done(); - }, function(error) { - console.log('ERROR', error); - test.done(); - }); -}; - -module.exports.testSelf = module.exports; diff --git a/tests/test-providers.js b/tests/test-providers.js deleted file mode 100644 index a0624d3c6..000000000 --- a/tests/test-providers.js +++ /dev/null @@ -1,105 +0,0 @@ -'use strict'; -var Wallet = require('../index.js'); - -var Web3 = require('web3'); - -// @TODO: We need to do a lot more test cases here: -// - homestead -// - sendTransaction -// - estimateGas with various parameters set and not set -// - estimateGas on a contract with from/value conditionals -// - Metamask-style injected Web3 - -module.exports = function(test) { - - var url = 'http://localhost:8545'; - var web3Provider = new Web3.providers.HttpProvider(url) - var web3 = new Web3(web3Provider) - - var providers = [ - (new Wallet.providers.Web3Provider(web3Provider)), - (new Wallet.providers.Web3Provider(web3)), - (new Wallet.providers.HttpProvider(url)), - (new Wallet.providers.EtherscanProvider({testnet: true})), - ] - - var pending = []; - - function checkMethod(method, params, expectedValue) { - var checks = []; - providers.forEach(function(provider) { - checks.push(new Promise(function(resolve, reject) { - provider[method].apply(provider, params).then(function(value) { - resolve(value); - }, function(error) { - reject(error); - }); - })); - }); - - pending.push(new Promise(function(resolve, reject) { - Promise.all(checks).then(function(results) { - if (!expectedValue) { expectedValue = results[0]; } - results.forEach(function(value) { - if (expectedValue instanceof Wallet.utils.BN) { - test.ok(expectedValue.eq(value), 'Failed ' + method); - } else if (typeof(expectedValue) === 'function') { - test.ok(expectedValue(value), 'Failed ' + method); - } else { - test.equal(value, expectedValue, 'Failed ' + method); - } - }); - resolve(); - }, function(error) { - console.log(error); - test.ok(false, 'Error - ' + error.message) - }); - })); - } - - checkMethod( - 'getBalance', ['0x7357589f8e367c2C31F51242fB77B350A11830F3'] - ); - checkMethod( - 'getTransactionCount', ['0x7357589f8e367c2C31F51242fB77B350A11830F3'] - ); - checkMethod('getGasPrice', []); - checkMethod( - 'call', [{to: '0xdfaf84077cF4bCECA4F79d167F47041Ed3006D5b', data: '0x20965255'}], - '0x00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000006666f6f6261720000000000000000000000000000000000000000000000000000' - ); - checkMethod( - 'estimateGas', [{ - to: '0xdfaf84077cF4bCECA4F79d167F47041Ed3006D5b', - from: '0x7357589f8e367c2C31F51242fB77B350A11830F3', - data: '0x93a0935200000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000003666f6f0000000000000000000000000000000000000000000000000000000000' - }], - new Wallet.utils.BN('35588') - ); - - var privateKey = new Buffer(32); - privateKey.fill(0x42); - - var wallet = new Wallet(privateKey, url); - pending.push(new Promise(function(resolve, reject) { - wallet.estimateGas({ - to: '0xdfaf84077cF4bCECA4F79d167F47041Ed3006D5b', - data: '0x93a0935200000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000003666f6f0000000000000000000000000000000000000000000000000000000000' - }).then(function(value) { - test.equal(value.toString(10), '35588', 'Failed to call wallet.estimateGas'); - resolve(); - }, function(error) { - reject(error); - }); - })); - - Promise.all(pending).then(function(results) { - test.done(); - }, function(error) { - console.log(error); - test.ok(false, 'Error occured: ' + error.message); - }); -} - -module.exports.testSelf = module.exports; -