Skip to content

Commit

Permalink
Displays negated evaluated failures (#30729)
Browse files Browse the repository at this point in the history
  • Loading branch information
Sam Morrison authored and JeffBezanson committed Jan 22, 2019
1 parent cd7dab3 commit dbad198
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 3 deletions.
19 changes: 17 additions & 2 deletions stdlib/Test/src/Test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ struct Threw <: ExecutionResult
source::LineNumberNode
end

function eval_test(evaluated::Expr, quoted::Expr, source::LineNumberNode)
function eval_test(evaluated::Expr, quoted::Expr, source::LineNumberNode, negate::Bool=false)
res = true
i = 1
evaled_args = evaluated.args
Expand Down Expand Up @@ -264,6 +264,12 @@ function eval_test(evaluated::Expr, quoted::Expr, source::LineNumberNode)
else
throw(ArgumentError("Unhandled expression type: $(evaluated.head)"))
end

if negate
res = !res
quoted = Expr(:call, :!, quoted)
end

Returned(res,
# stringify arguments in case of failure, for easy remote printing
res ? quoted : sprint(io->print(IOContext(io, :limit => true), quoted)),
Expand Down Expand Up @@ -394,6 +400,13 @@ end
# evaluate each term in the comparison individually so the results
# can be displayed nicely.
function get_test_result(ex, source)
negate = QuoteNode(false)
orig_ex = ex
# Evaluate `not` wrapped functions separately for pretty-printing failures
if isa(ex, Expr) && ex.head == :call && length(ex.args) == 2 && ex.args[1] === :!
negate = QuoteNode(true)
ex = ex.args[2]
end
# Normalize non-dot comparison operator calls to :comparison expressions
is_splat = x -> isa(x, Expr) && x.head == :...
if isa(ex, Expr) && ex.head == :call && length(ex.args) == 3 &&
Expand All @@ -409,6 +422,7 @@ function get_test_result(ex, source)
Expr(:comparison, $(escaped_terms...)),
Expr(:comparison, $(quoted_terms...)),
$(QuoteNode(source)),
$negate,
))
elseif isa(ex, Expr) && ex.head == :call && ex.args[1] in (:isequal, :isapprox, :)
escaped_func = esc(ex.args[1])
Expand Down Expand Up @@ -452,9 +466,10 @@ function get_test_result(ex, source)
Expr(:call, $escaped_func, Expr(:parameters, $(escaped_kwargs...)), $(escaped_args...)),
Expr(:call, $quoted_func),
$(QuoteNode(source)),
$negate,
))
else
testret = :(Returned($(esc(ex)), nothing, $(QuoteNode(source))))
testret = :(Returned($(esc(orig_ex)), nothing, $(QuoteNode(source))))
end
result = quote
try
Expand Down
23 changes: 22 additions & 1 deletion stdlib/Test/test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,12 @@ let fails = @testset NoThrowTestSet begin
# Fail - function keyword splatting
k = [(:atol, 0), (:nans, true)]
@test isapprox(1, 2; k...)
# Fail - call negation
@test !isequal(1, 2 - 1)
# Fail - comparison negation
@test !(2 + 3 == 1 + 4)
# Fail - chained negation
@test !(2 + 3 == 1 + 4 == 5)
# Error - unexpected pass
@test_broken true
# Error - converting a call into a comparison
Expand Down Expand Up @@ -185,11 +191,26 @@ let fails = @testset NoThrowTestSet begin
end

let str = sprint(show, fails[14])
@test occursin("Expression: !(isequal(1, 2 - 1))", str)
@test occursin("Evaluated: !(isequal(1, 1))", str)
end

let str = sprint(show, fails[15])
@test occursin("Expression: !(2 + 3 == 1 + 4)", str)
@test occursin("Evaluated: !(5 == 5)", str)
end

let str = sprint(show, fails[16])
@test occursin("Expression: !(2 + 3 == 1 + 4 == 5)", str)
@test occursin("Evaluated: !(5 == 5 == 5)", str)
end

let str = sprint(show, fails[17])
@test occursin("Unexpected Pass", str)
@test occursin("Expression: true", str)
end

let str = sprint(show, fails[15])
let str = sprint(show, fails[18])
@test occursin("Expression: ==(1, 1:2...)", str)
@test occursin("MethodError: no method matching ==(::$Int, ::$Int, ::$Int)", str)
end
Expand Down

0 comments on commit dbad198

Please sign in to comment.