some progress with new tests

This commit is contained in:
Iuri Matias 2016-08-21 18:05:35 -04:00
parent 0f14c247b0
commit 95615d14a7
5 changed files with 114 additions and 20 deletions

View File

@ -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();
});
});
});

View File

@ -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;
}

View File

@ -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);
}
};

73
lib/test.js Normal file
View File

@ -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;

View File

@ -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",