diff --git a/lib/tests/run_tests.js b/lib/tests/run_tests.js index ea8f3102..43f3a815 100644 --- a/lib/tests/run_tests.js +++ b/lib/tests/run_tests.js @@ -56,7 +56,8 @@ module.exports = { global.contract = function(describeName, callback) { return Mocha.describe(describeName, callback); }; - next(); + + test.init(next); } ], (err) => { if (err) { diff --git a/lib/tests/test.js b/lib/tests/test.js index e821b72a..3564459b 100644 --- a/lib/tests/test.js +++ b/lib/tests/test.js @@ -27,6 +27,8 @@ class Test { constructor(options) { this.options = options || {}; this.simOptions = this.options.simulatorOptions || {}; + this.contracts = {}; + this.web3 = new Web3(); if (this.simOptions.node) { this.web3.setProvider(new this.web3.providers.HttpProvider(this.simOptions.node)); @@ -57,6 +59,12 @@ class Test { this.engine.startService("codeGenerator"); } + init(callback) { + this.engine.contractsManager.build(() => { + callback(); + }); + } + config(options, callback) { this.options = utils.recursiveMerge(this.options, options); this.simOptions = this.options.simulatorOptions || {}; @@ -70,46 +78,77 @@ class Test { }); } - _deploy(config, cb) { + _deploy(config, callback) { const self = this; async.waterfall([ - function getConfig(callback) { + function getConfig(next) { let _versions_default = self.engine.config.contractsConfig.versions; self.engine.config.contractsConfig = {contracts: config.contracts, versions: _versions_default}; - callback(); - }, - function reloadConfig(callback) { self.engine.events.emit(constants.events.contractConfigChanged, self.engine.config.contractsConfig); - callback(); + next(); }, - function deploy(callback) { + function deploy(next) { + // self.engine.events.on('code-generator-ready', function () { + // self.engine.events.request('code-generator:contract:vanilla', function(vanillaABI) { + // console.log(vanillaABI); + // }); + // }); + self.engine.deployManager.gasLimit = 6000000; self.engine.contractsManager.gasLimit = 6000000; self.engine.deployManager.fatalErrors = true; self.engine.deployManager.deployOnlyOnConfig = true; - self.engine.deployManager.deployContracts(true, function(err, _result) { + self.engine.deployManager.deployContracts(function (err) { if (err) { - callback(err); + return next(err); } - callback(); + next(); }); + }, + function getAccounts(next) { + self.web3.eth.getAccounts(function(err, accounts) { + if (err) { + return next(err); + } + self.accounts = accounts; + self.web3.eth.defaultAccount = accounts[0]; + next(); + }); + }, + function createContractObject(next) { + async.each(Object.keys(self.contracts), (contractName, eachCb) => { + const contract = self.engine.contractsManager.contracts[contractName]; + self.contracts[contractName].contract = new self.web3.eth.Contract(contract.abiDefinition, contract.address, + {from: self.web3.defaultAccount, gas: 6000000}); + eachCb(); + }, next); } - ], function(err) { + ], function (err) { if (err) { console.log(__('terminating due to error')); - cb(err); + throw new Error(err); } - // this should be part of the waterfall and not just something done at the - // end - self.web3.eth.getAccounts(function(err, accounts) { - if (err) { - throw new Error(err); - } - self.web3.eth.defaultAccount = accounts[0]; - cb(null, accounts); - }); + callback(); }); } + + require(module) { + if (module.startsWith('contracts/')) { + const contractName = module.substr(10); + if (!this.engine.contractsManager.contracts[contractName]) { + throw new Error(__('No contract with the name %s', contractName)); + } + if (this.contracts[contractName]) { + return this.contracts[contractName]; + } + const contract = this.engine.contractsManager.contracts[contractName]; + this.contracts[contractName] = {}; + // TODO find better way to update contract + this.contracts[contractName].contract = new this.web3.eth.Contract(contract.abiDefinition, contract.address); + return this.contracts[contractName]; + } + throw new Error(__('Unknown module %s', module)); + } } module.exports = Test; diff --git a/test_apps/contracts_app/contracts/SimpleStorage.sol b/test_apps/contracts_app/contracts/simple_storage.sol similarity index 100% rename from test_apps/contracts_app/contracts/SimpleStorage.sol rename to test_apps/contracts_app/contracts/simple_storage.sol diff --git a/test_apps/contracts_app/test/simple_storage_spec.js b/test_apps/contracts_app/test/simple_storage_spec.js index b43e0b35..34b73f91 100644 --- a/test_apps/contracts_app/test/simple_storage_spec.js +++ b/test_apps/contracts_app/test/simple_storage_spec.js @@ -1,31 +1,33 @@ +/*global contract, before, it, embark, web3*/ +const assert = require('assert'); +const SimpleStorage = embark.require('contracts/SimpleStorage'); -contract("SimpleStorage", function() { - +contract("SimpleStorage", function () { this.timeout(0); - before(function(done) { - this.timeout(0); - //config({ - // node: "http://localhost:8545" - //}); - - var contractsConfig = { - "SimpleStorage": { - args: [100] + before(function (done) { + const contractsConfig = { + contracts: { + "SimpleStorage": { + args: [100] + } } }; - EmbarkSpec.deployAll(contractsConfig, () => { done() }); + embark.config(contractsConfig, () => { + done(); + }); }); - it("should set constructor value", async function() { - let result = await SimpleStorage.methods.storedData().call(); - assert.equal(result, 100); + it("should set constructor value", async function () { + let result = await SimpleStorage.contract.methods.storedData().call(); + assert.strictEqual(parseInt(result, 10), 100); }); - it("set storage value", async function() { - await SimpleStorage.methods.set(150).send(); - let result = await SimpleStorage.methods.get().call(); - assert.equal(result, 499650); + it("set storage value", async function () { + // TODO Solve from + await SimpleStorage.contract.methods.set(150).send({from: web3.eth.defaultAccount}); + let result = await SimpleStorage.contract.methods.get().call(); + assert.strictEqual(parseInt(result, 10), 499650); }); });