| 1 | // SPDX-License-Identifier: MIT |
| 2 | pragma solidity >=0.8.0 <0.9.0; |
| 3 | |
| 4 | import {stdError} from "../src/StdError.sol"; |
| 5 | import {Test} from "../src/Test.sol"; |
| 6 | |
| 7 | contract StdErrorsTest is Test { |
| 8 | ErrorsTest test; |
| 9 | |
| 10 | function setUp() public { |
| 11 | test = new ErrorsTest(); |
| 12 | } |
| 13 | |
| 14 | function test_RevertIf_AssertionError() public { |
| 15 | vm.expectRevert(stdError.assertionError); |
| 16 | test.assertionError(); |
| 17 | } |
| 18 | |
| 19 | function test_RevertIf_ArithmeticError() public { |
| 20 | vm.expectRevert(stdError.arithmeticError); |
| 21 | test.arithmeticError(10); |
| 22 | } |
| 23 | |
| 24 | function test_RevertIf_DivisionError() public { |
| 25 | vm.expectRevert(stdError.divisionError); |
| 26 | test.divError(0); |
| 27 | } |
| 28 | |
| 29 | function test_RevertIf_ModError() public { |
| 30 | vm.expectRevert(stdError.divisionError); |
| 31 | test.modError(0); |
| 32 | } |
| 33 | |
| 34 | function test_RevertIf_EnumConversionError() public { |
| 35 | vm.expectRevert(stdError.enumConversionError); |
| 36 | test.enumConversion(1); |
| 37 | } |
| 38 | |
| 39 | function test_RevertIf_EncodeStgError() public { |
| 40 | vm.expectRevert(stdError.encodeStorageError); |
| 41 | test.encodeStgError(); |
| 42 | } |
| 43 | |
| 44 | function test_RevertIf_PopError() public { |
| 45 | vm.expectRevert(stdError.popError); |
| 46 | test.pop(); |
| 47 | } |
| 48 | |
| 49 | function test_RevertIf_IndexOOBError() public { |
| 50 | vm.expectRevert(stdError.indexOOBError); |
| 51 | test.indexOOBError(1); |
| 52 | } |
| 53 | |
| 54 | function test_RevertIf_MemOverflowError() public { |
| 55 | vm.expectRevert(stdError.memOverflowError); |
| 56 | test.mem(); |
| 57 | } |
| 58 | |
| 59 | function test_RevertIf_InternError() public { |
| 60 | vm.expectRevert(stdError.zeroVarError); |
| 61 | test.intern(); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | contract ErrorsTest { |
| 66 | enum T { |
| 67 | T1 |
| 68 | } |
| 69 | |
| 70 | uint256[] public someArr; |
| 71 | bytes someBytes; |
| 72 | |
| 73 | function assertionError() public pure { |
| 74 | assert(false); |
| 75 | } |
| 76 | |
| 77 | function arithmeticError(uint256 a) public pure { |
| 78 | a -= 100; |
| 79 | } |
| 80 | |
| 81 | function divError(uint256 a) public pure { |
| 82 | 100 / a; |
| 83 | } |
| 84 | |
| 85 | function modError(uint256 a) public pure { |
| 86 | 100 % a; |
| 87 | } |
| 88 | |
| 89 | function enumConversion(uint256 a) public pure { |
| 90 | T(a); |
| 91 | } |
| 92 | |
| 93 | function encodeStgError() public { |
| 94 | /// @solidity memory-safe-assembly |
| 95 | assembly { |
| 96 | sstore(someBytes.slot, 1) |
| 97 | } |
| 98 | keccak256(someBytes); |
| 99 | } |
| 100 | |
| 101 | function pop() public { |
| 102 | someArr.pop(); |
| 103 | } |
| 104 | |
| 105 | function indexOOBError(uint256 a) public pure { |
| 106 | uint256[] memory t = new uint256[](0); |
| 107 | t[a]; |
| 108 | } |
| 109 | |
| 110 | function mem() public pure { |
| 111 | uint256 l = 2 ** 256 / 32; |
| 112 | new uint256[](l); |
| 113 | } |
| 114 | |
| 115 | function intern() public returns (uint256) { |
| 116 | function(uint256) internal returns (uint256) x; |
| 117 | x(2); |
| 118 | return 7; |
| 119 | } |
| 120 | } |