Skip to content

Commit

Permalink
Add pending test type, close #18
Browse files Browse the repository at this point in the history
  • Loading branch information
IainNZ committed Nov 3, 2014
1 parent 7297667 commit 61f4327
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 14 deletions.
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ facts("Testing basics") do
end
```

You can also provide custom error messages as a second argument, e.g.
You can provide custom error messages as a second argument, e.g.
```julia
facts("Messages") do
x = [1, 2, 3, 4]
Expand All @@ -70,6 +70,19 @@ produces
...
```

Finally, if you have an idea for a test you want to implement but haven't yet, you can using `@pending`. `@pending` doesn't attempt to check its assertion, or even evaluate the expression, it simply records that a pending test exists.
```julia
facts("Some pending") do
@fact 2*3 => 6
@pending divide(2,3) => :something
end
```
Some pending
Out of 2 total facts:
Verified: 1
Pending: 1
```
### Assertions
A `FactCheck` `=>` is more general than the `==` of `Base.Test.@test`.
Expand Down
54 changes: 41 additions & 13 deletions src/FactCheck.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

module FactCheck

export @fact, @fact_throws,
export @fact, @fact_throws, @pending,
facts, context,
getstats, exitstatus,
# Assertion helpers
Expand Down Expand Up @@ -49,6 +49,8 @@ type Error <: Result
backtrace
meta::Dict
end
type Pending <: Result
end

# Collection of all results across facts
allresults = Result[]
Expand Down Expand Up @@ -87,11 +89,16 @@ function Base.show(io::IO, s::Success)
print_with_color(:green, io, indent, "Success")
print(io, " :: $(format_assertion(s.expr))")
end
function Base.show(io::IO, p::Pending)
indent = isempty(handlers) ? "" : " "
print_with_color(:yellow, io, indent, "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")

######################################################################
# Core testing macros and functions
Expand Down Expand Up @@ -135,7 +142,6 @@ macro fact_throws(factex::Expr, args...)
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.
Expand All @@ -153,6 +159,18 @@ function do_fact(thunk::Function, factex::Expr, meta::Dict)
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)
push!(allresults, result)
CONFIG[:compact] && print_compact(result)
result
end
end

######################################################################
# Grouping of tests
#
Expand All @@ -165,24 +183,27 @@ end
type TestSuite
filename
desc
successes::Array{Success}
failures::Array{Failure}
errors::Array{Error}
successes::Vector{Success}
failures::Vector{Failure}
errors::Vector{Error}
pending::Vector{Pending}
end
TestSuite(f, d) = TestSuite(f, d, Success[], Failure[], Error[])
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)
if n_fail == 0 && n_err == 0
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
total = n_succ + n_fail + n_err
println(io, "Out of $total total $(pluralize("fact", total)):")
print_with_color(:green, io, " Verified: $n_succ\n")
print_with_color(:red, io, " Failed: $n_fail\n")
print_with_color(:red, io, " Errored: $n_err\n")
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

Expand Down Expand Up @@ -217,6 +238,9 @@ function make_handler(suite::TestSuite)
push!(suite.errors, r)
!CONFIG[:compact] && println(r)
end
function delayed_handler(p::Pending)
push!(suite.pending, p)
end
delayed_handler
end

Expand Down Expand Up @@ -292,17 +316,21 @@ function getstats()
s = 0
f = 0
e = 0
p = 0
for r in allresults
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 == length(allresults))
{"nSuccesses" => s, "nFailures" => f, "nErrors" => e, "nNonSuccessful" => f+e}
assert(s+f+e+p == length(allresults))
{"nSuccesses" => s, "nFailures" => f, "nErrors" => e,
"nNonSuccessful" => f+e, "nPending" => p}
end

function exitstatus()
Expand Down
3 changes: 3 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,15 @@ facts("Test error pathways") do
println(a_success)
a_failure = @fact 1 => 2 "one doesn't equal two!"
a_error = @fact 2^-1 => 0.5 "domains are tricky"
a_pending = @pending not_really_pending() "sorta pending"
println(a_pending)
end
stats = getstats()
FactCheck.clear_results()
@test stats["nSuccesses"] == 1
@test stats["nFailures"] == 1
@test stats["nErrors"] == 1
@test stats["nPending"] == 1
@test stats["nNonSuccessful"] == 2
print_with_color(:blue,"Done, begin actual FactCheck tests\n")

Expand Down

0 comments on commit 61f4327

Please sign in to comment.