Skip to content

Commit f1a7251

Browse files
committed
runtests: allow passing a string, which is then wrapped in a Regex
1 parent 710db5f commit f1a7251

File tree

2 files changed

+67
-6
lines changed

2 files changed

+67
-6
lines changed

src/InlineTest.jl

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -109,18 +109,47 @@ macro testset(args...)
109109
end
110110

111111
"""
112-
runtests([m::Module]; [wrap::Bool])
112+
runtests([m::Module], pattern = r""; [wrap::Bool])
113113
114114
Run all the tests declared in `@testset` blocks, within `m` if specified,
115115
or within all currently loaded modules otherwise.
116-
The `wrap` keyword specifies whether the collection of `@testset` blocks derived
117-
from `@testset` declarations should be grouped within a top-level `@testset`.
116+
The `wrap` keyword specifies whether the collection of `@testset` expressions
117+
should be grouped according to the parent modules within a top-level `@testset`.
118118
The default is `wrap=false` when `m` is specified, `true` otherwise.
119119
120-
Note: this function executes each (top-level) `@testset` block using `eval` *within* the module
121-
in which it was written (e.g. `m`, when specified).
120+
It's possible to filter run testsets by specifying `pattern`: the "subject" of a
121+
testset is the concatenation of the subject of its parent `@testset`, if any,
122+
with `"/\$description"` where `description` is the testset's description.
123+
For example:
124+
```julia
125+
@testset "a" begin # subject == "/a"
126+
@testset "b" begin # subject is "/a/b"
127+
end
128+
@testset "c\$i" for i=1:2 # subjects are "/a/c1" & "/a/c2"
129+
end
130+
end
131+
```
132+
A testset is guaranteed to run only when its parent's subject "partially matches"
133+
`pattern::Regex` (in PCRE2 parlance, i.e. `subject` might be the prefix of a string
134+
matched by `pattern`) and its subject matches `pattern`.
135+
136+
If the passed `pattern` is a string, then it is wrapped in a `Regex` prefixed with
137+
`".*"`, and must match literally the subjects.
138+
This means for example that `"a|b"` will match a subject like `"a|b"` but not like `"a"`.
139+
The `".*"` prefix is intended to allow matching subjects of nested testsets,
140+
e.g. in the example above, `r".*b"` partially matches the subject `"/a"` and
141+
matches the subject `"/a/b"` (so the corresponding nested testset is run),
142+
whereas `r"b"` does not partially match `"/a"`, even if it matches `"/a/b"`,
143+
so the testset is not run.
144+
145+
Note: this function executes each (top-level) `@testset` block using `eval` *within* the
146+
module in which it was written (e.g. `m`, when specified).
122147
"""
123-
function runtests(m::Module, regex::Regex = r""; wrap::Bool=false)
148+
function runtests(m::Module, pattern::Union{AbstractString,Regex} = r""; wrap::Bool=false)
149+
regex = pattern isa Regex ?
150+
pattern :
151+
r".*" * pattern
152+
124153
partial = partialize(regex)
125154
matches(desc, final) = Testset.partialoccursin((partial, regex)[1+final],
126155
string('/', desc, final ? "" : "/"))

test/runtests.jl

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,3 +120,35 @@ N.check(".*j1", ["i", "j", "l1"])
120120
N.check(".*j/1", ["i", "j", "l1"])
121121
N.check("^/i/j0", ["i"])
122122
N.check("^/i/l10", ["i"])
123+
124+
module P
125+
using InlineTest
126+
127+
RUN = []
128+
129+
# testing non-final non-toplevel testsets
130+
@testset "a" begin
131+
push!(RUN, "a")
132+
@test true
133+
134+
@testset "b" begin
135+
push!(RUN, "b")
136+
@test true
137+
end
138+
139+
@testset "b|c" begin
140+
push!(RUN, "b|c")
141+
@test true
142+
end
143+
end
144+
145+
function check(rx, list)
146+
empty!(RUN)
147+
runtests(P, rx)
148+
@test sort(RUN) == sort(list)
149+
end
150+
end
151+
152+
import .P # test InlineTest's wrapping of non-regex patterns
153+
P.check("b", ["a", "b", "b|c"]) # an implicit prefix r".*" is added
154+
P.check("b|c", ["a", "b|c"]) # "b" is not matched

0 commit comments

Comments
 (0)