embark/test/contracts.js

276 lines
8.3 KiB
JavaScript
Raw Normal View History

/*globals describe, it*/
2018-08-08 16:14:57 +00:00
let ContractsManager = require('../lib/modules/contracts_manager/index.js');
2018-07-26 16:37:33 +00:00
let Compiler = require('../lib/modules/compiler/');
2017-03-29 17:57:22 +00:00
let Logger = require('../lib/core/logger.js');
2017-07-08 16:39:53 +00:00
let File = require('../lib/core/file.js');
let TestLogger = require('../lib/tests/test_logger.js');
2018-04-27 17:50:57 +00:00
let Events = require('../lib/core/events');
2018-06-05 12:38:27 +00:00
let Ipc = require('../lib/core/ipc.js');
2017-03-29 17:57:22 +00:00
let assert = require('assert');
2017-12-16 22:55:27 +00:00
//let SolidityCompiler = require('../lib/modules/solidity');
let Plugins = require('../lib/core/plugins.js');
2017-03-29 17:57:22 +00:00
let readFile = function(file) {
return new File({filename: file, type: File.types.dapp_file, path: file});
};
2018-05-18 16:58:09 +00:00
const currentSolcVersion = require('../package.json').dependencies.solc;
const TestEvents = {
request: (cmd, cb) => {
cb(currentSolcVersion);
2018-08-09 19:59:36 +00:00
},
emit: (_ev, _data) => {}
2018-05-18 16:58:09 +00:00
};
2017-12-16 21:05:46 +00:00
describe('embark.Contracts', function() {
2017-03-30 11:12:39 +00:00
this.timeout(0);
describe('simple', function() {
2017-12-16 22:55:27 +00:00
let plugins = new Plugins({
2018-05-18 16:58:09 +00:00
logger: new TestLogger({}),
2018-05-30 16:26:49 +00:00
events: TestEvents,
config: {
contractDirectories: ['app/contracts/']
}
2017-12-16 22:55:27 +00:00
});
2018-06-05 12:38:27 +00:00
let ipcObject = new Ipc({
ipcRole: 'none'
});
plugins.loadInternalPlugin('solidity', {ipc: ipcObject});
2017-12-16 22:55:27 +00:00
2018-05-20 10:46:12 +00:00
let events = new Events();
2018-07-26 16:37:33 +00:00
let compiler = new Compiler({events: events, logger: plugins.logger}, {plugins: plugins});
2018-05-20 10:46:12 +00:00
2018-06-14 13:53:46 +00:00
events.setCommandHandler("config:contractsConfig", function(cb) {
cb(contractsConfig);
});
events.setCommandHandler("config:contractsFiles", (cb) => {
cb([
readFile('test/contracts/simple_storage.sol'),
readFile('test/contracts/token.sol')
]);
2018-06-14 13:53:46 +00:00
});
2018-06-14 20:28:29 +00:00
events.setCommandHandler("blockchain:gasPrice", (cb) => {
cb(null, 100);
});
2018-06-14 13:53:46 +00:00
let contractsConfig = {
"versions": {
"web3.js": "1.0.0-beta",
"solc": "0.4.17"
},
"deployment": {
"host": "localhost",
"port": 8545,
"type": "rpc"
},
"dappConnection": [
"$WEB3",
"localhost:8545"
],
"gas": "auto",
"contracts": {
"Token": {
"args": [
100
]
},
"SimpleStorage": {
"args": [
200
]
}
}
};
let embarkObj = {
2018-04-27 17:50:57 +00:00
logger: new Logger({}),
2018-05-20 10:46:12 +00:00
events: events
};
let contractsManager = new ContractsManager(embarkObj, {
contractDirectories: ['app/contracts']
});
describe('#build', function() {
2017-02-26 04:27:45 +00:00
it('generate contracts', function(done) {
contractsManager.build(function(err, result) {
if (err) {
throw err;
}
2017-03-29 17:57:22 +00:00
let contracts = contractsManager.listContracts();
2017-02-26 04:27:45 +00:00
assert.equal(contracts.length, 2);
assert.equal(contracts[0].deploy, true);
assert.deepEqual(contracts[0].args, [100]);
assert.equal(contracts[0].className, "Token");
2018-01-19 19:18:50 +00:00
//assert.deepEqual(contracts[0].gas, 725000);
assert.deepEqual(contracts[0].gas, 'auto');
2017-02-26 04:27:45 +00:00
//assert.equal(contracts[0].gasPrice, []); // TODO: test this one
assert.equal(contracts[0].type, 'file');
//assert.equal(contracts[0].abiDefinition, '');
//assert.equal(contracts[0].code, '');
//assert.equal(contracts[0].runtimeBytecode, '');
assert.equal(contracts[1].deploy, true);
assert.deepEqual(contracts[1].args, [200]);
assert.equal(contracts[1].className, "SimpleStorage");
2018-01-19 19:18:50 +00:00
//assert.deepEqual(contracts[1].gas, 725000);
assert.deepEqual(contracts[1].gas, 'auto');
2017-02-26 04:27:45 +00:00
//assert.equal(contracts[1].gasPrice, []); // TODO: test this one
assert.equal(contracts[1].type, 'file');
//assert.equal(contracts[1].abiDefinition, '');
//assert.equal(contracts[1].code, '');
//assert.equal(contracts[1].runtimeBytecode, '');
done();
});
});
});
});
describe('config with contract instances', function() {
2017-12-16 22:55:27 +00:00
let plugins = new Plugins({
2018-05-18 16:58:09 +00:00
logger: new TestLogger({}),
2018-05-30 16:26:49 +00:00
events: TestEvents,
config: {
contractDirectories: ['app/contracts/']
}
2017-12-16 22:55:27 +00:00
});
2018-06-05 12:38:27 +00:00
let ipcObject = new Ipc({
ipcRole: 'none'
});
plugins.loadInternalPlugin('solidity', {ipc: ipcObject});
2017-12-16 22:55:27 +00:00
2018-05-20 10:46:12 +00:00
let events = new Events();
2018-07-26 16:37:33 +00:00
let compiler = new Compiler({events: events, logger: plugins.logger}, {plugins: plugins});
2018-05-20 10:46:12 +00:00
2018-06-14 13:53:46 +00:00
events.setCommandHandler("config:contractsConfig", function(cb) {
cb(contractsConfig);
});
events.setCommandHandler("config:contractsFiles", (cb) => {
cb([
readFile('test/contracts/simple_storage.sol'),
readFile('test/contracts/token_storage.sol')
]);
2018-06-14 13:53:46 +00:00
});
2018-06-14 20:28:29 +00:00
events.setCommandHandler("blockchain:gasPrice", (cb) => {
cb(null, 100);
});
2018-06-14 13:53:46 +00:00
let contractsConfig = {
"versions": {
"web3.js": "1.0.0-beta",
"solc": "0.4.17"
},
"deployment": {
"host": "localhost",
"port": 8545,
"type": "rpc"
},
"dappConnection": [
"$WEB3",
"localhost:8545"
],
"gas": "auto",
"contracts": {
"TokenStorage": {
"args": [
100,
"$SimpleStorage"
]
},
"MySimpleStorage": {
"instanceOf": "SimpleStorage",
"args": [
300
]
},
"SimpleStorage": {
"args": [
200
]
},
"AnotherSimpleStorage": {
"instanceOf": "SimpleStorage"
}
}
}
let embarkObj = {
2018-04-27 17:50:57 +00:00
logger: new Logger({}),
2018-05-20 10:46:12 +00:00
events: events
}
let contractsManager = new ContractsManager(embarkObj, {
contractDirectories: ['app/contracts']
});
describe('#build', function() {
2017-02-26 04:27:45 +00:00
it('generate contracts', function(done) {
contractsManager.build(function(err, result) {
if (err) {
throw err;
}
2017-03-29 17:57:22 +00:00
let contracts = contractsManager.listContracts();
2017-02-26 04:27:45 +00:00
assert.equal(contracts.length, 4);
assert.equal(contracts[0].className, "MySimpleStorage");
assert.equal(contracts[1].className, "AnotherSimpleStorage");
assert.equal(contracts[2].className, "SimpleStorage");
assert.equal(contracts[3].className, "TokenStorage");
// TokenStorage
assert.equal(contracts[3].deploy, true);
assert.deepEqual(contracts[3].args, [100, '$SimpleStorage']);
2018-01-19 19:18:50 +00:00
//assert.deepEqual(contracts[3].gas, 725000);
assert.deepEqual(contracts[3].gas, 'auto');
2017-02-26 04:27:45 +00:00
assert.equal(contracts[3].type, 'file');
//assert.equal(contracts[3].abiDefinition, '');
//assert.equal(contracts[3].code, '');
//assert.equal(contracts[3].runtimeBytecode, '');
2017-03-29 17:57:22 +00:00
let parentContract = contracts[2];
2017-02-26 04:27:45 +00:00
//MySimpleStorage
assert.equal(contracts[0].deploy, true);
assert.deepEqual(contracts[0].args, [300]);
2018-01-19 19:18:50 +00:00
//assert.deepEqual(contracts[0].gas, 725000);
assert.deepEqual(contracts[0].gas, 'auto');
2017-02-26 04:27:45 +00:00
assert.equal(contracts[0].type, 'instance');
assert.equal(contracts[0].abiDefinition, parentContract.abiDefinition);
assert.equal(contracts[0].code, parentContract.code);
assert.equal(contracts[0].runtimeBytecode, parentContract.runtimeBytecode);
// SimpleStorage
assert.equal(contracts[2].deploy, true);
assert.deepEqual(contracts[2].args, [200]);
2018-01-19 19:18:50 +00:00
//assert.deepEqual(contracts[2].gas, 725000);
assert.deepEqual(contracts[2].gas, 'auto');
2017-02-26 04:27:45 +00:00
assert.equal(contracts[2].type, 'file');
//assert.equal(contracts[2].abiDefinition, '');
//assert.equal(contracts[2].code, '');
//assert.equal(contracts[2].runtimeBytecode, '');
// AnotherSimpleStorage
assert.equal(contracts[1].deploy, true);
assert.deepEqual(contracts[1].args, [200]);
2018-01-19 19:18:50 +00:00
//assert.deepEqual(contracts[1].gas, 725000);
assert.deepEqual(contracts[1].gas, 'auto');
2017-02-26 04:27:45 +00:00
assert.equal(contracts[1].type, 'instance');
assert.equal(contracts[1].abiDefinition, parentContract.abiDefinition);
assert.equal(contracts[1].code, parentContract.code);
assert.equal(contracts[1].runtimeBytecode, parentContract.runtimeBytecode);
done();
});
});
});
});
});