web3.js/test/event.encode.js

139 lines
2.9 KiB
JavaScript
Raw Normal View History

2015-04-22 00:26:00 -07:00
var chai = require('chai');
var assert = chai.assert;
var SolidityEvent = require('../lib/web3/event');
2015-04-22 00:44:26 -07:00
var address = '0x1234567890123456789012345678901234567890';
var signature = '0xffff';
2015-04-22 00:26:00 -07:00
var tests = [{
abi: {
name: 'event1',
inputs: []
},
indexed: {},
options: {},
expected: {
2015-04-22 00:44:26 -07:00
address: address,
2015-04-22 00:26:00 -07:00
topics: [
2015-04-22 00:44:26 -07:00
signature
2015-04-22 00:26:00 -07:00
]
}
}, {
abi: {
name: 'event1',
inputs: [{
type: 'int',
name: 'a',
indexed: true
}]
},
indexed: {
a: 16
},
options: {},
expected: {
2015-04-22 00:44:26 -07:00
address: address,
2015-04-22 00:26:00 -07:00
topics: [
2015-04-22 00:44:26 -07:00
signature,
2015-04-22 00:26:00 -07:00
'0x0000000000000000000000000000000000000000000000000000000000000010'
]
}
},{
abi: {
name: 'event1',
inputs: [{
type: 'int',
name: 'a',
indexed: true
}, {
type: 'int',
name: 'b',
indexed: true
}, {
type: 'int',
name: 'c',
indexed: false
}, {
type: 'int',
name: 'd',
indexed: true
}]
},
indexed: {
b: 4
},
options: {},
expected: {
2015-04-22 00:44:26 -07:00
address: address,
2015-04-22 00:26:00 -07:00
topics: [
2015-04-22 00:44:26 -07:00
signature, // signature
2015-04-22 00:26:00 -07:00
null, // a
'0x0000000000000000000000000000000000000000000000000000000000000004', // b
null // d
]
}
2015-04-22 00:39:28 -07:00
}, {
abi: {
name: 'event1',
inputs: [{
type: 'int',
name: 'a',
indexed: true
}, {
type: 'int',
name: 'b',
indexed: true
}]
},
indexed: {
a: [16, 1],
b: 2
},
options: {},
expected: {
2015-04-22 00:44:26 -07:00
address: address,
2015-04-22 00:39:28 -07:00
topics: [
2015-04-22 00:44:26 -07:00
signature,
2015-04-22 00:39:28 -07:00
['0x0000000000000000000000000000000000000000000000000000000000000010', '0x0000000000000000000000000000000000000000000000000000000000000001'],
'0x0000000000000000000000000000000000000000000000000000000000000002'
]
}
}, {
abi: {
name: 'event1',
inputs: [{
type: 'int',
name: 'a',
indexed: true
}]
},
indexed: {
a: null
},
options: {},
expected: {
2015-04-22 00:44:26 -07:00
address: address,
2015-04-22 00:39:28 -07:00
topics: [
2015-04-22 00:44:26 -07:00
signature,
2015-04-22 00:39:28 -07:00
null
]
}
2015-04-22 00:26:00 -07:00
}];
describe('lib/web3/event', function () {
describe('encode', function () {
tests.forEach(function (test, index) {
it('test no: ' + index, function () {
2015-04-22 00:44:26 -07:00
var event = new SolidityEvent(test.abi, address);
2015-04-22 00:26:00 -07:00
event.signature = function () { // inject signature
2015-04-22 00:44:26 -07:00
return signature.slice(2);
2015-04-22 00:26:00 -07:00
};
var result = event.encode(test.indexed, test.options);
assert.deepEqual(result, test.expected);
});
});
});
});