Skip to content

Commit 3edb481

Browse files
committed
make -pattern equivalent to not(pattern) when possible
1 parent c7099df commit 3edb481

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

src/patterns.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,9 +169,17 @@ if and only if it doesn't match `pattern`.
169169
170170
For example `not("a")` matches any testset whose subject doesn't contain `"a"`,
171171
and `not(1:3)` matches all the testsets but the first three of a module.
172+
173+
If `pattern` is an integer or a `ReTest` object (i.e. not a `AbstractString`,
174+
`Regex`, `Tuple` or `AbstractArray`), `not(pattern)` can be expressed as `-pattern`.
175+
176+
`String` patterns can also be negated by prepending `'-'`, see [`retest`](@ref)
177+
for details.
172178
"""
173179
not(x) = Not(make_pattern(x))
174180

181+
Base.:-(x::Pattern) = not(x)
182+
175183

176184
"""
177185
interpolated

test/test_patterns.jl

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
module TestPatterns
22
using Test
33

4-
using ReTest: and, or, not, interpolated, reachable
4+
using ReTest: and, or, not, interpolated, reachable, depth
55

66
@testset "patterns: ==" begin
7-
basics = [and(), or(), not(0), interpolated, 0, r""]
7+
basics = [and(), or(), not(0), interpolated, 0, r"", depth(2)]
88
VERSION >= v"1.3" && push!(basics, reachable(1))
99
for a = basics, b = basics
1010
if a === b
1111
@test a == b
12+
if !(a isa Regex || a isa Integer)
13+
@test -a == not(a)
14+
end
1215
else
1316
@test a != b
1417
for f in (and, or)
@@ -39,4 +42,12 @@ using ReTest: and, or, not, interpolated, reachable
3942
end
4043
end
4144

45+
@testset "patterns: not" begin
46+
pats = [or(1, 3), and(1, r"a"), not(1), interpolated, depth(3)]
47+
VERSION >= v"1.3" && push!(pats, reachable("c"))
48+
for p pats
49+
@test -p == not(p)
50+
end
51+
end
52+
4253
end # TestPatterns

0 commit comments

Comments
 (0)