From 737f01326c2184f1aca8d201520dabd46e11fc39 Mon Sep 17 00:00:00 2001 From: Mark Spanbroek Date: Mon, 11 Jan 2021 17:28:55 +0100 Subject: [PATCH] Make API identical to std/unittest Also includes a fix for GcUnsafe2 warnings with chronos. --- Readme.md | 14 ++++++-------- asynctest.nim | 31 ++++++++++++++++++++----------- asynctest.nimble | 2 +- tests/testAsyncTest.nim | 7 +++---- 4 files changed, 30 insertions(+), 24 deletions(-) diff --git a/Readme.md b/Readme.md index ae22718..8d7307d 100644 --- a/Readme.md +++ b/Readme.md @@ -11,22 +11,20 @@ Use the [Nimble][2] package manager to add asynctest to an existing project. Add the following to its .nimble file: ```nim -requires "asynctest >= 0.1.0 & < 0.2.0" +requires "asynctest >= 0.2.0 & < 0.3.0" ``` Usage ----- -Simply replace `test` with `asynctest` when you need to await asynchronous calls -in the test. The same holds for `asyncsetup` and `asyncteardown`, which allow -you to await asynchronous calls during test setup and teardown. +Simply replace `import unittest` with `import asynctest`, and you can await +asynchronous calls in tests, setup and teardown. Example ------- ```nim -import unittest import asynctest import asyncdispatch # alternatively: import chronos @@ -35,15 +33,15 @@ proc someAsyncProc {.async.} = suite "test async proc": - asyncsetup: + setup: # invoke await in the test setup: await someAsyncProc() - asyncteardown: + teardown: # invoke await in the test teardown: await someAsyncProc() - asynctest "some test": + test "async test": # invoke await in tests: await someAsyncProc() diff --git a/asynctest.nim b/asynctest.nim index 53dc553..ce88688 100644 --- a/asynctest.nim +++ b/asynctest.nim @@ -1,14 +1,23 @@ -template asynctest*(name, body) = +import unittest +export unittest except suite, test + +template suite*(name, body) = + suite name: + + template setup(setupBody) = + setup: + let asyncproc = proc {.async.} = setupBody + waitFor asyncproc() + + template teardown(teardownBody) = + teardown: + let asyncproc = proc {.async.} = teardownBody + waitFor asyncproc() + + let suiteproc = proc = body # Avoids GcUnsafe2 warnings with chronos + suiteproc() + +template test*(name, body) = test name: let asyncproc = proc {.async.} = body waitFor asyncproc() - -template asyncsetup*(body) = - setup: - let asyncproc = proc {.async.} = body - waitFor asyncproc() - -template asyncteardown*(body) = - teardown: - let asyncproc = proc {.async.} = body - waitFor asyncproc() diff --git a/asynctest.nimble b/asynctest.nimble index 33ef1ff..8b74e95 100644 --- a/asynctest.nimble +++ b/asynctest.nimble @@ -1,4 +1,4 @@ -version = "0.1.0" +version = "0.2.0" author = "asynctest Authors" description = "Test asynchronous code" license = "MIT" diff --git a/tests/testAsyncTest.nim b/tests/testAsyncTest.nim index a7d7caa..f20d5d2 100644 --- a/tests/testAsyncTest.nim +++ b/tests/testAsyncTest.nim @@ -1,4 +1,3 @@ -import std/unittest import std/asyncdispatch import pkg/asynctest @@ -8,14 +7,14 @@ proc someAsyncProc {.async.} = suite "test async proc": - asyncsetup: + setup: # invoke await in the test setup: await someAsyncProc() - asyncteardown: + teardown: # invoke await in the test teardown: await someAsyncProc() - asynctest "async test": + test "async test": # invoke await in tests: await someAsyncProc()