Skip to content

Commit

Permalink
Add CheckError (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
jw3126 authored Mar 21, 2018
1 parent 4e96e48 commit 4dfb99e
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/ArgCheck.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
__precompile__()
module ArgCheck
using Base.Meta
export @argcheck, @check
export @argcheck, @check, CheckError

include("checks.jl")
end
22 changes: 19 additions & 3 deletions src/checks.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
struct CheckError <: Exception
msg::String
end
abstract type AbstractCheckFlavor end
struct ArgCheckFlavor <: AbstractCheckFlavor end
struct CheckFlavor <: AbstractCheckFlavor end
Expand Down Expand Up @@ -164,7 +167,7 @@ function expr_error_block(info, condition, preamble...)
end

default_exception_type(::ArgCheckFlavor) = ArgumentError
default_exception_type(::CheckFlavor) = ErrorException
default_exception_type(::CheckFlavor) = CheckError

function build_error(info)
build_error(info, info.checkflavor, info.options...)
Expand All @@ -187,14 +190,27 @@ error_message(info::FallbackErrorInfo) = "$(info.code) must hold."
error_message(info::CallErrorInfo) = fancy_error_message(info)
error_message(info::ComparisonErrorInfo) = fancy_error_message(info)

function pretty_string(data)
io = IOBuffer()
ioc = IOContext(io; limit=true, compact=true)
show(ioc, data)
seekstart(io)
String(take!(io))
end
pretty_string(x::Number) = string(x)
pretty_string(s::Symbol) = string(s)
pretty_string(ex::Expr) = string(ex)

function fancy_error_message(info)
code = info.code
exprs = info.argument_expressions
values = info.argument_values
lines = String[]
foreach(exprs, values) do ex, val
if string(ex) != string(val)
push!(lines, "$ex => $val")
pex = pretty_string(ex)
pval = pretty_string(val)
if pex != pval
push!(lines, "$pex => $pval")
end
end
firstline = if isempty(lines)
Expand Down
32 changes: 29 additions & 3 deletions test/checks.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using ArgCheck: pretty_string
macro catch_exception_object(code)
quote
err = try
Expand Down Expand Up @@ -77,12 +78,12 @@ truthy(args...;kw...) = true
@test err === MyExoticError(1,2)
end

@testset "error message comparison" begin
@testset "error messages" begin
x = 1.23455475675
y = 2.345345345
# comparison
err = @catch_exception_object @argcheck x == y MyError
@test isa(err, MyError)
@test err isa MyError
msg = err.msg
@test contains(msg, string(x))
@test contains(msg, string(y))
Expand Down Expand Up @@ -110,6 +111,17 @@ end
@test contains(msg, string(x))
@test contains(msg, string(y))
@test contains(msg, "")

s = randstring()
arr = rand(1000:9999, 1000)
x = randn()
err = @catch_exception_object @check falsy(x, arr, s)
@test typeof(err) == CheckError
msg = err.msg
@test length(msg) < 2000
@test contains(msg, pretty_string(x))
@test contains(msg, pretty_string(arr))
@test contains(msg, pretty_string(s))
end

# In
Expand Down Expand Up @@ -172,8 +184,22 @@ end

@testset "@check" begin
@check true
E = ErrorException
E = CheckError
@test_throws E @check false
@test_throws E @check false "oh no"
@test_throws DimensionMismatch @check false DimensionMismatch
end

@testset "pretty_string" begin
@test pretty_string("asd") == "\"asd\""

data = rand(10000:99999, 1000)
str = pretty_string(data)
@test length(str) < 1000
@test contains(str, string(last(data)))
@test contains(str, string(first(data)))
@test !contains(str, "\n")

data = randn()
@test parse(Float64,pretty_string(data)) === data
end

0 comments on commit 4dfb99e

Please sign in to comment.