From 687e6642fec22e366db6ef9a10d3344234a6b43c Mon Sep 17 00:00:00 2001 From: cgewecke Date: Tue, 23 Jul 2019 16:31:06 -0700 Subject: [PATCH] Update assert tests --- lib/coverage.js | 7 +- test/units/assert.js | 180 +++++++++++++++++++++---------------------- test/units/if.js | 2 +- 3 files changed, 92 insertions(+), 97 deletions(-) diff --git a/lib/coverage.js b/lib/coverage.js index 5897347..b2379a0 100644 --- a/lib/coverage.js +++ b/lib/coverage.js @@ -2,6 +2,9 @@ * Converts instrumentation data accumulated a the vm steps to an instanbul spec coverage object. * @type {Coverage} */ + +const util = require('util'); + class Coverage { constructor() { @@ -92,7 +95,7 @@ class Coverage { for (let contractPath of contractPaths){ const contract = this.data[contractPath]; - Object.keys(contract.b).forEach((item, i) => { + for (let i = 1; i <= Object.keys(contract.b).length; i++) { const branch = this.assertData[contractPath][i]; // Was it an assert branch? @@ -102,7 +105,7 @@ class Coverage { branch.preEvents - branch.postEvents ] } - }) + } } return Object.assign({}, this.data); diff --git a/test/units/assert.js b/test/units/assert.js index 82be2f2..f9f8341 100644 --- a/test/units/assert.js +++ b/test/units/assert.js @@ -1,109 +1,101 @@ -/* eslint-env node, mocha */ - -/*const path = require('path'); -const getInstrumentedVersion = require('./../lib/instrumentSolidity.js'); -const util = require('./util/util.js'); -const CoverageMap = require('./../lib/coverageMap'); -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'); +const DataCollector = require('./../../lib/collector'); +const nodeUtil = require('util'); describe('asserts and requires', () => { - const filePath = path.resolve('./test.sol'); - const pathPrefix = './'; + let coverage; + let provider; + let collector; - it('should cover assert statements as if they are if statements when they pass', done => { - const contract = util.getCode('assert/Assert.sol'); - const info = getInstrumentedVersion(contract, filePath); - const coverage = new CoverageMap(); - coverage.addContract(info, filePath); + before(async () => ({ provider, collector } = await util.initializeProvider(ganache))); + beforeEach(() => coverage = new Coverage()); + after((done) => provider.close(done)); - vm.execute(info.contract, 'a', [true]).then(events => { - const mapping = coverage.generate(events, pathPrefix); - assert.deepEqual(mapping[filePath].l, { - 5: 1, - }); - assert.deepEqual(mapping[filePath].b, { - 1: [1, 0], - }); - assert.deepEqual(mapping[filePath].s, { - 1: 1, - }); - assert.deepEqual(mapping[filePath].f, { - 1: 1, - }); - done(); - }).catch(done); + it('should cover assert statements as `if` statements when they pass', async function() { + const contract = await util.bootstrapCoverage('assert/Assert', provider, collector); + coverage.addContract(contract.instrumented, util.filePath); + await contract.instance.a(true); + const mapping = coverage.generate(contract.data, util.pathPrefix); + + assert.deepEqual(mapping[util.filePath].l, { + 5: 1, + }); + assert.deepEqual(mapping[util.filePath].b, { + 1: [1, 0], + }); + assert.deepEqual(mapping[util.filePath].s, { + 1: 1, + }); + assert.deepEqual(mapping[util.filePath].f, { + 1: 1, + }); }); - it('should cover assert statements as if they are if statements when they fail', done => { - const contract = util.getCode('assert/Assert.sol'); - const info = getInstrumentedVersion(contract, filePath); - const coverage = new CoverageMap(); - coverage.addContract(info, filePath); + it('should cover assert statements as `if` statements when they fail', async function() { + const contract = await util.bootstrapCoverage('assert/Assert', provider, collector); + coverage.addContract(contract.instrumented, util.filePath); + + try { await contract.instance.a(false) } catch(err) { /* Invalid opcode */ } - vm.execute(info.contract, 'a', [false]).then(events => { - const mapping = coverage.generate(events, pathPrefix); - assert.deepEqual(mapping[filePath].l, { - 5: 1, - }); - assert.deepEqual(mapping[filePath].b, { - 1: [0, 1], - }); - assert.deepEqual(mapping[filePath].s, { - 1: 1, - }); - assert.deepEqual(mapping[filePath].f, { - 1: 1, - }); - done(); - }).catch(done); + const mapping = coverage.generate(contract.data, util.pathPrefix); + + assert.deepEqual(mapping[util.filePath].l, { + 5: 1, + }); + assert.deepEqual(mapping[util.filePath].b, { + 1: [0, 1], + }); + assert.deepEqual(mapping[util.filePath].s, { + 1: 1, + }); + assert.deepEqual(mapping[util.filePath].f, { + 1: 1, + }); }); - it('should cover multi-line require statements as if they are if statements when they pass', done => { - const contract = util.getCode('assert/RequireMultiline.sol'); - const info = getInstrumentedVersion(contract, filePath); - const coverage = new CoverageMap(); - coverage.addContract(info, filePath); + it('should cover multi-line require stmts as `if` statements when they pass', async function() { + const contract = await util.bootstrapCoverage('assert/RequireMultiline', provider, collector); + coverage.addContract(contract.instrumented, util.filePath); + await contract.instance.a(true, true, true); + const mapping = coverage.generate(contract.data, util.pathPrefix); - vm.execute(info.contract, 'a', [true, true, true]).then(events => { - const mapping = coverage.generate(events, pathPrefix); - assert.deepEqual(mapping[filePath].l, { - 5: 1, - }); - assert.deepEqual(mapping[filePath].b, { - 1: [1, 0], - }); - assert.deepEqual(mapping[filePath].s, { - 1: 1, - }); - assert.deepEqual(mapping[filePath].f, { - 1: 1, - }); - done(); - }).catch(done); + assert.deepEqual(mapping[util.filePath].l, { + 5: 1, + }); + assert.deepEqual(mapping[util.filePath].b, { + 1: [1, 0], + }); + assert.deepEqual(mapping[util.filePath].s, { + 1: 1, + }); + assert.deepEqual(mapping[util.filePath].f, { + 1: 1, + }); }); - it('should cover multi-line require statements as if they are if statements when they fail', done => { - const contract = util.getCode('assert/RequireMultiline.sol'); - const info = getInstrumentedVersion(contract, filePath); - const coverage = new CoverageMap(); - coverage.addContract(info, filePath); + it('should cover multi-line require stmts as `if` statements when they fail', async function() { + const contract = await util.bootstrapCoverage('assert/RequireMultiline', provider, collector); + coverage.addContract(contract.instrumented, util.filePath); + + try { await contract.instance.a(true, true, false) } catch(err) { /* Revert */ } + + const mapping = coverage.generate(contract.data, util.pathPrefix); - vm.execute(info.contract, 'a', [true, true, false]).then(events => { - const mapping = coverage.generate(events, pathPrefix); - assert.deepEqual(mapping[filePath].l, { - 5: 1, - }); - assert.deepEqual(mapping[filePath].b, { - 1: [0, 1], - }); - assert.deepEqual(mapping[filePath].s, { - 1: 1, - }); - assert.deepEqual(mapping[filePath].f, { - 1: 1, - }); - done(); - }).catch(done); + assert.deepEqual(mapping[util.filePath].l, { + 5: 1, + }); + assert.deepEqual(mapping[util.filePath].b, { + 1: [0, 1], + }); + assert.deepEqual(mapping[util.filePath].s, { + 1: 1, + }); + assert.deepEqual(mapping[util.filePath].f, { + 1: 1, + }); }); -});*/ +}); diff --git a/test/units/if.js b/test/units/if.js index d9a62ba..d8a29e5 100644 --- a/test/units/if.js +++ b/test/units/if.js @@ -5,7 +5,7 @@ const ganache = require('ganache-core'); const Coverage = require('./../../lib/coverage'); const DataCollector = require('./../../lib/collector'); -describe.only('if, else, and else if statements', () => { +describe('if, else, and else if statements', () => { let coverage; let provider; let collector;