Skip to content

Instantly share code, notes, and snippets.

@mjmau
Last active February 13, 2017 08:31
Show Gist options
  • Save mjmau/9f24ba4fd791ccd482fe9413f5fa5039 to your computer and use it in GitHub Desktop.
Save mjmau/9f24ba4fd791ccd482fe9413f5fa5039 to your computer and use it in GitHub Desktop.
pragma solidity ^0.4.8;
contract Experiment1 {
event LogDebug(uint val);
function trigger(uint[] a, bytes32 b) external {
LogDebug(a.length);
}
}
'use strict';
let Experiment1 = artifacts.require('./Experiment1.sol');
contract('Experiment1', function(accounts) {
it('should log input array of length 2', function () {
let instance;
return Experiment1.deployed().then(function (inst) {
instance = inst;
let a = [web3.sha3('one'), web3.sha3('two')];
let b = Buffer.alloc(32);
return instance.trigger(a, b, { from: accounts[0] });
}).then(function (res) {
console.log('Logs:', res.logs);
console.log('Logs[0].args:', res.logs[0].args);
console.log('Logs[0].args.val as string:', hextostr(res.logs[0].args.val.toString(16)));
assert.equal(res.logs[0].args.val.toString(16), '2', 'should have logged array length 2');
});
});
});
function hextobin(h) { return Buffer.from(h.replace('0x', ''), 'hex'); }
function hextostr(h) { return hextobin(h).toString('utf8'); }
$ truffle test
Using network 'development'.
Contract: Experiment1
Logs: [ { logIndex: 0,
transactionIndex: 0,
transactionHash: '0xc5f145993081ed979c54396b319a838c028cb0b02c0b0a3cbca4b76d9cba23bd',
blockHash: '0x9ccaf9737df1a75108474f2334954eb5febf9d6905a7d4944e261325350779e4',
blockNumber: 647,
address: '0xd254267d3472e90b7e18bc10aa30bad4664619ab',
type: 'mined',
event: 'LogDebug',
args: { val: [Object] } } ]
Logs[0].args: { val:
{ [String: '1.9986878972343340240046310863176586764876631823265828342326149237773911993392e+76']
s: 1,
e: 76,
c:
[ 1998687,
89723433402400,
46310863176586,
76487663182326,
58283423261492,
37773911993392 ] } }
Logs[0].args.val as string: ,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
1) should log input array of length 2
Events emitted during test:
---------------------------
---------------------------
0 passing (77ms)
1 failing
1) Contract: Experiment1 should log input array of length 2:
AssertionError: should have logged array length 2: expected '2c302c302c302c302c302c302c302c302c302c302c302c302c302c302c302c30' to equal '2'
at test/TestExperiment1.js:20:14
at process._tickDomainCallback (internal/process/next_tick.js:129:7)
let Experiment1 = artifacts.require("./Experiment1.sol");
module.exports = function(deployer) {
deployer.deploy(Experiment1);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment