Skip to content

Commit

Permalink
fix nightly (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
jw3126 authored Jan 3, 2018
1 parent 08ec44e commit 75b6a98
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 25 deletions.
17 changes: 10 additions & 7 deletions src/ArgCheck.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -67,7 +66,9 @@ function argcheck_call(ex, args...)
)
quote
$(assignments...)
if !($condition)
if $condition
nothing
else
throw($err)
end
end
Expand All @@ -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
Expand All @@ -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...)
Expand Down
50 changes: 32 additions & 18 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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))
Expand Down

0 comments on commit 75b6a98

Please sign in to comment.