From 915fcadb97a428b6874c759428b946db34aa1daf Mon Sep 17 00:00:00 2001 From: "David E. Perez Negron R." Date: Sun, 17 Sep 2023 22:43:56 -0600 Subject: [PATCH] Testing Errors --- src/Error.sol | 16 ++++++++++++++++ test/Error.t.sol | 45 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+) create mode 100644 src/Error.sol create mode 100644 test/Error.t.sol diff --git a/src/Error.sol b/src/Error.sol new file mode 100644 index 0000000..fecbf91 --- /dev/null +++ b/src/Error.sol @@ -0,0 +1,16 @@ +// SPDX-License-Identifier: UNLICENSED +pragma solidity ^0.8.20; + +contract Error { + error NotAuthorized(); + + function throwError() external { + require(false, "not authorized"); + } + + function throwCustomError() external { + revert NotAuthorized(); + } + + +} diff --git a/test/Error.t.sol b/test/Error.t.sol new file mode 100644 index 0000000..deeec26 --- /dev/null +++ b/test/Error.t.sol @@ -0,0 +1,45 @@ +// SPDX-License-Identifier: UNLICENSED +pragma solidity ^0.8.20; + +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(); + } + + // Does the same test as above + function testRevert() public { + vm.expectRevert(); + err.throwError(); + } + + // Test with require error message + function testRequireMessage() public { + vm.expectRevert(bytes("not authorized")); + err.throwError(); + } + + // Test custom error + function testCustomError() public { + vm.expectRevert(Error.NotAuthorized.selector); + err.throwCustomError(); + } + + // Test Error Label + function testErrorLabel() public { + assertEq(uint(1), uint(1), "test 1"); + assertEq(uint(1), uint(1), "test 2"); + assertEq(uint(1), uint(1), "test 3"); + assertEq(uint(1), uint(1), "test 4"); + // assertEq(uint(0), uint(2), "test 5"); + } + +}