Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
163 changes: 56 additions & 107 deletions src/AssertHelper.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,16 @@ abstract contract AssertHelper {
event AssertLteFail(string);
event AssertLtFail(string);

function assertWithMsg(bool b, string memory reason) internal {
if (!b) {
/// @notice asserts that a is true. Violations are logged using reason.
function t(bool a, string memory reason) internal {
if (!a) {
emit AssertFail(reason);
assert(false);
}
}

/// @notice asserts that a is equal to b. Violations are logged using reason.
function assertEq(
uint256 a,
uint256 b,
string memory reason
) internal {
function eq(uint256 a, uint256 b, string memory reason) internal {
if (a != b) {
string memory aStr = FuzzLibString.toString(a);
string memory bStr = FuzzLibString.toString(b);
Expand All @@ -42,12 +39,8 @@ abstract contract AssertHelper {
}
}

/// @notice int256 version of assertEq
function assertEq(
int256 a,
int256 b,
string memory reason
) internal {
/// @notice int256 version of `eq`
function eq(int256 a, int256 b, string memory reason) internal {
if (a != b) {
string memory aStr = FuzzLibString.toString(a);
string memory bStr = FuzzLibString.toString(b);
Expand All @@ -64,12 +57,8 @@ abstract contract AssertHelper {
}
}

/// @notice bytes4 version of assertEq
function assertEq(
bytes4 a,
bytes4 b,
string memory reason
) internal {
/// @notice bytes4 version of `eq`
function eq(bytes4 a, bytes4 b, string memory reason) internal {
if (a != b) {
bytes memory aBytes = abi.encodePacked(a);
bytes memory bBytes = abi.encodePacked(b);
Expand All @@ -89,11 +78,7 @@ abstract contract AssertHelper {
}

/// @notice asserts that a is not equal to b. Violations are logged using reason.
function assertNeq(
uint256 a,
uint256 b,
string memory reason
) internal {
function neq(uint256 a, uint256 b, string memory reason) internal {
if (a == b) {
string memory aStr = FuzzLibString.toString(a);
string memory bStr = FuzzLibString.toString(b);
Expand All @@ -110,12 +95,8 @@ abstract contract AssertHelper {
}
}

/// @notice int256 version of assertNeq
function assertNeq(
int256 a,
int256 b,
string memory reason
) internal {
/// @notice int256 version of `neq`
function neq(int256 a, int256 b, string memory reason) internal {
if (a == b) {
string memory aStr = FuzzLibString.toString(a);
string memory bStr = FuzzLibString.toString(b);
Expand All @@ -132,178 +113,146 @@ abstract contract AssertHelper {
}
}

/// @notice asserts that a is greater than or equal to b. Violations are logged using reason.
function assertGte(
uint256 a,
uint256 b,
string memory reason
) internal {
if (!(a >= b)) {
/// @notice asserts that a is greater than b. Violations are logged using reason.
function gt(uint256 a, uint256 b, string memory reason) internal {
if (!(a > b)) {
string memory aStr = FuzzLibString.toString(a);
string memory bStr = FuzzLibString.toString(b);
bytes memory assertMsg = abi.encodePacked(
"Invalid: ",
aStr,
"<",
"<=",
bStr,
" failed, reason: ",
reason
);
emit AssertGteFail(string(assertMsg));
emit AssertGtFail(string(assertMsg));
assert(false);
}
}

/// @notice int256 version of assertGte
function assertGte(
int256 a,
int256 b,
string memory reason
) internal {
if (!(a >= b)) {
/// @notice int256 version of `gt`
function gt(int256 a, int256 b, string memory reason) internal {
if (!(a > b)) {
string memory aStr = FuzzLibString.toString(a);
string memory bStr = FuzzLibString.toString(b);
bytes memory assertMsg = abi.encodePacked(
"Invalid: ",
aStr,
"<",
"<=",
bStr,
" failed, reason: ",
reason
);
emit AssertGteFail(string(assertMsg));
emit AssertGtFail(string(assertMsg));
assert(false);
}
}

/// @notice asserts that a is greater than b. Violations are logged using reason.
function assertGt(
uint256 a,
uint256 b,
string memory reason
) internal {
if (!(a > b)) {
/// @notice asserts that a is greater than or equal to b. Violations are logged using reason.
function gte(uint256 a, uint256 b, string memory reason) internal {
if (!(a >= b)) {
string memory aStr = FuzzLibString.toString(a);
string memory bStr = FuzzLibString.toString(b);
bytes memory assertMsg = abi.encodePacked(
"Invalid: ",
aStr,
"<=",
"<",
bStr,
" failed, reason: ",
reason
);
emit AssertGtFail(string(assertMsg));
emit AssertGteFail(string(assertMsg));
assert(false);
}
}

/// @notice int256 version of assertGt
function assertGt(
int256 a,
int256 b,
string memory reason
) internal {
if (!(a > b)) {
/// @notice int256 version of assertGte
function gte(int256 a, int256 b, string memory reason) internal {
if (!(a >= b)) {
string memory aStr = FuzzLibString.toString(a);
string memory bStr = FuzzLibString.toString(b);
bytes memory assertMsg = abi.encodePacked(
"Invalid: ",
aStr,
"<=",
"<",
bStr,
" failed, reason: ",
reason
);
emit AssertGtFail(string(assertMsg));
emit AssertGteFail(string(assertMsg));
assert(false);
}
}

/// @notice asserts that a is less than or equal to b. Violations are logged using reason.
function assertLte(
uint256 a,
uint256 b,
string memory reason
) internal {
if (!(a <= b)) {
/// @notice asserts that a is less than b. Violations are logged using reason.
function lt(uint256 a, uint256 b, string memory reason) internal {
if (!(a < b)) {
string memory aStr = FuzzLibString.toString(a);
string memory bStr = FuzzLibString.toString(b);
bytes memory assertMsg = abi.encodePacked(
"Invalid: ",
aStr,
">",
">=",
bStr,
" failed, reason: ",
reason
);
emit AssertLteFail(string(assertMsg));
emit AssertLtFail(string(assertMsg));
assert(false);
}
}

/// @notice int256 version of assertLte
function assertLte(
int256 a,
int256 b,
string memory reason
) internal {
if (!(a <= b)) {
/// @notice int256 version of assertLt
function lt(int256 a, int256 b, string memory reason) internal {
if (!(a < b)) {
string memory aStr = FuzzLibString.toString(a);
string memory bStr = FuzzLibString.toString(b);
bytes memory assertMsg = abi.encodePacked(
"Invalid: ",
aStr,
">",
">=",
bStr,
" failed, reason: ",
reason
);
emit AssertLteFail(string(assertMsg));
emit AssertLtFail(string(assertMsg));
assert(false);
}
}

/// @notice asserts that a is less than b. Violations are logged using reason.
function assertLt(
uint256 a,
uint256 b,
string memory reason
) internal {
if (!(a < b)) {
/// @notice asserts that a is less than or equal to b. Violations are logged using reason.
function lte(uint256 a, uint256 b, string memory reason) internal {
if (!(a <= b)) {
string memory aStr = FuzzLibString.toString(a);
string memory bStr = FuzzLibString.toString(b);
bytes memory assertMsg = abi.encodePacked(
"Invalid: ",
aStr,
">=",
">",
bStr,
" failed, reason: ",
reason
);
emit AssertLtFail(string(assertMsg));
emit AssertLteFail(string(assertMsg));
assert(false);
}
}

/// @notice int256 version of assertLt
function assertLt(
int256 a,
int256 b,
string memory reason
) internal {
if (!(a < b)) {
/// @notice int256 version of assertLte
function lte(int256 a, int256 b, string memory reason) internal {
if (!(a <= b)) {
string memory aStr = FuzzLibString.toString(a);
string memory bStr = FuzzLibString.toString(b);
bytes memory assertMsg = abi.encodePacked(
"Invalid: ",
aStr,
">=",
">",
bStr,
" failed, reason: ",
reason
);
emit AssertLtFail(string(assertMsg));
emit AssertLteFail(string(assertMsg));
assert(false);
}
}
Expand All @@ -313,15 +262,15 @@ abstract contract AssertHelper {
string memory reason
) internal {
bool isEqual = FuzzLibString.isRevertReasonEqual(returnData, reason);
assertWithMsg(!isEqual, reason);
t(!isEqual, reason);
}

function assertRevertReasonEqual(
bytes memory returnData,
string memory reason
) internal {
bool isEqual = FuzzLibString.isRevertReasonEqual(returnData, reason);
assertWithMsg(isEqual, reason);
t(isEqual, reason);
}

function assertRevertReasonEqual(
Expand All @@ -331,7 +280,7 @@ abstract contract AssertHelper {
) internal {
bool isEqual = FuzzLibString.isRevertReasonEqual(returnData, reason1) ||
FuzzLibString.isRevertReasonEqual(returnData, reason2);
assertWithMsg(isEqual, string.concat(reason1, " OR ", reason2));
t(isEqual, string.concat(reason1, " OR ", reason2));
}

function assertRevertReasonEqual(
Expand All @@ -343,7 +292,7 @@ abstract contract AssertHelper {
bool isEqual = FuzzLibString.isRevertReasonEqual(returnData, reason1) ||
FuzzLibString.isRevertReasonEqual(returnData, reason2) ||
FuzzLibString.isRevertReasonEqual(returnData, reason3);
assertWithMsg(isEqual, string.concat(reason1, " OR ", reason2, " OR ", reason3));
t(isEqual, string.concat(reason1, " OR ", reason2, " OR ", reason3));
}

function assertRevertReasonEqual(
Expand All @@ -357,7 +306,7 @@ abstract contract AssertHelper {
FuzzLibString.isRevertReasonEqual(returnData, reason2) ||
FuzzLibString.isRevertReasonEqual(returnData, reason3) ||
FuzzLibString.isRevertReasonEqual(returnData, reason4);
assertWithMsg(
t(
isEqual,
string.concat(
reason1,
Expand Down
Loading