embark/lib/test.js

74 lines
2.1 KiB
JavaScript
Raw Normal View History

2015-06-13 13:02:19 +00:00
var python = require('python').shell;
var mm = require('methodmissing');
var sync = require('sync-me');
py_exec = function(cmd) {
2015-06-15 09:46:18 +00:00
return sync(python, cmd)[1].trim();
2015-07-04 02:41:39 +00:00
};
2015-06-13 13:02:19 +00:00
TestContractWrapper = (function() {
2015-06-15 01:38:00 +00:00
function TestContractWrapper(contract, className, args) {
2015-07-04 20:52:05 +00:00
this.contract = contract.compiled;
2015-06-13 13:02:19 +00:00
this.className = className;
2015-06-15 01:38:00 +00:00
this.args = args;
2015-06-13 13:02:19 +00:00
this.initializeContract();
}
TestContractWrapper.prototype.initializeContract = function() {
2015-07-04 02:41:39 +00:00
example_abi = JSON.stringify(this.contract.info.abiDefinition);
example_binary = this.contract.code.slice(2);
2015-06-13 13:02:19 +00:00
2015-07-04 02:41:39 +00:00
py_exec("example_abi = '" + example_abi + "'");
py_exec("example_abi");
py_exec("example_binary = '" + example_binary + "'.decode('hex')");
py_exec("example_binary");
2015-06-15 01:38:00 +00:00
2015-07-04 02:41:39 +00:00
if (this.args === undefined) {
py_exec(this.className + "_contract = EvmContract(example_abi, example_binary, '" + this.className + "')");
2015-06-15 01:38:00 +00:00
}
else {
2015-07-04 02:41:39 +00:00
py_exec(this.className + "_contract = EvmContract(example_abi, example_binary, '" + this.className + "', [" + this.args.join(",") + "])");
2015-06-15 01:38:00 +00:00
}
2015-06-13 13:02:19 +00:00
2015-07-04 02:41:39 +00:00
this.contractVariable = this.className + "_contract";
2015-06-13 13:02:19 +00:00
};
TestContractWrapper.prototype.execCmd = function(method, args) {
2015-07-04 02:41:39 +00:00
var arg_list = [];
for (var key in args) {
var value = args[key];
2015-06-13 13:02:19 +00:00
arg_list.push(value);
}
data = py_exec(this.className + "_contract." + method + "(" + arg_list.join(",") + ")");
return data;
};
return TestContractWrapper;
})();
2015-06-15 01:38:00 +00:00
TestContract = function(contract, className, args) {
var wrapper = new TestContractWrapper(contract, className, args);
2015-06-13 13:02:19 +00:00
var Obj = mm(wrapper, function (key, args) {
return wrapper.execCmd(key, args);
});
return Obj;
}
2015-07-04 02:27:17 +00:00
test = function(contractsConfig, contractFiles) {
2015-08-01 15:19:21 +00:00
contractsConfig.init(contractFiles, 'development');
2015-06-13 13:02:19 +00:00
contractsConfig.compileContracts();
2015-07-04 02:27:17 +00:00
this.contractDB = contractsConfig.contractDB;
2015-06-13 13:02:19 +00:00
}
2015-07-04 02:27:17 +00:00
test.prototype.request = function(className, args) {
var contract = this.contractDB[className];
2015-07-04 02:41:39 +00:00
py_exec("from ethertdd import EvmContract");
return TestContract(contract, className, args);
2015-06-13 13:21:29 +00:00
}
2015-07-04 02:27:17 +00:00
module.exports = test;
2015-06-13 13:02:19 +00:00