add spec for contracs config; fix contracts arguments when it's an instanceOf

This commit is contained in:
Iuri Matias 2017-01-12 20:42:33 -05:00
parent 97a49f3136
commit 087578c8e6
4 changed files with 233 additions and 4 deletions

View File

@ -39,10 +39,10 @@ ContractsManager.prototype.build = function(done) {
var compiler = new Compiler();
try {
self.compiledContracts = compiler.compile_solidity(self.contractFiles);
return callback();
} catch(err) {
return callback(new Error(err.message));
}
return callback();
},
function prepareContractsFromConfig(callback) {
var className, contract;
@ -110,7 +110,7 @@ ContractsManager.prototype.build = function(done) {
continue;
}
if (parentContract.args && parentContract.args.length > 0 && contract.args === []) {
if (parentContract.args && parentContract.args.length > 0 && ((contract.args && contract.args.length === 0) || contract.args === undefined)) {
contract.args = parentContract.args;
}
@ -126,6 +126,7 @@ ContractsManager.prototype.build = function(done) {
contract.gas = contract.gas || parentContract.gas;
contract.gasPrice = contract.gasPrice || parentContract.gasPrice;
contract.type = 'instance';
}
callback();

View File

@ -9,7 +9,7 @@ describe('embark.Console', function() {
describe('command: help', function() {
it('i should provide a help text', function(done) {
it('it should provide a help text', function(done) {
console.executeCmd('help', function(output) {
var lines = output.split('\n');
assert.equal(lines[0], 'Welcome to Embark 2');

161
test/contracts.js Normal file
View File

@ -0,0 +1,161 @@
/*globals describe, it*/
var ContractsManager = require('../lib/contracts.js');
var Logger = require('../lib/logger.js');
var assert = require('assert');
var fs = require('fs');
var readFile = function(file) {
return {filename: file, content: fs.readFileSync(file).toString()};
};
describe('embark.Contratcs', function() {
describe('simple', function() {
var contractsManager = new ContractsManager({
contractFiles: [
readFile('test/contracts/simple_storage.sol'),
readFile('test/contracts/token.sol')
],
contractsConfig: {
"gas": "auto",
"contracts": {
"Token": {
"args": [
100
]
},
"SimpleStorage": {
"args": [
200
]
}
}
},
logger: new Logger({})
});
describe('#build', function() {
it('generate contracts', function() {
contractsManager.build(function(err, result) {
if (err) {
throw err;
}
});
var contracts = contractsManager.listContracts();
assert.equal(contracts.length, 2);
assert.equal(contracts[0].deploy, true);
assert.deepEqual(contracts[0].args, [100]);
assert.equal(contracts[0].className, "Token");
assert.deepEqual(contracts[0].gas, 700000);
//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");
assert.deepEqual(contracts[1].gas, 700000);
//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, '');
});
});
});
describe('config with contract instances', function() {
var contractsManager = new ContractsManager({
contractFiles: [
readFile('test/contracts/simple_storage.sol'),
readFile('test/contracts/token_storage.sol')
],
contractsConfig: {
"gas": "auto",
"contracts": {
"TokenStorage": {
"args": [
100,
"$SimpleStorage"
]
},
"MySimpleStorage": {
"instanceOf": "SimpleStorage",
"args": [
300
]
},
"SimpleStorage": {
"args": [
200
]
},
"AnotherSimpleStorage": {
"instanceOf": "SimpleStorage"
}
}
},
logger: new Logger({})
});
describe('#build', function() {
it('generate contracts', function() {
contractsManager.build(function(err, result) {
if (err) {
throw err;
}
});
var contracts = contractsManager.listContracts();
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']);
assert.deepEqual(contracts[3].gas, 700000);
assert.equal(contracts[3].type, 'file');
//assert.equal(contracts[3].abiDefinition, '');
//assert.equal(contracts[3].code, '');
//assert.equal(contracts[3].runtimeBytecode, '');
var parentContract = contracts[2];
//MySimpleStorage
assert.equal(contracts[0].deploy, true);
assert.deepEqual(contracts[0].args, [300]);
assert.deepEqual(contracts[0].gas, 700000);
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]);
assert.deepEqual(contracts[2].gas, 700000);
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]);
assert.deepEqual(contracts[1].gas, 700000);
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);
});
});
});
});

View File

@ -0,0 +1,67 @@
// https://github.com/nexusdev/erc20/blob/master/contracts/base.sol
pragma solidity ^0.4.7;
contract TokenStorage {
event Transfer(address indexed from, address indexed to, uint value);
event Approval( address indexed owner, address indexed spender, uint value);
mapping( address => uint ) _balances;
mapping( address => mapping( address => uint ) ) _approvals;
uint _supply;
address public addr;
function TokenStorage( uint initial_balance, address _addr) {
_balances[msg.sender] = initial_balance;
_supply = initial_balance;
addr = _addr;
}
function totalSupply() constant returns (uint supply) {
return _supply;
}
function balanceOf( address who ) constant returns (uint value) {
return _balances[who];
}
function transfer( address to, uint value) returns (bool ok) {
if( _balances[msg.sender] < value ) {
throw;
}
if( !safeToAdd(_balances[to], value) ) {
throw;
}
_balances[msg.sender] -= value;
_balances[to] += value;
Transfer( msg.sender, to, value );
return true;
}
function transferFrom( address from, address to, uint value) returns (bool ok) {
// if you don't have enough balance, throw
if( _balances[from] < value ) {
throw;
}
// if you don't have approval, throw
if( _approvals[from][msg.sender] < value ) {
throw;
}
if( !safeToAdd(_balances[to], value) ) {
throw;
}
// transfer and return true
_approvals[from][msg.sender] -= value;
_balances[from] -= value;
_balances[to] += value;
Transfer( from, to, value );
return true;
}
function approve(address spender, uint value) returns (bool ok) {
// TODO: should increase instead
_approvals[msg.sender][spender] = value;
Approval( msg.sender, spender, value );
return true;
}
function allowance(address owner, address spender) constant returns (uint _allowance) {
return _approvals[owner][spender];
}
function safeToAdd(uint a, uint b) internal returns (bool) {
return (a + b >= a);
}
}