-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.jl
69 lines (60 loc) · 1.71 KB
/
main.jl
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
@use "github.com/jkroso/Emitter.jl" Events emit
struct Result
title::Vector{AbstractString}
time::AbstractFloat
pass::Bool
end
struct Suite
title::AbstractString
results::Vector{Result}
end
const stack = Vector{Suite}()
const reporter = Events()
"""
Run a suite of tests and sub-suites
"""
function testset(body::Function, title::AbstractString)
push!(stack, Suite(title, []))
full_title = map(t -> t.title, stack)
emit(reporter, "before test", full_title)
time = @elapsed(body())
pass = all(r -> r.pass, stack[end].results)
result = Result(full_title, time, pass)
length(stack) > 1 && push!(stack[end-1].results, result)
emit(reporter, "after test", result)
pop!(stack)
result
end
"""
Run an assertion returning a `Result`
"""
function assertion(body::Function, title::AbstractString)
full_title = vcat(map(t -> t.title, stack), title)
emit(reporter, "before assertion", full_title)
time = @elapsed ok = body()::Bool
result = Result(full_title, time, ok)
isempty(stack) || push!(stack[end].results, result)
emit(reporter, "after assertion", result)
result
end
"""
Define an assertion from an expression
"""
macro test(expr)
:(assertion(function() $(esc(expr)) end, $(repr(expr))))
end
@eval macro $(:catch)(expr)
quote
(function()
try $(esc(expr)) catch e return e end
error("did not throw an error")
end)()
end
end
Base.show(io::IO, r::Result) = write(io, "$(r.pass ? '✓' : '✗') $(round(Int, r.time * 1000))ms")
Base.show(io::IO, ::MIME"text/html", r::Result) = begin
css = "color:$(r.pass ? "rgb(0, 226, 0)" : "red");"
text = "$(r.pass ? '✓' : '✗') $(round(Int, r.time * 1000))ms"
write(io, "<div style=\"$css\">$text</div>")
end
export testset, @test, @catch