diff --git a/src/ArgCheck.jl b/src/ArgCheck.jl index a1a2d41..7276554 100644 --- a/src/ArgCheck.jl +++ b/src/ArgCheck.jl @@ -22,7 +22,6 @@ macro argcheck(code,args...) end function argcheck(ex, args...) - ex = canonicalize(ex) if isexpr(ex, :comparison) argcheck_comparison(ex, args...) elseif is_simple_call(ex) @@ -67,7 +66,9 @@ function argcheck_call(ex, args...) ) quote $(assignments...) - if !($condition) + if $condition + nothing + else throw($err) end end @@ -77,11 +78,11 @@ function argcheck_comparison(ex, args...) exprs = ex.args[1:2:end] ops = ex.args[2:2:end] variables = [gensym() for _ in 1:length(exprs)] - ret = quote end + ret = [] rhs = exprs[1] vrhs = variables[1] assignment = Expr(:(=), vrhs, esc(rhs)) - push!(ret.args, assignment) + push!(ret, assignment) for i in eachindex(ops) op = ops[i] lhs = rhs @@ -96,13 +97,15 @@ function argcheck_comparison(ex, args...) vlhs, vrhs, esc.(args)...) reti = quote $assignment - if !($condition) + if $condition + nothing + else throw($err) end end - append!(ret.args, reti.args) + append!(ret, reti.args) end - ret + Expr(:block, ret...) end function build_error(code, T::Type{<:Exception}, args...) diff --git a/test/runtests.jl b/test/runtests.jl index fbba7c3..a4d1aae 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -68,28 +68,38 @@ end end # exotic cases -f(args...) = false -t(args...) = true -@argcheck t() -@test_throws ArgumentError @argcheck f() - -op() = (x,y) -> x < y -x = 1; y = 2 -@argcheck op()(x,y) -@test_throws ArgumentError @argcheck op()(y,x) -@test_throws ArgumentError @argcheck begin false end -@test_throws DivideError @argcheck f() DivideError() - +struct MyError <: Exception + msg::String +end struct MyExoticError <: Exception a::Int b::Int end -err = @catch_exception_object @argcheck false MyExoticError(1,2) -@test err === MyExoticError(1,2) +falsy(args...) = false +truthy(args...) = true -struct MyError <: Exception - msg::String +@testset "exotic cases" begin + @argcheck truthy() + @test_throws ArgumentError @argcheck falsy() + + @argcheck begin + multi_line_true_is_no_problem = true + multi_line_true_is_no_problem + end + @test_throws DimensionMismatch @argcheck let + falsy(1,2) + end DimensionMismatch + + + op() = (x,y) -> x < y + x = 1; y = 2 + @argcheck op()(x,y) + @test_throws ArgumentError @argcheck op()(y,x) + @test_throws ArgumentError @argcheck begin false end + @test_throws DivideError @argcheck falsy() DivideError() + err = @catch_exception_object @argcheck false MyExoticError(1,2) + @test err === MyExoticError(1,2) end @testset "error message comparison" begin @@ -127,11 +137,15 @@ end @test contains(msg, "≦") end -@testset "error message call" begin +# In +# We can't wrap this in @testset on julia v0.6 +# because of https://github.com/JuliaLang/julia/issues/24316 +# @testset "error message call" begin +let x = 1.2 y = 1.34 z = -345.234 - err = @catch_exception_object @argcheck f([x y; z z]) + err = @catch_exception_object @argcheck falsy([x y; z z]) msg = err.msg @test contains(msg, string(x)) @test contains(msg, string(z))