implement test functionality

This commit is contained in:
Iuri Matias 2016-10-02 16:57:13 -04:00
parent 97954ead32
commit ed6b1fd5c2
6 changed files with 157 additions and 98 deletions

View File

@ -7,7 +7,7 @@
"name": "token"
},
"577bcb0c85129a6ff9e4dcde08f04e6f094e94c74a4423fc90e5947df9a21b82": {
"address": "0xb2f17f39c8a7d8f1745a68525a8450519f50b91c",
"address": "0xf88f536ed745bb1242a10c085feb5aca8701711d",
"name": "SimpleStorage"
},
"708fa6b699f419627ab3c4c2d9c82f8f1a6fab03c122d0a9ee55d2d0d0ad1e4b": {
@ -15,7 +15,7 @@
"name": "token"
},
"e0d35d6564373021d9749a7a8815cf58cc5ca7b7edaf4740c1913898561531c3": {
"address": "0x1cc76358b7e5a8debb43bd83f84718d3c1d98836",
"address": "0x7d4d2c27d1ed2d40df055bbc1586c12ce97f19f8",
"name": "SimpleStorage2"
},
"f3765f8b702ccb44eb19f1adecbf5a216175713fbd41d9fae100d8e3dfc5e74f": {

View File

@ -1,43 +1,33 @@
var assert = require('assert');
//var Embark = require('embark-framework');
var SimpleStorage;
var Embark = require('../../lib/index.js');
var EmbarkSpec = Embark.initTests();
var web3 = EmbarkSpec.web3;
describe("SimpleStorage", function() {
before(function(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();
});
//EmbarkSpec.deployContract('SimpleStorage', [100], done);
var contractsConfig = {
"SimpleStorage": {
args: [100, '0x123']
}
};
EmbarkSpec.deployAll(contractsConfig, done);
});
it("should set constructor value", function(done) {
SimpleStorage.storedData()
.then(function(value) {
assert.equal(value.toNumber(), 100);
SimpleStorage.storedData(function(err, result) {
assert.equal(result.toNumber(), 100);
done();
});
});
it("set storage value", function(done) {
var self = this;
//console.log(SimpleStorage);
SimpleStorage.set(150)
.then(function() {
return SimpleStorage.get();
})
.then(function(value) {
assert.equal(value.toNumber(), 150);
SimpleStorage.set(150, function() {
SimpleStorage.get(function(err, result) {
assert.equal(result.toNumber(), 150);
done();
});
});
});

View File

@ -80,35 +80,37 @@ Deploy.prototype.deployContract = function(contract, params, callback) {
var contractParams = (params || contract.args).slice();
//console.log("using address" + this.web3.eth.accounts[0]);
this.web3.eth.getAccounts(function(err, accounts) {
//console.log("using address" + this.web3.eth.accounts[0]);
// TODO: probably needs to be defaultAccoun
// TODO: it wouldn't necessary be the first address
// idea: use defined blockchain address or first address
contractParams.push({
//from: this.web3.eth.coinbase,
from: this.web3.eth.accounts[0],
data: contract.code,
gas: contract.gas,
gasPrice: contract.gasPrice
// TODO: probably needs to be defaultAccount
// TODO: it wouldn't necessary be the first address
// use defined blockchain address or first address
contractParams.push({
//from: this.web3.eth.coinbase,
from: accounts[0],
data: contract.code,
gas: contract.gas,
gasPrice: contract.gasPrice
});
self.logger.info("deploying " + contract.className);
contractParams.push(function(err, transaction) {
self.logger.contractsState(self.contractsManager.contractsState());
if (err) {
self.logger.error("error deploying contract: " + contract.className);
self.logger.error(err.toString());
callback(new Error(err));
} else if (transaction.address !== undefined) {
self.logger.info(contract.className + " deployed at " + transaction.address);
contract.deployedAddress = transaction.address;
callback(null, transaction.address);
}
});
contractObject["new"].apply(contractObject, contractParams);
});
self.logger.info("deploying " + contract.className);
contractParams.push(function(err, transaction) {
self.logger.contractsState(self.contractsManager.contractsState());
if (err) {
self.logger.error("error deploying contract: " + contract.className);
self.logger.error(err.toString());
callback(new Error(err));
} else if (transaction.address !== undefined) {
self.logger.info(contract.className + " deployed at " + transaction.address);
contract.deployedAddress = transaction.address;
callback(null, transaction.address);
}
});
contractObject["new"].apply(contractObject, contractParams);
};
Deploy.prototype.deployAll = function(done) {

View File

@ -7,6 +7,11 @@ var DeployTracker = function(options) {
this.chainConfig = options.chainConfig;
this.web3 = options.web3;
if (this.chainConfig === false) {
this.currentChain = {contracts: []};
return;
};
var block = this.web3.eth.getBlock(0);
var chainId = block.hash;
@ -41,6 +46,7 @@ DeployTracker.prototype.getContract = function(contractName, code, args) {
// TODO: abstract this
// chainConfig can be an abstract PersistentObject
DeployTracker.prototype.save = function() {
if (this.chainConfig === false) { return };
fs.writeFileSync("./chains.json", prettyJson(this.chainConfig));
};

View File

@ -1,65 +1,72 @@
var async = require('async');
var Web3 = require('web3');
var Deploy = require('./deploy.js');
var Embark = require('./index.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 Deploy = require('./deploy.js');
var TestLogger = require('./logger.js');
var Config = require('./config.js');
var ABIGenerator = require('./abi.js');
var Test = function(options) {
try {
this.EtherSim = require('ethersim');
this.sim = require('ethereumjs-testrpc');
} catch(e) {
this.EtherSim = false;
this.sim = 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');
if (this.sim === false) {
console.log('Simulator not found; Please install it with "npm install -g ethereumjs-testrpc');
console.log('For more information see https://github.com/ethereumjs/testrpc');
exit();
}
};
Test.prototype.deployContract = function(className, args, cb) {
Test.prototype.deployAll = function(contractsConfig, cb) {
var self = this;
this.web3 = new Web3();
this.sim = new this.EtherSim.init();
this.web3.setProvider(this.sim.provider);
this.web3.setProvider(this.sim.provider());
var logger = new TestLogger({logLevel: 'debug'});
var contractsManager = new ContractsManager('./config/', ['app/contracts/*.sol'], 'development');
contractsManager.init();
contractsManager.build();
async.waterfall([
function buildContracts(callback) {
var deploy = new Deploy(this.web3, contractsManager);
var contract = contractsManager.contracts[className];
var config = new Config('test');
config.contractsFiles = config.loadFiles(["app/contracts/**"]);
config.contractsConfig = {contracts: contractsConfig} ;
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);
var contractsManager = new ContractsManager({
contractFiles: config.contractsFiles,
contractsConfig: config.contractsConfig,
logger: logger
});
contractsManager.build();
callback(null, contractsManager);
},
function deployContracts(contractsManager, callback) {
var deploy = new Deploy({
web3: self.web3,
contractsManager: contractsManager,
logger: logger,
chainConfig: false,
env: 'test'
});
deploy.deployAll(function() {
callback(null, contractsManager);
});
},
function generateABI(contractsManager, callback) {
var abiGenerator = new ABIGenerator({}, contractsManager);
var ABI = abiGenerator.generateContracts(false);
callback(null, ABI);
}
], function(err, result) {
self.web3.eth.getAccounts(function(err, accounts) {
var web3 = self.web3;
web3.eth.defaultAccount = accounts[0];
eval(result);
cb();
});
});
};
module.exports = Test;

54
lib/test_logger.js Normal file
View File

@ -0,0 +1,54 @@
var colors = require('colors');
// TODO: just logFunction changes, probably doesn't need a whole new module just
// for this
var TestLogger = function(options) {
this.logLevels = ['error', 'warn', 'info', 'debug', 'trace'];
this.logs = [];
this.logLevel = options.logLevel || 'info';
this.contractsState = options.contractsState || console.log;
this.availableServices = options.availableServices || console.log;
};
TestLogger.prototype.logFunction = function() {
this.logs.push(arguments);
};
TestLogger.prototype.contractsState = function() {
this.logs.push(arguments);
};
TestLogger.prototype.availableServices = function() {
this.logs.push(arguments);
};
TestLogger.prototype.error = function(txt) {
if (!(this.shouldLog('error'))) { return; }
this.logFunction(txt.red);
};
TestLogger.prototype.warn = function(txt) {
if (!(this.shouldLog('warn'))) { return; }
this.logFunction(txt.yellow);
};
TestLogger.prototype.info = function(txt) {
if (!(this.shouldLog('info'))) { return; }
this.logFunction(txt.green);
};
TestLogger.prototype.debug = function(txt) {
if (!(this.shouldLog('debug'))) { return; }
this.logFunction(txt);
};
TestLogger.prototype.trace = function(txt) {
if (!(this.shouldLog('trace'))) { return; }
this.logFunction(txt);
};
TestLogger.prototype.shouldLog = function(level) {
return (this.logLevels.indexOf(level) <= this.logLevels.indexOf(this.logLevel));
};
module.exports = TestLogger;