From 8ad4de001618f50ca5f700c5c5e39a792296c050 Mon Sep 17 00:00:00 2001 From: Dilum Aluthge Date: Sun, 14 Apr 2024 14:05:52 -0400 Subject: [PATCH] `@test_throws`: don't throw an error if the same field is undefined in both the observed exception and the expected exception --- stdlib/Test/src/Test.jl | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/stdlib/Test/src/Test.jl b/stdlib/Test/src/Test.jl index ddd62ca9a10f8..a10d60cda9074 100644 --- a/stdlib/Test/src/Test.jl +++ b/stdlib/Test/src/Test.jl @@ -812,7 +812,22 @@ function do_test_throws(result::ExecutionResult, orig_expr, extype) if isa(exc, typeof(extype)) success = true for fld in 1:nfields(extype) - if !isequal(getfield(extype, fld), getfield(exc, fld)) + # We check three cases. + if isdefined(extype, fld) && isdefined(exc, fld) + # Case 1: extype.fld is defined and exc.fld is defined. + # In this case, we compare the two values. + # It is a success iff extype.fld is equal to exc.fld + if !isequal(getfield(extype, fld), getfield(exc, fld)) + success = false + break + end + elseif !isdefined(extype, fld) && !isdefined(exc, fld) + # Case 2: extype.fld is undefined and exc.fld is undefined. + # In this case, because both of the fields are undefined, + # we consider this case to be a success. + else + # Case 3: one is defined and the other is undefined. + # We consider this case to be a failure. success = false break end