From 2e00a43236387c7fd53757cdd48352fc14f7e13a Mon Sep 17 00:00:00 2001 From: Mark Spanbroek Date: Wed, 7 Jul 2021 10:32:50 +0200 Subject: [PATCH] Add support for unittest2 --- Readme.md | 7 ++++++ asynctest.nim | 24 ++----------------- asynctest.nimble | 6 +++++ asynctest/templates.nim | 20 ++++++++++++++++ asynctest/unittest.nim | 4 ++++ asynctest/unittest2.nim | 4 ++++ testmodules/chronos/nim.cfg | 1 + testmodules/chronos/nimbledeps/.keep | 0 testmodules/chronos/test.nim | 4 ++++ testmodules/chronos/test.nimble | 9 +++++++ testmodules/stdlib/nim.cfg | 1 + testmodules/stdlib/nimbledeps/.keep | 0 testmodules/stdlib/test.nim | 4 ++++ testmodules/stdlib/test.nimble | 7 ++++++ .../stdlib/testbody.nim | 3 --- testmodules/unittest2/nim.cfg | 1 + testmodules/unittest2/test.nim | 4 ++++ testmodules/unittest2/test.nimble | 10 ++++++++ tests/nim.cfg | 1 - 19 files changed, 84 insertions(+), 26 deletions(-) create mode 100644 asynctest/templates.nim create mode 100644 asynctest/unittest.nim create mode 100644 asynctest/unittest2.nim create mode 100644 testmodules/chronos/nim.cfg create mode 100644 testmodules/chronos/nimbledeps/.keep create mode 100644 testmodules/chronos/test.nim create mode 100644 testmodules/chronos/test.nimble create mode 100644 testmodules/stdlib/nim.cfg create mode 100644 testmodules/stdlib/nimbledeps/.keep create mode 100644 testmodules/stdlib/test.nim create mode 100644 testmodules/stdlib/test.nimble rename tests/testAsyncTest.nim => testmodules/stdlib/testbody.nim (87%) create mode 100644 testmodules/unittest2/nim.cfg create mode 100644 testmodules/unittest2/test.nim create mode 100644 testmodules/unittest2/test.nimble delete mode 100644 tests/nim.cfg diff --git a/Readme.md b/Readme.md index 8d7307d..9e6424f 100644 --- a/Readme.md +++ b/Readme.md @@ -47,5 +47,12 @@ suite "test async proc": ``` +Unittest2 +--------- + +The [unittest2][3] package is supported. Make sure that you +`import asynctest/unittest2` instead of the normal import. + [1]: https://nim-lang.org/docs/unittest.html [2]: https://github.com/nim-lang/nimble +[3]: https://github.com/status-im/nim-unittest2 diff --git a/asynctest.nim b/asynctest.nim index 1016607..bfc35e4 100644 --- a/asynctest.nim +++ b/asynctest.nim @@ -1,23 +1,3 @@ -import unittest -export unittest except suite, test +import ./asynctest/unittest -template suite*(name, body) = - suite name: - - template setup(setupBody) {.used.} = - setup: - let asyncproc = proc {.async.} = setupBody - waitFor asyncproc() - - template teardown(teardownBody) {.used.} = - 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() +export unittest diff --git a/asynctest.nimble b/asynctest.nimble index fd848d5..5275ba1 100644 --- a/asynctest.nimble +++ b/asynctest.nimble @@ -4,3 +4,9 @@ description = "Test asynchronous code" license = "MIT" requires "nim >= 1.2.0 & < 2.0.0" + +task test, "Runs the test suite": + for module in ["stdlib", "chronos", "unittest2"]: + withDir "testmodules/" & module: + exec "nimble install -d -y" + exec "nimble test -y" diff --git a/asynctest/templates.nim b/asynctest/templates.nim new file mode 100644 index 0000000..5666475 --- /dev/null +++ b/asynctest/templates.nim @@ -0,0 +1,20 @@ +template suite*(name, body) = + suite name: + + template setup(setupBody) {.used.} = + setup: + let asyncproc = proc {.async.} = setupBody + waitFor asyncproc() + + template teardown(teardownBody) {.used.} = + 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() diff --git a/asynctest/unittest.nim b/asynctest/unittest.nim new file mode 100644 index 0000000..216f53b --- /dev/null +++ b/asynctest/unittest.nim @@ -0,0 +1,4 @@ +import std/unittest +export unittest except suite, test + +include ./templates diff --git a/asynctest/unittest2.nim b/asynctest/unittest2.nim new file mode 100644 index 0000000..ce1d1ef --- /dev/null +++ b/asynctest/unittest2.nim @@ -0,0 +1,4 @@ +import pkg/unittest2 +export unittest2 except suite, test + +include ./templates diff --git a/testmodules/chronos/nim.cfg b/testmodules/chronos/nim.cfg new file mode 100644 index 0000000..1c2f0c1 --- /dev/null +++ b/testmodules/chronos/nim.cfg @@ -0,0 +1 @@ +--path:"../.." diff --git a/testmodules/chronos/nimbledeps/.keep b/testmodules/chronos/nimbledeps/.keep new file mode 100644 index 0000000..e69de29 diff --git a/testmodules/chronos/test.nim b/testmodules/chronos/test.nim new file mode 100644 index 0000000..9a542b6 --- /dev/null +++ b/testmodules/chronos/test.nim @@ -0,0 +1,4 @@ +import pkg/asynctest +import pkg/chronos + +include ../stdlib/testbody diff --git a/testmodules/chronos/test.nimble b/testmodules/chronos/test.nimble new file mode 100644 index 0000000..4b84514 --- /dev/null +++ b/testmodules/chronos/test.nimble @@ -0,0 +1,9 @@ +version = "0.1.0" +author = "Asynctest Authors" +description = "Asynctest tests for std/unittest and pkg/chronos" +license = "MIT" + +requires "chronos" + +task test, "Runs the test suite": + exec "nim c -f -r test.nim" diff --git a/testmodules/stdlib/nim.cfg b/testmodules/stdlib/nim.cfg new file mode 100644 index 0000000..1c2f0c1 --- /dev/null +++ b/testmodules/stdlib/nim.cfg @@ -0,0 +1 @@ +--path:"../.." diff --git a/testmodules/stdlib/nimbledeps/.keep b/testmodules/stdlib/nimbledeps/.keep new file mode 100644 index 0000000..e69de29 diff --git a/testmodules/stdlib/test.nim b/testmodules/stdlib/test.nim new file mode 100644 index 0000000..e8d872b --- /dev/null +++ b/testmodules/stdlib/test.nim @@ -0,0 +1,4 @@ +import std/asyncdispatch +import pkg/asynctest + +include ./testbody diff --git a/testmodules/stdlib/test.nimble b/testmodules/stdlib/test.nimble new file mode 100644 index 0000000..1c12c6b --- /dev/null +++ b/testmodules/stdlib/test.nimble @@ -0,0 +1,7 @@ +version = "0.1.0" +author = "Asynctest Authors" +description = "Asynctest tests for std/unittest and std/asyncdispatch" +license = "MIT" + +task test, "Runs the test suite": + exec "nim c -f -r test.nim" diff --git a/tests/testAsyncTest.nim b/testmodules/stdlib/testbody.nim similarity index 87% rename from tests/testAsyncTest.nim rename to testmodules/stdlib/testbody.nim index f20d5d2..967fc52 100644 --- a/tests/testAsyncTest.nim +++ b/testmodules/stdlib/testbody.nim @@ -1,6 +1,3 @@ -import std/asyncdispatch -import pkg/asynctest - proc someAsyncProc {.async.} = # perform some async operations using await discard diff --git a/testmodules/unittest2/nim.cfg b/testmodules/unittest2/nim.cfg new file mode 100644 index 0000000..1c2f0c1 --- /dev/null +++ b/testmodules/unittest2/nim.cfg @@ -0,0 +1 @@ +--path:"../.." diff --git a/testmodules/unittest2/test.nim b/testmodules/unittest2/test.nim new file mode 100644 index 0000000..77bdd7c --- /dev/null +++ b/testmodules/unittest2/test.nim @@ -0,0 +1,4 @@ +import pkg/asynctest/unittest2 +import pkg/chronos + +include ../stdlib/testbody diff --git a/testmodules/unittest2/test.nimble b/testmodules/unittest2/test.nimble new file mode 100644 index 0000000..bc64818 --- /dev/null +++ b/testmodules/unittest2/test.nimble @@ -0,0 +1,10 @@ +version = "0.1.0" +author = "Asynctest Authors" +description = "Asynctest tests for pkg/unittest2 and pkg/chronos" +license = "MIT" + +requires "unittest2" +requires "chronos" + +task test, "Runs the test suite": + exec "nim c -f -r test.nim" diff --git a/tests/nim.cfg b/tests/nim.cfg deleted file mode 100644 index 0f840a1..0000000 --- a/tests/nim.cfg +++ /dev/null @@ -1 +0,0 @@ ---path:".."