destructure.rs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. // SPDX-License-Identifier: Apache-2.0
  2. use crate::{build_solidity, BorshToken};
  3. use num_bigint::BigInt;
  4. use num_traits::One;
  5. #[test]
  6. fn conditional_destructure() {
  7. // test that the abi encoder can handle fixed arrays
  8. let mut vm = build_solidity(
  9. r#"
  10. contract foo {
  11. function f(bool cond1, bool cond2) public returns (int, int) {
  12. (int a, int b) = cond1 ? (cond2 ? (1, 2) : (3, 4)) : (5, 6);
  13. return (a, b);
  14. }
  15. }"#,
  16. );
  17. vm.constructor("foo", &[]);
  18. let returns = vm.function("f", &[BorshToken::Bool(true), BorshToken::Bool(true)]);
  19. assert_eq!(
  20. returns,
  21. vec![
  22. BorshToken::Int {
  23. width: 256,
  24. value: BigInt::one(),
  25. },
  26. BorshToken::Int {
  27. width: 256,
  28. value: BigInt::from(2u8),
  29. }
  30. ]
  31. );
  32. let returns = vm.function("f", &[BorshToken::Bool(true), BorshToken::Bool(false)]);
  33. assert_eq!(
  34. returns,
  35. vec![
  36. BorshToken::Int {
  37. width: 256,
  38. value: BigInt::from(3u8),
  39. },
  40. BorshToken::Int {
  41. width: 256,
  42. value: BigInt::from(4u8),
  43. }
  44. ]
  45. );
  46. let returns = vm.function("f", &[BorshToken::Bool(false), BorshToken::Bool(false)]);
  47. assert_eq!(
  48. returns,
  49. vec![
  50. BorshToken::Int {
  51. width: 256,
  52. value: BigInt::from(5u8),
  53. },
  54. BorshToken::Int {
  55. width: 256,
  56. value: BigInt::from(6u8),
  57. }
  58. ]
  59. );
  60. let returns = vm.function("f", &[BorshToken::Bool(false), BorshToken::Bool(true)]);
  61. assert_eq!(
  62. returns,
  63. vec![
  64. BorshToken::Int {
  65. width: 256,
  66. value: BigInt::from(5u8),
  67. },
  68. BorshToken::Int {
  69. width: 256,
  70. value: BigInt::from(6u8),
  71. }
  72. ]
  73. );
  74. }
  75. #[test]
  76. fn casting_destructure() {
  77. let mut vm = build_solidity(
  78. r#"
  79. contract foo {
  80. int[] arr;
  81. function f() public returns (int, int) {
  82. int[] storage ptrArr = arr;
  83. ptrArr.push(1);
  84. ptrArr.push(2);
  85. (int a, int b) = (ptrArr[0], ptrArr[1]);
  86. return (a, b);
  87. }
  88. }"#,
  89. );
  90. vm.constructor("foo", &[]);
  91. let returns = vm.function("f", &[]);
  92. assert_eq!(
  93. returns,
  94. vec![
  95. BorshToken::Int {
  96. width: 256,
  97. value: BigInt::one(),
  98. },
  99. BorshToken::Int {
  100. width: 256,
  101. value: BigInt::from(2u8),
  102. }
  103. ]
  104. );
  105. let mut vm = build_solidity(
  106. r#"
  107. contract foo {
  108. function f() public returns (string) {
  109. (string a, string b) = ("Hello", "World!");
  110. return (a);
  111. }
  112. }"#,
  113. );
  114. vm.constructor("foo", &[]);
  115. let returns = vm.function("f", &[]);
  116. assert_eq!(returns, vec![BorshToken::String(String::from("Hello")),]);
  117. }