web3.js/test/coder.encodeParam.js

144 lines
15 KiB
JavaScript
Raw Normal View History

var chai = require('chai');
var assert = chai.assert;
var coder = require('../lib/solidity/coder');
describe('lib/solidity/coder', function () {
describe('encodeParam', function () {
2015-04-25 23:34:00 -07:00
var test = function (t) {
it('should turn ' + t.value + ' to ' + t.expected, function () {
assert.equal(coder.encodeParam(t.type, t.value), t.expected);
});
2015-04-25 23:34:00 -07:00
};
test({ type: 'int', value: 1, expected: '0000000000000000000000000000000000000000000000000000000000000001'});
test({ type: 'int', value: 16, expected: '0000000000000000000000000000000000000000000000000000000000000010'});
test({ type: 'int', value: -1, expected: 'ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff'});
test({ type: 'int', value: 0.1, expected: '0000000000000000000000000000000000000000000000000000000000000000'});
test({ type: 'int', value: 3.9, expected: '0000000000000000000000000000000000000000000000000000000000000003'});
2015-04-25 23:34:00 -07:00
test({ type: 'int256', value: 1, expected: '0000000000000000000000000000000000000000000000000000000000000001'});
test({ type: 'int256', value: 16, expected: '0000000000000000000000000000000000000000000000000000000000000010'});
test({ type: 'int256', value: -1, expected: 'ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff'});
test({ type: 'bytes32', value: 'gavofyork', expected: '6761766f66796f726b0000000000000000000000000000000000000000000000'});
test({ type: 'bytes', value: 'gavofyork', expected: '0000000000000000000000000000000000000000000000000000000000000020' +
'0000000000000000000000000000000000000000000000000000000000000009' +
2015-04-25 23:34:00 -07:00
'6761766f66796f726b0000000000000000000000000000000000000000000000'});
test({ type: 'int[]', value: [3], expected: '0000000000000000000000000000000000000000000000000000000000000020' +
'0000000000000000000000000000000000000000000000000000000000000001' +
2015-04-25 23:34:00 -07:00
'0000000000000000000000000000000000000000000000000000000000000003'});
test({ type: 'int256[]', value: [3], expected: '0000000000000000000000000000000000000000000000000000000000000020' +
'0000000000000000000000000000000000000000000000000000000000000001' +
2015-04-25 23:34:00 -07:00
'0000000000000000000000000000000000000000000000000000000000000003'});
test({ type: 'int[]', value: [1,2,3], expected: '0000000000000000000000000000000000000000000000000000000000000020' +
'0000000000000000000000000000000000000000000000000000000000000003' +
2015-04-25 23:55:08 -07:00
'0000000000000000000000000000000000000000000000000000000000000001' +
'0000000000000000000000000000000000000000000000000000000000000002' +
'0000000000000000000000000000000000000000000000000000000000000003'});
test({ type: 'bool', value: true, expected: '0000000000000000000000000000000000000000000000000000000000000001'});
test({ type: 'bool', value: false, expected: '0000000000000000000000000000000000000000000000000000000000000000'});
test({ type: 'address', value: '0x407d73d8a49eeb85d32cf465507dd71d507100c1',
expected: '000000000000000000000000407d73d8a49eeb85d32cf465507dd71d507100c1'});
test({ type: 'real', value: 1, expected: '0000000000000000000000000000000100000000000000000000000000000000'});
test({ type: 'real', value: 2.125, expected: '0000000000000000000000000000000220000000000000000000000000000000'});
test({ type: 'real', value: 8.5, expected: '0000000000000000000000000000000880000000000000000000000000000000'});
test({ type: 'real', value: -1, expected: 'ffffffffffffffffffffffffffffffff00000000000000000000000000000000'});
test({ type: 'ureal', value: 1, expected: '0000000000000000000000000000000100000000000000000000000000000000'});
test({ type: 'ureal', value: 2.125, expected: '0000000000000000000000000000000220000000000000000000000000000000'});
test({ type: 'ureal', value: 8.5, expected: '0000000000000000000000000000000880000000000000000000000000000000'});
2015-04-25 23:34:00 -07:00
});
});
describe('lib/solidity/coder', function () {
describe('encodeParams', function () {
var test = function (t) {
2015-04-26 01:51:32 -07:00
it('should turn ' + t.values + ' to ' + t.expected, function () {
2015-04-25 23:34:00 -07:00
assert.equal(coder.encodeParams(t.types, t.values), t.expected);
});
};
test({ types: ['int'], values: [1], expected: '0000000000000000000000000000000000000000000000000000000000000001'});
test({ types: ['int'], values: [16], expected: '0000000000000000000000000000000000000000000000000000000000000010'});
test({ types: ['int'], values: [-1], expected: 'ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff'});
test({ types: ['int256'], values: [1], expected: '0000000000000000000000000000000000000000000000000000000000000001'});
test({ types: ['int256'], values: [16], expected: '0000000000000000000000000000000000000000000000000000000000000010'});
test({ types: ['int256'], values: [-1], expected: 'ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff'});
test({ types: ['bytes32'], values: ['gavofyork'], expected: '6761766f66796f726b0000000000000000000000000000000000000000000000'});
test({ types: ['bytes'], values: ['gavofyork'], expected: '0000000000000000000000000000000000000000000000000000000000000020' +
'0000000000000000000000000000000000000000000000000000000000000009' +
2015-04-25 23:34:00 -07:00
'6761766f66796f726b0000000000000000000000000000000000000000000000'});
test({ types: ['int[]'], values: [[3]], expected: '0000000000000000000000000000000000000000000000000000000000000020' +
'0000000000000000000000000000000000000000000000000000000000000001' +
2015-04-25 23:34:00 -07:00
'0000000000000000000000000000000000000000000000000000000000000003'});
test({ types: ['int256[]'], values: [[3]], expected: '0000000000000000000000000000000000000000000000000000000000000020' +
'0000000000000000000000000000000000000000000000000000000000000001' +
2015-04-25 23:34:00 -07:00
'0000000000000000000000000000000000000000000000000000000000000003'});
test({ types: ['int256[]'], values: [[1,2,3]], expected: '0000000000000000000000000000000000000000000000000000000000000020' +
'0000000000000000000000000000000000000000000000000000000000000003' +
2015-04-25 23:34:00 -07:00
'0000000000000000000000000000000000000000000000000000000000000001' +
'0000000000000000000000000000000000000000000000000000000000000002' +
'0000000000000000000000000000000000000000000000000000000000000003'});
test({ types: ['int[]', 'int[]'], values: [[1,2], [3,4]],
expected: '0000000000000000000000000000000000000000000000000000000000000040' +
'00000000000000000000000000000000000000000000000000000000000000a0' +
'0000000000000000000000000000000000000000000000000000000000000002' +
'0000000000000000000000000000000000000000000000000000000000000001' +
'0000000000000000000000000000000000000000000000000000000000000002' +
'0000000000000000000000000000000000000000000000000000000000000002' +
'0000000000000000000000000000000000000000000000000000000000000003' +
'0000000000000000000000000000000000000000000000000000000000000004'});
2015-04-25 23:34:00 -07:00
test({ types: ['bytes32', 'int'], values: ['gavofyork', 5],
expected: '6761766f66796f726b0000000000000000000000000000000000000000000000' +
'0000000000000000000000000000000000000000000000000000000000000005'});
test({ types: ['int', 'bytes32'], values: [5, 'gavofyork'],
expected: '0000000000000000000000000000000000000000000000000000000000000005' +
'6761766f66796f726b0000000000000000000000000000000000000000000000'});
test({ types: ['bytes', 'int'], values: ['gavofyork', 5],
expected: '0000000000000000000000000000000000000000000000000000000000000040' +
'0000000000000000000000000000000000000000000000000000000000000005' +
'0000000000000000000000000000000000000000000000000000000000000009' +
'6761766f66796f726b0000000000000000000000000000000000000000000000'});
2015-05-08 00:59:19 -07:00
test({ types: ['bytes', 'bool', 'int[]'], values: ['gavofyork', true, [1, 2, 3]],
expected: '0000000000000000000000000000000000000000000000000000000000000060' +
2015-05-08 00:59:19 -07:00
'0000000000000000000000000000000000000000000000000000000000000001' +
'00000000000000000000000000000000000000000000000000000000000000a0' +
'0000000000000000000000000000000000000000000000000000000000000009' +
'6761766f66796f726b0000000000000000000000000000000000000000000000' +
'0000000000000000000000000000000000000000000000000000000000000003' +
2015-05-08 00:59:19 -07:00
'0000000000000000000000000000000000000000000000000000000000000001' +
'0000000000000000000000000000000000000000000000000000000000000002' +
'0000000000000000000000000000000000000000000000000000000000000003'});
2015-05-07 11:46:58 -07:00
test({ types: ['bytes', 'int[]'], values: ['gavofyork', [1, 2, 3]],
expected: '0000000000000000000000000000000000000000000000000000000000000040' +
'0000000000000000000000000000000000000000000000000000000000000080' +
'0000000000000000000000000000000000000000000000000000000000000009' +
2015-05-07 11:46:58 -07:00
'6761766f66796f726b0000000000000000000000000000000000000000000000' +
'0000000000000000000000000000000000000000000000000000000000000003' +
2015-05-07 11:46:58 -07:00
'0000000000000000000000000000000000000000000000000000000000000001' +
'0000000000000000000000000000000000000000000000000000000000000002' +
'0000000000000000000000000000000000000000000000000000000000000003'});
2015-04-25 23:34:00 -07:00
test({ types: ['int', 'bytes'], values: [5, 'gavofyork'],
expected: '0000000000000000000000000000000000000000000000000000000000000005' +
'0000000000000000000000000000000000000000000000000000000000000040' +
'0000000000000000000000000000000000000000000000000000000000000009' +
2015-04-25 23:34:00 -07:00
'6761766f66796f726b0000000000000000000000000000000000000000000000'});
test({ types: ['int', 'bytes', 'int', 'int', 'int', 'int[]'], values: [1, 'gavofyork', 2, 3, 4, [5, 6, 7]],
expected: '0000000000000000000000000000000000000000000000000000000000000001' +
'00000000000000000000000000000000000000000000000000000000000000c0' +
2015-04-25 23:34:00 -07:00
'0000000000000000000000000000000000000000000000000000000000000002' +
'0000000000000000000000000000000000000000000000000000000000000003' +
'0000000000000000000000000000000000000000000000000000000000000004' +
'0000000000000000000000000000000000000000000000000000000000000100' +
'0000000000000000000000000000000000000000000000000000000000000009' +
'6761766f66796f726b0000000000000000000000000000000000000000000000' +
'0000000000000000000000000000000000000000000000000000000000000003' +
2015-04-25 23:34:00 -07:00
'0000000000000000000000000000000000000000000000000000000000000005' +
'0000000000000000000000000000000000000000000000000000000000000006' +
'0000000000000000000000000000000000000000000000000000000000000007'});
});
});
2015-04-25 23:34:00 -07:00