Update function & expression tests

pull/381/head
cgewecke 5 years ago
parent c3f6db01c2
commit a7c49f1669
  1. 24
      test/units/expressions.js
  2. 185
      test/units/function.js

@ -1,24 +1,14 @@
/* eslint-env node, mocha */ const assert = require('assert');
const util = require('./../util/util.js');
/*const solc = require('solc');
const getInstrumentedVersion = require('./../lib/instrumentSolidity.js');
const util = require('./util/util.js');
const path = require('path');
describe('generic expressions', () => { describe('generic expressions', () => {
const filePath = path.resolve('./test.sol');
it('should compile after instrumenting a single binary expression', () => { it('should compile after instrumenting a single binary expression', () => {
const contract = util.getCode('expressions/single-binary-expression.sol'); const info = util.instrumentAndCompile('expressions/single-binary-expression');
const info = getInstrumentedVersion(contract, filePath); util.report(info.solcOutput.errors);
const output = JSON.parse(solc.compile(util.codeToCompilerInput(info.contract)));
util.report(output.errors);
}); });
it('should compile after instrumenting a new expression', () => { it('should compile after instrumenting a new expression', () => {
const contract = util.getCode('expressions/new-expression.sol'); const info = util.instrumentAndCompile('expressions/new-expression');
const info = getInstrumentedVersion(contract, filePath); util.report(info.solcOutput.errors);
const output = JSON.parse(solc.compile(util.codeToCompilerInput(info.contract)));
util.report(output.errors);
}); });
});*/ });

