Skip to content

Commit dbef00b

Browse files
antks03Antonio Gabriel
andauthored
assert tests (#49)
* assert tests * just eq bool tests * adjusts and int256 version of eqtest * adjusts of failReasons * only eq bool tests * last adjusts * final adjusts --------- Co-authored-by: Antonio Gabriel <[email protected]>
1 parent ac52942 commit dbef00b

File tree

2 files changed

+93
-71
lines changed

2 files changed

+93
-71
lines changed

src/helpers/HelperAssert.sol

Lines changed: 32 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ abstract contract HelperAssert is HelperBase {
3131
if (a != b) {
3232
string memory aStr = FuzzLibString.toString(a);
3333
string memory bStr = FuzzLibString.toString(b);
34-
bytes memory assertMsg = createAssertFailMessage(aStr, bStr, "!=", reason);
35-
emit AssertEqFail(string(assertMsg));
34+
string memory assertMsg = createAssertFailMessage(aStr, bStr, "!=", reason);
35+
emit AssertEqFail(assertMsg);
3636
platform.assertFail();
3737
}
3838
}
@@ -46,8 +46,8 @@ abstract contract HelperAssert is HelperBase {
4646
if (a != b) {
4747
string memory aStr = FuzzLibString.toString(a);
4848
string memory bStr = FuzzLibString.toString(b);
49-
bytes memory assertMsg = createAssertFailMessage(aStr, bStr, "!=", reason);
50-
emit AssertEqFail(string(assertMsg));
49+
string memory assertMsg = createAssertFailMessage(aStr, bStr, "!=", reason);
50+
emit AssertEqFail(assertMsg);
5151
platform.assertFail();
5252
}
5353
}
@@ -61,8 +61,8 @@ abstract contract HelperAssert is HelperBase {
6161
if (a != b) {
6262
string memory aStr = a ? "true" : "false";
6363
string memory bStr = b ? "true" : "false";
64-
bytes memory assertMsg = createAssertFailMessage(aStr, bStr, "!=", reason);
65-
emit AssertEqFail(string(assertMsg));
64+
string memory assertMsg = createAssertFailMessage(aStr, bStr, "!=", reason);
65+
emit AssertEqFail(assertMsg);
6666
platform.assertFail();
6767
}
6868
}
@@ -76,8 +76,8 @@ abstract contract HelperAssert is HelperBase {
7676
if (a != b) {
7777
string memory aStr = FuzzLibString.toString(a);
7878
string memory bStr = FuzzLibString.toString(b);
79-
bytes memory assertMsg = createAssertFailMessage(aStr, bStr, "!=", reason);
80-
emit AssertEqFail(string(assertMsg));
79+
string memory assertMsg = createAssertFailMessage(aStr, bStr, "!=", reason);
80+
emit AssertEqFail(assertMsg);
8181
platform.assertFail();
8282
}
8383
}
@@ -93,8 +93,8 @@ abstract contract HelperAssert is HelperBase {
9393
bytes memory bBytes = abi.encodePacked(b);
9494
string memory aStr = FuzzLibString.toHexString(aBytes);
9595
string memory bStr = FuzzLibString.toHexString(bBytes);
96-
bytes memory assertMsg = createAssertFailMessage(aStr, bStr, "!=", reason);
97-
emit AssertEqFail(string(assertMsg));
96+
string memory assertMsg = createAssertFailMessage(aStr, bStr, "!=", reason);
97+
emit AssertEqFail(assertMsg);
9898
platform.assertFail();
9999
}
100100
}
@@ -108,8 +108,8 @@ abstract contract HelperAssert is HelperBase {
108108
if (a == b) {
109109
string memory aStr = FuzzLibString.toString(a);
110110
string memory bStr = FuzzLibString.toString(b);
111-
bytes memory assertMsg = createAssertFailMessage(aStr, bStr, "==", reason);
112-
emit AssertNeqFail(string(assertMsg));
111+
string memory assertMsg = createAssertFailMessage(aStr, bStr, "==", reason);
112+
emit AssertNeqFail(assertMsg);
113113
platform.assertFail();
114114
}
115115
}
@@ -123,8 +123,8 @@ abstract contract HelperAssert is HelperBase {
123123
if (a == b) {
124124
string memory aStr = FuzzLibString.toString(a);
125125
string memory bStr = FuzzLibString.toString(b);
126-
bytes memory assertMsg = createAssertFailMessage(aStr, bStr, "==", reason);
127-
emit AssertNeqFail(string(assertMsg));
126+
string memory assertMsg = createAssertFailMessage(aStr, bStr, "==", reason);
127+
emit AssertNeqFail(assertMsg);
128128
platform.assertFail();
129129
}
130130
}
@@ -138,8 +138,8 @@ abstract contract HelperAssert is HelperBase {
138138
if (!(a >= b)) {
139139
string memory aStr = FuzzLibString.toString(a);
140140
string memory bStr = FuzzLibString.toString(b);
141-
bytes memory assertMsg = createAssertFailMessage(aStr, bStr, "<", reason);
142-
emit AssertGteFail(string(assertMsg));
141+
string memory assertMsg = createAssertFailMessage(aStr, bStr, "<", reason);
142+
emit AssertGteFail(assertMsg);
143143
platform.assertFail();
144144
}
145145
}
@@ -153,8 +153,8 @@ abstract contract HelperAssert is HelperBase {
153153
if (!(a >= b)) {
154154
string memory aStr = FuzzLibString.toString(a);
155155
string memory bStr = FuzzLibString.toString(b);
156-
bytes memory assertMsg = createAssertFailMessage(aStr, bStr, "<", reason);
157-
emit AssertGteFail(string(assertMsg));
156+
string memory assertMsg = createAssertFailMessage(aStr, bStr, "<", reason);
157+
emit AssertGteFail(assertMsg);
158158
platform.assertFail();
159159
}
160160
}
@@ -168,8 +168,8 @@ abstract contract HelperAssert is HelperBase {
168168
if (!(a > b)) {
169169
string memory aStr = FuzzLibString.toString(a);
170170
string memory bStr = FuzzLibString.toString(b);
171-
bytes memory assertMsg = createAssertFailMessage(aStr, bStr, "<=", reason);
172-
emit AssertGtFail(string(assertMsg));
171+
string memory assertMsg = createAssertFailMessage(aStr, bStr, "<=", reason);
172+
emit AssertGtFail(assertMsg);
173173
platform.assertFail();
174174
}
175175
}
@@ -183,15 +183,8 @@ abstract contract HelperAssert is HelperBase {
183183
if (!(a > b)) {
184184
string memory aStr = FuzzLibString.toString(a);
185185
string memory bStr = FuzzLibString.toString(b);
186-
bytes memory assertMsg = abi.encodePacked(
187-
"Invalid: ",
188-
aStr,
189-
"<=",
190-
bStr,
191-
" failed, reason: ",
192-
reason
193-
);
194-
emit AssertGtFail(string(assertMsg));
186+
string memory assertMsg = createAssertFailMessage(aStr, bStr, "<=", reason);
187+
emit AssertGtFail(assertMsg);
195188
platform.assertFail();
196189
}
197190
}
@@ -205,15 +198,8 @@ abstract contract HelperAssert is HelperBase {
205198
if (!(a <= b)) {
206199
string memory aStr = FuzzLibString.toString(a);
207200
string memory bStr = FuzzLibString.toString(b);
208-
bytes memory assertMsg = abi.encodePacked(
209-
"Invalid: ",
210-
aStr,
211-
">",
212-
bStr,
213-
" failed, reason: ",
214-
reason
215-
);
216-
emit AssertLteFail(string(assertMsg));
201+
string memory assertMsg = createAssertFailMessage(aStr, bStr, ">", reason);
202+
emit AssertLteFail(assertMsg);
217203
platform.assertFail();
218204
}
219205
}
@@ -227,15 +213,8 @@ abstract contract HelperAssert is HelperBase {
227213
if (!(a <= b)) {
228214
string memory aStr = FuzzLibString.toString(a);
229215
string memory bStr = FuzzLibString.toString(b);
230-
bytes memory assertMsg = abi.encodePacked(
231-
"Invalid: ",
232-
aStr,
233-
">",
234-
bStr,
235-
" failed, reason: ",
236-
reason
237-
);
238-
emit AssertLteFail(string(assertMsg));
216+
string memory assertMsg = createAssertFailMessage(aStr, bStr, ">", reason);
217+
emit AssertLteFail(assertMsg);
239218
platform.assertFail();
240219
}
241220
}
@@ -249,8 +228,8 @@ abstract contract HelperAssert is HelperBase {
249228
if (!(a < b)) {
250229
string memory aStr = FuzzLibString.toString(a);
251230
string memory bStr = FuzzLibString.toString(b);
252-
bytes memory assertMsg = createAssertFailMessage(aStr, bStr, ">=", reason);
253-
emit AssertLtFail(string(assertMsg));
231+
string memory assertMsg = createAssertFailMessage(aStr, bStr, ">=", reason);
232+
emit AssertLtFail(assertMsg);
254233
platform.assertFail();
255234
}
256235
}
@@ -264,8 +243,8 @@ abstract contract HelperAssert is HelperBase {
264243
if (!(a < b)) {
265244
string memory aStr = FuzzLibString.toString(a);
266245
string memory bStr = FuzzLibString.toString(b);
267-
bytes memory assertMsg = createAssertFailMessage(aStr, bStr, ">=", reason);
268-
emit AssertLtFail(string(assertMsg));
246+
string memory assertMsg = createAssertFailMessage(aStr, bStr, ">=", reason);
247+
emit AssertLtFail(assertMsg);
269248
platform.assertFail();
270249
}
271250
}
@@ -371,8 +350,8 @@ abstract contract HelperAssert is HelperBase {
371350
t(allowed, messages[passIndex]);
372351
}
373352

