From 95615d14a7b982069558196ad54c5476c73e283d Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Sun, 21 Aug 2016 18:05:35 -0400 Subject: [PATCH] some progress with new tests --- demo/test/simple_storage_spec.js | 41 +++++++++++------- js/embark.js | 14 +++--- lib/index.js | 5 +++ lib/test.js | 73 ++++++++++++++++++++++++++++++++ package.json | 1 + 5 files changed, 114 insertions(+), 20 deletions(-) create mode 100644 lib/test.js diff --git a/demo/test/simple_storage_spec.js b/demo/test/simple_storage_spec.js index b71da69c7..c2974eec9 100644 --- a/demo/test/simple_storage_spec.js +++ b/demo/test/simple_storage_spec.js @@ -1,32 +1,43 @@ var assert = require('assert'); -var Embark = require('embark-framework'); -var EmbarkSpec = Embark.initTests(); -var web3 = EmbarkSpec.web3; +//var Embark = require('embark-framework'); + +var SimpleStorage; describe("SimpleStorage", function() { before(function(done) { - EmbarkSpec.sim.createAccounts(10, function() { - EmbarkSpec.sim.setBalance(web3.eth.accounts[0], 1000000000000000000000, function() { - EmbarkSpec.deployAll(done); - // or - // EmbarkSpec.deployContract('SimpleStorage', [100], done); - }); + var self = this; + + var Embark = require('../../lib/index.js'); + var EmbarkSpec = Embark.initTests(); + //var web3 = EmbarkSpec.web3; + + //var contracts = EmbarkSpec.deployAll(done); + //var SimpleStorage = contracts.SimpleStorage; + // or + EmbarkSpec.deployContract('SimpleStorage', [100], function(contract) { + SimpleStorage = contract; + done(); }); }); it("should set constructor value", function(done) { - SimpleStorage.storedData(function(err, result) { - assert.equal(result.toNumber(), 100); + SimpleStorage.storedData() + .then(function(value) { + assert.equal(value.toNumber(), 100); done(); }); }); it("set storage value", function(done) { - SimpleStorage.set(150, function() { - SimpleStorage.get(function(err, result) { - assert.equal(result.toNumber(), 150); + var self = this; + //console.log(SimpleStorage); + SimpleStorage.set(150) + .then(function() { + return SimpleStorage.get(); + }) + .then(function(value) { + assert.equal(value.toNumber(), 150); done(); - }); }); }); diff --git a/js/embark.js b/js/embark.js index 801decd81..47f511cfe 100644 --- a/js/embark.js +++ b/js/embark.js @@ -1,3 +1,6 @@ +if (typeof module !== 'undefined') { + var Promise = require('bluebird'); +} var EmbarkJS = { }; @@ -23,7 +26,7 @@ EmbarkJS.Contract = function(options) { this.code = options.code; this.web3 = options.web3 || web3; - var ContractClass = web3.eth.contract(this.abi); + var ContractClass = this.web3.eth.contract(this.abi); this._originalContractObject = ContractClass.at(this.address); this._methods = Object.getOwnPropertyNames(this._originalContractObject).filter(function (p) { @@ -34,7 +37,6 @@ EmbarkJS.Contract = function(options) { } return false; }); - }; EmbarkJS.Contract.prototype.deploy = function(args) { @@ -44,13 +46,13 @@ EmbarkJS.Contract.prototype.deploy = function(args) { contractParams = args; contractParams.push({ - from: web3.eth.accounts[0], + from: this.web3.eth.accounts[0], data: this.code, gasLimit: 500000, gasPrice: 10000000000000 }); - var contractObject = web3.eth.contract(this.abi); + var contractObject = this.web3.eth.contract(this.abi); var promise = new Promise(function(resolve, reject) { contractParams.push(function(err, transaction) { @@ -97,4 +99,6 @@ EmbarkJS.Messages.Whisper.listenTo = function(options) { }; - +if (typeof module !== 'undefined') { + module.exports = EmbarkJS; +} diff --git a/lib/index.js b/lib/index.js index f95079fc4..741aba2e4 100644 --- a/lib/index.js +++ b/lib/index.js @@ -16,6 +16,7 @@ var Blockchain = require('./blockchain.js'); var Server = require('./server.js'); var Watch = require('./watch.js'); var Pipeline = require('./pipeline.js'); +var Test = require('./test.js'); var Embark = { @@ -108,6 +109,10 @@ var Embark = { ], function(err, result) { done(result); }); + }, + + initTests: function(options) { + return new Test(options); } }; diff --git a/lib/test.js b/lib/test.js new file mode 100644 index 000000000..30fbcef10 --- /dev/null +++ b/lib/test.js @@ -0,0 +1,73 @@ +var Web3 = require('web3'); +var Deploy = require('./deploy.js'); +var ContractsManager = require('./contracts.js'); +var EmbarkJS = require('../js/embark.js'); + +var initAccounts = function(sim, web3, done) { + sim.createAccounts(10, function() { + sim.setBalance(web3.eth.accounts[0], 1000000000000000000000, function() { + done(); + }); + }); +}; + +var Test = function(options) { + try { + this.EtherSim = require('ethersim'); + } catch(e) { + this.EtherSim = false; + } + + if (this.EtherSim === false) { + console.log('EtherSim not found; Please install it with "npm install ethersim --save"'); + console.log('For more information see https://github.com/iurimatias/ethersim'); + exit(); + } +}; + +//Test.prototype.deployAll = function(cb) { +// var web3 = this.web3; +// Embark.deployContracts('development', this.contractFiles, "/tmp/abi.js", "chains.json", false, false, function(abi) { +// eval(abi); +// cb(); +// }); +//}; + +Test.prototype.deployContract = function(className, args, cb) { + var self = this; + this.web3 = new Web3(); + this.sim = new this.EtherSim.init(); + this.web3.setProvider(this.sim.provider); + + var contractsManager = new ContractsManager('./config/', ['app/contracts/*.sol'], 'development'); + contractsManager.init(); + contractsManager.build(); + + var deploy = new Deploy(this.web3, contractsManager); + var contract = contractsManager.contracts[className]; + + initAccounts(this.sim, this.web3, function() { + deploy.deployContract(contract, args, function(err, address) { + console.log("deployed"); + console.log(address); + + console.log(contract); + + var deployedContract = new EmbarkJS.Contract({ + abi: contract.abiDefinition, + address: address, + code: contract.code, + web3: self.web3 + }); + + cb(deployedContract); + }); + }); +}; + + + + + + +module.exports = Test; diff --git a/package.json b/package.json index 0ddac11fa..6f38424f1 100644 --- a/package.json +++ b/package.json @@ -15,6 +15,7 @@ "dependencies": { "async": "^2.0.1", "bignumber.js": "debris/bignumber.js#master", + "bluebird": "^3.4.1", "chokidar": "^1.6.0", "colors": "^1.1.2", "commander": "^2.8.1",