@@ -214,9 +214,10 @@ struct Error <: Result
214
214
orig_expr:: String
215
215
value:: String
216
216
backtrace:: String
217
+ context:: Union{Nothing, String}
217
218
source:: LineNumberNode
218
219
219
- function Error (test_type:: Symbol , orig_expr, value, bt, source:: LineNumberNode )
220
+ function Error (test_type:: Symbol , orig_expr, value, bt, source:: LineNumberNode , context :: Union{Nothing, String} = nothing )
220
221
if test_type === :test_error
221
222
bt = scrub_exc_stack (bt, nothing , extract_file (source))
222
223
end
@@ -249,8 +250,14 @@ struct Error <: Result
249
250
string (orig_expr),
250
251
value,
251
252
bt_str,
253
+ context,
252
254
source)
253
255
end
256
+
257
+ # Internal constructor for creating Error with pre-processed values (used by ContextTestSet)
258
+ function Error (test_type:: Symbol , orig_expr:: String , value:: String , backtrace:: String , context:: Union{Nothing, String} , source:: LineNumberNode )
259
+ return new (test_type, orig_expr, value, backtrace, context, source)
260
+ end
254
261
end
255
262
256
263
function Base. show (io:: IO , t:: Error )
@@ -268,6 +275,9 @@ function Base.show(io::IO, t::Error)
268
275
elseif t. test_type === :test_error
269
276
println (io, " Test threw exception" )
270
277
println (io, " Expression: " , t. orig_expr)
278
+ if t. context != = nothing
279
+ println (io, " Context: " , t. context)
280
+ end
271
281
# Capture error message and indent to match
272
282
join (io, (" " * line for line in filter! (! isempty, split (t. backtrace, " \n " ))), " \n " )
273
283
elseif t. test_type === :test_unbroken
@@ -752,13 +762,13 @@ function do_test(result::ExecutionResult, orig_expr)
752
762
Fail (:test , orig_expr, result. data, value, nothing , result. source, false )
753
763
else
754
764
# If the result is non-Boolean, this counts as an Error
755
- Error (:test_nonbool , orig_expr, value, nothing , result. source)
765
+ Error (:test_nonbool , orig_expr, value, nothing , result. source, nothing )
756
766
end
757
767
else
758
768
# The predicate couldn't be evaluated without throwing an
759
769
# exception, so that is an Error and not a Fail
760
770
@assert isa (result, Threw)
761
- testres = Error (:test_error , orig_expr, result. exception, result. backtrace:: Vector{Any} , result. source)
771
+ testres = Error (:test_error , orig_expr, result. exception, result. backtrace:: Vector{Any} , result. source, nothing )
762
772
end
763
773
isa (testres, Pass) || trigger_test_failure_break (result)
764
774
record (get_testset (), testres)
@@ -771,11 +781,11 @@ function do_broken_test(result::ExecutionResult, orig_expr)
771
781
value = result. value
772
782
if isa (value, Bool)
773
783
if value
774
- testres = Error (:test_unbroken , orig_expr, value, nothing , result. source)
784
+ testres = Error (:test_unbroken , orig_expr, value, nothing , result. source, nothing )
775
785
end
776
786
else
777
787
# If the result is non-Boolean, this counts as an Error
778
- testres = Error (:test_nonbool , orig_expr, value, nothing , result. source)
788
+ testres = Error (:test_nonbool , orig_expr, value, nothing , result. source, nothing )
779
789
end
780
790
end
781
791
record (get_testset (), testres)
@@ -1109,6 +1119,13 @@ function record(c::ContextTestSet, t::Fail)
1109
1119
context = t. context === nothing ? context : string (t. context, " \n " , context)
1110
1120
record (c. parent_ts, Fail (t. test_type, t. orig_expr, t. data, t. value, context, t. source, t. message_only))
1111
1121
end
1122
+ function record (c:: ContextTestSet , t:: Error )
1123
+ context = string (c. context_name, " = " , c. context)
1124
+ context = t. context === nothing ? context : string (t. context, " \n " , context)
1125
+ # Create a new Error with the same data but updated context using internal constructor
1126
+ new_error = Error (t. test_type, t. orig_expr, t. value, t. backtrace, context, t. source)
1127
+ record (c. parent_ts, new_error)
1128
+ end
1112
1129
1113
1130
# -----------------------------------------------------------------------
1114
1131
@@ -1845,7 +1862,7 @@ function testset_beginend_call(args, tests, source)
1845
1862
if is_failfast_error (err)
1846
1863
get_testset_depth () > 1 ? rethrow () : failfast_print ()
1847
1864
else
1848
- record (ts, Error (:nontest_error , Expr (:tuple ), err, Base. current_exceptions (), $ (QuoteNode (source))))
1865
+ record (ts, Error (:nontest_error , Expr (:tuple ), err, Base. current_exceptions (), $ (QuoteNode (source)), nothing ))
1849
1866
end
1850
1867
finally
1851
1868
copy! (default_rng (), default_rng_orig)
@@ -1933,7 +1950,7 @@ function testset_forloop(args, testloop, source)
1933
1950
if is_failfast_error (err)
1934
1951
get_testset_depth () > 1 ? rethrow () : failfast_print ()
1935
1952
else
1936
- record (ts, Error (:nontest_error , Expr (:tuple ), err, Base. current_exceptions (), $ (QuoteNode (source))))
1953
+ record (ts, Error (:nontest_error , Expr (:tuple ), err, Base. current_exceptions (), $ (QuoteNode (source)), nothing ))
1937
1954
end
1938
1955
end
1939
1956
end
0 commit comments