374-
function createAssertFailMessage(string memory aStr, string memory bStr, string memory operator, string memory reason)internal pure returns (bytes memory) {
375-
return bytes(abi.encodePacked("Invalid: ", aStr, operator, bStr, ", reason: ", reason));
353+
function createAssertFailMessage(string memory aStr, string memory bStr, string memory operator, string memory reason)internal pure returns (string memory) {
354+
return string(abi.encodePacked("Invalid: ", aStr, operator, bStr, ", reason: ", reason));
376355
}
377356

378357
}

test/Assert.t.sol

Lines changed: 61 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -28,28 +28,26 @@ contract TestAsserts is Test, HelperAssert {
2828
t(false, reason);
2929
}
3030

31-
function test_HelperAssert_eq_x_x_concrete() public {
31+
function test_eq_x_x() public {
3232
uint256 x = 1;
33-
eq(x, x, "eq does not revert with equal values");
33+
eq(x, x, "example message");
3434
}
3535

36-
function testFuzz_HelperAssert_eq_x_x_fuzz(uint256 x) public {
37-
eq(x, x, "eq does not revert with the fuzz values");
36+
function testFuzz_eq_x_x(uint256 x) public {
37+
eq(x, x, "example message");
3838
}
3939

40-
function test_HelperAssert_eq_x_y_concrete() public {
40+
function test_eq_x_y() public {
4141
uint256 x = 2;
4242
uint256 y = 4;
4343

44-
string memory reason = "eq reverts with different values.";
45-
string memory failReason = string(abi.encodePacked(
46-
"Invalid: ",
44+
string memory reason = "example message";
45+
string memory failReason = createAssertFailMessage(
4746
FuzzLibString.toString(x),
48-
"!=",
4947
FuzzLibString.toString(y),
50-
", reason: ",
48+
"!=",
5149
reason
52-
));
50+
);
5351
vm.expectEmit(true, false, false, true);
5452
emit AssertEqFail(failReason);
5553

@@ -58,23 +56,68 @@ contract TestAsserts is Test, HelperAssert {
5856
eq(x, y, reason);
5957
}
6058

61-
function testFuzz_HelperAssert_eq_x_y_fuzz(uint256 x, uint256 y) public {
59+
function testFuzz_eq_x_y(uint256 x, uint256 y) public {
6260
vm.assume(x != y);
63-
string memory reason = "eq reverts with fuzz values.";
61+
string memory reason = "example message";
6462

6563
vm.expectEmit(true, false, false, true);
66-
string memory failReason = string(abi.encodePacked(
67-
"Invalid: ",
64+
string memory failReason = createAssertFailMessage(
6865
FuzzLibString.toString(x),
69-
"!=",
7066
FuzzLibString.toString(y),
71-
", reason: ",
67+
"!=",
68+
reason
69+
);
70+
emit AssertEqFail(failReason);
71+
72+
vm.expectRevert(PlatformTest.TestAssertFail.selector);
73+
74+
eq(x, y, reason);
75+
}
76+
77+
/// @notice bool version of eq tests
78+
function test_eq_bool_x_x() public {
79+
bool x = true;
80+
eq(x, x, "example message");
81+
}
82+
83+
function testFuzz_eq_bool_x_x(bool x) public {
84+
eq(x, x, "example message");
85+
}
86+
87+
function test_eq_bool_x_y() public {
88+
bool x = true;
89+
bool y = false;
90+
91+
string memory reason = "example message";
92+
string memory failReason = createAssertFailMessage(
93+
x ? "true" : "false",
94+
y ? "true" : "false",
95+
"!=",
7296
reason
73-
));
97+
);
98+
vm.expectEmit(true, false, false, true);
7499
emit AssertEqFail(failReason);
75100

76101
vm.expectRevert(PlatformTest.TestAssertFail.selector);
77102

78103
eq(x, y, reason);
79104
}
105+
106+
function testFuzz_eq_bool_x_y(bool x, bool y) public {
107+
vm.assume(x != y);
108+
string memory reason = "example message";
109+
110+
vm.expectEmit(true, false, false, true);
111+
string memory failReason = createAssertFailMessage(
112+
x ? "true" : "false",
113+
y ? "true" : "false",
114+
"!=",
115+
reason
116+
);
117+
emit AssertEqFail(failReason);
118+
119+
vm.expectRevert(PlatformTest.TestAssertFail.selector);
120+
121+
eq(x, y, reason);
122+
}
80123
}

0 commit comments

Comments
 (0)