2016-10-14 11:01:54 +00:00
|
|
|
/*globals describe, it*/
|
2017-08-03 23:29:09 +00:00
|
|
|
let CodeGenerator = require('../lib/contracts/code_generator.js');
|
2017-03-29 17:57:22 +00:00
|
|
|
let assert = require('assert');
|
2016-10-14 11:01:54 +00:00
|
|
|
|
2018-04-10 20:38:49 +00:00
|
|
|
function replaceCRLF(string) {
|
|
|
|
return string.replace(/\r\n/g, '\n');
|
|
|
|
}
|
|
|
|
|
2016-10-14 11:01:54 +00:00
|
|
|
// TODO: instead 'eval' the code with a fake web3 object
|
|
|
|
// and check the generate code interacts as expected
|
2017-08-03 23:29:09 +00:00
|
|
|
describe('embark.CodeGenerator', function() {
|
2017-03-08 14:56:00 +00:00
|
|
|
this.timeout(0);
|
2016-10-14 11:01:54 +00:00
|
|
|
|
|
|
|
describe('#generateContracts', function() {
|
2018-05-22 18:49:25 +00:00
|
|
|
let contracts = [
|
|
|
|
{
|
|
|
|
className: 'SimpleStorage',
|
|
|
|
abiDefinition: [{"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"}],
|
|
|
|
gasEstimates: 12000,
|
|
|
|
deployedAddress: "0x123",
|
|
|
|
code: '12345'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
className: 'Foo',
|
|
|
|
abiDefinition: [{"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"}],
|
|
|
|
gasEstimates: 12000,
|
|
|
|
deployedAddress: "0x124",
|
|
|
|
code: '123456'
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
2017-08-03 23:29:09 +00:00
|
|
|
let generator = new CodeGenerator({blockchainConfig: {}, contractsManager: {
|
2016-10-14 11:01:54 +00:00
|
|
|
contracts: {
|
2018-05-22 18:49:25 +00:00
|
|
|
SimpleStorage: contracts[0],
|
|
|
|
Foo: contracts[1]
|
2016-10-14 11:01:54 +00:00
|
|
|
}
|
2016-12-07 02:33:31 +00:00
|
|
|
}});
|
2016-10-14 11:01:54 +00:00
|
|
|
|
|
|
|
describe('with EmbarkJS', function() {
|
2017-03-29 17:57:22 +00:00
|
|
|
let withEmbarkJS = true;
|
2016-10-14 11:01:54 +00:00
|
|
|
|
|
|
|
it('should generate contract code', function() {
|
2017-10-14 00:16:39 +00:00
|
|
|
var contractCode = "\n__mainContext.__loadManagerInstance.execWhenReady(function() {\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});\n__mainContext.__loadManagerInstance.execWhenReady(function() {\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\n});\n";
|
2018-05-22 18:49:25 +00:00
|
|
|
assert.strictEqual(replaceCRLF(generator.generateContracts(contracts, withEmbarkJS)), contractCode);
|
2016-10-14 11:01:54 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('with default interface', function() {
|
2017-03-29 17:57:22 +00:00
|
|
|
let withEmbarkJS = false;
|
2016-10-14 11:01:54 +00:00
|
|
|
|
|
|
|
it('should generate contract code', function() {
|
2018-01-17 15:04:11 +00:00
|
|
|
var contractCode = "\n__mainContext.__loadManagerInstance.execWhenReady(function() {\n SimpleStorageAbi = [{\"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\"}];\nSimpleStorage = new web3.eth.Contract(SimpleStorageAbi);\nSimpleStorage.options.address = '0x123';\nSimpleStorage.address = '0x123';\nSimpleStorage.options.from = web3.eth.defaultAccount;\n\n\n});\n__mainContext.__loadManagerInstance.execWhenReady(function() {\n FooAbi = [{\"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\"}];\nFoo = new web3.eth.Contract(FooAbi);\nFoo.options.address = '0x124';\nFoo.address = '0x124';\nFoo.options.from = web3.eth.defaultAccount;\n\n\n});\n";
|
2018-05-22 18:49:25 +00:00
|
|
|
assert.strictEqual(replaceCRLF(generator.generateContracts(contracts, withEmbarkJS)), contractCode);
|
2016-10-14 11:01:54 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
//describe('#generateABI', function() {
|
|
|
|
//});
|
|
|
|
});
|