From a85be0463ee6c60ad7ec1101f9f456a45c3e287f Mon Sep 17 00:00:00 2001 From: Alejandro Cabeza Romero Date: Wed, 7 Aug 2024 21:05:33 +0200 Subject: [PATCH] Add utility functions. --- tests/utils/assertions.nim | 10 ++++++++++ tests/utils/async.nim | 10 ++++++++++ 2 files changed, 20 insertions(+) create mode 100644 tests/utils/assertions.nim create mode 100644 tests/utils/async.nim diff --git a/tests/utils/assertions.nim b/tests/utils/assertions.nim new file mode 100644 index 000000000..86e97fea2 --- /dev/null +++ b/tests/utils/assertions.nim @@ -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() & "\"" diff --git a/tests/utils/async.nim b/tests/utils/async.nim new file mode 100644 index 000000000..2ddf82f6e --- /dev/null +++ b/tests/utils/async.nim @@ -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)