destructure.rs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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(&[]);
  18. let returns = vm
  19. .function("f", &[BorshToken::Bool(true), BorshToken::Bool(true)])
  20. .unwrap()
  21. .unwrap_tuple();
  22. assert_eq!(
  23. returns,
  24. vec![
  25. BorshToken::Int {
  26. width: 256,
  27. value: BigInt::one(),
  28. },
  29. BorshToken::Int {
  30. width: 256,
  31. value: BigInt::from(2u8),
  32. }
  33. ]
  34. );
  35. let returns = vm
  36. .function("f", &[BorshToken::Bool(true), BorshToken::Bool(false)])
  37. .unwrap()
  38. .unwrap_tuple();
  39. assert_eq!(
  40. returns,
  41. vec![
  42. BorshToken::Int {
  43. width: 256,
  44. value: BigInt::from(3u8),
  45. },
  46. BorshToken::Int {
  47. width: 256,
  48. value: BigInt::from(4u8),
  49. }
  50. ]
  51. );
  52. let returns = vm
  53. .function("f", &[BorshToken::Bool(false), BorshToken::Bool(false)])
  54. .unwrap()
  55. .unwrap_tuple();
  56. assert_eq!(
  57. returns,
  58. vec![
  59. BorshToken::Int {
  60. width: 256,
  61. value: BigInt::from(5u8),
  62. },
  63. BorshToken::Int {
  64. width: 256,
  65. value: BigInt::from(6u8),
  66. }
  67. ]
  68. );
  69. let returns = vm
  70. .function("f", &[BorshToken::Bool(false), BorshToken::Bool(true)])
  71. .unwrap()
  72. .unwrap_tuple();
  73. assert_eq!(
  74. returns,
  75. vec![
  76. BorshToken::Int {
  77. width: 256,
  78. value: BigInt::from(5u8),
  79. },
  80. BorshToken::Int {
  81. width: 256,
  82. value: BigInt::from(6u8),
  83. }
  84. ]
  85. );
  86. }
  87. #[test]
  88. fn casting_destructure() {
  89. let mut vm = build_solidity(
  90. r#"
  91. contract foo {
  92. int[] arr;
  93. function f() public returns (int, int) {
  94. int[] storage ptrArr = arr;
  95. ptrArr.push(1);
  96. ptrArr.push(2);
  97. (int a, int b) = (ptrArr[0], ptrArr[1]);
  98. return (a, b);
  99. }
  100. }"#,
  101. );
  102. vm.constructor(&[]);
  103. let returns = vm.function("f", &[]).unwrap().unwrap_tuple();
  104. assert_eq!(
  105. returns,
  106. vec![
  107. BorshToken::Int {
  108. width: 256,
  109. value: BigInt::one(),
  110. },
  111. BorshToken::Int {
  112. width: 256,
  113. value: BigInt::from(2u8),
  114. }
  115. ]
  116. );
  117. let mut vm = build_solidity(
  118. r#"
  119. contract foo {
  120. function f() public returns (string) {
  121. (string a, string b) = ("Hello", "World!");
  122. return (a);
  123. }
  124. }"#,
  125. );
  126. vm.constructor(&[]);
  127. let returns = vm.function("f", &[]).unwrap();
  128. assert_eq!(returns, BorshToken::String(String::from("Hello")));
  129. }