github.com/jonasnick/go-ethereum@v0.7.12-0.20150216215225-22176f05d387/cmd/mist/assets/ext/ethereum.js/test/abi.parsers.js (about) 1 var assert = require('assert'); 2 var BigNumber = require('bignumber.js'); 3 var abi = require('../lib/abi.js'); 4 var clone = function (object) { return JSON.parse(JSON.stringify(object)); }; 5 6 var description = [{ 7 "name": "test", 8 "inputs": [{ 9 "name": "a", 10 "type": "uint256" 11 } 12 ], 13 "outputs": [ 14 { 15 "name": "d", 16 "type": "uint256" 17 } 18 ] 19 }]; 20 21 describe('abi', function() { 22 describe('inputParser', function() { 23 it('should parse input uint', function() { 24 25 // given 26 var d = clone(description); 27 28 d[0].inputs = [ 29 { type: "uint" } 30 ]; 31 32 // when 33 var parser = abi.inputParser(d); 34 35 // then 36 assert.equal(parser.test(1), "0000000000000000000000000000000000000000000000000000000000000001"); 37 assert.equal(parser.test(10), "000000000000000000000000000000000000000000000000000000000000000a"); 38 assert.equal( 39 parser.test("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"), 40 "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" 41 ); 42 assert.equal( 43 parser.test(new BigNumber("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", 16)), 44 "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" 45 ); 46 assert.equal(parser.test(0.1), "0000000000000000000000000000000000000000000000000000000000000000"); 47 assert.equal(parser.test(3.9), "0000000000000000000000000000000000000000000000000000000000000003"); 48 assert.equal(parser.test('0.1'), "0000000000000000000000000000000000000000000000000000000000000000"); 49 assert.equal(parser.test('3.9'), "0000000000000000000000000000000000000000000000000000000000000003"); 50 51 }); 52 53 it('should parse input uint128', function() { 54 55 // given 56 var d = clone(description); 57 58 d[0].inputs = [ 59 { type: "uint128" } 60 ]; 61 62 // when 63 var parser = abi.inputParser(d); 64 65 // then 66 assert.equal(parser.test(1), "0000000000000000000000000000000000000000000000000000000000000001"); 67 assert.equal(parser.test(10), "000000000000000000000000000000000000000000000000000000000000000a"); 68 assert.equal( 69 parser.test("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"), 70 "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" 71 ); 72 assert.equal( 73 parser.test(new BigNumber("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", 16)), 74 "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" 75 ); 76 assert.equal(parser.test(0.1), "0000000000000000000000000000000000000000000000000000000000000000"); 77 assert.equal(parser.test(3.9), "0000000000000000000000000000000000000000000000000000000000000003"); 78 assert.equal(parser.test('0.1'), "0000000000000000000000000000000000000000000000000000000000000000"); 79 assert.equal(parser.test('3.9'), "0000000000000000000000000000000000000000000000000000000000000003"); 80 81 }); 82 83 it('should parse input uint256', function() { 84 85 // given 86 var d = clone(description); 87 88 d[0].inputs = [ 89 { type: "uint256" } 90 ]; 91 92 // when 93 var parser = abi.inputParser(d); 94 95 // then 96 assert.equal(parser.test(1), "0000000000000000000000000000000000000000000000000000000000000001"); 97 assert.equal(parser.test(10), "000000000000000000000000000000000000000000000000000000000000000a"); 98 assert.equal( 99 parser.test("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"), 100 "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" 101 ); 102 assert.equal( 103 parser.test(new BigNumber("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", 16)), 104 "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" 105 ); 106 assert.equal(parser.test(0.1), "0000000000000000000000000000000000000000000000000000000000000000"); 107 assert.equal(parser.test(3.9), "0000000000000000000000000000000000000000000000000000000000000003"); 108 assert.equal(parser.test('0.1'), "0000000000000000000000000000000000000000000000000000000000000000"); 109 assert.equal(parser.test('3.9'), "0000000000000000000000000000000000000000000000000000000000000003"); 110 111 }); 112 113 it('should parse input int', function() { 114 115 // given 116 var d = clone(description); 117 118 d[0].inputs = [ 119 { type: "int" } 120 ]; 121 122 // when 123 var parser = abi.inputParser(d); 124 125 // then 126 assert.equal(parser.test(1), "0000000000000000000000000000000000000000000000000000000000000001"); 127 assert.equal(parser.test(10), "000000000000000000000000000000000000000000000000000000000000000a"); 128 assert.equal(parser.test(-1), "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"); 129 assert.equal(parser.test(-2), "fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe"); 130 assert.equal(parser.test(-16), "fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0"); 131 assert.equal( 132 parser.test("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"), 133 "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" 134 ); 135 assert.equal( 136 parser.test(new BigNumber("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", 16)), 137 "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" 138 ); 139 assert.equal(parser.test(0.1), "0000000000000000000000000000000000000000000000000000000000000000"); 140 assert.equal(parser.test(3.9), "0000000000000000000000000000000000000000000000000000000000000003"); 141 assert.equal(parser.test('0.1'), "0000000000000000000000000000000000000000000000000000000000000000"); 142 assert.equal(parser.test('3.9'), "0000000000000000000000000000000000000000000000000000000000000003"); 143 }); 144 145 it('should parse input int128', function() { 146 147 // given 148 var d = clone(description); 149 150 d[0].inputs = [ 151 { type: "int128" } 152 ]; 153 154 // when 155 var parser = abi.inputParser(d); 156 157 // then 158 assert.equal(parser.test(1), "0000000000000000000000000000000000000000000000000000000000000001"); 159 assert.equal(parser.test(10), "000000000000000000000000000000000000000000000000000000000000000a"); 160 assert.equal(parser.test(-1), "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"); 161 assert.equal(parser.test(-2), "fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe"); 162 assert.equal(parser.test(-16), "fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0"); 163 assert.equal( 164 parser.test("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"), 165 "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" 166 ); 167 assert.equal( 168 parser.test(new BigNumber("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", 16)), 169 "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" 170 ); 171 assert.equal(parser.test(0.1), "0000000000000000000000000000000000000000000000000000000000000000"); 172 assert.equal(parser.test(3.9), "0000000000000000000000000000000000000000000000000000000000000003"); 173 assert.equal(parser.test('0.1'), "0000000000000000000000000000000000000000000000000000000000000000"); 174 assert.equal(parser.test('3.9'), "0000000000000000000000000000000000000000000000000000000000000003"); 175 176 }); 177 178 it('should parse input int256', function() { 179 180 // given 181 var d = clone(description); 182 183 d[0].inputs = [ 184 { type: "int256" } 185 ]; 186 187 // when 188 var parser = abi.inputParser(d); 189 190 // then 191 assert.equal(parser.test(1), "0000000000000000000000000000000000000000000000000000000000000001"); 192 assert.equal(parser.test(10), "000000000000000000000000000000000000000000000000000000000000000a"); 193 assert.equal(parser.test(-1), "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"); 194 assert.equal(parser.test(-2), "fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe"); 195 assert.equal(parser.test(-16), "fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0"); 196 assert.equal( 197 parser.test("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"), 198 "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" 199 ); 200 assert.equal( 201 parser.test(new BigNumber("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", 16)), 202 "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" 203 ); 204 assert.equal(parser.test(0.1), "0000000000000000000000000000000000000000000000000000000000000000"); 205 assert.equal(parser.test(3.9), "0000000000000000000000000000000000000000000000000000000000000003"); 206 assert.equal(parser.test('0.1'), "0000000000000000000000000000000000000000000000000000000000000000"); 207 assert.equal(parser.test('3.9'), "0000000000000000000000000000000000000000000000000000000000000003"); 208 209 }); 210 211 it('should parse input bool', function() { 212 213 // given 214 var d = clone(description); 215 216 d[0].inputs = [ 217 { type: 'bool' } 218 ]; 219 220 // when 221 var parser = abi.inputParser(d); 222 223 // then 224 assert.equal(parser.test(true), "0000000000000000000000000000000000000000000000000000000000000001"); 225 assert.equal(parser.test(false), "0000000000000000000000000000000000000000000000000000000000000000"); 226 227 }); 228 229 it('should parse input hash', function() { 230 231 // given 232 var d = clone(description); 233 234 d[0].inputs = [ 235 { type: "hash" } 236 ]; 237 238 // when 239 var parser = abi.inputParser(d); 240 241 // then 242 assert.equal(parser.test("0x407d73d8a49eeb85d32cf465507dd71d507100c1"), "000000000000000000000000407d73d8a49eeb85d32cf465507dd71d507100c1"); 243 244 }); 245 246 it('should parse input hash256', function() { 247 248 // given 249 var d = clone(description); 250 251 d[0].inputs = [ 252 { type: "hash256" } 253 ]; 254 255 // when 256 var parser = abi.inputParser(d); 257 258 // then 259 assert.equal(parser.test("0x407d73d8a49eeb85d32cf465507dd71d507100c1"), "000000000000000000000000407d73d8a49eeb85d32cf465507dd71d507100c1"); 260 261 }); 262 263 264 it('should parse input hash160', function() { 265 // given 266 var d = clone(description); 267 268 d[0].inputs = [ 269 { type: "hash160" } 270 ]; 271 272 // when 273 var parser = abi.inputParser(d); 274 275 // then 276 assert.equal(parser.test("0x407d73d8a49eeb85d32cf465507dd71d507100c1"), "000000000000000000000000407d73d8a49eeb85d32cf465507dd71d507100c1"); 277 }); 278 279 it('should parse input address', function () { 280 281 // given 282 var d = clone(description); 283 284 d[0].inputs = [ 285 { type: "address" } 286 ]; 287 288 // when 289 var parser = abi.inputParser(d) 290 291 // then 292 assert.equal(parser.test("0x407d73d8a49eeb85d32cf465507dd71d507100c1"), "000000000000000000000000407d73d8a49eeb85d32cf465507dd71d507100c1"); 293 294 }); 295 296 it('should parse input string', function () { 297 298 // given 299 var d = clone(description); 300 301 d[0].inputs = [ 302 { type: "string" } 303 ]; 304 305 // when 306 var parser = abi.inputParser(d); 307 308 // then 309 assert.equal( 310 parser.test('hello'), 311 "000000000000000000000000000000000000000000000000000000000000000568656c6c6f000000000000000000000000000000000000000000000000000000" 312 ); 313 assert.equal( 314 parser.test('world'), 315 "0000000000000000000000000000000000000000000000000000000000000005776f726c64000000000000000000000000000000000000000000000000000000" 316 ); 317 }); 318 319 it('should use proper method name', function () { 320 321 // given 322 var d = clone(description); 323 d[0].name = 'helloworld(int)'; 324 d[0].inputs = [ 325 { type: "int" } 326 ]; 327 328 // when 329 var parser = abi.inputParser(d); 330 331 // then 332 assert.equal(parser.helloworld(1), "0000000000000000000000000000000000000000000000000000000000000001"); 333 assert.equal(parser.helloworld['int'](1), "0000000000000000000000000000000000000000000000000000000000000001"); 334 335 }); 336 337 it('should parse multiple methods', function () { 338 339 // given 340 var d = [{ 341 name: "test", 342 inputs: [{ type: "int" }], 343 outputs: [{ type: "int" }] 344 },{ 345 name: "test2", 346 inputs: [{ type: "string" }], 347 outputs: [{ type: "string" }] 348 }]; 349 350 // when 351 var parser = abi.inputParser(d); 352 353 //then 354 assert.equal(parser.test(1), "0000000000000000000000000000000000000000000000000000000000000001"); 355 assert.equal( 356 parser.test2('hello'), 357 "000000000000000000000000000000000000000000000000000000000000000568656c6c6f000000000000000000000000000000000000000000000000000000" 358 ); 359 360 }); 361 362 it('should parse input array of ints', function () { 363 364 // given 365 var d = clone(description); 366 367 d[0].inputs = [ 368 { type: "int[]" } 369 ]; 370 371 // when 372 var parser = abi.inputParser(d); 373 374 // then 375 assert.equal( 376 parser.test([5, 6]), 377 "0000000000000000000000000000000000000000000000000000000000000002" + 378 "0000000000000000000000000000000000000000000000000000000000000005" + 379 "0000000000000000000000000000000000000000000000000000000000000006" 380 ); 381 }); 382 383 it('should parse input real', function () { 384 385 // given 386 var d = clone(description); 387 388 d[0].inputs = [ 389 { type: 'real' } 390 ]; 391 392 // when 393 var parser = abi.inputParser(d); 394 395 // then 396 assert.equal(parser.test([1]), "0000000000000000000000000000000100000000000000000000000000000000"); 397 assert.equal(parser.test([2.125]), "0000000000000000000000000000000220000000000000000000000000000000"); 398 assert.equal(parser.test([8.5]), "0000000000000000000000000000000880000000000000000000000000000000"); 399 assert.equal(parser.test([-1]), "ffffffffffffffffffffffffffffffff00000000000000000000000000000000"); 400 401 }); 402 403 it('should parse input ureal', function () { 404 405 // given 406 var d = clone(description); 407 408 d[0].inputs = [ 409 { type: 'ureal' } 410 ]; 411 412 // when 413 var parser = abi.inputParser(d); 414 415 // then 416 assert.equal(parser.test([1]), "0000000000000000000000000000000100000000000000000000000000000000"); 417 assert.equal(parser.test([2.125]), "0000000000000000000000000000000220000000000000000000000000000000"); 418 assert.equal(parser.test([8.5]), "0000000000000000000000000000000880000000000000000000000000000000"); 419 420 }); 421 422 }); 423 424 describe('outputParser', function() { 425 it('should parse output string', function() { 426 427 // given 428 var d = clone(description); 429 430 d[0].outputs = [ 431 { type: "string" } 432 ]; 433 434 // when 435 var parser = abi.outputParser(d); 436 437 // then 438 assert.equal( 439 parser.test("0x" + 440 "0000000000000000000000000000000000000000000000000000000000000005" + 441 "68656c6c6f000000000000000000000000000000000000000000000000000000")[0], 442 'hello' 443 ); 444 assert.equal( 445 parser.test("0x" + 446 "0000000000000000000000000000000000000000000000000000000000000005" + 447 "776f726c64000000000000000000000000000000000000000000000000000000")[0], 448 'world' 449 ); 450 451 }); 452 453 it('should parse output uint', function() { 454 455 // given 456 var d = clone(description); 457 458 d[0].outputs = [ 459 { type: 'uint' } 460 ]; 461 462 // when 463 var parser = abi.outputParser(d); 464 465 // then 466 assert.equal(parser.test("0x0000000000000000000000000000000000000000000000000000000000000001")[0], 1); 467 assert.equal(parser.test("0x000000000000000000000000000000000000000000000000000000000000000a")[0], 10); 468 assert.equal( 469 parser.test("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")[0].toString(10), 470 new BigNumber("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", 16).toString(10) 471 ); 472 assert.equal( 473 parser.test("0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0")[0].toString(10), 474 new BigNumber("fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0", 16).toString(10) 475 ); 476 }); 477 478 it('should parse output uint256', function() { 479 480 // given 481 var d = clone(description); 482 483 d[0].outputs = [ 484 { type: 'uint256' } 485 ]; 486 487 // when 488 var parser = abi.outputParser(d); 489 490 // then 491 assert.equal(parser.test("0x0000000000000000000000000000000000000000000000000000000000000001")[0], 1); 492 assert.equal(parser.test("0x000000000000000000000000000000000000000000000000000000000000000a")[0], 10); 493 assert.equal( 494 parser.test("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")[0].toString(10), 495 new BigNumber("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", 16).toString(10) 496 ); 497 assert.equal( 498 parser.test("0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0")[0].toString(10), 499 new BigNumber("fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0", 16).toString(10) 500 ); 501 }); 502 503 it('should parse output uint128', function() { 504 505 // given 506 var d = clone(description); 507 508 d[0].outputs = [ 509 { type: 'uint128' } 510 ]; 511 512 // when 513 var parser = abi.outputParser(d); 514 515 // then 516 assert.equal(parser.test("0x0000000000000000000000000000000000000000000000000000000000000001")[0], 1); 517 assert.equal(parser.test("0x000000000000000000000000000000000000000000000000000000000000000a")[0], 10); 518 assert.equal( 519 parser.test("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")[0].toString(10), 520 new BigNumber("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", 16).toString(10) 521 ); 522 assert.equal( 523 parser.test("0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0")[0].toString(10), 524 new BigNumber("fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0", 16).toString(10) 525 ); 526 }); 527 528 it('should parse output int', function() { 529 530 // given 531 var d = clone(description); 532 533 d[0].outputs = [ 534 { type: 'int' } 535 ]; 536 537 // when 538 var parser = abi.outputParser(d); 539 540 // then 541 assert.equal(parser.test("0x0000000000000000000000000000000000000000000000000000000000000001")[0], 1); 542 assert.equal(parser.test("0x000000000000000000000000000000000000000000000000000000000000000a")[0], 10); 543 assert.equal(parser.test("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")[0], -1); 544 assert.equal(parser.test("0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0")[0], -16); 545 }); 546 547 it('should parse output int256', function() { 548 549 // given 550 var d = clone(description); 551 552 d[0].outputs = [ 553 { type: 'int256' } 554 ]; 555 556 // when 557 var parser = abi.outputParser(d); 558 559 // then 560 assert.equal(parser.test("0x0000000000000000000000000000000000000000000000000000000000000001")[0], 1); 561 assert.equal(parser.test("0x000000000000000000000000000000000000000000000000000000000000000a")[0], 10); 562 assert.equal(parser.test("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")[0], -1); 563 assert.equal(parser.test("0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0")[0], -16); 564 }); 565 566 it('should parse output int128', function() { 567 568 // given 569 var d = clone(description); 570 571 d[0].outputs = [ 572 { type: 'int128' } 573 ]; 574 575 // when 576 var parser = abi.outputParser(d); 577 578 // then 579 assert.equal(parser.test("0x0000000000000000000000000000000000000000000000000000000000000001")[0], 1); 580 assert.equal(parser.test("0x000000000000000000000000000000000000000000000000000000000000000a")[0], 10); 581 assert.equal(parser.test("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")[0], -1); 582 assert.equal(parser.test("0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0")[0], -16); 583 }); 584 585 it('should parse output hash', function() { 586 587 // given 588 var d = clone(description); 589 590 d[0].outputs = [ 591 { type: 'hash' } 592 ]; 593 594 // when 595 var parser = abi.outputParser(d); 596 597 // then 598 assert.equal( 599 parser.test("0x000000000000000000000000407d73d8a49eeb85d32cf465507dd71d507100c1")[0], 600 "0x000000000000000000000000407d73d8a49eeb85d32cf465507dd71d507100c1" 601 ); 602 }); 603 604 it('should parse output hash256', function() { 605 606 // given 607 var d = clone(description); 608 609 d[0].outputs = [ 610 { type: 'hash256' } 611 ]; 612 613 // when 614 var parser = abi.outputParser(d); 615 616 // then 617 assert.equal( 618 parser.test("0x000000000000000000000000407d73d8a49eeb85d32cf465507dd71d507100c1")[0], 619 "0x000000000000000000000000407d73d8a49eeb85d32cf465507dd71d507100c1" 620 ); 621 }); 622 623 it('should parse output hash160', function() { 624 625 // given 626 var d = clone(description); 627 628 d[0].outputs = [ 629 { type: 'hash160' } 630 ]; 631 632 // when 633 var parser = abi.outputParser(d); 634 635 // then 636 assert.equal( 637 parser.test("0x000000000000000000000000407d73d8a49eeb85d32cf465507dd71d507100c1")[0], 638 "0x000000000000000000000000407d73d8a49eeb85d32cf465507dd71d507100c1" 639 ); 640 // TODO shouldnt' the expected hash be shorter? 641 }); 642 643 it('should parse output address', function() { 644 645 // given 646 var d = clone(description); 647 648 d[0].outputs = [ 649 { type: 'address' } 650 ]; 651 652 // when 653 var parser = abi.outputParser(d); 654 655 // then 656 assert.equal( 657 parser.test("0x000000000000000000000000407d73d8a49eeb85d32cf465507dd71d507100c1")[0], 658 "0x407d73d8a49eeb85d32cf465507dd71d507100c1" 659 ); 660 }); 661 662 it('should parse output bool', function() { 663 664 // given 665 var d = clone(description); 666 667 d[0].outputs = [ 668 { type: 'bool' } 669 ]; 670 671 // when 672 var parser = abi.outputParser(d); 673 674 // then 675 assert.equal(parser.test("0x0000000000000000000000000000000000000000000000000000000000000001")[0], true); 676 assert.equal(parser.test("0x0000000000000000000000000000000000000000000000000000000000000000")[0], false); 677 678 679 }); 680 681 it('should parse output real', function() { 682 683 // given 684 var d = clone(description); 685 686 d[0].outputs = [ 687 { type: 'real' } 688 ]; 689 690 // when 691 var parser = abi.outputParser(d); 692 693 // then 694 assert.equal(parser.test("0x0000000000000000000000000000000100000000000000000000000000000000")[0], 1); 695 assert.equal(parser.test("0x0000000000000000000000000000000220000000000000000000000000000000")[0], 2.125); 696 assert.equal(parser.test("0x0000000000000000000000000000000880000000000000000000000000000000")[0], 8.5); 697 assert.equal(parser.test("0xffffffffffffffffffffffffffffffff00000000000000000000000000000000")[0], -1); 698 699 }); 700 701 it('should parse output ureal', function() { 702 703 // given 704 var d = clone(description); 705 706 d[0].outputs = [ 707 { type: 'ureal' } 708 ]; 709 710 // when 711 var parser = abi.outputParser(d); 712 713 // then 714 assert.equal(parser.test("0x0000000000000000000000000000000100000000000000000000000000000000")[0], 1); 715 assert.equal(parser.test("0x0000000000000000000000000000000220000000000000000000000000000000")[0], 2.125); 716 assert.equal(parser.test("0x0000000000000000000000000000000880000000000000000000000000000000")[0], 8.5); 717 718 }); 719 720 721 it('should parse multiple output strings', function() { 722 723 // given 724 var d = clone(description); 725 726 d[0].outputs = [ 727 { type: "string" }, 728 { type: "string" } 729 ]; 730 731 // when 732 var parser = abi.outputParser(d); 733 734 // then 735 assert.equal( 736 parser.test("0x" + 737 "0000000000000000000000000000000000000000000000000000000000000005" + 738 "0000000000000000000000000000000000000000000000000000000000000005" + 739 "68656c6c6f000000000000000000000000000000000000000000000000000000" + 740 "776f726c64000000000000000000000000000000000000000000000000000000")[0], 741 'hello' 742 ); 743 assert.equal( 744 parser.test("0x" + 745 "0000000000000000000000000000000000000000000000000000000000000005" + 746 "0000000000000000000000000000000000000000000000000000000000000005" + 747 "68656c6c6f000000000000000000000000000000000000000000000000000000" + 748 "776f726c64000000000000000000000000000000000000000000000000000000")[1], 749 'world' 750 ); 751 752 }); 753 754 it('should use proper method name', function () { 755 756 // given 757 var d = clone(description); 758 d[0].name = 'helloworld(int)'; 759 d[0].outputs = [ 760 { type: "int" } 761 ]; 762 763 // when 764 var parser = abi.outputParser(d); 765 766 // then 767 assert.equal(parser.helloworld("0x0000000000000000000000000000000000000000000000000000000000000001")[0], 1); 768 assert.equal(parser.helloworld['int']("0x0000000000000000000000000000000000000000000000000000000000000001")[0], 1); 769 770 }); 771 772 773 it('should parse multiple methods', function () { 774 775 // given 776 var d = [{ 777 name: "test", 778 inputs: [{ type: "int" }], 779 outputs: [{ type: "int" }] 780 },{ 781 name: "test2", 782 inputs: [{ type: "string" }], 783 outputs: [{ type: "string" }] 784 }]; 785 786 // when 787 var parser = abi.outputParser(d); 788 789 //then 790 assert.equal(parser.test("0000000000000000000000000000000000000000000000000000000000000001")[0], 1); 791 assert.equal(parser.test2("0x" + 792 "0000000000000000000000000000000000000000000000000000000000000005" + 793 "68656c6c6f000000000000000000000000000000000000000000000000000000")[0], 794 "hello" 795 ); 796 797 }); 798 799 it('should parse output array', function () { 800 801 // given 802 var d = clone(description); 803 d[0].outputs = [ 804 { type: 'int[]' } 805 ]; 806 807 // when 808 var parser = abi.outputParser(d); 809 810 // then 811 assert.equal(parser.test("0x" + 812 "0000000000000000000000000000000000000000000000000000000000000002" + 813 "0000000000000000000000000000000000000000000000000000000000000005" + 814 "0000000000000000000000000000000000000000000000000000000000000006")[0][0], 815 5 816 ); 817 assert.equal(parser.test("0x" + 818 "0000000000000000000000000000000000000000000000000000000000000002" + 819 "0000000000000000000000000000000000000000000000000000000000000005" + 820 "0000000000000000000000000000000000000000000000000000000000000006")[0][1], 821 6 822 ); 823 824 }); 825 826 it('should parse 0x value', function () { 827 828 // given 829 var d = clone(description); 830 d[0].outputs = [ 831 { type: 'int' } 832 ]; 833 834 // when 835 var parser = abi.outputParser(d); 836 837 // then 838 assert.equal(parser.test("0x")[0], 0); 839 840 }); 841 842 it('should parse 0x value', function () { 843 844 // given 845 var d = clone(description); 846 d[0].outputs = [ 847 { type: 'uint' } 848 ]; 849 850 // when 851 var parser = abi.outputParser(d); 852 853 // then 854 assert.equal(parser.test("0x")[0], 0); 855 856 }); 857 858 }); 859 }); 860