Merge pull request #33 from sc-forks/events-unit-test

Add (disabled) events filter unit test
pull/34/head
c-g-e-w-e-k-e- 8 years ago committed by GitHub
commit 937ad30c28
  1. 7
      bin/exec.js
  2. 28
      test/cli.js
  3. 24
      test/cli/events.js
  4. 4
      test/cli/testrpc-options.js
  5. 17
      test/sources/cli/Events.sol

@ -25,10 +25,11 @@ const coverageDir = './coverageEnv'; // Env that instrumented .sols are t
// Options // Options
let coverageOption = '--network coverage'; // Default truffle network execution flag let coverageOption = '--network coverage'; // Default truffle network execution flag
let silence = ''; // Default log level: configurable by --silence let silence = ''; // Default log level: configurable by --silence
let log = console.log; // Default log level: configurable by --silence let log = console.log;
let testrpcProcess; // ref to testrpc server we need to close on exit let testrpcProcess; // ref to testrpc server we need to close on exit
let events; // ref to string loaded from 'allFiredEvents' let events; // ref to string loaded from 'allFiredEvents'
let testsErrored = null; // flag set to non-null if truffle tests error
// --------------------------------------- Utilities ----------------------------------------------- // --------------------------------------- Utilities -----------------------------------------------
/** /**
@ -47,6 +48,9 @@ function cleanUp(err) {
if (err) { if (err) {
log(`${err}\nExiting without generating coverage...`); log(`${err}\nExiting without generating coverage...`);
process.exit(1); process.exit(1);
} else if (testsErrored) {
log('Some truffle tests failed while running coverage');
process.exit(1);
} else { } else {
process.exit(0); process.exit(0);
} }
@ -161,6 +165,7 @@ try {
const command = config.testCommand || defaultCommand; const command = config.testCommand || defaultCommand;
shell.cd('./coverageEnv'); shell.cd('./coverageEnv');
shell.exec(command); shell.exec(command);
testsErrored = shell.error();
shell.cd('./..'); shell.cd('./..');
} catch (err) { } catch (err) {
cleanUp(err); cleanUp(err);

@ -170,6 +170,30 @@ describe('cli', () => {
collectGarbage(); collectGarbage();
}); });
it('contract tests events: tests should pass without errors', () => {
assert(pathExists('./coverage') === false, 'should start without: coverage');
assert(pathExists('./coverage.json') === false, 'should start without: coverage.json');
mock.install('Events.sol', 'events.js', config);
shell.exec(script);
// **** DISABLED PENDING TESTRPC FILTER WORK *****
// assert(shell.error() === null, 'script should not error');
// ***********************************************
// Directory should have coverage report
assert(pathExists('./coverage') === true, 'script should gen coverage folder');
assert(pathExists('./coverage.json') === true, 'script should gen coverage.json');
// Coverage should be real.
// This test is tightly bound to the function names in Simple.sol
const produced = JSON.parse(fs.readFileSync('./coverage.json', 'utf8'));
const path = Object.keys(produced)[0];
assert(produced[path].fnMap['1'].name === 'test', 'coverage.json should map "test"');
assert(produced[path].fnMap['2'].name === 'getX', 'coverage.json should map "getX"');
collectGarbage();
});
it('contract uses inheritance: should generate coverage, cleanup & exit(0)', () => { it('contract uses inheritance: should generate coverage, cleanup & exit(0)', () => {
// Run against a contract that 'is' another contract // Run against a contract that 'is' another contract
assert(pathExists('./coverage') === false, 'should start without: coverage'); assert(pathExists('./coverage') === false, 'should start without: coverage');
@ -191,14 +215,14 @@ describe('cli', () => {
collectGarbage(); collectGarbage();
}); });
it('truffle tests failing: should generate coverage, cleanup & exit(0)', () => { it('truffle tests failing: should generate coverage, cleanup & exit(1)', () => {
assert(pathExists('./coverage') === false, 'should start without: coverage'); assert(pathExists('./coverage') === false, 'should start without: coverage');
assert(pathExists('./coverage.json') === false, 'should start without: coverage.json'); assert(pathExists('./coverage.json') === false, 'should start without: coverage.json');
// Run with Simple.sol and a failing assertion in a truffle test // Run with Simple.sol and a failing assertion in a truffle test
mock.install('Simple.sol', 'truffle-test-fail.js', config); mock.install('Simple.sol', 'truffle-test-fail.js', config);
shell.exec(script); shell.exec(script);
assert(shell.error() === null, 'script should not error'); assert(shell.error() !== null, 'script should exit 1');
assert(pathExists('./coverage') === true, 'script should gen coverage folder'); assert(pathExists('./coverage') === true, 'script should gen coverage folder');
assert(pathExists('./coverage.json') === true, 'script should gen coverage.json'); assert(pathExists('./coverage.json') === true, 'script should gen coverage.json');

@ -0,0 +1,24 @@
/* eslint-env node, mocha */
/* global artifacts, contract, assert */
const Events = artifacts.require('./Events.sol');
contract('Events', () => {
it('logs events correctly', done => {
const loggedEvents = [];
Events.deployed().then(instance => {
const allEvents = instance.allEvents();
allEvents.watch((error, event) => { loggedEvents.push(event); });
instance.test(5).then(() => {
if (loggedEvents.length > 2) {
assert(false, 'Did not filter events correctly');
} else {
assert(true);
}
done();
});
});
});
});

@ -6,9 +6,7 @@ const Simple = artifacts.require('./Simple.sol');
contract('Simple', accounts => { contract('Simple', accounts => {
// Crash truffle if the account loaded in the options string isn't found here. // Crash truffle if the account loaded in the options string isn't found here.
it('should load with expected account', () => { it('should load with expected account', () => {
if (accounts[0] !== '0xa4860cedd5143bd63f347cab453bf91425f8404f') { assert(accounts[0] === '0xa4860cedd5143bd63f347cab453bf91425f8404f');
process.exit(1);
}
}); });
// Generate some coverage so the script doesn't exit(1) because there are no events // Generate some coverage so the script doesn't exit(1) because there are no events

@ -0,0 +1,17 @@
pragma solidity ^0.4.3;
contract Events {
uint x = 0;
event LogEventOne( uint x, address y);
event LogEventTwo( uint x, address y);
function test(uint val) {
LogEventOne(100, msg.sender);
x = x + val;
LogEventTwo(200, msg.sender);
}
function getX() returns (uint){
return x;
}
}
Loading…
Cancel
Save