abi.rs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. // SPDX-License-Identifier: Apache-2.0
  2. use crate::build_solidity;
  3. #[test]
  4. fn packed() {
  5. let mut vm = build_solidity(
  6. r#"
  7. struct s {
  8. int32 f1;
  9. uint8 f2;
  10. string f3;
  11. uint16[2] f4;
  12. }
  13. contract bar {
  14. function test() public {
  15. uint16 a = 0xfd01;
  16. assert(abi.encodePacked(a) == hex"01fd");
  17. uint32 b = 0xaabbccdd;
  18. assert(abi.encodePacked(true, b, false) == hex"01ddccbbaa00");
  19. }
  20. function test2() public {
  21. string b = "foobar";
  22. bytes c = abi.encodePacked(b);
  23. assert(abi.encodePacked(b) == "foobar");
  24. assert(abi.encodePacked("foobar") == "foobar");
  25. assert(abi.encodePacked("foo", "bar") == "foobar");
  26. }
  27. function test3() public {
  28. s x = s({ f1: 511, f2: 0xf7, f3: "testie", f4: [ 4, 5 ] });
  29. assert(abi.encodePacked(x) == hex"ff010000f774657374696504000500");
  30. }
  31. function test4() public {
  32. uint8[] vec = new uint8[](2);
  33. vec[0] = 0xca;
  34. vec[1] = 0xfe;
  35. assert(abi.encodePacked(vec) == hex"cafe");
  36. }
  37. }"#,
  38. );
  39. vm.constructor("bar", &[]);
  40. vm.function("test", &[]);
  41. vm.function("test2", &[]);
  42. vm.function("test3", &[]);
  43. vm.function("test4", &[]);
  44. }
  45. #[test]
  46. fn inherited() {
  47. let mut vm = build_solidity(
  48. r#"
  49. contract foo {
  50. function test() public {
  51. }
  52. }
  53. contract bar is foo { }"#,
  54. );
  55. vm.constructor("bar", &[]);
  56. vm.function("test", &[]);
  57. let mut vm = build_solidity(
  58. r#"
  59. contract foo {
  60. int public test;
  61. }
  62. contract bar is foo { }"#,
  63. );
  64. vm.constructor("bar", &[]);
  65. vm.function("test", &[]);
  66. }