Skip to content

Commit

Permalink
Merge pull request #32 from DilumAluthge/da/nowarn-all
Browse files Browse the repository at this point in the history
Add `nowarn=:all` option to `@check` and `check`
  • Loading branch information
pfitzseb authored Aug 23, 2019
2 parents c236177 + 3272fef commit 9896adf
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 5 deletions.
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`"
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

0 comments on commit 9896adf

Please sign in to comment.