Skip to content

Commit 1cd023c

Browse files
committed
rename at-addtest to at-testset (suggested by at-oxinabox)
1 parent af4d108 commit 1cd023c

File tree

2 files changed

+32
-20
lines changed

2 files changed

+32
-20
lines changed

README.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ This is useful when one likes to have definitions of methods and corresponding t
55
This is also useful for code which is not (yet) organized as a package, and where one doesn't want to maintain
66
a separate set of files for tests.
77

8-
The `@addtest` macro can be used as a direct replacement for `@testset`, and `runtests()` has to
9-
be called for the tests to be executed. See the docstrings for more details.
8+
The exported `InlineTest.@testset` macro can be used as a direct replacement for `Test.@testset`,
9+
and `runtests()` has to be called for the tests to be executed. See the docstrings for more details.
1010
When used in a package `MyPackage`, the following can be added to its `test/runtests.jl` file
11-
in order to have `@addtest` tests run as part of the usual package testing process: `runtests(MyPackage)`
11+
in order to have `InlineTest.@testset` tests run as part of the usual package testing process:
12+
`runtests(MyPackage)` (depending on whether `Test` is imported in the test file -- in which case
13+
there would be a name resolution conflict for `@testset` -- you might bring
14+
`runtests` in scope either with `using InlineTest` or `using InlineTest: runtests`).

src/InlineTest.jl

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
module InlineTest
22

3-
export @addtest, runtests, @testset, @test, @test_throws
3+
export runtests, @testset, @test, @test_throws, Test
44

5-
using Test: @test, @test_throws, @testset
6-
import Test
5+
using Test: Test, @test, @test_throws
76

87
const INLINE_TEST = Ref{Symbol}(:__INLINE_TEST__)
98

@@ -17,40 +16,50 @@ function tests(m)
1716
getfield(m, inline_test)
1817
end
1918

19+
replacetestset(x) = x
20+
21+
# replace unqualified `@testset` by Test.@testset
22+
function replacetestset(x::Expr)
23+
x.head === :macrocall && x.args[1] === Symbol("@testset") ?
24+
Expr(:macrocall, Expr(:., :Test, QuoteNode(Symbol("@testset"))), map(replacetestset, x.args[2:end])...) :
25+
Expr(x.head, map(replacetestset, x.args)...)
26+
end
27+
2028
function addtest(args::Tuple, m::Module)
21-
push!(tests(m), :(@testset($(args...))))
29+
args = map(replacetestset, args)
30+
push!(tests(m), :(InlineTest.Test.@testset($(args...))))
2231
nothing
2332
end
2433

2534
"""
26-
@addtest args...
35+
@testset args...
2736
28-
Similar to `@testset args...`, but the contained tests are not run immediately,
37+
Similar to `Test.@testset args...`, but the contained tests are not run immediately,
2938
and are instead stored for later execution, triggered by `runtests()`.
30-
Invocations of `@addtest` should appear only at the top level, and not be nested
31-
(`@testset` can be nested within `@addtest`).
32-
Internally, `@addtest` is converted to `@testset` at execution time.
39+
Invocations of `@testset` can be nested, but qualified invocations of
40+
`InlineTest.@testset` can't.
41+
Internally, `@testset` invocations are converted to `Test.@testset` at execution time.
3342
"""
34-
macro addtest(args...)
43+
macro testset(args...)
3544
Expr(:call, :addtest, args, __module__)
3645
end
3746

3847
"""
3948
runtests([m::Module]; [wrap::Bool])
4049
41-
Run all the tests declared in `@addtest` blocks, within `m` if specified,
50+
Run all the tests declared in `@testset` blocks, within `m` if specified,
4251
or within all currently loaded modules otherwise.
4352
The `wrap` keyword specifies whether the collection of `@testset` blocks derived
44-
from `@addtest` declarations should be grouped within a top-level `@testset`.
53+
from `@testset` declarations should be grouped within a top-level `@testset`.
4554
The default is `wrap=false` when `m` is specified, `true` otherwise.
4655
47-
Note: this function executes each `@testset` block using `eval` *within* the module
48-
in which the corresponding `@addtest` block was written (e.g. `m`, when specified).
56+
Note: this function executes each (top-level) `@testset` block using `eval` *within* the module
57+
in which it was written (e.g. `m`, when specified).
4958
"""
5059
function runtests(m::Module; wrap::Bool=false)
5160
Core.eval(m,
5261
if wrap
53-
:(@testset $("Tests for module $m") begin
62+
:(InlineTest.Test.@testset $("Tests for module $m") begin
5463
$(tests(m)...)
5564
end)
5665
else
@@ -71,13 +80,13 @@ end
7180
module InlineTestTest
7281

7382
using ..InlineTest
74-
@addtest "test Test in sub-module" begin
83+
@testset "test Test in sub-module" begin
7584
@test 1 == 1
7685
end
7786

7887
end # module InlineTestTest
7988

80-
@addtest "self test" begin
89+
@testset "self test" begin
8190
@assert typeof(@__MODULE__) == Module
8291
@test 1 != 2
8392
runtests(InlineTestTest)

0 commit comments

Comments
 (0)