Guest User

Untitled

a guest
Jun 21st, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.85 KB | None | 0 0
  1. /* Testing with solidity tests. */
  2.  
  3. import "truffle/Assert.sol";
  4. import "truffle/DeployedAddresses.sol";
  5. import "../contracts/MyContract.sol";
  6.  
  7. contract TestMyContract {
  8.  
  9. function testInitialStoredValue() {
  10. MyContract mycontract = new MyContract();
  11.  
  12. uint expected = 24;
  13.  
  14. Assert.equal(mycontract.mynumber(), expected, "First number set should be 24.");
  15. }
  16.  
  17. function testTheThrow() {
  18. MyContract mycontract = new MyContract();
  19.  
  20. ThrowProxy throwproxy = new ThrowProxy(address(mycontract));
  21. uint num = 7;
  22. MyContract(address(throwproxy)).storeNum(num);
  23.  
  24. bool r = throwproxy.execute.gas(200000)();
  25.  
  26. Assert.isFalse(r, "Should be false because is should throw!");
  27.  
  28. }
  29.  
  30. function testNoThrow() {
  31. MyContract mycontract = new MyContract();
  32.  
  33. ThrowProxy throwproxy = new ThrowProxy(address(mycontract));
  34.  
  35. MyContract(address(throwproxy)).storeNum(22);
  36.  
  37. bool r = throwproxy.execute.gas(200000)();
  38.  
  39. Assert.isTrue(r, "Should be true because is should throw!");
  40.  
  41. }
  42.  
  43. }
  44.  
  45. // Proxy contract for testing throws
  46. contract ThrowProxy {
  47. address public target;
  48. bytes data;
  49.  
  50. function ThrowProxy(address _target) {
  51. target = _target;
  52. }
  53.  
  54. //prime the data using the fallback function.
  55. function() {
  56. data = msg.data;
  57. }
  58.  
  59. function execute() returns (bool) {
  60. return target.call(data);
  61. }
  62. }
  63.  
  64. function storeNum(uint mynum)
  65. public
  66. returns (bool)
  67. {
  68. require(mynum > 10);
  69. mynumber = mynum;
  70. return true;
  71. }
  72.  
  73. function storeNum(uint mynum)
  74. public
  75. {
  76. require(mynum > 10);
  77. mynumber = mynum;
  78. return true;
  79. }
  80.  
  81. function storeNum(uint mynum)
  82. public
  83. returns (bool success)
  84. {
  85. require(mynum > 10);
  86. mynumber = mynum;
  87. return true;
  88. }
  89.  
  90. function storeNum(uint mynum)
  91. public
  92. returns (bool) // you should define at least the type
  93. {
  94. require(mynum > 10);
  95. mynumber = mynum;
  96. return true;
  97. }
  98.  
  99. function storeNum(uint mynum)
  100. public
  101. returns (bool success)
  102. {
  103. require(mynum > 10);
  104. mynumber = mynum;
  105. success = true; // don't need to use return
  106. }
  107.  
  108. /*
  109. Contract wrapper that inherits from MyContract. This contract wraps the calls of non void functions when
  110. testing for exceptions.
  111.  
  112. Why not invoke directly the function?: https://github.com/trufflesuite/truffle/issues/1001
  113. */
  114.  
  115. contract MyExposedContract is MyContract() {
  116.  
  117.  
  118. function callStoreNum(uint256 num) public {
  119. storeNum(num);
  120. }
  121.  
  122. }
  123.  
  124. function testTheThrow() {
  125. MyExposedContract mycontract = new MyExposedContract ();
  126.  
  127. ThrowProxy throwproxy = new ThrowProxy(address(mycontract));
  128. uint num = 7;
  129. MyExposedContract (address(throwproxy)).storeNum(num);
  130.  
  131. bool r = throwproxy.execute.gas(200000)();
  132.  
  133. Assert.isFalse(r, "Should be false because it should throw!");
  134.  
  135. }
Add Comment
Please, Sign In to add comment