mirror of
https://github.com/status-im/nim-libp2p.git
synced 2025-01-20 18:00:04 +00:00
Add utility functions.
This commit is contained in:
parent
c6e8fadbda
commit
a85be0463e
10
tests/utils/assertions.nim
Normal file
10
tests/utils/assertions.nim
Normal file
@ -0,0 +1,10 @@
|
||||
import stew/results, options
|
||||
|
||||
proc assertIsOk*[T, E](res: Result[T, E]) =
|
||||
assert res.isOk, res.error
|
||||
|
||||
proc assertIsErr*[T, E](res: Result[T, E], error: Option[E] = E.none()) =
|
||||
assert res.isErr, "Result was \"Err\" but expected \"Ok\""
|
||||
if error.isSome():
|
||||
assert res.error == error.get(),
|
||||
"Result was \"" & res.error & "\" but expected \"" & error.get() & "\""
|
10
tests/utils/async.nim
Normal file
10
tests/utils/async.nim
Normal file
@ -0,0 +1,10 @@
|
||||
import chronos/futures, stew/results, chronos
|
||||
|
||||
proc waitForResult*[T](
|
||||
fut: Future[T], timeout: Duration
|
||||
): Future[Result[T, string]] {.async.} =
|
||||
try:
|
||||
let val = await fut.wait(timeout)
|
||||
return Result[T, string].ok(val)
|
||||
except Exception as e:
|
||||
return Result[T, string].err(e.msg)
|
Loading…
x
Reference in New Issue
Block a user