From 881771328f6cd851dcf7ad35d3671bc529bd0b3c Mon Sep 17 00:00:00 2001 From: cgewecke Date: Tue, 23 Jul 2019 18:28:16 -0700 Subject: [PATCH] Update statements tests --- test/units/statements.js | 262 +++++++++++++++++---------------------- 1 file changed, 112 insertions(+), 150 deletions(-) diff --git a/test/units/statements.js b/test/units/statements.js index ca9d465..f74a35e 100644 --- a/test/units/statements.js +++ b/test/units/statements.js @@ -1,188 +1,150 @@ -/* eslint-env node, mocha */ - -/*const solc = require('solc'); -const getInstrumentedVersion = require('./../lib/instrumentSolidity.js'); -const util = require('./util/util.js'); -const CoverageMap = require('./../lib/coverageMap'); -const path = require('path'); -const vm = require('./util/vm'); const assert = require('assert'); +const util = require('./../util/util.js'); + +const ganache = require('ganache-core'); +const Coverage = require('./../../lib/coverage'); describe('generic statements', () => { - const filePath = path.resolve('./test.sol'); - const pathPrefix = './'; + let coverage; + let provider; + let collector; + + before(async () => ({ provider, collector } = await util.initializeProvider(ganache))); + beforeEach(() => coverage = new Coverage()); + after((done) => provider.close(done)); it('should compile function defined in a struct', () => { - const contract = util.getCode('statements/fn-struct.sol'); - const info = getInstrumentedVersion(contract, filePath); - const output = JSON.parse(solc.compile(util.codeToCompilerInput(info.contract))); - util.report(output.errors); + const info = util.instrumentAndCompile('statements/fn-struct'); + util.report(info.solcOutput.errors); }) it('should compile when using the type keyword', () => { - const contract = util.getCode('statements/type-keyword.sol'); - const info = getInstrumentedVersion(contract, filePath); - const output = JSON.parse(solc.compile(util.codeToCompilerInput(info.contract))); - util.report(output.errors); + const info = util.instrumentAndCompile('statements/type-keyword'); + util.report(info.solcOutput.errors); }) - it('should compile after instrumenting a single statement (first line of function)', () => { - const contract = util.getCode('statements/single.sol'); - const info = getInstrumentedVersion(contract, filePath); - const output = JSON.parse(solc.compile(util.codeToCompilerInput(info.contract))); - util.report(output.errors); + it('should instrument a single statement (first line of function)', () => { + const info = util.instrumentAndCompile('statements/single'); + util.report(info.solcOutput.errors); }); - it('should compile after instrumenting multiple statements', () => { - const contract = util.getCode('statements/multiple.sol'); - const info = getInstrumentedVersion(contract, filePath); - const output = JSON.parse(solc.compile(util.codeToCompilerInput(info.contract))); - util.report(output.errors); + it('should instrument multiple statements', () => { + const info = util.instrumentAndCompile('statements/multiple'); + util.report(info.solcOutput.errors); }); - it('should compile after instrumenting a statement that is a function argument (single line)', () => { - const contract = util.getCode('statements/fn-argument.sol'); - const info = getInstrumentedVersion(contract, filePath); - const output = JSON.parse(solc.compile(util.codeToCompilerInput(info.contract))); - util.report(output.errors); + it('should instrument a statement that is a function argument (single line)', () => { + const info = util.instrumentAndCompile('statements/fn-argument'); + util.report(info.solcOutput.errors); }); - it('should compile after instrumenting a statement that is a function argument (multi-line)', () => { - const contract = util.getCode('statements/fn-argument-multiline.sol'); - const info = getInstrumentedVersion(contract, filePath); - const output = JSON.parse(solc.compile(util.codeToCompilerInput(info.contract))); - util.report(output.errors); + it('should instrument a statement that is a function argument (multi-line)', () => { + const info = util.instrumentAndCompile('statements/fn-argument-multiline'); + util.report(info.solcOutput.errors); }); - it('should compile after instrumenting an empty-contract-body', () => { - const contract = util.getCode('statements/empty-contract-ala-melonport.sol'); - const info = getInstrumentedVersion(contract, filePath); - const output = JSON.parse(solc.compile(util.codeToCompilerInput(info.contract))); - util.report(output.errors); + it('should instrument an empty-contract-body', () => { + const info = util.instrumentAndCompile('statements/empty-contract-ala-melonport'); + util.report(info.solcOutput.errors); }); it('should NOT pass tests if the contract has a compilation error', () => { - const contract = util.getCode('statements/compilation-error.sol'); - const info = getInstrumentedVersion(contract, filePath); - const output = JSON.parse(solc.compile(util.codeToCompilerInput(info.contract))); + const info = util.instrumentAndCompile('../errors/compilation-error'); try { util.report(output.errors); - assert.fail('WRONG'); // We shouldn't hit this. + assert.fail('failure'); // We shouldn't hit this. } catch (err) { - (err.actual === 'WRONG') ? assert(false) : assert(true); + (err.actual === 'failure') ? assert(false) : assert(true); } }); - it('should compile after instrumenting an emit statement after an un-enclosed if statement', () => { - const contract = util.getCode('statements/emit-instrument.sol'); - const info = getInstrumentedVersion(contract, filePath); - const output = JSON.parse(solc.compile(util.codeToCompilerInput(info.contract))); - util.report(output.errors); + it('should instrument an emit statement after an un-enclosed if statement', () => { + const info = util.instrumentAndCompile('statements/emit-instrument'); + util.report(info.solcOutput.errors); }); - it('should cover an emitted event statement', done => { - const contract = util.getCode('statements/emit-coverage.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, { - 6: 1 - }); - assert.deepEqual(mapping[filePath].b, {}); - assert.deepEqual(mapping[filePath].s, { - 1: 1 - }); - assert.deepEqual(mapping[filePath].f, { - 1: 1, - }); - done(); - }).catch(done); + it('should cover an emitted event statement', async function() { + const contract = await util.bootstrapCoverage('statements/emit-coverage', provider, collector); + coverage.addContract(contract.instrumented, util.filePath); + await contract.instance.a(0); + const mapping = coverage.generate(contract.data, util.pathPrefix); + + assert.deepEqual(mapping[util.filePath].l, { + 6: 1 + }); + assert.deepEqual(mapping[util.filePath].b, {}); + assert.deepEqual(mapping[util.filePath].s, { + 1: 1 + }); + assert.deepEqual(mapping[util.filePath].f, { + 1: 1, + }); }); - it('should cover a statement following a close brace', done => { - const contract = util.getCode('statements/post-close-brace.sol'); - const info = getInstrumentedVersion(contract, filePath); - const coverage = new CoverageMap(); - coverage.addContract(info, filePath); - - vm.execute(info.contract, 'a', [1]).then(events => { - const mapping = coverage.generate(events, pathPrefix); - assert.deepEqual(mapping[filePath].l, { - 5: 1, 6: 0, 8: 1, - }); - assert.deepEqual(mapping[filePath].b, { - 1: [0, 1], - }); - assert.deepEqual(mapping[filePath].s, { - 1: 1, 2: 0, 3: 1, - }); - assert.deepEqual(mapping[filePath].f, { - 1: 1, - }); - done(); - }).catch(done); + it('should cover a statement following a close brace', async function() { + const contract = await util.bootstrapCoverage('statements/post-close-brace', provider, collector); + coverage.addContract(contract.instrumented, util.filePath); + await contract.instance.a(1); + const mapping = coverage.generate(contract.data, util.pathPrefix); + + assert.deepEqual(mapping[util.filePath].l, { + 5: 1, 6: 0, 8: 1, + }); + assert.deepEqual(mapping[util.filePath].b, { + 1: [0, 1], + }); + assert.deepEqual(mapping[util.filePath].s, { + 1: 1, 2: 0, 3: 1, + }); + assert.deepEqual(mapping[util.filePath].f, { + 1: 1, + }); }); - it('should cover a library statement and an invoked library method', done => { - const contract = util.getCode('statements/library.sol'); - const info = getInstrumentedVersion(contract, filePath); - const coverage = new CoverageMap(); - coverage.addContract(info, filePath); - - vm.execute(info.contract, 'not', []).then(events => { - const mapping = coverage.generate(events, pathPrefix); - assert.deepEqual(mapping[filePath].l, { - 9: 1, 10: 1, 19: 1, - }); - assert.deepEqual(mapping[filePath].b, {}); - assert.deepEqual(mapping[filePath].s, { - 1: 1, 2: 1, 3: 1, - }); - assert.deepEqual(mapping[filePath].f, { - 1: 1, 2: 1, - }); - done(); - }).catch(done); + it('should cover a library statement and an invoked library method', async function() { + const contract = await util.bootstrapCoverage('statements/library', provider, collector); + coverage.addContract(contract.instrumented, util.filePath); + await contract.instance.not(); + const mapping = coverage.generate(contract.data, util.pathPrefix); + + assert.deepEqual(mapping[util.filePath].l, { + 9: 1, 10: 1, 19: 1, + }); + assert.deepEqual(mapping[util.filePath].b, {}); + assert.deepEqual(mapping[util.filePath].s, { + 1: 1, 2: 1, 3: 1, + }); + assert.deepEqual(mapping[util.filePath].f, { + 1: 1, 2: 1, + }); }); - it('should cover a tuple statement', done => { - const contract = util.getCode('statements/tuple.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, 10: 1, 11: 1 - }); - assert.deepEqual(mapping[filePath].b, {}); - assert.deepEqual(mapping[filePath].s, { - 1: 1, 2: 1, 3: 1, - }); - assert.deepEqual(mapping[filePath].f, { - 1: 1, 2: 1, - }); - done(); - }).catch(done); + it('should cover a tuple statement', async function() { + const contract = await util.bootstrapCoverage('statements/tuple', provider, collector); + coverage.addContract(contract.instrumented, util.filePath); + await contract.instance.a(); + const mapping = coverage.generate(contract.data, util.pathPrefix); + + assert.deepEqual(mapping[util.filePath].l, { + 6: 1, 10: 1, 11: 1 + }); + assert.deepEqual(mapping[util.filePath].b, {}); + assert.deepEqual(mapping[util.filePath].s, { + 1: 1, 2: 1, 3: 1, + }); + assert.deepEqual(mapping[util.filePath].f, { + 1: 1, 2: 1, + }); }); - it('should cover an empty bodied contract statement', done => { - const contract = util.getCode('statements/empty-contract-body.sol'); - const info = getInstrumentedVersion(contract, filePath); - const coverage = new CoverageMap(); - coverage.addContract(info, filePath); - - vm.execute(info.contract, null, []).then(events => { - const mapping = coverage.generate(events, pathPrefix); - assert.deepEqual(mapping[filePath].l, {}); - assert.deepEqual(mapping[filePath].b, {}); - assert.deepEqual(mapping[filePath].s, {}); - assert.deepEqual(mapping[filePath].f, {}); - done(); - }).catch(done); + it('should cover an empty bodied contract statement', async function() { + const contract = await util.bootstrapCoverage('statements/empty-contract-body', provider, collector); + coverage.addContract(contract.instrumented, util.filePath); + const mapping = coverage.generate(contract.data, util.pathPrefix); + + assert.deepEqual(mapping[util.filePath].l, {}); + assert.deepEqual(mapping[util.filePath].b, {}); + assert.deepEqual(mapping[util.filePath].s, {}); + assert.deepEqual(mapping[util.filePath].f, {}); }); -});*/ +});