@ -1,186 +1,159 @@
/*const solc = require('solc');
const getInstrumentedVersion = require('./../lib/instrumentSolidity.js');
const util = require('./util/util.js');
const path = require('path');
const CoverageMap = require('./../lib/coverageMap');
const vm = require('./util/vm');
const assert = require('assert'); const assert = require('assert');
const util = require('./../util/util.js');
const ganache = require('ganache-core');
const Coverage = require('./../../lib/coverage');
describe('function declarations', () => { describe('function declarations', () => {
const filePath = path.resolve('./test.sol'); let coverage;
const pathPrefix = './'; let provider;
let collector;
before(async () => ({ provider, collector } = await util.initializeProvider(ganache)));
beforeEach(() => coverage = new Coverage());
after((done) => provider.close(done));
it('should compile after instrumenting an ordinary function declaration', () => { it('should compile after instrumenting an ordinary function declaration', () => {
const contract = util.getCode('function/function.sol'); const info = util.instrumentAndCompile('function/function');
const info = getInstrumentedVersion(contract, 'test.sol'); util.report(info.solcOutput.errors);
const output = JSON.parse(solc.compile(util.codeToCompilerInput(info.contract)));
util.report(output.errors);
}); });
it('should compile after instrumenting an abstract function declaration', () => { it('should compile after instrumenting an abstract function declaration', () => {
const contract = util.getCode('function/abstract.sol'); const info = util.instrumentAndCompile('function/abstract');
const info = getInstrumentedVersion(contract, 'test.sol'); util.report(info.solcOutput.errors);
const output = JSON.parse(solc.compile(util.codeToCompilerInput(info.contract)));
util.report(output.errors);
}); });
it('should compile after instrumenting a function declaration with an empty body', () => { it('should compile after instrumenting a function declaration with an empty body', () => {
const contract = util.getCode('function/empty-body.sol'); const info = util.instrumentAndCompile('function/empty-body');
const info = getInstrumentedVersion(contract, 'test.sol'); util.report(info.solcOutput.errors);
const output = JSON.parse(solc.compile(util.codeToCompilerInput(info.contract)));
util.report(output.errors);
}); });
it('should compile after instrumenting lots of declarations in row', () => { it('should compile after instrumenting lots of declarations in row', () => {
const contract = util.getCode('function/multiple.sol'); const info = util.instrumentAndCompile('function/multiple');
const info = getInstrumentedVersion(contract, 'test.sol'); util.report(info.solcOutput.errors);
const output = JSON.parse(solc.compile(util.codeToCompilerInput(info.contract)));
util.report(output.errors);
}); });
it('should compile after instrumenting a new->constructor-->method chain', () => { it('should compile after instrumenting a new->constructor-->method chain', () => {
const contract = util.getCode('function/chainable-new.sol'); const info = util.instrumentAndCompile('function/chainable-new');
const info = getInstrumentedVersion(contract, 'test.sol'); util.report(info.solcOutput.errors);
const output = JSON.parse(solc.compile(util.codeToCompilerInput(info.contract)));
util.report(output.errors);
}); });
it('should compile after instrumenting a constructor call that chains to a method call', () => { it('should compile after instrumenting a constructor call that chains to a method call', () => {
const contract = util.getCode('function/chainable.sol'); const info = util.instrumentAndCompile('function/chainable');
const info = getInstrumentedVersion(contract, 'test.sol'); util.report(info.solcOutput.errors);
const output = JSON.parse(solc.compile(util.codeToCompilerInput(info.contract)));
util.report(output.errors);
}); });
it('should compile after instrumenting a function with calldata keyword', () => { it('should compile after instrumenting a function with calldata keyword', () => {
const contract = util.getCode('function/calldata.sol'); const info = util.instrumentAndCompile('function/calldata');
const info = getInstrumentedVersion(contract, 'test.sol'); util.report(info.solcOutput.errors);
const output = JSON.parse(solc.compile(util.codeToCompilerInput(info.contract)));
util.report(output.errors);
}); });
it('should compile after instrumenting a constructor-->method-->value chain', () => { it('should compile after instrumenting a constructor-->method-->value chain', () => {
const contract = util.getCode('function/chainable-value.sol'); const info = util.instrumentAndCompile('function/chainable-value');
const info = getInstrumentedVersion(contract, 'test.sol'); util.report(info.solcOutput.errors);
const output = JSON.parse(solc.compile(util.codeToCompilerInput(info.contract)));
util.report(output.errors);
}); });
it('should cover a simple invoked function call', done => { it('should cover a simple invoked function call', async function() {
const contract = util.getCode('function/function-call.sol'); const contract = await util.bootstrapCoverage('function/function-call', provider, collector);
const info = getInstrumentedVersion(contract, filePath); coverage.addContract(contract.instrumented, util.filePath);
const coverage = new CoverageMap(); await contract.instance.a();
coverage.addContract(info, filePath); const mapping = coverage.generate(contract.data, util.pathPrefix);
vm.execute(info.contract, 'a', []).then(events => { assert.deepEqual(mapping[util.filePath].l, {
const mapping = coverage.generate(events, pathPrefix);
assert.deepEqual(mapping[filePath].l, {
7: 1, 7: 1,
}); });
assert.deepEqual(mapping[filePath].b, {}); assert.deepEqual(mapping[util.filePath].b, {});
assert.deepEqual(mapping[filePath].s, { assert.deepEqual(mapping[util.filePath].s, {
1: 1, 1: 1,
}); });
assert.deepEqual(mapping[filePath].f, { assert.deepEqual(mapping[util.filePath].f, {
1: 1, 1: 1,
2: 1, 2: 1,
}); });
done();
}).catch(done);
}); });
it('should cover a modifier used on a function', done => { it('should cover a modifier used on a function', async function() {
const contract = util.getCode('function/modifier.sol'); const contract = await util.bootstrapCoverage('function/modifier', provider, collector);
const info = getInstrumentedVersion(contract, filePath); coverage.addContract(contract.instrumented, util.filePath);
const coverage = new CoverageMap(); await contract.instance.a(0);
coverage.addContract(info, filePath); const mapping = coverage.generate(contract.data, util.pathPrefix);
vm.execute(info.contract, 'a', [0]).then(events => { assert.deepEqual(mapping[util.filePath].l, {
const mapping = coverage.generate(events, pathPrefix);
assert.deepEqual(mapping[filePath].l, {
5: 1, 6: 1, 9: 1, 5: 1, 6: 1, 9: 1,
}); });
assert.deepEqual(mapping[filePath].b, {}); assert.deepEqual(mapping[util.filePath].b, {});
assert.deepEqual(mapping[filePath].s, { assert.deepEqual(mapping[util.filePath].s, {
1: 1 1: 1
}); });
assert.deepEqual(mapping[filePath].f, { assert.deepEqual(mapping[util.filePath].f, {
1: 1, 1: 1,
2: 1, 2: 1,
}); });
done();
}).catch(done);
}); });
it('should cover a constructor that uses the `constructor` keyword', done => { it('should cover a constructor that uses the `constructor` keyword', async function() {
const contract = util.getCode('function/constructor-keyword.sol'); const contract = await util.bootstrapCoverage('function/constructor-keyword', provider, collector);
const info = getInstrumentedVersion(contract, filePath); coverage.addContract(contract.instrumented, util.filePath);
const coverage = new CoverageMap(); await contract.instance.a();
coverage.addContract(info, filePath); const mapping = coverage.generate(contract.data, util.pathPrefix);
vm.execute(info.contract, 'a', []).then(events => { assert.deepEqual(mapping[util.filePath].l, {
const mapping = coverage.generate(events, pathPrefix);
assert.deepEqual(mapping[filePath].l, {
6: 1, 11: 1 6: 1, 11: 1
}); });
assert.deepEqual(mapping[filePath].b, {}); assert.deepEqual(mapping[util.filePath].b, {});
assert.deepEqual(mapping[filePath].s, { assert.deepEqual(mapping[util.filePath].s, {
1: 1, 2: 1 1: 1, 2: 1
}); });
assert.deepEqual(mapping[filePath].f, { assert.deepEqual(mapping[util.filePath].f, {
1: 1, 1: 1,
2: 1, 2: 1,
}); });
done();
}).catch(done);
}); });
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);
// We try and call a contract at an address where it doesn't exist and the VM // 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. // throws, but we can verify line / statement / fn coverage is getting mapped.
vm.execute(info.contract, 'a', []).then(events => { it('should cover a constructor call that chains to a method call', async function() {
const mapping = coverage.generate(events, pathPrefix); const contract = await util.bootstrapCoverage('function/chainable', provider, collector);
assert.deepEqual(mapping[filePath].l, { coverage.addContract(contract.instrumented, util.filePath);
try { await contract.instance.a() } catch(err){}
const mapping = coverage.generate(contract.data, util.pathPrefix);
assert.deepEqual(mapping[util.filePath].l, {
9: 1, 9: 1,
}); });
assert.deepEqual(mapping[filePath].b, {}); assert.deepEqual(mapping[util.filePath].b, {});
assert.deepEqual(mapping[filePath].s, { assert.deepEqual(mapping[util.filePath].s, {
1: 1, 1: 1,
}); });
assert.deepEqual(mapping[filePath].f, { assert.deepEqual(mapping[util.filePath].f, {
1: 0, 1: 0,
2: 1, 2: 1,
}); });
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 // The vm runs out of gas here - but we can verify line / statement / fn
// coverage is getting mapped. // coverage is getting mapped.
vm.execute(info.contract, 'a', []).then(events => { it('should cover a constructor call that chains to a method call', async function() {
const mapping = coverage.generate(events, pathPrefix); const contract = await util.bootstrapCoverage('function/chainable-value', provider, collector);
assert.deepEqual(mapping[filePath].l, { coverage.addContract(contract.instrumented, util.filePath);
try { await contract.instance.a() } catch(err){}
const mapping = coverage.generate(contract.data, util.pathPrefix);
assert.deepEqual(mapping[util.filePath].l, {
10: 1, 10: 1,
}); });
assert.deepEqual(mapping[filePath].b, {}); assert.deepEqual(mapping[util.filePath].b, {});
assert.deepEqual(mapping[filePath].s, { assert.deepEqual(mapping[util.filePath].s, {
1: 1, 1: 1,
}); });
assert.deepEqual(mapping[filePath].f, { assert.deepEqual(mapping[util.filePath].f, {
1: 0, 1: 0,
2: 1, 2: 1,
}); });
done();
}).catch(done);
}); });
}); });
*/

Loading…
Cancel
Save