Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Testing with solidity tests. */
- import "truffle/Assert.sol";
- import "truffle/DeployedAddresses.sol";
- import "../contracts/MyContract.sol";
- contract TestMyContract {
- function testInitialStoredValue() {
- MyContract mycontract = new MyContract();
- uint expected = 24;
- Assert.equal(mycontract.mynumber(), expected, "First number set should be 24.");
- }
- function testTheThrow() {
- MyContract mycontract = new MyContract();
- ThrowProxy throwproxy = new ThrowProxy(address(mycontract));
- uint num = 7;
- MyContract(address(throwproxy)).storeNum(num);
- bool r = throwproxy.execute.gas(200000)();
- Assert.isFalse(r, "Should be false because is should throw!");
- }
- function testNoThrow() {
- MyContract mycontract = new MyContract();
- ThrowProxy throwproxy = new ThrowProxy(address(mycontract));
- MyContract(address(throwproxy)).storeNum(22);
- bool r = throwproxy.execute.gas(200000)();
- Assert.isTrue(r, "Should be true because is should throw!");
- }
- }
- // Proxy contract for testing throws
- contract ThrowProxy {
- address public target;
- bytes data;
- function ThrowProxy(address _target) {
- target = _target;
- }
- //prime the data using the fallback function.
- function() {
- data = msg.data;
- }
- function execute() returns (bool) {
- return target.call(data);
- }
- }
- function storeNum(uint mynum)
- public
- returns (bool)
- {
- require(mynum > 10);
- mynumber = mynum;
- return true;
- }
- function storeNum(uint mynum)
- public
- {
- require(mynum > 10);
- mynumber = mynum;
- return true;
- }
- function storeNum(uint mynum)
- public
- returns (bool success)
- {
- require(mynum > 10);
- mynumber = mynum;
- return true;
- }
- function storeNum(uint mynum)
- public
- returns (bool) // you should define at least the type
- {
- require(mynum > 10);
- mynumber = mynum;
- return true;
- }
- function storeNum(uint mynum)
- public
- returns (bool success)
- {
- require(mynum > 10);
- mynumber = mynum;
- success = true; // don't need to use return
- }
- /*
- Contract wrapper that inherits from MyContract. This contract wraps the calls of non void functions when
- testing for exceptions.
- Why not invoke directly the function?: https://github.com/trufflesuite/truffle/issues/1001
- */
- contract MyExposedContract is MyContract() {
- function callStoreNum(uint256 num) public {
- storeNum(num);
- }
- }
- function testTheThrow() {
- MyExposedContract mycontract = new MyExposedContract ();
- ThrowProxy throwproxy = new ThrowProxy(address(mycontract));
- uint num = 7;
- MyExposedContract (address(throwproxy)).storeNum(num);
- bool r = throwproxy.execute.gas(200000)();
- Assert.isFalse(r, "Should be false because it should throw!");
- }
Add Comment
Please, Sign In to add comment