Add utility functions.

This commit is contained in:
Alejandro Cabeza Romero 2024-08-07 21:05:33 +02:00
parent c6e8fadbda
commit a85be0463e
No known key found for this signature in database
GPG Key ID: DA3D14AE478030FD
2 changed files with 20 additions and 0 deletions

View 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
View 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)