Skip to content

Commit 1fc23b7

Browse files
committed
Add nowarn=:allexcept and except=[f, g, h, ...] options to @check and check
1 parent c48305b commit 1fc23b7

File tree

2 files changed

+40
-17
lines changed

2 files changed

+40
-17
lines changed

src/check.jl

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,25 +16,40 @@ 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, set `nowarn=:allexcept` and list the exceptions in
29+
the `except` variable, i.e. set `except=[f, g, h, ...]`
2430
"""
25-
function check(f; nowarn=Any[], kwargs...)
31+
function check(f; nowarn=Any[], except=Any[], kwargs...)
2632
if nowarn isa Symbol
2733
_nowarn = Any[]
28-
_nowarn_all = nowarn == :all
34+
if nowarn == :all
35+
_nowarn_all = true
36+
_nowarn_allexcept = false
37+
elseif nowarn == :allexcept
38+
_nowarn_all = false
39+
_nowarn_allexcept = true
40+
else
41+
throw(ArgumentError(":$(nowarn) is not a valid value for nowarn"))
42+
end
2943
else
3044
_nowarn = nowarn
3145
_nowarn_all = false
46+
_nowarn_allexcept = false
3247
end
3348
failed = false
3449
wp = warning_printer()
3550
result = trace(f; kwargs...) do warning
3651
ix = findfirst(warning.stack) do call
37-
_nowarn_all || call.f in should_not_warn || call.f in _nowarn
52+
_nowarn_all || call.f in should_not_warn || call.f in _nowarn || (_nowarn_allexcept && !(call.f in except))
3853
end
3954
if ix != nothing
4055
tagged_function = warning.stack[ix].f
@@ -49,11 +64,17 @@ function check(f; nowarn=Any[], kwargs...)
4964
end
5065

5166
"""
52-
@check fun(args...) nowarn=[] maxdepth=typemax(Int)
67+
@check fun(args...) nowarn=[] except=[] maxdepth=typemax(Int)
5368
5469
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`.
70+
tagged with `@should_not_warn` or specified in `nowarn`.
71+
72+
To throw an error if any warnings occur inside any functions, set
73+
`nowarn=:all`.
74+
75+
To throw an error if any warnings occur inside any functions EXCEPT for a
76+
certain set of functions, set `nowarn=:allexcept` and list the exceptions in
77+
the `except` variable, i.e. set `except=[f, g, h, ...]`
5778
"""
5879
macro check(expr, args...)
5980
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), nowarn=:allexcept, except=[bar]) == 1.0
137+
@test_nowarn @check(bar(2), nowarn=:allexcept, except=[bar], maxdepth=100) == 1.0
138+
@test_nowarn @check(bar(2), nowarn=:allexcept, except=Any[bar]) == 1.0
139+
@test_nowarn @check(bar(2), nowarn=:allexcept, except=Any[bar], maxdepth=100) == 1.0
138140
end
139141
end

0 commit comments

Comments
 (0)