~ubuntu-branches/ubuntu/wily/julia/wily

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
module Test

export @test, @test_fails

abstract Result
type Success <: Result
    expr
end
type Failure <: Result
    expr
end
type Error <: Result
    expr
    err
end

default_handler(r::Success) = nothing
default_handler(r::Failure) = error("test failed: $(r.expr)")
default_handler(r::Error)   = error("test error during $(r.expr)\n$(r.err)")

const handlers = [default_handler]

function do_test(thk, qex)
    handlers[end](try
        thk() ? Success(qex) : Failure(qex)
    catch err
        Error(qex,err)
    end)
end

function do_test_fails(thk, qex)
    handlers[end](try
        thk()
        Failure(qex)
    catch err
        Success(qex)
    end)
end

macro test(ex)
    :(do_test(()->($(esc(ex))),$(expr(:quote,ex))))
end

macro test_fails(ex)
    :(do_test_fails(()->($(esc(ex))),$(expr(:quote,ex))))
end

end # module