You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
187 lines
6.8 KiB
187 lines
6.8 KiB
5 years ago
|
/*const solc = require('solc');
|
||
8 years ago
|
const getInstrumentedVersion = require('./../lib/instrumentSolidity.js');
|
||
8 years ago
|
const util = require('./util/util.js');
|
||
8 years ago
|
const path = require('path');
|
||
8 years ago
|
const CoverageMap = require('./../lib/coverageMap');
|
||
8 years ago
|
const vm = require('./util/vm');
|
||
|
const assert = require('assert');
|
||
8 years ago
|
|
||
8 years ago
|
describe('function declarations', () => {
|
||
7 years ago
|
const filePath = path.resolve('./test.sol');
|
||
8 years ago
|
const pathPrefix = './';
|
||
|
|
||
8 years ago
|
it('should compile after instrumenting an ordinary function declaration', () => {
|
||
|
const contract = util.getCode('function/function.sol');
|
||
8 years ago
|
const info = getInstrumentedVersion(contract, 'test.sol');
|
||
6 years ago
|
const output = JSON.parse(solc.compile(util.codeToCompilerInput(info.contract)));
|
||
8 years ago
|
util.report(output.errors);
|
||
8 years ago
|
});
|
||
8 years ago
|
|
||
8 years ago
|
it('should compile after instrumenting an abstract function declaration', () => {
|
||
|
const contract = util.getCode('function/abstract.sol');
|
||
8 years ago
|
const info = getInstrumentedVersion(contract, 'test.sol');
|
||
6 years ago
|
const output = JSON.parse(solc.compile(util.codeToCompilerInput(info.contract)));
|
||
8 years ago
|
util.report(output.errors);
|
||
8 years ago
|
});
|
||
8 years ago
|
|
||
8 years ago
|
it('should compile after instrumenting a function declaration with an empty body', () => {
|
||
|
const contract = util.getCode('function/empty-body.sol');
|
||
8 years ago
|
const info = getInstrumentedVersion(contract, 'test.sol');
|
||
6 years ago
|
const output = JSON.parse(solc.compile(util.codeToCompilerInput(info.contract)));
|
||
8 years ago
|
util.report(output.errors);
|
||
8 years ago
|
});
|
||
8 years ago
|
|
||
8 years ago
|
it('should compile after instrumenting lots of declarations in row', () => {
|
||
|
const contract = util.getCode('function/multiple.sol');
|
||
8 years ago
|
const info = getInstrumentedVersion(contract, 'test.sol');
|
||
6 years ago
|
const output = JSON.parse(solc.compile(util.codeToCompilerInput(info.contract)));
|
||
8 years ago
|
util.report(output.errors);
|
||
8 years ago
|
});
|
||
8 years ago
|
|
||
8 years ago
|
it('should compile after instrumenting a new->constructor-->method chain', () => {
|
||
|
const contract = util.getCode('function/chainable-new.sol');
|
||
|
const info = getInstrumentedVersion(contract, 'test.sol');
|
||
6 years ago
|
const output = JSON.parse(solc.compile(util.codeToCompilerInput(info.contract)));
|
||
8 years ago
|
util.report(output.errors);
|
||
|
});
|
||
|
|
||
8 years ago
|
it('should compile after instrumenting a constructor call that chains to a method call', () => {
|
||
|
const contract = util.getCode('function/chainable.sol');
|
||
|
const info = getInstrumentedVersion(contract, 'test.sol');
|
||
6 years ago
|
const output = JSON.parse(solc.compile(util.codeToCompilerInput(info.contract)));
|
||
8 years ago
|
util.report(output.errors);
|
||
|
});
|
||
8 years ago
|
|
||
6 years ago
|
it('should compile after instrumenting a function with calldata keyword', () => {
|
||
|
const contract = util.getCode('function/calldata.sol');
|
||
|
const info = getInstrumentedVersion(contract, 'test.sol');
|
||
|
const output = JSON.parse(solc.compile(util.codeToCompilerInput(info.contract)));
|
||
|
util.report(output.errors);
|
||
|
});
|
||
|
|
||
8 years ago
|
it('should compile after instrumenting a constructor-->method-->value chain', () => {
|
||
|
const contract = util.getCode('function/chainable-value.sol');
|
||
|
const info = getInstrumentedVersion(contract, 'test.sol');
|
||
6 years ago
|
const output = JSON.parse(solc.compile(util.codeToCompilerInput(info.contract)));
|
||
8 years ago
|
util.report(output.errors);
|
||
|
});
|
||
|
|
||
|
it('should cover a simple invoked function call', done => {
|
||
|
const contract = util.getCode('function/function-call.sol');
|
||
|
const info = getInstrumentedVersion(contract, filePath);
|
||
|
const coverage = new CoverageMap();
|
||
|
coverage.addContract(info, filePath);
|
||
|
|
||
|
vm.execute(info.contract, 'a', []).then(events => {
|
||
|
const mapping = coverage.generate(events, pathPrefix);
|
||
|
assert.deepEqual(mapping[filePath].l, {
|
||
|
7: 1,
|
||
|
});
|
||
|
assert.deepEqual(mapping[filePath].b, {});
|
||
|
assert.deepEqual(mapping[filePath].s, {
|
||
|
1: 1,
|
||
|
});
|
||
|
assert.deepEqual(mapping[filePath].f, {
|
||
|
1: 1,
|
||
|
2: 1,
|
||
|
});
|
||
|
done();
|
||
|
}).catch(done);
|
||
|
});
|
||
|
|
||
6 years ago
|
it('should cover a modifier used on a function', done => {
|
||
|
const contract = util.getCode('function/modifier.sol');
|
||
|
const info = getInstrumentedVersion(contract, filePath);
|
||
|
const coverage = new CoverageMap();
|
||
|
coverage.addContract(info, filePath);
|
||
|
|
||
|
vm.execute(info.contract, 'a', [0]).then(events => {
|
||
|
const mapping = coverage.generate(events, pathPrefix);
|
||
|
assert.deepEqual(mapping[filePath].l, {
|
||
|
5: 1, 6: 1, 9: 1,
|
||
|
});
|
||
|
assert.deepEqual(mapping[filePath].b, {});
|
||
|
assert.deepEqual(mapping[filePath].s, {
|
||
|
1: 1
|
||
|
});
|
||
|
assert.deepEqual(mapping[filePath].f, {
|
||
|
1: 1,
|
||
|
2: 1,
|
||
|
});
|
||
|
done();
|
||
|
}).catch(done);
|
||
|
});
|
||
|
|
||
7 years ago
|
it('should cover a constructor that uses the `constructor` keyword', done => {
|
||
|
const contract = util.getCode('function/constructor-keyword.sol');
|
||
|
const info = getInstrumentedVersion(contract, filePath);
|
||
|
const coverage = new CoverageMap();
|
||
|
coverage.addContract(info, filePath);
|
||
|
|
||
|
vm.execute(info.contract, 'a', []).then(events => {
|
||
|
const mapping = coverage.generate(events, pathPrefix);
|
||
|
assert.deepEqual(mapping[filePath].l, {
|
||
|
6: 1, 11: 1
|
||
|
});
|
||
|
assert.deepEqual(mapping[filePath].b, {});
|
||
|
assert.deepEqual(mapping[filePath].s, {
|
||
6 years ago
|
1: 1, 2: 1
|
||
7 years ago
|
});
|
||
|
assert.deepEqual(mapping[filePath].f, {
|
||
|
1: 1,
|
||
|
2: 1,
|
||
|
});
|
||
|
done();
|
||
|
}).catch(done);
|
||
|
});
|
||
|
|
||
8 years ago
|
it('should cover a constructor call that chains to a method call', done => {
|
||
|
const contract = util.getCode('function/chainable.sol');
|
||
|
const info = getInstrumentedVersion(contract, filePath);
|
||
|
const coverage = new CoverageMap();
|
||
|
coverage.addContract(info, filePath);
|
||
7 years ago
|
// We try and call a contract at an address where it doesn't exist and the VM
|
||
|
// throws, but we can verify line / statement / fn coverage is getting mapped.
|
||
8 years ago
|
vm.execute(info.contract, 'a', []).then(events => {
|
||
|
const mapping = coverage.generate(events, pathPrefix);
|
||
|
assert.deepEqual(mapping[filePath].l, {
|
||
7 years ago
|
9: 1,
|
||
8 years ago
|
});
|
||
|
assert.deepEqual(mapping[filePath].b, {});
|
||
|
assert.deepEqual(mapping[filePath].s, {
|
||
7 years ago
|
1: 1,
|
||
8 years ago
|
});
|
||
|
assert.deepEqual(mapping[filePath].f, {
|
||
|
1: 0,
|
||
7 years ago
|
2: 1,
|
||
8 years ago
|
});
|
||
|
done();
|
||
|
}).catch(done);
|
||
|
});
|
||
|
|
||
|
it('should cover a constructor call that chains to a method call', done => {
|
||
|
const contract = util.getCode('function/chainable-value.sol');
|
||
|
const info = getInstrumentedVersion(contract, filePath);
|
||
|
const coverage = new CoverageMap();
|
||
|
coverage.addContract(info, filePath);
|
||
|
// The vm runs out of gas here - but we can verify line / statement / fn
|
||
|
// coverage is getting mapped.
|
||
|
vm.execute(info.contract, 'a', []).then(events => {
|
||
|
const mapping = coverage.generate(events, pathPrefix);
|
||
|
assert.deepEqual(mapping[filePath].l, {
|
||
7 years ago
|
10: 1,
|
||
8 years ago
|
});
|
||
|
assert.deepEqual(mapping[filePath].b, {});
|
||
|
assert.deepEqual(mapping[filePath].s, {
|
||
7 years ago
|
1: 1,
|
||
8 years ago
|
});
|
||
|
assert.deepEqual(mapping[filePath].f, {
|
||
|
1: 0,
|
||
7 years ago
|
2: 1,
|
||
8 years ago
|
});
|
||
|
done();
|
||
|
}).catch(done);
|
||
|
});
|
||
8 years ago
|
});
|
||
5 years ago
|
*/
|