β Error
Last updated
Last updated
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.18;
contract Error {
error NotAuthorized();
function throwError() external {
require(false, "not authorized");
}
function throwCustomError() external {
revert NotAuthorized();
}
}// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.18;
import "forge-std/Test.sol";
import {Error} from "../src/Error.sol";
contract ErrorTest is Test {
Error public err;
function setUp() public {
err = new Error();
}
function testFail() public {
err.throwError();
}
function testRevert() public {
vm.expectRevert();
err.throwError();
}
function testRequireMessage() public {
vm.expectRevert(bytes("not authorized"));
err.throwError();
}
function testCustomError() public {
vm.expectRevert(Error.NotAuthorized.selector);
err.throwCustomError();
}
// Add label to assertions
function testErrorLabel() public {
assertEq(uint256(1), uint256(1), "test 1");
assertEq(uint256(1), uint256(1), "test 2");
assertEq(uint256(1), uint256(1), "test 3");
assertEq(uint256(1), uint256(2), "test 4");
assertEq(uint256(1), uint256(1), "test 5");
}
} function testFail() public {
err.throwError();
} function testRevert() public {
vm.expectRevert();
err.throwError();
} function throwError() external {
require(false, "not authorized");
} function testRequireMessage() public {
vm.expectRevert(bytes("not authorized"));
err.throwError();
} function throwCustomError() external {
revert NotAuthorized();
} function testCustomError() public {
vm.expectRevert(Error.NotAuthorized.selector);
err.throwCustomError();
} function testErrorLabel() public {
assertEq(uint256(1), uint256(1));
assertEq(uint256(1), uint256(1));
assertEq(uint256(1), uint256(1));
assertEq(uint256(1), uint256(2));
assertEq(uint256(1), uint256(1));
} // Add label to assertions
function testErrorLabel() public {
assertEq(uint256(1), uint256(1), "test 1");
assertEq(uint256(1), uint256(1), "test 2");
assertEq(uint256(1), uint256(1), "test 3");
assertEq(uint256(1), uint256(2), "test 4");
assertEq(uint256(1), uint256(1), "test 5");
}