enums.rs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. // SPDX-License-Identifier: Apache-2.0
  2. use parity_scale_codec::{Decode, Encode};
  3. use crate::build_solidity;
  4. #[test]
  5. fn weekdays() {
  6. #[derive(Debug, PartialEq, Eq, Encode, Decode)]
  7. struct Val(u8);
  8. // parse
  9. let mut runtime = build_solidity(
  10. "
  11. enum Weekday { Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday }
  12. contract enum_example {
  13. function is_weekend(Weekday day) public pure returns (bool) {
  14. return (day == Weekday.Saturday || day == Weekday.Sunday);
  15. }
  16. function test_values() public pure {
  17. assert(int8(Weekday.Monday) == 0);
  18. assert(int8(Weekday.Tuesday) == 1);
  19. assert(int8(Weekday.Wednesday) == 2);
  20. assert(int8(Weekday.Thursday) == 3);
  21. assert(int8(Weekday.Friday) == 4);
  22. assert(int8(Weekday.Saturday) == 5);
  23. assert(int8(Weekday.Sunday) == 6);
  24. Weekday x;
  25. x = Weekday.Monday;
  26. assert(uint(x) == 0);
  27. x = Weekday.Sunday;
  28. assert(int16(x) == 6);
  29. x = Weekday(2);
  30. assert(x == Weekday.Wednesday);
  31. }
  32. }",
  33. );
  34. runtime.function("is_weekend", Val(4).encode());
  35. assert_eq!(runtime.output(), Val(0).encode());
  36. runtime.function("is_weekend", Val(5).encode());
  37. assert_eq!(runtime.output(), Val(1).encode());
  38. runtime.function("test_values", Vec::new());
  39. }
  40. #[test]
  41. fn enums_other_contracts() {
  42. #[derive(Debug, PartialEq, Eq, Encode, Decode)]
  43. struct Val(u8);
  44. // parse
  45. let mut runtime = build_solidity(
  46. "
  47. contract a {
  48. c.foo bar;
  49. constructor() public {
  50. bar = c.foo.bar;
  51. }
  52. function test(c.foo x) public {
  53. assert(x == c.foo.bar2);
  54. assert(c.foo.bar2 != c.foo.bar3);
  55. }
  56. }
  57. abstract contract c {
  58. enum foo { bar, bar2, bar3 }
  59. }
  60. ",
  61. );
  62. runtime.function("test", Val(1).encode());
  63. }