update tests; fix test module

This commit is contained in:
Iuri Matias 2017-10-13 05:56:42 -04:00
parent 2f25876722
commit 322fabab3d
7 changed files with 56 additions and 12 deletions

View File

@ -27,7 +27,7 @@ class CodeGenerator {
});
this.events.setCommandHandler('abi-contracts-vanila', function(cb) {
let vanillaContractsABI = self.generateContracts(false, true);
let vanillaContractsABI = self.generateContracts(false, true, false);
let contractsJSON = self.generateContractsJSON();
cb(vanillaContractsABI, contractsJSON);
@ -168,11 +168,24 @@ result += "\n";
return result;
}
generateContracts(useEmbarkJS, isDeployment) {
generateContracts(useEmbarkJS, isDeployment, useLoader) {
let self = this;
let result = "\n";
let contractsPlugins;
if (useLoader === false) {
let result;
for (let className in this.contractsManager.contracts) {
let contract = this.contractsManager.contracts[className];
let abi = JSON.stringify(contract.abiDefinition);
result += "\n" + className + "Abi = " + abi + ";";
result += "\n" + className + "Contract = web3.eth.contract(" + className + "Abi);";
result += "\n" + className + " = " + className + "Contract.at('" + contract.deployedAddress + "');";
}
return result;
}
let mainContext = "";
if (isDeployment) {
mainContext = "__mainContext.";
@ -287,7 +300,7 @@ result += "\n";
let result = "";
result += this.generateProvider(options.deployment);
result += this.generateContracts(options.useEmbarkJS, options.deployment);
result += this.generateContracts(options.useEmbarkJS, options.deployment, true);
result += this.generateStorageInitialization(options.useEmbarkJS);
result += this.generateCommunicationInitialization(options.useEmbarkJS);

View File

@ -12,7 +12,11 @@ class ContractsManager {
this.contracts = {};
this.logger = options.logger;
this.plugins = options.plugins;
this.solcVersion = options.contractsConfig.versions.solc;
if (!options.contractsConfig.versions) {
this.solcVersion = "0.4.11";
} else {
this.solcVersion = options.contractsConfig.versions.solc;
}
this.contractDependencies = {};
}

View File

@ -82,7 +82,7 @@ class Deploy {
if (contract.onDeploy !== undefined) {
self.logger.info('executing onDeploy commands');
let codeGenerator = new CodeGenerator({contractsManager: self.contractsManager});
let code = codeGenerator.generateContracts(false, true);
let code = codeGenerator.generateContracts(false, true, true);
let cmds = contract.onDeploy.join(';\n');
RunCode.doEval(code + "\n" + cmds, self.web3);

View File

@ -257,7 +257,7 @@ Config.prototype.loadFiles = function(files) {
//readFiles.push(new File({filename: 'web3-' + web3Version + '.js', type: 'custom', resolver: function(callback) {
readFiles.push(new File({filename: 'web3.js', type: 'custom', resolver: function(callback) {
if (web3Version === "1.0.0-beta") {
callback(fs.readFileSync(fs.embarkPath('js/web3-1.0.min.js')).toString());
return callback(fs.readFileSync(fs.embarkPath('js/web3-1.0.min.js')).toString());
} else {
let npm = new Npm({logger: self.logger});
npm.getPackageVersion('web3', web3Version, 'dist/web3.min.js', true, function(web3Content) {

View File

@ -7,25 +7,36 @@ function warnIfLegacy(eventName) {
}
}
function log(eventType, eventName) {
if (['end', 'prefinish', 'error', 'new', 'demo', 'block', 'version'].indexOf(eventName) >= 0) {
return;
}
//console.log(eventType, eventName);
}
const _on = EventEmitter.prototype.on;
const _setHandler = EventEmitter.prototype.setHandler;
EventEmitter.prototype.on = function(requestName, cb) {
log("listening to event: ", requestName);
warnIfLegacy(requestName);
return _on.call(this, requestName, cb);
};
EventEmitter.prototype.setHandler = function(requestName, cb) {
log("setting handler for: ", requestName);
warnIfLegacy(requestName);
return _setHandler.call(this, requestName, cb);
};
EventEmitter.prototype.request = function(requestName, cb) {
log("requesting: ", requestName);
warnIfLegacy(requestName);
return this.emit('request:' + requestName, cb);
};
EventEmitter.prototype.setCommandHandler = function(requestName, cb) {
log("setting command handler for: ", requestName);
return this.on('request:' + requestName, function(_cb) {
cb.call(this, _cb);
});

View File

@ -11,6 +11,7 @@ var Config = require('./config.js');
var RunCode = require('./runCode.js');
var TestLogger = require('./test_logger.js');
var web3;
var __mainContext;
var getSimulator = function() {
try {
@ -62,7 +63,7 @@ Test.prototype.deployAll = function(contractsConfig, cb) {
},
function startServices(callback) {
//{abiType: 'contracts', embarkJS: false}
self.engine.startService("abi");
self.engine.startService("codeGenerator");
self.engine.startService("deployment", {
web3: self.web3,
trackContracts: false
@ -70,10 +71,25 @@ Test.prototype.deployAll = function(contractsConfig, cb) {
callback();
},
function deploy(callback) {
self.engine.events.setHandler('abi-contracts-vanila', function(vanillaABI) {
callback(null, vanillaABI);
//self.engine.events.setHandler('abi-contracts-vanila', function(vanillaABI) {
//elf.engine.events.on('abi-contracts-vanila', function(vanillaABI) {
self.engine.events.on('code-generator-ready', function () {
self.engine.events.request('abi-contracts-vanila', function(vanillaABI) {
console.log("got abi-contracts-vanila request");
callback(null, vanillaABI);
});
//self.engine.events.on('abi-contracts-vanila', function(vanillaABI) {
// console.log("got abi-contracts-vanila");
// callback(null, vanillaABI);
//});
//self.engine.events.request('abi', function(vanillaABI) {
// console.log("got abi");
// callback(null, vanillaABI);
//});
});
self.engine.deployManager.deployContracts(function(err, result) {
console.log("deployed contracts");
if (err) {
console.log(err);
callback(err);

View File

@ -10,7 +10,7 @@ describe('embark.CodeGenerator', function() {
let generator = new CodeGenerator({contractsConfig: {"dappConnection": [ "$WEB3", "http://somehost:1234" ] }, contractsManager: {}});
it('should generate code to connect to a provider', function() {
var providerCode = "\nvar whenEnvIsLoaded = function(cb) {\n if (typeof document !== 'undefined' && document !== null) {\n document.addEventListener('DOMContentLoaded', cb);\n } else {\n cb();\n }\n}\nwhenEnvIsLoaded(function() {\nif (typeof web3 !== 'undefined' && typeof Web3 !== 'undefined') {\n\tweb3 = new Web3(web3.currentProvider);\n} if (typeof Web3 !== 'undefined' && ((typeof web3 === 'undefined') || (typeof web3 !== 'undefined' && !web3.isConnected()))) {\n\tweb3 = new Web3(new Web3.providers.HttpProvider(\"http://somehost:1234\"));\n}\nweb3.eth.defaultAccount = web3.eth.accounts[0];\n})";
var providerCode = "\nfunction __reduce(arr, memo, iteratee, cb) {\n if (typeof cb !== 'function') {\n if (typeof memo === 'function' && typeof iteratee === 'function') {\n cb = iteratee;\n iteratee = memo;\n memo = [];\n } else {\n throw new TypeError('expected callback to be a function');\n }\n }\n\n if (!Array.isArray(arr)) {\n cb(new TypeError('expected an array'));\n return;\n }\n\n if (typeof iteratee !== 'function') {\n cb(new TypeError('expected iteratee to be a function'));\n return;\n }\n\n (function next(i, acc) {\n if (i === arr.length) {\n cb(null, acc);\n return;\n }\n\n iteratee(acc, arr[i], function(err, val) {\n if (err) {\n cb(err);\n return;\n }\n next(i + 1, val);\n });\n })(0, memo);\n};\nvar __mainContext = __mainContext || this;\n__mainContext.__LoadManager = function() { this.list = []; this.done = false; }\n__mainContext.__LoadManager.prototype.execWhenReady = function(cb) { if (this.done) { cb(); } else { this.list.push(cb) } }\n__mainContext.__LoadManager.prototype.doFirst = function(todo) { var self = this; todo(function() { self.done = true; self.list.map((x) => x.apply()) }) }\n__mainContext.__loadManagerInstance = new __mainContext.__LoadManager();\nvar whenEnvIsLoaded = function(cb) {\n if (typeof document !== 'undefined' && document !== null) {\n document.addEventListener('DOMContentLoaded', cb);\n } else {\n cb();\n }\n}\nwhenEnvIsLoaded(function(){__mainContext.__loadManagerInstance.doFirst(function(done) {\n\nif (typeof window !== 'undefined') { window.web3 = undefined; }\n__reduce([\"$WEB3\",\"http://somehost:1234\"], function(prev, value, next) {\nif (prev === false) {\n return next(null, false);\n}\n if (value === '$WEB3' && (typeof web3 !== 'undefined' && typeof Web3 !== 'undefined')) {\n\tweb3 = new Web3(web3.currentProvider);\n } else if (value !== '$WEB3' && (typeof Web3 !== 'undefined' && ((typeof web3 === 'undefined') || (typeof web3 !== 'undefined' && (!web3.isConnected || (web3.isConnected && !web3.isConnected())))))) {\n\tweb3 = new Web3(new Web3.providers.HttpProvider(value));\n}\nelse if (value === '$WEB3') {\n\treturn next(null, '');\n}\nweb3.eth.getAccounts(function(err, account) { if(err) { next(null, true) } else { next(null, false) }})\n}, function(err, _result) {\nweb3.eth.getAccounts(function(err, accounts) {;\nweb3.eth.defaultAccount = accounts[0];\ndone();\n});\n});\n})})";
assert.equal(generator.generateProvider(), providerCode);
});
@ -38,7 +38,7 @@ describe('embark.CodeGenerator', function() {
let withEmbarkJS = true;
it('should generate contract code', function() {
var contractCode = "\n\nif (whenEnvIsLoaded === undefined) {\n var whenEnvIsLoaded = function(cb) {\n if (typeof document !== 'undefined' && document !== null) {\n document.addEventListener('DOMContentLoaded', cb);\n } else {\n cb();\n }\n }\n}\nwhenEnvIsLoaded(function() {\nSimpleStorage = new EmbarkJS.Contract({abi: [{\"constant\":true,\"inputs\":[],\"name\":\"storedData\",\"outputs\":[{\"name\":\"\",\"type\":\"uint256\"}],\"payable\":false,\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"x\",\"type\":\"uint256\"}],\"name\":\"set\",\"outputs\":[],\"payable\":false,\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"get\",\"outputs\":[{\"name\":\"retVal\",\"type\":\"uint256\"}],\"payable\":false,\"type\":\"function\"},{\"inputs\":[{\"name\":\"initialValue\",\"type\":\"uint256\"}],\"type\":\"constructor\"}], address: '0x123', code: '12345', gasEstimates: 12000});\n});\nif (whenEnvIsLoaded === undefined) {\n var whenEnvIsLoaded = function(cb) {\n if (typeof document !== 'undefined' && document !== null) {\n document.addEventListener('DOMContentLoaded', cb);\n } else {\n cb();\n }\n }\n}\nwhenEnvIsLoaded(function() {\nFoo = new EmbarkJS.Contract({abi: [{\"constant\":true,\"inputs\":[],\"name\":\"storedData\",\"outputs\":[{\"name\":\"\",\"type\":\"uint256\"}],\"payable\":false,\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"x\",\"type\":\"uint256\"}],\"name\":\"set\",\"outputs\":[],\"payable\":false,\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"get\",\"outputs\":[{\"name\":\"retVal\",\"type\":\"uint256\"}],\"payable\":false,\"type\":\"function\"},{\"inputs\":[{\"name\":\"initialValue\",\"type\":\"uint256\"}],\"type\":\"constructor\"}], address: '0x124', code: '123456', gasEstimates: 12000});\n});";
var contractCode = "\n\n__mainContext.__loadManagerInstance.execWhenReady(function() {\nif (typeof window !== 'undefined') { window.SimpleStorage = undefined; }\n__mainContext.SimpleStorage = new EmbarkJS.Contract({abi: [{\"constant\":true,\"inputs\":[],\"name\":\"storedData\",\"outputs\":[{\"name\":\"\",\"type\":\"uint256\"}],\"payable\":false,\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"x\",\"type\":\"uint256\"}],\"name\":\"set\",\"outputs\":[],\"payable\":false,\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"get\",\"outputs\":[{\"name\":\"retVal\",\"type\":\"uint256\"}],\"payable\":false,\"type\":\"function\"},{\"inputs\":[{\"name\":\"initialValue\",\"type\":\"uint256\"}],\"type\":\"constructor\"}], address: '0x123', code: '12345', gasEstimates: 12000});\n});\n__mainContext.__loadManagerInstance.execWhenReady(function() {\nif (typeof window !== 'undefined') { window.Foo = undefined; }\n__mainContext.Foo = new EmbarkJS.Contract({abi: [{\"constant\":true,\"inputs\":[],\"name\":\"storedData\",\"outputs\":[{\"name\":\"\",\"type\":\"uint256\"}],\"payable\":false,\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"x\",\"type\":\"uint256\"}],\"name\":\"set\",\"outputs\":[],\"payable\":false,\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"get\",\"outputs\":[{\"name\":\"retVal\",\"type\":\"uint256\"}],\"payable\":false,\"type\":\"function\"},{\"inputs\":[{\"name\":\"initialValue\",\"type\":\"uint256\"}],\"type\":\"constructor\"}], address: '0x124', code: '123456', gasEstimates: 12000});\n});";
assert.equal(generator.generateContracts(withEmbarkJS), contractCode);
});
});
@ -47,7 +47,7 @@ describe('embark.CodeGenerator', function() {
let withEmbarkJS = false;
it('should generate contract code', function() {
var contractCode = "\n\nif (whenEnvIsLoaded === undefined) {\n var whenEnvIsLoaded = function(cb) {\n if (typeof document !== 'undefined' && document !== null) {\n document.addEventListener('DOMContentLoaded', cb);\n } else {\n cb();\n }\n }\n}\nwhenEnvIsLoaded(function() {\nSimpleStorageAbi = [{\"constant\":true,\"inputs\":[],\"name\":\"storedData\",\"outputs\":[{\"name\":\"\",\"type\":\"uint256\"}],\"payable\":false,\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"x\",\"type\":\"uint256\"}],\"name\":\"set\",\"outputs\":[],\"payable\":false,\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"get\",\"outputs\":[{\"name\":\"retVal\",\"type\":\"uint256\"}],\"payable\":false,\"type\":\"function\"},{\"inputs\":[{\"name\":\"initialValue\",\"type\":\"uint256\"}],\"type\":\"constructor\"}];\nSimpleStorageContract = web3.eth.contract(SimpleStorageAbi);\nSimpleStorage = SimpleStorageContract.at('0x123');\n});\nif (whenEnvIsLoaded === undefined) {\n var whenEnvIsLoaded = function(cb) {\n if (typeof document !== 'undefined' && document !== null) {\n document.addEventListener('DOMContentLoaded', cb);\n } else {\n cb();\n }\n }\n}\nwhenEnvIsLoaded(function() {\nFooAbi = [{\"constant\":true,\"inputs\":[],\"name\":\"storedData\",\"outputs\":[{\"name\":\"\",\"type\":\"uint256\"}],\"payable\":false,\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"x\",\"type\":\"uint256\"}],\"name\":\"set\",\"outputs\":[],\"payable\":false,\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"get\",\"outputs\":[{\"name\":\"retVal\",\"type\":\"uint256\"}],\"payable\":false,\"type\":\"function\"},{\"inputs\":[{\"name\":\"initialValue\",\"type\":\"uint256\"}],\"type\":\"constructor\"}];\nFooContract = web3.eth.contract(FooAbi);\nFoo = FooContract.at('0x124');\n});";
var contractCode = "\n\n__mainContext.__loadManagerInstance.execWhenReady(function() {\nif (typeof window !== 'undefined') { window.SimpleStorage = undefined; }\nSimpleStorageAbi = [{\"constant\":true,\"inputs\":[],\"name\":\"storedData\",\"outputs\":[{\"name\":\"\",\"type\":\"uint256\"}],\"payable\":false,\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"x\",\"type\":\"uint256\"}],\"name\":\"set\",\"outputs\":[],\"payable\":false,\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"get\",\"outputs\":[{\"name\":\"retVal\",\"type\":\"uint256\"}],\"payable\":false,\"type\":\"function\"},{\"inputs\":[{\"name\":\"initialValue\",\"type\":\"uint256\"}],\"type\":\"constructor\"}];\nSimpleStorageContract = web3.eth.contract(SimpleStorageAbi);\nSimpleStorage = SimpleStorageContract.at('0x123');\n});\n__mainContext.__loadManagerInstance.execWhenReady(function() {\nif (typeof window !== 'undefined') { window.Foo = undefined; }\nFooAbi = [{\"constant\":true,\"inputs\":[],\"name\":\"storedData\",\"outputs\":[{\"name\":\"\",\"type\":\"uint256\"}],\"payable\":false,\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"x\",\"type\":\"uint256\"}],\"name\":\"set\",\"outputs\":[],\"payable\":false,\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"get\",\"outputs\":[{\"name\":\"retVal\",\"type\":\"uint256\"}],\"payable\":false,\"type\":\"function\"},{\"inputs\":[{\"name\":\"initialValue\",\"type\":\"uint256\"}],\"type\":\"constructor\"}];\nFooContract = web3.eth.contract(FooAbi);\nFoo = FooContract.at('0x124');\n});";
assert.equal(generator.generateContracts(withEmbarkJS), contractCode);
});
});