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