Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add nowarn=:all option to @check and check #32

Merged
merged 3 commits into from
Aug 23, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions src/check.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,25 @@ macro should_not_warn(expr)
end

"""
check(f::Function)
check(f::Function; nowarn=[], kwargs...)

Run Traceur on `f`, and throw an error if any warnings occur inside functions
tagged with `@should_not_warn`.
tagged with `@should_not_warn` or specified in `nowarn`. To throw an error
if any warnings occur inside any functions, set `nowarn=:all`.
"""
function check(f; nowarn=Any[], kwargs...)
if nowarn isa Symbol
_nowarn = Any[]
_nowarn_all = nowarn == :all
else
_nowarn = nowarn
_nowarn_all = false
end
failed = false
wp = warning_printer()
result = trace(f; kwargs...) do warning
ix = findfirst(warning.stack) do call
call.f in should_not_warn || call.f in nowarn
_nowarn_all || call.f in should_not_warn || call.f in _nowarn
end
if ix != nothing
tagged_function = warning.stack[ix].f
Expand All @@ -36,15 +44,16 @@ function check(f; nowarn=Any[], kwargs...)
failed = true
end
end
@assert !failed "One or more warnings occured inside functions tagged with `@should_not_warn`"
@assert !failed "One or more warnings occured inside functions tagged with `@should_not_warn or specified with `nowarn`"
DilumAluthge marked this conversation as resolved.
Show resolved Hide resolved
result
end

"""
@check fun(args...) nowarn=[] maxdepth=typemax(Int)

Run Traceur on `fun`, and throw an error if any warnings occur inside functions
tagged with `@should_not_warn` or specified in `nowarn`.
tagged with `@should_not_warn` or specified in `nowarn`. To throw an error
if any warnings occur inside any functions, set `nowarn=:all`.
"""
macro check(expr, args...)
quote
Expand Down
18 changes: 18 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -100,5 +100,23 @@ my_stable_add_undecorated(y) = my_add(y)
@test_nowarn @check my_add(1)
@test_throws AssertionError @check my_stable_add(1)
@test_throws AssertionError @check my_stable_add_undecorated(1) nowarn=[my_stable_add_undecorated]
@test_throws AssertionError @check my_stable_add_undecorated(1) nowarn=:all
function bar(x)
x > 0 ? 1.0 : 1
end
@test @check(bar(2)) == 1.0
@test @check(bar(2), maxdepth=100) == 1.0
@test @check(bar(2), nowarn=:none) == 1.0
@test @check(bar(2), nowarn=:none, maxdepth=100) == 1.0
@test @check(bar(2), nowarn=[]) == 1.0
@test @check(bar(2), nowarn=[], maxdepth=100) == 1.0
@test @check(bar(2), nowarn=Any[]) == 1.0
@test @check(bar(2), nowarn=Any[], maxdepth=100) == 1.0
@test_throws AssertionError @check(bar(2), nowarn=[bar])
@test_throws AssertionError @check(bar(2), nowarn=[bar], maxdepth=100)
@test_throws AssertionError @check(bar(2), nowarn=Any[bar])
@test_throws AssertionError @check(bar(2), nowarn=Any[bar], maxdepth=100)
@test_throws AssertionError @check(bar(2), nowarn=:all)
@test_throws AssertionError @check(bar(2), nowarn=:all, maxdepth=100)
end
end