diff --git a/abi.inputParser.js b/abi.inputParser.js index 12b735153..edfc2b58f 100644 --- a/abi.inputParser.js +++ b/abi.inputParser.js @@ -29,7 +29,7 @@ describe('abi', function() { d[0].inputs = [ { type: "uint" } ]; - + // when var parser = abi.inputParser(d); @@ -37,7 +37,7 @@ describe('abi', function() { assert.equal(parser.test(1), "0000000000000000000000000000000000000000000000000000000000000001"); assert.equal(parser.test(10), "000000000000000000000000000000000000000000000000000000000000000a"); assert.equal( - parser.test("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"), + parser.test("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"), "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" ); assert.equal( @@ -67,7 +67,7 @@ describe('abi', function() { assert.equal(parser.test(1), "0000000000000000000000000000000000000000000000000000000000000001"); assert.equal(parser.test(10), "000000000000000000000000000000000000000000000000000000000000000a"); assert.equal( - parser.test("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"), + parser.test("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"), "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" ); assert.equal( @@ -80,7 +80,7 @@ describe('abi', function() { assert.equal(parser.test('3.9'), "0000000000000000000000000000000000000000000000000000000000000003"); }); - + it('should parse input uint256', function() { // given @@ -97,7 +97,7 @@ describe('abi', function() { assert.equal(parser.test(1), "0000000000000000000000000000000000000000000000000000000000000001"); assert.equal(parser.test(10), "000000000000000000000000000000000000000000000000000000000000000a"); assert.equal( - parser.test("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"), + parser.test("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"), "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" ); assert.equal( @@ -108,7 +108,7 @@ describe('abi', function() { assert.equal(parser.test(3.9), "0000000000000000000000000000000000000000000000000000000000000003"); assert.equal(parser.test('0.1'), "0000000000000000000000000000000000000000000000000000000000000000"); assert.equal(parser.test('3.9'), "0000000000000000000000000000000000000000000000000000000000000003"); - + }); it('should parse input int', function() { @@ -119,7 +119,7 @@ describe('abi', function() { d[0].inputs = [ { type: "int" } ]; - + // when var parser = abi.inputParser(d); @@ -130,7 +130,7 @@ describe('abi', function() { assert.equal(parser.test(-2), "fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe"); assert.equal(parser.test(-16), "fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0"); assert.equal( - parser.test("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"), + parser.test("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"), "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" ); assert.equal( @@ -162,7 +162,7 @@ describe('abi', function() { assert.equal(parser.test(-2), "fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe"); assert.equal(parser.test(-16), "fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0"); assert.equal( - parser.test("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"), + parser.test("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"), "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" ); assert.equal( @@ -177,7 +177,7 @@ describe('abi', function() { }); it('should parse input int256', function() { - + // given var d = clone(description); @@ -195,7 +195,7 @@ describe('abi', function() { assert.equal(parser.test(-2), "fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe"); assert.equal(parser.test(-16), "fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0"); assert.equal( - parser.test("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"), + parser.test("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"), "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" ); assert.equal( @@ -206,11 +206,11 @@ describe('abi', function() { assert.equal(parser.test(3.9), "0000000000000000000000000000000000000000000000000000000000000003"); assert.equal(parser.test('0.1'), "0000000000000000000000000000000000000000000000000000000000000000"); assert.equal(parser.test('3.9'), "0000000000000000000000000000000000000000000000000000000000000003"); - + }); it('should parse input bool', function() { - + // given var d = clone(description); @@ -235,14 +235,14 @@ describe('abi', function() { d[0].inputs = [ { type: "hash" } ]; - + // when var parser = abi.inputParser(d); // then assert.equal(parser.test("0x407d73d8a49eeb85d32cf465507dd71d507100c1"), "000000000000000000000000407d73d8a49eeb85d32cf465507dd71d507100c1"); - }); + }); it('should parse input hash256', function() { @@ -272,7 +272,7 @@ describe('abi', function() { // when var parser = abi.inputParser(d); - + // then assert.equal(parser.test("0x407d73d8a49eeb85d32cf465507dd71d507100c1"), "000000000000000000000000407d73d8a49eeb85d32cf465507dd71d507100c1"); }); @@ -285,17 +285,17 @@ describe('abi', function() { d[0].inputs = [ { type: "address" } ]; - + // when var parser = abi.inputParser(d) - + // then assert.equal(parser.test("0x407d73d8a49eeb85d32cf465507dd71d507100c1"), "000000000000000000000000407d73d8a49eeb85d32cf465507dd71d507100c1"); }); it('should parse input string', function () { - + // given var d = clone(description); @@ -308,8 +308,9 @@ describe('abi', function() { // then assert.equal( - parser.test('hello'), - "000000000000000000000000000000000000000000000000000000000000000568656c6c6f000000000000000000000000000000000000000000000000000000" + parser.test('hello'), + "0000000000000000000000000000000000000000000000000000000000000005" + + "68656c6c6f000000000000000000000000000000000000000000000000000000" ); assert.equal( parser.test('world'), @@ -317,8 +318,52 @@ describe('abi', function() { ); }); + it('should parse input int followed by a string', function () { + + // given + var d = clone(description); + + d[0].inputs = [ + { type: "int" }, + { type: "string" } + ]; + + // when + var parser = abi.inputParser(d); + + // then + assert.equal( + parser.test(9, 'hello'), + "0000000000000000000000000000000000000000000000000000000000000005" + + "0000000000000000000000000000000000000000000000000000000000000009" + + "68656c6c6f000000000000000000000000000000000000000000000000000000" + ); + }); + + it('should parse input string followed by an int', function () { + + // given + var d = clone(description); + + d[0].inputs = [ + { type: "string" }, + { type: "int" } + ]; + + // when + var parser = abi.inputParser(d); + + // then + assert.equal( + parser.test('hello', 9), + "0000000000000000000000000000000000000000000000000000000000000005" + + "0000000000000000000000000000000000000000000000000000000000000009" + + "68656c6c6f000000000000000000000000000000000000000000000000000000" + ); + }); + it('should use proper method name', function () { - + // given var d = clone(description); d[0].name = 'helloworld(int)'; @@ -334,9 +379,9 @@ describe('abi', function() { assert.equal(parser.helloworld['int'](1), "0000000000000000000000000000000000000000000000000000000000000001"); }); - + it('should parse multiple methods', function () { - + // given var d = [{ name: "test", @@ -356,14 +401,14 @@ describe('abi', function() { //then assert.equal(parser.test(1), "0000000000000000000000000000000000000000000000000000000000000001"); assert.equal( - parser.test2('hello'), + parser.test2('hello'), "000000000000000000000000000000000000000000000000000000000000000568656c6c6f000000000000000000000000000000000000000000000000000000" ); }); it('should parse input array of ints', function () { - + // given var d = clone(description); @@ -377,14 +422,91 @@ describe('abi', function() { // then assert.equal( parser.test([5, 6]), - "0000000000000000000000000000000000000000000000000000000000000002" + - "0000000000000000000000000000000000000000000000000000000000000005" + + "0000000000000000000000000000000000000000000000000000000000000002" + + "0000000000000000000000000000000000000000000000000000000000000005" + "0000000000000000000000000000000000000000000000000000000000000006" ); }); + it('should parse an array followed by an int', function () { + + // given + var d = clone(description); + + d[0].inputs = [ + { type: "int[]" }, + { type: "int" } + ]; + + // when + var parser = abi.inputParser(d); + + // then + assert.equal( + parser.test([5, 6], 3), + "0000000000000000000000000000000000000000000000000000000000000002" + + "0000000000000000000000000000000000000000000000000000000000000003" + + "0000000000000000000000000000000000000000000000000000000000000005" + + "0000000000000000000000000000000000000000000000000000000000000006" + ); + }); + + it('should parse an int followed by an array', function () { + + // given + var d = clone(description); + + d[0].inputs = [ + { type: "int" }, + { type: "int[]" } + ]; + + // when + var parser = abi.inputParser(d); + + // then + assert.equal( + parser.test(3, [5, 6]), + "0000000000000000000000000000000000000000000000000000000000000002" + + "0000000000000000000000000000000000000000000000000000000000000003" + + "0000000000000000000000000000000000000000000000000000000000000005" + + "0000000000000000000000000000000000000000000000000000000000000006" + ); + }); + + it('should parse mixture of arrays and ints', function () { + + // given + var d = clone(description); + + d[0].inputs = [ + { type: "int" }, + { type: "int[]" }, + { type: "int" }, + { type: "int[]" } + ]; + + // when + var parser = abi.inputParser(d); + + // then + assert.equal( + parser.test(3, [5, 6, 1, 2], 7, [8, 9]), + "0000000000000000000000000000000000000000000000000000000000000004" + + "0000000000000000000000000000000000000000000000000000000000000002" + + "0000000000000000000000000000000000000000000000000000000000000003" + + "0000000000000000000000000000000000000000000000000000000000000007" + + "0000000000000000000000000000000000000000000000000000000000000005" + + "0000000000000000000000000000000000000000000000000000000000000006" + + "0000000000000000000000000000000000000000000000000000000000000001" + + "0000000000000000000000000000000000000000000000000000000000000002" + + "0000000000000000000000000000000000000000000000000000000000000008" + + "0000000000000000000000000000000000000000000000000000000000000009" + ); + }); + it('should parse input real', function () { - + // given var d = clone(description); @@ -396,15 +518,15 @@ describe('abi', function() { var parser = abi.inputParser(d); // then - assert.equal(parser.test([1]), "0000000000000000000000000000000100000000000000000000000000000000"); - assert.equal(parser.test([2.125]), "0000000000000000000000000000000220000000000000000000000000000000"); - assert.equal(parser.test([8.5]), "0000000000000000000000000000000880000000000000000000000000000000"); - assert.equal(parser.test([-1]), "ffffffffffffffffffffffffffffffff00000000000000000000000000000000"); - + assert.equal(parser.test([1]), "0000000000000000000000000000000100000000000000000000000000000000"); + assert.equal(parser.test([2.125]), "0000000000000000000000000000000220000000000000000000000000000000"); + assert.equal(parser.test([8.5]), "0000000000000000000000000000000880000000000000000000000000000000"); + assert.equal(parser.test([-1]), "ffffffffffffffffffffffffffffffff00000000000000000000000000000000"); + }); - + it('should parse input ureal', function () { - + // given var d = clone(description); @@ -416,12 +538,11 @@ describe('abi', function() { var parser = abi.inputParser(d); // then - assert.equal(parser.test([1]), "0000000000000000000000000000000100000000000000000000000000000000"); - assert.equal(parser.test([2.125]), "0000000000000000000000000000000220000000000000000000000000000000"); - assert.equal(parser.test([8.5]), "0000000000000000000000000000000880000000000000000000000000000000"); - + assert.equal(parser.test([1]), "0000000000000000000000000000000100000000000000000000000000000000"); + assert.equal(parser.test([2.125]), "0000000000000000000000000000000220000000000000000000000000000000"); + assert.equal(parser.test([8.5]), "0000000000000000000000000000000880000000000000000000000000000000"); + }); }); }); -