Skip to content

Commit f30cef5

Browse files
authored
Merge pull request #38 from DilumAluthge/da/all-except
Add `nowarn=:allexcept` and `except=[f, g, h, ...]` options to `@check` and `check`
2 parents c48305b + 8f38db0 commit f30cef5

File tree

2 files changed

+38
-18
lines changed

2 files changed

+38
-18
lines changed

src/check.jl

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,25 +16,37 @@ macro should_not_warn(expr)
1616
end
1717

1818
"""
19-
check(f::Function; nowarn=[], kwargs...)
19+
check(f::Function; nowarn=[], except=[], kwargs...)
2020
2121
Run Traceur on `f`, and throw an error if any warnings occur inside functions
22-
tagged with `@should_not_warn` or specified in `nowarn`. To throw an error
23-
if any warnings occur inside any functions, set `nowarn=:all`.
22+
tagged with `@should_not_warn` or specified in `nowarn`.
23+
24+
To throw an error if any warnings occur inside any functions, set
25+
`nowarn=:all`.
26+
27+
To throw an error if any warnings occur inside any functions EXCEPT for a
28+
certain set of functions, list the exceptions in the `except` variable,
29+
for example `except=[f,g,h]`
2430
"""
25-
function check(f; nowarn=Any[], kwargs...)
26-
if nowarn isa Symbol
31+
function check(f; nowarn=Any[], except=Any[], kwargs...)
32+
if !isempty(except) # if `except` is provided, we ignore the value of `nowarn`
33+
_nowarn = Any[]
34+
_nowarn_all = false
35+
_nowarn_allexcept = true
36+
elseif nowarn isa Symbol
2737
_nowarn = Any[]
2838
_nowarn_all = nowarn == :all
39+
_nowarn_allexcept = false
2940
else
3041
_nowarn = nowarn
3142
_nowarn_all = false
32-
end
43+
_nowarn_allexcept = false
44+
end
3345
failed = false
3446
wp = warning_printer()
3547
result = trace(f; kwargs...) do warning
3648
ix = findfirst(warning.stack) do call
37-
_nowarn_all || call.f in should_not_warn || call.f in _nowarn
49+
_nowarn_all || call.f in should_not_warn || call.f in _nowarn || (_nowarn_allexcept && !(call.f in except))
3850
end
3951
if ix != nothing
4052
tagged_function = warning.stack[ix].f
@@ -49,11 +61,17 @@ function check(f; nowarn=Any[], kwargs...)
4961
end
5062

5163
"""
52-
@check fun(args...) nowarn=[] maxdepth=typemax(Int)
64+
@check fun(args...) nowarn=[] except=[] maxdepth=typemax(Int)
5365
5466
Run Traceur on `fun`, and throw an error if any warnings occur inside functions
55-
tagged with `@should_not_warn` or specified in `nowarn`. To throw an error
56-
if any warnings occur inside any functions, set `nowarn=:all`.
67+
tagged with `@should_not_warn` or specified in `nowarn`.
68+
69+
To throw an error if any warnings occur inside any functions, set
70+
`nowarn=:all`.
71+
72+
To throw an error if any warnings occur inside any functions EXCEPT for a
73+
certain set of functions, list the exceptions in the `except` variable,
74+
for example `except=[f,g,h]`
5775
"""
5876
macro check(expr, args...)
5977
quote

test/runtests.jl

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -121,19 +121,21 @@ my_stable_add_undecorated(y) = my_add(y)
121121
function bar(x)
122122
x > 0 ? 1.0 : 1
123123
end
124-
@test @check(bar(2)) == 1.0
125-
@test @check(bar(2), maxdepth=100) == 1.0
126-
@test @check(bar(2), nowarn=:none) == 1.0
127-
@test @check(bar(2), nowarn=:none, maxdepth=100) == 1.0
128-
@test @check(bar(2), nowarn=[]) == 1.0
129-
@test @check(bar(2), nowarn=[], maxdepth=100) == 1.0
130-
@test @check(bar(2), nowarn=Any[]) == 1.0
131-
@test @check(bar(2), nowarn=Any[], maxdepth=100) == 1.0
124+
@test_nowarn @check(bar(2)) == 1.0
125+
@test_nowarn @check(bar(2), maxdepth=100) == 1.0
126+
@test_nowarn @check(bar(2), nowarn=[]) == 1.0
127+
@test_nowarn @check(bar(2), nowarn=[], maxdepth=100) == 1.0
128+
@test_nowarn @check(bar(2), nowarn=Any[]) == 1.0
129+
@test_nowarn @check(bar(2), nowarn=Any[], maxdepth=100) == 1.0
132130
@test_throws AssertionError @check(bar(2), nowarn=[bar])
133131
@test_throws AssertionError @check(bar(2), nowarn=[bar], maxdepth=100)
134132
@test_throws AssertionError @check(bar(2), nowarn=Any[bar])
135133
@test_throws AssertionError @check(bar(2), nowarn=Any[bar], maxdepth=100)
136134
@test_throws AssertionError @check(bar(2), nowarn=:all)
137135
@test_throws AssertionError @check(bar(2), nowarn=:all, maxdepth=100)
136+
@test_nowarn @check(bar(2), except=[bar]) == 1.0
137+
@test_nowarn @check(bar(2), except=[bar], maxdepth=100) == 1.0
138+
@test_nowarn @check(bar(2), except=Any[bar]) == 1.0
139+
@test_nowarn @check(bar(2), except=Any[bar], maxdepth=100) == 1.0
138140
end
139141
end

0 commit comments

Comments
 (0)