modifiers.rs 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. // SPDX-License-Identifier: Apache-2.0
  2. use crate::{build_solidity, BorshToken};
  3. use num_bigint::BigInt;
  4. #[test]
  5. fn returns_and_phis_needed() {
  6. let mut vm = build_solidity(
  7. r#"
  8. contract c {
  9. int foo;
  10. bool bar;
  11. function func(bool cond) external mod(cond) returns (int, bool) {
  12. return (foo, bar);
  13. }
  14. modifier mod(bool cond) {
  15. bar = cond;
  16. if (cond) {
  17. foo = 12;
  18. _;
  19. } else {
  20. foo = 40;
  21. _;
  22. }
  23. }
  24. }"#,
  25. );
  26. let data_account = vm.initialize_data_account();
  27. vm.function("new")
  28. .accounts(vec![("dataAccount", data_account)])
  29. .call();
  30. let returns = vm
  31. .function("func")
  32. .arguments(&[BorshToken::Bool(false)])
  33. .accounts(vec![("dataAccount", data_account)])
  34. .call()
  35. .unwrap()
  36. .unwrap_tuple();
  37. assert_eq!(
  38. returns,
  39. vec![
  40. BorshToken::Int {
  41. width: 256,
  42. value: BigInt::from(40u8),
  43. },
  44. BorshToken::Bool(false)
  45. ]
  46. );
  47. let returns = vm
  48. .function("func")
  49. .arguments(&[BorshToken::Bool(true)])
  50. .accounts(vec![("dataAccount", data_account)])
  51. .call()
  52. .unwrap()
  53. .unwrap_tuple();
  54. assert_eq!(
  55. returns,
  56. vec![
  57. BorshToken::Int {
  58. width: 256,
  59. value: BigInt::from(12u8)
  60. },
  61. BorshToken::Bool(true)
  62. ]
  63. );
  64. }