PR comments

1. rename helpers to testing and expose externally via `import pkg/ethers/testing`
2. Change detection of revert from `EthersError` to `JsonRpcProviderError`
3, Remove catch of `CatchableError` from revert detection as this would swallow errors. Update tests accordingly.
This commit is contained in:
Eric Mastro 2022-09-20 11:59:39 +10:00 committed by Eric Mastro
parent f8ba91a297
commit 5fe41a76ab
3 changed files with 41 additions and 32 deletions

View File

@ -2,7 +2,7 @@ import std/json
import std/strutils import std/strutils
import pkg/ethers import pkg/ethers
proc revertReason*(e: ref EthersError): string = proc revertReason*(e: ref JsonRpcProviderError): string =
try: try:
let json = parseJson(e.msg) let json = parseJson(e.msg)
var msg = json{"message"}.getStr var msg = json{"message"}.getStr
@ -25,10 +25,8 @@ template reverts*(body: untyped): untyped =
try: try:
body body
return false return false
except EthersError: except JsonRpcProviderError:
return true return true
except CatchableError:
return false
waitFor asyncproc() waitFor asyncproc()
template revertsWith*(reason: string, body: untyped): untyped = template revertsWith*(reason: string, body: untyped): untyped =
@ -36,10 +34,8 @@ template revertsWith*(reason: string, body: untyped): untyped =
try: try:
body body
return false return false
except EthersError as e: except JsonRpcProviderError as e:
return reason == revertReason(e) return reason == revertReason(e)
except CatchableError as e:
return false
waitFor asyncproc() waitFor asyncproc()
template doesNotRevert*(body: untyped): untyped = template doesNotRevert*(body: untyped): untyped =
@ -50,4 +46,4 @@ template doesNotRevert*(body: untyped): untyped =
template doesNotRevertWith*(reason: string, body: untyped): untyped = template doesNotRevertWith*(reason: string, body: untyped): untyped =
let asyncproc = proc(): Future[bool] {.async.} = let asyncproc = proc(): Future[bool] {.async.} =
return not revertsWith(reason, body) return not revertsWith(reason, body)
waitFor asyncproc() waitFor asyncproc()

View File

@ -5,5 +5,6 @@ import ./testReturns
import ./testEnums import ./testEnums
import ./testEvents import ./testEvents
import ./testWallet import ./testWallet
import ./testTesting
{.warning[UnusedImport]:off.} {.warning[UnusedImport]:off.}

View File

@ -3,10 +3,10 @@ import std/strformat
import pkg/asynctest import pkg/asynctest
import pkg/chronos import pkg/chronos
import pkg/ethers import pkg/ethers
import pkg/ethers/testing
import ./hardhat import ./hardhat
import ./helpers
suite "Revert helpers": suite "Testing helpers":
let revertReason = "revert reason" let revertReason = "revert reason"
let rpcResponse = %* { let rpcResponse = %* {
@ -16,7 +16,7 @@ suite "Revert helpers":
test "can use block syntax async": test "can use block syntax async":
let ethCallAsync = proc() {.async.} = let ethCallAsync = proc() {.async.} =
raise newException(EthersError, $rpcResponse) raise newException(JsonRpcProviderError, $rpcResponse)
check: check:
reverts: reverts:
@ -24,7 +24,7 @@ suite "Revert helpers":
test "can use block syntax sync": test "can use block syntax sync":
let ethCall = proc() = let ethCall = proc() =
raise newException(EthersError, $rpcResponse) raise newException(JsonRpcProviderError, $rpcResponse)
check: check:
reverts: reverts:
@ -32,21 +32,21 @@ suite "Revert helpers":
test "can use parameter syntax async": test "can use parameter syntax async":
let ethCallAsync = proc() {.async.} = let ethCallAsync = proc() {.async.} =
raise newException(EthersError, $rpcResponse) raise newException(JsonRpcProviderError, $rpcResponse)
check: check:
reverts (await ethCallAsync()) reverts (await ethCallAsync())
test "can use parameter syntax sync": test "can use parameter syntax sync":
let ethCall = proc() = let ethCall = proc() =
raise newException(EthersError, $rpcResponse) raise newException(JsonRpcProviderError, $rpcResponse)
check: check:
reverts ethCall() reverts ethCall()
test "successfully checks revert reason async": test "successfully checks revert reason async":
let ethCallAsync = proc() {.async.} = let ethCallAsync = proc() {.async.} =
raise newException(EthersError, $rpcResponse) raise newException(JsonRpcProviderError, $rpcResponse)
check: check:
revertsWith revertReason: revertsWith revertReason:
@ -54,7 +54,7 @@ suite "Revert helpers":
test "successfully checks revert reason sync": test "successfully checks revert reason sync":
let ethCall = proc() = let ethCall = proc() =
raise newException(EthersError, $rpcResponse) raise newException(JsonRpcProviderError, $rpcResponse)
check: check:
revertsWith revertReason: revertsWith revertReason:
@ -68,13 +68,33 @@ suite "Revert helpers":
doesNotRevert: doesNotRevert:
ethCall() ethCall()
test "only checks EthersErrors": test "revert only checks JsonRpcProviderErrors":
let ethCall = proc() = let ethCall = proc() =
raise newException(ValueError, $rpcResponse) raise newException(ContractError, "test")
check: var success = false
doesNotRevert:
ethCall() 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": test "revertsWith is false when there is no revert":
let ethCall = proc() = discard let ethCall = proc() = discard
@ -83,17 +103,9 @@ suite "Revert helpers":
doesNotRevertWith revertReason: doesNotRevertWith revertReason:
ethCall() 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": test "revertsWith is false when the revert reason doesn't match":
let ethCall = proc() = let ethCall = proc() =
raise newException(EthersError, "other reason") raise newException(JsonRpcProviderError, "other reason")
check: check:
doesNotRevertWith revertReason: doesNotRevertWith revertReason:
@ -103,7 +115,7 @@ suite "Revert helpers":
let nonStdMsg = fmt"Provider VM Exception: reverted with {revertReason}" let nonStdMsg = fmt"Provider VM Exception: reverted with {revertReason}"
let nonStdRpcResponse = %* { "message": nonStdMsg } let nonStdRpcResponse = %* { "message": nonStdMsg }
let ethCall = proc() = let ethCall = proc() =
raise newException(EthersError, $nonStdRpcResponse) raise newException(JsonRpcProviderError, $nonStdRpcResponse)
check: check:
revertsWith nonStdMsg: revertsWith nonStdMsg:
@ -115,7 +127,7 @@ type
method revertsWith*(self: TestHelpers, method revertsWith*(self: TestHelpers,
revertReason: string) {.base, contract, view.} revertReason: string) {.base, contract, view.}
suite "Revert helpers - current provider": suite "Testing helpers - current provider":
var helpersContract: TestHelpers var helpersContract: TestHelpers
var provider: JsonRpcProvider var provider: JsonRpcProvider