diff --git a/testmodule/helpers.nim b/ethers/testing.nim similarity index 84% rename from testmodule/helpers.nim rename to ethers/testing.nim index 3a577d2..ae5fe4a 100644 --- a/testmodule/helpers.nim +++ b/ethers/testing.nim @@ -2,7 +2,7 @@ import std/json import std/strutils import pkg/ethers -proc revertReason*(e: ref EthersError): string = +proc revertReason*(e: ref JsonRpcProviderError): string = try: let json = parseJson(e.msg) var msg = json{"message"}.getStr @@ -25,10 +25,8 @@ template reverts*(body: untyped): untyped = try: body return false - except EthersError: + except JsonRpcProviderError: return true - except CatchableError: - return false waitFor asyncproc() template revertsWith*(reason: string, body: untyped): untyped = @@ -36,10 +34,8 @@ template revertsWith*(reason: string, body: untyped): untyped = try: body return false - except EthersError as e: + except JsonRpcProviderError as e: return reason == revertReason(e) - except CatchableError as e: - return false waitFor asyncproc() template doesNotRevert*(body: untyped): untyped = @@ -50,4 +46,4 @@ template doesNotRevert*(body: untyped): untyped = template doesNotRevertWith*(reason: string, body: untyped): untyped = let asyncproc = proc(): Future[bool] {.async.} = return not revertsWith(reason, body) - waitFor asyncproc() \ No newline at end of file + waitFor asyncproc() diff --git a/testmodule/test.nim b/testmodule/test.nim index cc4199d..e4da7c3 100644 --- a/testmodule/test.nim +++ b/testmodule/test.nim @@ -5,5 +5,6 @@ import ./testReturns import ./testEnums import ./testEvents import ./testWallet +import ./testTesting {.warning[UnusedImport]:off.} diff --git a/testmodule/testHelpers.nim b/testmodule/testTesting.nim similarity index 71% rename from testmodule/testHelpers.nim rename to testmodule/testTesting.nim index 25aadef..95044d6 100644 --- a/testmodule/testHelpers.nim +++ b/testmodule/testTesting.nim @@ -3,10 +3,10 @@ import std/strformat import pkg/asynctest import pkg/chronos import pkg/ethers +import pkg/ethers/testing import ./hardhat -import ./helpers -suite "Revert helpers": +suite "Testing helpers": let revertReason = "revert reason" let rpcResponse = %* { @@ -16,7 +16,7 @@ suite "Revert helpers": test "can use block syntax async": let ethCallAsync = proc() {.async.} = - raise newException(EthersError, $rpcResponse) + raise newException(JsonRpcProviderError, $rpcResponse) check: reverts: @@ -24,7 +24,7 @@ suite "Revert helpers": test "can use block syntax sync": let ethCall = proc() = - raise newException(EthersError, $rpcResponse) + raise newException(JsonRpcProviderError, $rpcResponse) check: reverts: @@ -32,21 +32,21 @@ suite "Revert helpers": test "can use parameter syntax async": let ethCallAsync = proc() {.async.} = - raise newException(EthersError, $rpcResponse) + raise newException(JsonRpcProviderError, $rpcResponse) check: reverts (await ethCallAsync()) test "can use parameter syntax sync": let ethCall = proc() = - raise newException(EthersError, $rpcResponse) + raise newException(JsonRpcProviderError, $rpcResponse) check: reverts ethCall() test "successfully checks revert reason async": let ethCallAsync = proc() {.async.} = - raise newException(EthersError, $rpcResponse) + raise newException(JsonRpcProviderError, $rpcResponse) check: revertsWith revertReason: @@ -54,7 +54,7 @@ suite "Revert helpers": test "successfully checks revert reason sync": let ethCall = proc() = - raise newException(EthersError, $rpcResponse) + raise newException(JsonRpcProviderError, $rpcResponse) check: revertsWith revertReason: @@ -68,13 +68,33 @@ suite "Revert helpers": doesNotRevert: ethCall() - test "only checks EthersErrors": + test "revert only checks JsonRpcProviderErrors": let ethCall = proc() = - raise newException(ValueError, $rpcResponse) + raise newException(ContractError, "test") - check: - doesNotRevert: - ethCall() + var success = false + + try: + check: + reverts: + ethCall() + except ContractError: + success = true + check success + + test "revertsWith only checks JsonRpcProviderErrors": + let ethCall = proc() = + raise newException(ContractError, "test") + + var success = false + + try: + check: + revertsWith revertReason: + ethCall() + except ContractError: + success = true + check success test "revertsWith is false when there is no revert": let ethCall = proc() = discard @@ -83,17 +103,9 @@ suite "Revert helpers": doesNotRevertWith revertReason: ethCall() - test "revertsWith is false when not an EthersError": - let ethCall = proc() = - raise newException(ValueError, $rpcResponse) - - check: - doesNotRevertWith revertReason: - ethCall() - test "revertsWith is false when the revert reason doesn't match": let ethCall = proc() = - raise newException(EthersError, "other reason") + raise newException(JsonRpcProviderError, "other reason") check: doesNotRevertWith revertReason: @@ -103,7 +115,7 @@ suite "Revert helpers": let nonStdMsg = fmt"Provider VM Exception: reverted with {revertReason}" let nonStdRpcResponse = %* { "message": nonStdMsg } let ethCall = proc() = - raise newException(EthersError, $nonStdRpcResponse) + raise newException(JsonRpcProviderError, $nonStdRpcResponse) check: revertsWith nonStdMsg: @@ -115,7 +127,7 @@ type method revertsWith*(self: TestHelpers, revertReason: string) {.base, contract, view.} -suite "Revert helpers - current provider": +suite "Testing helpers - current provider": var helpersContract: TestHelpers var provider: JsonRpcProvider