/
FactCheck.jl
576 lines (521 loc) · 18.2 KB
/
FactCheck.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
######################################################################
# FactCheck.jl
# A testing framework for Julia
# http://github.com/JuliaLang/FactCheck.jl
# MIT Licensed
######################################################################
module FactCheck
using Compat
import Compat.String
export @fact, @fact_throws, @pending,
facts, context,
getstats, exitstatus,
# Assertion helpers
not,
exactly,
roughly,
anyof,
less_than, less_than_or_equal,
greater_than, greater_than_or_equal
const INDENT = " "
# Global configuration for FactCheck
const CONFIG = @compat Dict(:compact => false, :only_stats => false) # Compact output off by default
# Not exported: sets output style
function setstyle(style)
global CONFIG
CONFIG[:compact] = (style == :compact)
end
function onlystats(flag)
global CONFIG
CONFIG[:only_stats] = flag
end
######################################################################
# Success, Failure, Error <: Result
# Represents the result of a test. These are very similar to the types
# with the same names in Base.Test, except for the addition of the
# `ResultMetadata` type that is used to retain information about the test,
# such as its file, line number, description, etc.
abstract Result
type ResultMetadata
line
msg
function ResultMetadata(;line=nothing, msg=nothing)
new(line, msg)
end
end
type Success <: Result
expr::Expr
fact_type::Symbol
lhs # What it was
rhs # What it should have been
meta::ResultMetadata
end
type Failure <: Result
expr::Expr
fact_type::Symbol
lhs # What it was
rhs # What it should have been
meta::ResultMetadata
end
type Error <: Result
expr::Expr
fact_type::Symbol
err::Exception
backtrace
meta::ResultMetadata
end
type Pending <: Result
end
# Collection of all results across facts
allresults = Result[]
clear_results() = (global allresults; allresults = Result[])
# Formats a fact expression
function format_fact(ex::Expr)
if ex.head == :(-->) || ex.head == :(=>)
# :(fn(1) --> 2) to 'fn(1) --> 2'
# :("1"*"1" --> "11") to '"1" * "1" --> "11"'
# We handle non-expresion arguments differently,
# otherwise, e.g. quote marks on strings disappear
x, y = ex.args
x_str = sprint(isa(x,Expr) || isa(x,Symbol) ? print : show, x)
y_str = sprint(isa(y,Expr) || isa(y,Symbol) ? print : show, y)
string(x_str, " --> ", y_str)
else
# Something else, that maybe didn't have a -->
# such as @fact_throws. Punt and just stringify
string(ex)
end
end
# Builds string with line and context annotations, if available
format_line(r::Result) = string(
r.meta.line != nothing ? " :: (line:$(r.meta.line))" : "",
isempty(contexts) ? "" : " :: $(contexts[end])",
r.meta.msg != nothing ? " :: $(r.meta.msg)" : "")
# Define printing functions for the result types
function Base.show(io::IO, f::Failure)
base_ind, sub_ind = get_indent()
print_with_color(:red, io, base_ind, "Failure")
if f.fact_type == :fact_throws
# @fact_throws didn't get an error, or the right type of error
println(io, format_line(f), " :: ", f.lhs)
print(io, sub_ind, "Expression: ", f.expr)
if f.rhs != :fact_throws_noerror
println(io)
println(io, sub_ind, " Expected: ", f.rhs[1])
print( io, sub_ind, " Occurred: ", f.rhs[2])
end
elseif f.fact_type == :fact
# @fact didn't get the right result
args = f.expr.args
println(io, format_line(f), " :: fact was false")
println(io, sub_ind, "Expression: ", format_fact(f.expr))
if length(args) >= 2 && _factcheck_function(args[2]) != nothing
# Fancy helper fact
fcFunc = _factcheck_function(args[2])
if haskey(FACTCHECK_FUN_NAMES, fcFunc)
print(io, sub_ind, " Expected: ",
sprint(show, f.lhs),
" ", FACTCHECK_FUN_NAMES[fcFunc], " ",
sprint(show, f.rhs))
else
print(io, sub_ind, " Expected: ",
sprint(show, f.lhs), " --> ", fcFunc,
"(", sprint(show, f.rhs), ")")
end
else
# Normal equality-test-style fact
println(io, sub_ind, " Expected: ", sprint(show, f.rhs))
print( io, sub_ind, " Occurred: ", sprint(show, f.lhs))
end
else
error("Unknown fact type: ", f.fact_type)
end
end
function Base.show(io::IO, e::Error)
base_ind, sub_ind = get_indent()
print_with_color(:red, io, base_ind, "Error")
println(io, format_line(e))
println(io, sub_ind, "Expression: ", format_fact(e.expr))
bt_str = sprint(showerror, e.err, e.backtrace)
print(io, join(map(line->string(sub_ind,line),
split(bt_str, "\n")), "\n"))
end
function Base.show(io::IO, s::Success)
base_ind, sub_ind = get_indent()
print_with_color(:green, io, base_ind, "Success")
print(io, format_line(s))
if s.rhs == :fact_throws_error
print(io, " :: ", s.lhs)
else
println(io, " :: fact was true")
println(io, sub_ind, "Expression: ", format_fact(s.expr))
println(io, sub_ind, " Expected: ", sprint(show, s.rhs))
print( io, sub_ind, " Occurred: ", sprint(show, s.lhs))
end
end
function Base.show(io::IO, p::Pending)
base_ind, sub_ind = get_indent()
print_with_color(:yellow, io, base_ind, "Pending")
end
# When in compact mode, we simply print a single character
print_compact(f::Failure) = print_with_color(:red, "F")
print_compact(e::Error) = print_with_color(:red, "E")
print_compact(s::Success) = print_with_color(:green, ".")
print_compact(s::Pending) = print_with_color(:yellow, "P")
const SPECIAL_FACTCHECK_FUNCTIONS =
Set([:not, :exactly, :roughly, :anyof,
:less_than, :less_than_or_equal, :greater_than, :greater_than_or_equal])
@compat const FACTCHECK_FUN_NAMES =
Dict{Symbol,AbstractString}(
:roughly => "≅",
:less_than => "<",
:less_than_or_equal => "≤",
:greater_than => ">",
:greater_than_or_equal => "≥")
isexpr(x) = isa(x, Expr)
iscallexpr(x) = isexpr(x) && x.head == :call
isdotexpr(x) = isexpr(x) && x.head == :.
isquoteexpr(x) = isexpr(x) && x.head == :quote
isparametersexpr(x) = isexpr(x) && x.head == :parameters
function _factcheck_function(assertion)
iscallexpr(assertion) || return nothing
# checking for lhs => roughly(rhs)
if assertion.args[1] in SPECIAL_FACTCHECK_FUNCTIONS
return assertion.args[1]
end
# checking for lhs => FactCheck.roughly(rhs)
isdotexpr(assertion.args[1]) || return nothing
dotexpr = assertion.args[1]
length(dotexpr.args) >= 2 || return nothing
if isquoteexpr(dotexpr.args[2])
quoteexpr = dotexpr.args[2]
if length(quoteexpr.args) >= 1 && quoteexpr.args[1] in SPECIAL_FACTCHECK_FUNCTIONS
return quoteexpr.args[1]
else
return nothing
end
end
# sometimes it shows up as a QuoteNode...
if isa(dotexpr.args[2], QuoteNode) && dotexpr.args[2].value in SPECIAL_FACTCHECK_FUNCTIONS
return dotexpr.args[2].value
end
nothing
end
######################################################################
# Core testing macros and functions
# @fact takes an assertion of the form LHS --> RHS, and replaces it
# with code to evaluate that fact (depending on the type of the RHS),
# and produce and record a result based on the outcome
macro fact(factex::Expr, args...)
if factex.head != :(-->) && factex.head != :(=>)
error("Incorrect usage of @fact: $factex")
end
if factex.head == :(=>)
Base.warn_once("The `=>` syntax is deprecated, use `-->` instead")
end
# Extract the two sides of the fact
lhs, initial_rhs = factex.args
# If there is another argument to the macro, assume it is a
# message and record it
msg = length(args) > 0 ? args[1] : (:nothing)
# rhs is the assertion, unless it's wrapped by a special FactCheck function
rhs = initial_rhs
if _factcheck_function(initial_rhs) != nothing
rhs = initial_rhs.args[isparametersexpr(initial_rhs.args[2]) ? 3 : 2]
end
quote
# Build a function (predicate) that, depending on the nature of
# the RHS, either compares the sides or applies the RHS to the LHS
predicate = function(lhs_value)
rhs_value = $(esc(initial_rhs))
if isa(rhs_value, Function)
# The RHS is a function, so instead of testing for equality,
# return the value of applying the RHS to the LHS
(rhs_value(lhs_value), lhs_value, $(esc(rhs)))
else
# The RHS is a value, so test for equality
(rhs_value == lhs_value, lhs_value, $(esc(rhs)))
end
end
# Replace @fact with a call to the do_fact function that constructs
# the test result object by evaluating the
do_fact(() -> predicate($(esc(lhs))),
$(Expr(:quote, factex)),
:fact,
ResultMetadata(line=getline(),
msg=$(esc(msg))))
end
end
# `@fact_throws` is similar to `@fact`, except it only checks if
# the expression throws an error or not - there is no explict
# assertion to compare against.
macro fact_throws(args...)
expr, extype, msg = nothing, nothing, nothing
nargs = length(args)
if nargs == 1
if isa(args[1],Expr)
expr = args[1]
else
throw(ArgumentError("invalid @fact_throws macro"))
end
elseif nargs == 2
if (isa(args[1],Symbol) || isa(args[1],Expr)) && isa(args[2],Expr)
extype, expr = args
elseif isa(args[1],Expr)
expr, msg = args
else
throw(ArgumentError("invalid @fact_throws macro"))
end
elseif nargs >= 3
if (isa(args[1],Symbol) || isa(args[1], Expr)) && isa(args[2],Expr)
extype, expr, msg = args
else
throw(ArgumentError("invalid @fact_throws macro"))
end
end
quote
do_fact(() -> try
$(esc(expr))
(false, "no exception was thrown", :fact_throws_noerror)
catch ex
$(if extype === nothing
:((true, "an exception was thrown", :fact_throws_error))
else
:(if isa(ex,$(esc(extype)))
(true, "correct exception was throw", :fact_throws_error)
else
(false, "wrong exception was thrown",
($(esc(extype)),typeof(ex)) )
end)
end)
end,
$(Expr(:quote, expr)),
:fact_throws,
ResultMetadata(line=getline(),msg=$(esc(msg))))
end
end
# `do_fact` constructs a Success, Failure, or Error depending on the
# outcome of a test and passes it off to the active test handler
# `FactCheck.handlers[end]`. It finally returns the test result.
function do_fact(thunk::Function, factex::Expr, fact_type::Symbol, meta::ResultMetadata)
result = try
res, val, rhs = thunk()
res ? Success(factex, fact_type, val, rhs, meta) :
Failure(factex, fact_type, val, rhs, meta)
catch err
Error(factex, fact_type, err, catch_backtrace(), meta)
end
!isempty(handlers) && handlers[end](result)
if CONFIG[:only_stats]
updatestats!(getstats([result]))
else
push!(allresults, result)
end
CONFIG[:compact] && print_compact(result)
result
end
# `@pending` is a no-op test - it doesn't do anything except record
# its existance in the final totals of tests "run"
macro pending(factex::Expr, args...)
quote
result = Pending()
!isempty(handlers) && handlers[end](result)
if CONFIG[:only_stats]
updatestats!(getstats([result]))
else
push!(allresults, result)
end
CONFIG[:compact] && print_compact(result)
result
end
end
######################################################################
# Grouping of tests
#
# `facts` describes a top-level test scope, which can contain
# `contexts` to group similar tests. Test results will be collected
# instead of throwing an exception immediately.
# A TestSuite collects the results of a series of tests, as well as
# some information about the tests such as their file and description.
type TestSuite
filename
desc
successes::Vector{Success}
failures::Vector{Failure}
errors::Vector{Error}
pending::Vector{Pending}
end
TestSuite(f, d) = TestSuite(f, d, Success[], Failure[], Error[], Pending[])
function Base.print(io::IO, suite::TestSuite)
n_succ = length(suite.successes)
n_fail = length(suite.failures)
n_err = length(suite.errors)
n_pend = length(suite.pending)
total = n_succ + n_fail + n_err + n_pend
if n_fail == 0 && n_err == 0 && n_pend == 0
print_with_color(:green, io, "$n_succ $(pluralize("fact", n_succ)) verified.\n")
else
println(io, "Out of $total total $(pluralize("fact", total)):")
n_succ > 0 && print_with_color(:green, io, " Verified: $n_succ\n")
n_fail > 0 && print_with_color(:red, io, " Failed: $n_fail\n")
n_err > 0 && print_with_color(:red, io, " Errored: $n_err\n")
n_pend > 0 && print_with_color(:yellow,io, " Pending: $n_pend\n")
end
end
function print_header(suite::TestSuite)
print_with_color(:bold,
suite.desc != nothing ? "$(suite.desc)" : "",
suite.filename != nothing ? " ($(suite.filename))" : "",
CONFIG[:compact] ? ": " : "\n")
end
# The last handler function found in `handlers` will be passed
# test results.
const handlers = Function[]
# A list of test contexts. `contexts[end]` should be the
# inner-most context.
const contexts = AbstractString[]
# Constructs a function that handles Successes, Failures, and Errors,
# pushing them into a given TestSuite and printing Failures and Errors
# as they arrive (unless in compact mode, in which case we delay
# printing details until the end).
function make_handler(suite::TestSuite)
function delayed_handler(r::Success)
push!(suite.successes, r)
end
function delayed_handler(r::Failure)
push!(suite.failures, r)
!CONFIG[:compact] && println(r)
end
function delayed_handler(r::Error)
push!(suite.errors, r)
!CONFIG[:compact] && println(r)
end
function delayed_handler(p::Pending)
push!(suite.pending, p)
end
delayed_handler
end
# facts
# Creates testing scope. It is responsible for setting up a testing
# environment, which means constructing a `TestSuite`, generating
# and registering test handlers, and reporting results.
function facts(f::Function, desc)
suite = TestSuite(nothing, desc)
handler = make_handler(suite)
push!(handlers, handler)
print_header(suite)
f()
if !CONFIG[:compact]
# Print out summary of test suite
print(suite)
else
# If in compact mode, we need to display all the
# failures we hit along along the way
println() # End line with dots
map(println, suite.failures)
map(println, suite.errors)
end
pop!(handlers)
end
facts(f::Function) = facts(f, nothing)
# context
# Executes a battery of tests in some descriptive context, intended
# for use inside of `facts`. Displays the string in default mode.
# for use inside of facts
global LEVEL = 0
function context(f::Function, desc::AbstractString)
global LEVEL
push!(contexts, desc)
LEVEL += 1
!CONFIG[:compact] && println(INDENT^LEVEL, "> ", desc)
try
f()
finally
pop!(contexts)
LEVEL -= 1
end
end
context(f::Function) = context(f, "")
# get_indent
# Gets indent levels to use for displaying results
function get_indent()
ind_level = isempty(handlers) ? 0 : LEVEL+1
return INDENT^ind_level, INDENT^(ind_level+1)
end
######################################################################
if VERSION < v"0.5.0-dev+2428"
# HACK: get the current line number
#
# This only works inside of a function body:
#
# julia> hmm = function()
# 2
# 3
# getline()
# end
#
# julia> hmm()
# 4
#
function getline()
bt = backtrace()
issecond = false
for frame in bt
lookup = ccall(:jl_lookup_code_address, Any, (Ptr{Void}, Int32), frame, 0)
if lookup != ()
if issecond
return lookup[3]
else
issecond = true
end
end
end
end
else
@noinline getline() = StackTraces.stacktrace()[2].line
end
pluralize(s::AbstractString, n::Number) = n == 1 ? s : string(s, "s")
# `getstats` return a dictionary with a summary over all tests run
getstats() = getstats(allresults)
function getstats(results)
s = 0
f = 0
e = 0
p = 0
for r in results
if isa(r, Success)
s += 1
elseif isa(r, Failure)
f += 1
elseif isa(r, Error)
e += 1
elseif isa(r, Pending)
p += 1
end
end
assert(s+f+e+p == length(results))
@compat(Dict{String,Int}("nSuccesses" => s,
"nFailures" => f,
"nErrors" => e,
"nNonSuccessful" => f+e,
"nPending" => p))
end
const allstats = getstats()
function updatestats!(stats)
for (key, value) in stats
allstats[key] += value
end
end
function exitstatus()
global CONFIG
if CONFIG[:only_stats]
ns = allstats["nNonSuccessful"]
else
ns = getstats()["nNonSuccessful"]
end
ns > 0 && error("FactCheck finished with $ns non-successful tests.")
end
############################################################
# Assertion helpers
include("helpers.jl")
end # module FactCheck