Skip to content

Commit 3da7227

Browse files
committed
Add information to passing tests (JuliaLang#25483)
1 parent 33f92d6 commit 3da7227

File tree

5 files changed

+39
-8
lines changed

5 files changed

+39
-8
lines changed

stdlib/Test/docs/src/index.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,13 @@ If the condition is true, a `Pass` is returned:
4242
```jldoctest testfoo
4343
julia> @test foo("bar") == 9
4444
Test Passed
45+
Expression: foo("bar") == 9
46+
Evaluated: 9 == 9
4547
4648
julia> @test foo("fizz") >= 10
4749
Test Passed
50+
Expression: foo("fizz") >= 10
51+
Evaluated: 16 >= 10
4852
```
4953

5054
If the condition is false, then a `Fail` is returned and an exception is thrown:

stdlib/Test/src/Test.jl

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,9 @@ struct Pass <: Result
8585
orig_expr
8686
data
8787
value
88-
function Pass(test_type::Symbol, orig_expr, data, thrown)
89-
return new(test_type, orig_expr, data, thrown isa String ? "String" : thrown)
88+
source::Union{Nothing,LineNumberNode}
89+
function Pass(test_type::Symbol, orig_expr, data, thrown, source)
90+
return new(test_type, orig_expr, data, thrown isa String ? "String" : thrown, source)
9091
end
9192
end
9293

@@ -604,7 +605,7 @@ function do_test(result::ExecutionResult, orig_expr)
604605
value = result.value
605606
testres = if isa(value, Bool)
606607
# a true value Passes
607-
value ? Pass(:test, nothing, nothing, value) :
608+
value ? Pass(:test, orig_expr, result.data, value, result.source) :
608609
Fail(:test, orig_expr, result.data, value, result.source)
609610
else
610611
# If the result is non-Boolean, this counts as an Error
@@ -707,7 +708,7 @@ function do_test_throws(result::ExecutionResult, orig_expr, extype)
707708
end
708709
end
709710
if success
710-
testres = Pass(:test_throws, nothing, nothing, exc)
711+
testres = Pass(:test_throws, orig_expr, extype, exc, result.source)
711712
else
712713
testres = Fail(:test_throws_wrong, orig_expr, extype, exc, result.source)
713714
end

stdlib/Test/src/logging.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ macro test_logs(exs...)
178178
$(esc(expression))
179179
end
180180
if didmatch
181-
testres = Pass(:test, nothing, nothing, value)
181+
testres = Pass(:test, $orig_expr, nothing, value, $sourceloc)
182182
else
183183
testres = LogTestFailure($orig_expr, $sourceloc,
184184
$(QuoteNode(exs[1:end-1])), logs)

stdlib/Test/test/runtests.jl

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ end
281281

282282
let retval_tests = @testset NoThrowTestSet begin
283283
ts = Test.DefaultTestSet("Mock for testing retval of record(::DefaultTestSet, ::T <: Result) methods")
284-
pass_mock = Test.Pass(:test, 1, 2, LineNumberNode(0, "A Pass Mock"))
284+
pass_mock = Test.Pass(:test, 1, 2, 3, LineNumberNode(0, "A Pass Mock"))
285285
@test Test.record(ts, pass_mock) isa Test.Pass
286286
error_mock = Test.Error(:test, 1, 2, 3, LineNumberNode(0, "An Error Mock"))
287287
@test Test.record(ts, error_mock) isa Test.Error
@@ -981,7 +981,7 @@ end
981981

982982
let ex = :(something_complex + [1, 2, 3])
983983
b = PipeBuffer()
984-
let t = Test.Pass(:test, (ex, 1), (ex, 2), (ex, 3))
984+
let t = Test.Pass(:test, (ex, 1), (ex, 2), (ex, 3), LineNumberNode(@__LINE__, @__FILE__))
985985
serialize(b, t)
986986
@test string(t) == string(deserialize(b))
987987
@test eof(b)
@@ -1170,3 +1170,29 @@ end
11701170
# Decorated LoadErrors are not unwrapped if a LoadError was thrown.
11711171
@test_throws LoadError("file", 111, ErrorException("Real error")) @macroexpand @test_macro_throw_2
11721172
end
1173+
1174+
# Issue 25483
1175+
mutable struct PassInformationTestSet <: Test.AbstractTestSet
1176+
results::Vector
1177+
PassInformationTestSet(desc) = new([])
1178+
end
1179+
Test.record(ts::PassInformationTestSet, t::Test.Result) = (push!(ts.results, t); t)
1180+
Test.finish(ts::PassInformationTestSet) = ts
1181+
@testset "Information in Pass result (Issue 25483)" begin
1182+
ts = @testset PassInformationTestSet begin
1183+
@test 1 == 1
1184+
@test_throws ErrorException throw(ErrorException("Msg"))
1185+
end
1186+
test_line_number = (@__LINE__) - 3
1187+
test_throws_line_number = (@__LINE__) - 3
1188+
@test ts.results[1].test_type == :test
1189+
@test ts.results[1].orig_expr == :(1 == 1)
1190+
@test ts.results[1].data == Expr(:comparison, 1, :(==), 1)
1191+
@test ts.results[1].value == true
1192+
@test ts.results[1].source == LineNumberNode(test_line_number, @__FILE__)
1193+
@test ts.results[2].test_type == :test_throws
1194+
@test ts.results[2].orig_expr == :(throw(ErrorException("Msg")))
1195+
@test ts.results[2].data == ErrorException
1196+
@test ts.results[2].value == ErrorException("Msg")
1197+
@test ts.results[2].source == LineNumberNode(test_throws_line_number, @__FILE__)
1198+
end

test/runtests.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,7 @@ cd(@__DIR__) do
363363
elseif isa(resp, Test.TestSetException)
364364
fake = Test.DefaultTestSet(testname)
365365
for i in 1:resp.pass
366-
Test.record(fake, Test.Pass(:test, nothing, nothing, nothing))
366+
Test.record(fake, Test.Pass(:test, nothing, nothing, nothing, LineNumberNode(@__LINE__, @__FILE__)))
367367
end
368368
for i in 1:resp.broken
369369
Test.record(fake, Test.Broken(:test, nothing))

0 commit comments

Comments
 (0)