mod.rs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. use parity_scale_codec::Encode;
  2. use parity_scale_codec_derive::{Decode, Encode};
  3. use super::build_solidity;
  4. #[test]
  5. fn simple_solidiy_compile_and_run() {
  6. #[derive(Debug, PartialEq, Encode, Decode)]
  7. struct FooReturn {
  8. value: u32,
  9. }
  10. // parse
  11. let (runtime, mut store) = build_solidity(
  12. "
  13. contract test {
  14. function foo() public returns (uint32) {
  15. return 2;
  16. }
  17. }",
  18. );
  19. runtime.function(&mut store, "foo", Vec::new());
  20. let ret = FooReturn { value: 2 };
  21. assert_eq!(store.scratch, ret.encode());
  22. }
  23. #[test]
  24. fn flipper() {
  25. // parse
  26. let (runtime, mut store) = build_solidity(
  27. "
  28. contract flipper {
  29. bool private value;
  30. constructor(bool initvalue) public {
  31. value = initvalue;
  32. }
  33. function flip() public {
  34. value = !value;
  35. }
  36. function get() public view returns (bool) {
  37. return value;
  38. }
  39. }
  40. ",
  41. );
  42. #[derive(Debug, PartialEq, Encode, Decode)]
  43. struct GetReturn(bool);
  44. runtime.function(&mut store, "get", Vec::new());
  45. assert_eq!(store.scratch, GetReturn(false).encode());
  46. runtime.function(&mut store, "flip", Vec::new());
  47. runtime.function(&mut store, "flip", Vec::new());
  48. runtime.function(&mut store, "flip", Vec::new());
  49. runtime.function(&mut store, "get", Vec::new());
  50. assert_eq!(store.scratch, GetReturn(true).encode());
  51. }
  52. #[test]
  53. fn contract_storage_initializers() {
  54. #[derive(Debug, PartialEq, Encode, Decode)]
  55. struct FooReturn {
  56. value: u32,
  57. }
  58. // parse
  59. let (runtime, mut store) = build_solidity(
  60. "
  61. contract test {
  62. uint32 a = 100;
  63. uint32 b = 200;
  64. constructor() public {
  65. b = 300;
  66. }
  67. function foo() public returns (uint32) {
  68. return a + b;
  69. }
  70. }",
  71. );
  72. runtime.constructor(&mut store, 0, Vec::new());
  73. runtime.function(&mut store, "foo", Vec::new());
  74. let ret = FooReturn { value: 400 };
  75. assert_eq!(store.scratch, ret.encode());
  76. }
  77. #[test]
  78. fn contract_constants() {
  79. #[derive(Debug, PartialEq, Encode, Decode)]
  80. struct FooReturn {
  81. value: u32,
  82. }
  83. // parse
  84. let (runtime, mut store) = build_solidity(
  85. "
  86. contract test {
  87. uint32 constant a = 300 + 100;
  88. function foo() public pure returns (uint32) {
  89. uint32 ret = a;
  90. return ret;
  91. }
  92. }",
  93. );
  94. runtime.constructor(&mut store, 0, Vec::new());
  95. runtime.function(&mut store, "foo", Vec::new());
  96. let ret = FooReturn { value: 400 };
  97. assert_eq!(store.scratch, ret.encode());
  98. }
  99. #[test]
  100. fn large_contract_variables() {
  101. #[derive(Debug, PartialEq, Encode, Decode)]
  102. struct ValBool(u8);
  103. // parse
  104. let (runtime, mut store) = build_solidity("
  105. contract test {
  106. int constant large = 0x7fff0000_7fff0000_7fff0000_7fff0000__7fff0000_7fff0000_7fff0000_7fff0000;
  107. int bar = large + 10;
  108. function foo() public view returns (int) {
  109. return (bar - 10);
  110. }
  111. }",
  112. );
  113. runtime.constructor(&mut store, 0, Vec::new());
  114. runtime.function(&mut store, "foo", Vec::new());
  115. assert_eq!(store.scratch, b"\x00\x00\xff\x7f\x00\x00\xff\x7f\x00\x00\xff\x7f\x00\x00\xff\x7f\x00\x00\xff\x7f\x00\x00\xff\x7f\x00\x00\xff\x7f\x00\x00\xff\x7f");
  116. }
  117. #[test]
  118. fn assert_ok() {
  119. // parse
  120. let (runtime, mut store) = build_solidity(
  121. "
  122. contract test {
  123. function foo() public pure returns (uint32) {
  124. assert(true);
  125. return 0;
  126. }
  127. }",
  128. );
  129. runtime.constructor(&mut store, 0, Vec::new());
  130. runtime.function(&mut store, "foo", Vec::new());
  131. }
  132. #[test]
  133. #[should_panic]
  134. fn assert_not_ok() {
  135. // parse
  136. let (runtime, mut store) = build_solidity(
  137. "
  138. contract test {
  139. function foo() public pure returns (uint32) {
  140. assert(false);
  141. return 0;
  142. }
  143. }",
  144. );
  145. runtime.constructor(&mut store, 0, Vec::new());
  146. runtime.function(&mut store, "foo", Vec::new());
  147. }