Add branch coverage for logical OR conditions (#499)
* Add initial OR coverage test cases * Add logicalOR coverage for "require" conditions * Add logicalOR coverage for "while" conditions * Add logicalOR coverage for "return" conditions * Add logicalOR coverage for "if" conditions * Add logicalOR branch highlighting for html reportlogical-or-safe
parent
7bdcac1206
commit
15ad590989
@ -0,0 +1,8 @@ |
||||
// Testing hooks
|
||||
const fn = (msg, config) => config.logger.log(msg); |
||||
|
||||
module.exports = { |
||||
skipFiles: ['Migrations.sol'], |
||||
silent: process.env.SILENT ? true : false, |
||||
istanbulReporter: ['json-summary', 'text'], |
||||
} |
@ -0,0 +1,7 @@ |
||||
const { loadPluginFile } = require("@nomiclabs/buidler/plugins-testing"); |
||||
loadPluginFile(__dirname + "/../plugins/buidler.plugin"); |
||||
usePlugin("@nomiclabs/buidler-truffle5"); |
||||
|
||||
module.exports={ |
||||
defaultNetwork: "buidlerevm" |
||||
}; |
@ -0,0 +1,40 @@ |
||||
pragma solidity ^0.5.0; |
||||
|
||||
|
||||
contract ContractA { |
||||
|
||||
function _if(uint i) public pure { |
||||
if (i == 0 || i > 5){ |
||||
/* ignore */ |
||||
} |
||||
} |
||||
|
||||
function _if_and(uint i) public pure { |
||||
if (i != 0 && (i < 2 || i > 5)){ |
||||
/* ignore */ |
||||
} |
||||
} |
||||
|
||||
function _return(uint i) public pure returns (bool){ |
||||
return (i != 0 && i != 1 ) || |
||||
((i + 1) == 2); |
||||
} |
||||
|
||||
function _while(uint i) public pure returns (bool){ |
||||
uint counter; |
||||
while( (i == 1 || i == 2) && counter < 2 ){ |
||||
counter++; |
||||
} |
||||
} |
||||
|
||||
function _require(uint x) public { |
||||
require(x == 1 || x == 2); |
||||
} |
||||
|
||||
function _require_multi_line(uint x) public { |
||||
require( |
||||
(x == 1 || x == 2) || |
||||
x == 3 |
||||
); |
||||
} |
||||
} |
@ -0,0 +1,33 @@ |
||||
const ContractA = artifacts.require("ContractA"); |
||||
|
||||
contract("contracta", function(accounts) { |
||||
let instance; |
||||
|
||||
before(async () => instance = await ContractA.new()) |
||||
|
||||
it('_if', async function(){ |
||||
await instance._if(0); |
||||
await instance._if(7); |
||||
}); |
||||
|
||||
it('_if_and', async function(){ |
||||
await instance._if_and(1); |
||||
}); |
||||
|
||||
it('_return', async function(){ |
||||
await instance._return(4); |
||||
}); |
||||
|
||||
it('_while', async function(){ |
||||
await instance._while(1); |
||||
}); |
||||
|
||||
it('_require', async function(){ |
||||
await instance._require(2); |
||||
}) |
||||
|
||||
it('_require_multi_line', async function(){ |
||||
await instance._require_multi_line(1); |
||||
await instance._require_multi_line(3); |
||||
}) |
||||
}); |
@ -0,0 +1,7 @@ |
||||
module.exports = { |
||||
networks: {}, |
||||
mocha: {}, |
||||
compilers: { |
||||
solc: {} |
||||
} |
||||
} |
@ -0,0 +1,11 @@ |
||||
pragma solidity ^0.5.0; |
||||
|
||||
contract Test { |
||||
function getBool(bool _b) public pure returns (bool){ |
||||
return _b; |
||||
} |
||||
|
||||
function a(bool _a) public { |
||||
require(getBool(_a), "mi ritrovai per una selva oscura"); |
||||
} |
||||
} |
@ -0,0 +1,11 @@ |
||||
pragma solidity ^0.5.0; |
||||
|
||||
contract Test { |
||||
function getBool(bool _b) public pure returns (bool){ |
||||
return _b; |
||||
} |
||||
|
||||
function a(bool _a) public { |
||||
require(getBool(_a)); |
||||
} |
||||
} |
@ -0,0 +1,11 @@ |
||||
pragma solidity ^0.5.0; |
||||
|
||||
contract Test { |
||||
function a(uint x) public { |
||||
if ((x == 1) && (x == 2 || true)) { |
||||
/* ignore */ |
||||
} else { |
||||
revert(); |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,11 @@ |
||||
pragma solidity ^0.5.0; |
||||
|
||||
contract Test { |
||||
function a(uint x) public { |
||||
if (x == 1 && true || x == 2) { |
||||
/* ignore */ |
||||
} else { |
||||
revert(); |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,19 @@ |
||||
pragma solidity ^0.5.0; |
||||
|
||||
contract Test { |
||||
function isFalse(uint _a, uint _b) public pure returns (bool){ |
||||
return false; |
||||
} |
||||
|
||||
function a(uint x) public { |
||||
require(( |
||||
x == 1 && |
||||
x == 2 ) || |
||||
!isFalse( |
||||
x, |
||||
3 |
||||
), |
||||
"unhealthy position" |
||||
); |
||||
} |
||||
} |
@ -0,0 +1,11 @@ |
||||
pragma solidity ^0.5.0; |
||||
|
||||
contract Test { |
||||
function a(uint x) public { |
||||
if (x == 1 || x == 2) { |
||||
/* ignore */ |
||||
} else { |
||||
revert(); |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,11 @@ |
||||
pragma solidity ^0.5.0; |
||||
|
||||
contract Test { |
||||
function a(uint x) public { |
||||
if ((x == 1) || (x == 2 || true)) { |
||||
/* ignore */ |
||||
} else { |
||||
revert(); |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,11 @@ |
||||
pragma solidity ^0.5.0; |
||||
|
||||
contract Test { |
||||
function a(uint x) public { |
||||
require( |
||||
x == 1 || |
||||
x == 2 || |
||||
x == 3 |
||||
); |
||||
} |
||||
} |
@ -0,0 +1,7 @@ |
||||
pragma solidity ^0.5.0; |
||||
|
||||
contract Test { |
||||
function a(uint x) public { |
||||
require(x == 1 || x == 2); |
||||
} |
||||
} |
@ -0,0 +1,7 @@ |
||||
pragma solidity ^0.5.0; |
||||
|
||||
contract Test { |
||||
function a(uint x) public pure returns (bool) { |
||||
return (x == 1 && true) || (x == 2 && true); |
||||
} |
||||
} |
@ -0,0 +1,10 @@ |
||||
pragma solidity ^0.5.0; |
||||
|
||||
contract Test { |
||||
function a(uint x) public { |
||||
uint counter; |
||||
while( (x == 1 || x == 2) && counter < 2 ){ |
||||
counter++; |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,7 @@ |
||||
pragma solidity ^0.5.0; |
||||
|
||||
contract Test { |
||||
function a(uint x) public pure { |
||||
return; |
||||
} |
||||
} |
@ -0,0 +1,11 @@ |
||||
pragma solidity ^0.5.0; |
||||
|
||||
contract Test { |
||||
function a(uint x) public pure returns (uint) { |
||||
return x > 3 ? x : 1; |
||||
} |
||||
|
||||
function b(uint x) public pure returns (uint) { |
||||
return (x > 3) ? x : 1; |
||||
} |
||||
} |
@ -0,0 +1,375 @@ |
||||
const assert = require('assert'); |
||||
const util = require('./../util/util.js'); |
||||
|
||||
const ganache = require('ganache-core-sc'); |
||||
const Coverage = require('./../../lib/coverage'); |
||||
|
||||
describe('logical OR branches', () => { |
||||
let coverage; |
||||
let provider; |
||||
let collector; |
||||
|
||||
before(async () => ({ provider, collector } = await util.initializeProvider(ganache))); |
||||
beforeEach(() => coverage = new Coverage()); |
||||
after((done) => provider.close(done)); |
||||
|
||||
// if (x == 1 || x == 2) { } else ...
|
||||
it('should cover an if statement with a simple OR condition (single branch)', async function() { |
||||
const contract = await util.bootstrapCoverage('or/if-or', 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, 8: 0 |
||||
}); |
||||
assert.deepEqual(mapping[util.filePath].b, { |
||||
1: [1, 0], 2: [1, 0] |
||||
}); |
||||
assert.deepEqual(mapping[util.filePath].s, { |
||||
1: 1, 2: 0, |
||||
}); |
||||
assert.deepEqual(mapping[util.filePath].f, { |
||||
1: 1, |
||||
}); |
||||
}); |
||||
|
||||
// if (x == 1 || x == 2) { } else ...
|
||||
it('should cover an if statement with a simple OR condition (both branches)', async function() { |
||||
const contract = await util.bootstrapCoverage('or/if-or', provider, collector); |
||||
coverage.addContract(contract.instrumented, util.filePath); |
||||
await contract.instance.a(1); |
||||
await contract.instance.a(2); |
||||
|
||||
const mapping = coverage.generate(contract.data, util.pathPrefix); |
||||
|
||||
assert.deepEqual(mapping[util.filePath].l, { |
||||
5: 2, 8: 0 |
||||
}); |
||||
assert.deepEqual(mapping[util.filePath].b, { |
||||
1: [2, 0], 2: [1, 1] |
||||
}); |
||||
assert.deepEqual(mapping[util.filePath].s, { |
||||
1: 2, 2: 0, |
||||
}); |
||||
assert.deepEqual(mapping[util.filePath].f, { |
||||
1: 2, |
||||
}); |
||||
}); |
||||
|
||||
// require(x == 1 || x == 2)
|
||||
it('should cover a require statement with a simple OR condition (single branch)', async function() { |
||||
const contract = await util.bootstrapCoverage('or/require-or', 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, |
||||
}); |
||||
assert.deepEqual(mapping[util.filePath].b, { |
||||
1: [1, 0], 2: [1, 0] |
||||
}); |
||||
assert.deepEqual(mapping[util.filePath].s, { |
||||
1: 1, |
||||
}); |
||||
assert.deepEqual(mapping[util.filePath].f, { |
||||
1: 1, |
||||
}); |
||||
}); |
||||
|
||||
// require(
|
||||
// x == 1 ||
|
||||
// x == 2 ||
|
||||
// x == 3
|
||||
// )
|
||||
it('should cover a require statement with multiline OR condition (two branches)', async function() { |
||||
const contract = await util.bootstrapCoverage('or/require-multiline-or', provider, collector); |
||||
coverage.addContract(contract.instrumented, util.filePath); |
||||
await contract.instance.a(1); |
||||
await contract.instance.a(3); |
||||
|
||||
const mapping = coverage.generate(contract.data, util.pathPrefix); |
||||
|
||||
assert.deepEqual(mapping[util.filePath].l, { |
||||
5: 2, |
||||
}); |
||||
assert.deepEqual(mapping[util.filePath].b, { |
||||
1: [2, 0], 2: [1, 0], 3: [0, 1] |
||||
}); |
||||
assert.deepEqual(mapping[util.filePath].s, { |
||||
1: 2, |
||||
}); |
||||
assert.deepEqual(mapping[util.filePath].f, { |
||||
1: 2, |
||||
}); |
||||
}); |
||||
|
||||
// require(x == 1 || x == 2)
|
||||
it('should cover a require statement with a simple OR condition (both branches)', async function() { |
||||
const contract = await util.bootstrapCoverage('or/require-or', provider, collector); |
||||
coverage.addContract(contract.instrumented, util.filePath); |
||||
await contract.instance.a(1); |
||||
await contract.instance.a(2); |
||||
|
||||
const mapping = coverage.generate(contract.data, util.pathPrefix); |
||||
|
||||
assert.deepEqual(mapping[util.filePath].l, { |
||||
5: 2, |
||||
}); |
||||
assert.deepEqual(mapping[util.filePath].b, { |
||||
1: [2, 0], 2: [1, 1] |
||||
}); |
||||
assert.deepEqual(mapping[util.filePath].s, { |
||||
1: 2, |
||||
}); |
||||
assert.deepEqual(mapping[util.filePath].f, { |
||||
1: 2, |
||||
}); |
||||
}); |
||||
|
||||
// while( (x == 1 || x == 2) && counter < 2 ){
|
||||
it('should cover a while statement with a simple OR condition (single branch)', async function() { |
||||
const contract = await util.bootstrapCoverage('or/while-or', 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: 1, 7: 2 |
||||
}); |
||||
assert.deepEqual(mapping[util.filePath].b, { |
||||
1: [3, 0] |
||||
}); |
||||
assert.deepEqual(mapping[util.filePath].s, { |
||||
1: 1, 2: 1 |
||||
}); |
||||
assert.deepEqual(mapping[util.filePath].f, { |
||||
1: 1, |
||||
}); |
||||
}); |
||||
|
||||
// while( (x == 1 || x == 2) && counter < 2 ){
|
||||
it('should cover a while statement with a simple OR condition (both branches)', async function() { |
||||
const contract = await util.bootstrapCoverage('or/while-or', provider, collector); |
||||
coverage.addContract(contract.instrumented, util.filePath); |
||||
await contract.instance.a(1); |
||||
await contract.instance.a(2); |
||||
|
||||
const mapping = coverage.generate(contract.data, util.pathPrefix); |
||||
|
||||
assert.deepEqual(mapping[util.filePath].l, { |
||||
5: 2, 6: 2, 7: 4 |
||||
}); |
||||
assert.deepEqual(mapping[util.filePath].b, { |
||||
1: [3, 3] |
||||
}); |
||||
assert.deepEqual(mapping[util.filePath].s, { |
||||
1: 2, 2: 2 |
||||
}); |
||||
assert.deepEqual(mapping[util.filePath].f, { |
||||
1: 2, |
||||
}); |
||||
}); |
||||
|
||||
// return (x == 1 && true) || (x == 2 && true);
|
||||
it('should cover a return statement with ANDED OR conditions (single branch)', async function() { |
||||
const contract = await util.bootstrapCoverage('or/return-or', 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, |
||||
}); |
||||
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, |
||||
}); |
||||
}); |
||||
|
||||
// return (x == 1 && true) || (x == 2 && true);
|
||||
it('should cover a return statement with ANDED OR conditions (both branches)', async function() { |
||||
const contract = await util.bootstrapCoverage('or/return-or', provider, collector); |
||||
coverage.addContract(contract.instrumented, util.filePath); |
||||
await contract.instance.a(1); |
||||
await contract.instance.a(2); |
||||
|
||||
const mapping = coverage.generate(contract.data, util.pathPrefix); |
||||
|
||||
assert.deepEqual(mapping[util.filePath].l, { |
||||
5: 2, |
||||
}); |
||||
assert.deepEqual(mapping[util.filePath].b, { |
||||
1: [ 1, 1 ] |
||||
}); |
||||
assert.deepEqual(mapping[util.filePath].s, { |
||||
1: 2, |
||||
}); |
||||
assert.deepEqual(mapping[util.filePath].f, { |
||||
1: 2, |
||||
}); |
||||
}); |
||||
|
||||
//if (x == 1 && true || x == 2) {
|
||||
it('should cover an if statement with OR and AND conditions (single branch)', async function() { |
||||
const contract = await util.bootstrapCoverage('or/and-or', 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, 8: 0 |
||||
}); |
||||
assert.deepEqual(mapping[util.filePath].b, { |
||||
1: [1, 0], 2: [1, 0] |
||||
}); |
||||
assert.deepEqual(mapping[util.filePath].s, { |
||||
1: 1, 2: 0, |
||||
}); |
||||
assert.deepEqual(mapping[util.filePath].f, { |
||||
1: 1, |
||||
}); |
||||
}); |
||||
|
||||
//if (x == 1 && true || x == 2) {
|
||||
it('should cover an if statement with OR and AND conditions (both branches)', async function() { |
||||
const contract = await util.bootstrapCoverage('or/and-or', provider, collector); |
||||
coverage.addContract(contract.instrumented, util.filePath); |
||||
await contract.instance.a(1); |
||||
await contract.instance.a(2); |
||||
|
||||
const mapping = coverage.generate(contract.data, util.pathPrefix); |
||||
|
||||
assert.deepEqual(mapping[util.filePath].l, { |
||||
5: 2, 8: 0 |
||||
}); |
||||
assert.deepEqual(mapping[util.filePath].b, { |
||||
1: [2, 0], 2: [1, 1] |
||||
}); |
||||
assert.deepEqual(mapping[util.filePath].s, { |
||||
1: 2, 2: 0, |
||||
}); |
||||
assert.deepEqual(mapping[util.filePath].f, { |
||||
1: 2, |
||||
}); |
||||
}); |
||||
|
||||
// if ((x == 1) && (x == 2 || true)) {
|
||||
it('should cover an if statement with bracked ANDED OR conditions (rightmost sub-branch)', async function() { |
||||
const contract = await util.bootstrapCoverage('or/and-or-brackets', 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, 8: 0 |
||||
}); |
||||
assert.deepEqual(mapping[util.filePath].b, { |
||||
1: [1, 0], 2: [0, 1] |
||||
}); |
||||
assert.deepEqual(mapping[util.filePath].s, { |
||||
1: 1, 2: 0, |
||||
}); |
||||
assert.deepEqual(mapping[util.filePath].f, { |
||||
1: 1, |
||||
}); |
||||
}); |
||||
|
||||
// if ((x == 1) || (x == 2 || true)) {
|
||||
it('should cover an if statement with multiple (bracketed) OR conditions (branch 1)', async function() { |
||||
const contract = await util.bootstrapCoverage('or/multi-or', 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, 8: 0 |
||||
}); |
||||
assert.deepEqual(mapping[util.filePath].b, { |
||||
1: [1, 0], 2: [0, 0], 3: [1, 0] // Item 3 is the "outer" (left) OR clause
|
||||
}); |
||||
assert.deepEqual(mapping[util.filePath].s, { |
||||
1: 1, 2: 0, |
||||
}); |
||||
assert.deepEqual(mapping[util.filePath].f, { |
||||
1: 1, |
||||
}); |
||||
}); |
||||
|
||||
// if ((x == 1) || (x == 2 || true)) {
|
||||
it('should cover an if statement with multiple (bracketed) OR conditions (branch 2)', async function() { |
||||
const contract = await util.bootstrapCoverage('or/multi-or', provider, collector); |
||||
coverage.addContract(contract.instrumented, util.filePath); |
||||
await contract.instance.a(2); |
||||
|
||||
const mapping = coverage.generate(contract.data, util.pathPrefix); |
||||
|
||||
assert.deepEqual(mapping[util.filePath].l, { |
||||
5: 1, 8: 0 |
||||
}); |
||||
assert.deepEqual(mapping[util.filePath].b, { |
||||
1: [1, 0], 2: [1, 0], 3: [0, 1] // Item 3 is the "outer" (left) OR clause
|
||||
}); |
||||
assert.deepEqual(mapping[util.filePath].s, { |
||||
1: 1, 2: 0, |
||||
}); |
||||
assert.deepEqual(mapping[util.filePath].f, { |
||||
1: 1, |
||||
}); |
||||
}); |
||||
|
||||
// if ((x == 1) || (x == 2 || true)) {
|
||||
it('should cover an if statement with multiple (bracketed) OR conditions (branch 3)', async function() { |
||||
const contract = await util.bootstrapCoverage('or/multi-or', provider, collector); |
||||
coverage.addContract(contract.instrumented, util.filePath); |
||||
await contract.instance.a(3); |
||||
|
||||
const mapping = coverage.generate(contract.data, util.pathPrefix); |
||||
|
||||
assert.deepEqual(mapping[util.filePath].l, { |
||||
5: 1, 8: 0 |
||||
}); |
||||
assert.deepEqual(mapping[util.filePath].b, { |
||||
1: [1, 0], 2: [0, 1], 3: [0, 1] |
||||
}); |
||||
assert.deepEqual(mapping[util.filePath].s, { |
||||
1: 1, 2: 0, |
||||
}); |
||||
assert.deepEqual(mapping[util.filePath].f, { |
||||
1: 1, |
||||
}); |
||||
}); |
||||
|
||||
it('should cover the bzx example', async function(){ |
||||
const contract = await util.bootstrapCoverage('or/bzx-or', provider, collector); |
||||
coverage.addContract(contract.instrumented, util.filePath); |
||||
await contract.instance.a(3); |
||||
|
||||
const mapping = coverage.generate(contract.data, util.pathPrefix); |
||||
|
||||
assert.deepEqual(mapping[util.filePath].l, { |
||||
5: 1, 9: 1 |
||||
}); |
||||
assert.deepEqual(mapping[util.filePath].b, { |
||||
1: [1, 0], 2: [0, 1] |
||||
}); |
||||
assert.deepEqual(mapping[util.filePath].s, { |
||||
1: 1, 2: 1, |
||||
}); |
||||
assert.deepEqual(mapping[util.filePath].f, { |
||||
1: 1, 2: 1 |
||||
}); |
||||
}) |
||||
}); |
Loading…
Reference in new issue