You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: NEWS.md
+1Lines changed: 1 addition & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -81,6 +81,7 @@ Standard library changes
81
81
82
82
* Test failures when using the `@test` macro now show evaluated arguments for all function calls ([#57825], [#57839]).
83
83
* Transparent test sets (`@testset let`) now show context when tests error ([#58727]).
84
+
*`@test_throws` now supports a three-argument form `@test_throws ExceptionType pattern expr` to test both exception type and message pattern in one call ([#59117]).
Tests that the expression `expr` throws `exception`.
804
805
The exception may specify either a type,
805
806
a string, regular expression, or list of strings occurring in the displayed error message,
806
807
a matching function,
807
808
or a value (which will be tested for equality by comparing fields).
809
+
810
+
In the two-argument form, `@test_throws exception expr`, the `exception` can be a type or a pattern.
811
+
812
+
In the three-argument form, `@test_throws extype pattern expr`, both the exception type and
813
+
a message pattern are tested. The `extype` must be a type, and `pattern` may be
814
+
a string, regular expression, or list of strings occurring in the displayed error message,
815
+
a matching function, or a value.
816
+
808
817
Note that `@test_throws` does not support a trailing keyword form.
809
818
810
819
!!! compat "Julia 1.8"
811
820
The ability to specify anything other than a type or a value as `exception` requires Julia v1.8 or later.
812
821
822
+
!!! compat "Julia 1.13"
823
+
The three-argument form `@test_throws extype pattern expr` requires Julia v1.12 or later.
824
+
813
825
# Examples
814
826
```jldoctest
815
827
julia> @test_throws BoundsError [1, 2, 3][4]
@@ -823,13 +835,19 @@ Test Passed
823
835
julia> @test_throws "Try sqrt(Complex" sqrt(-1)
824
836
Test Passed
825
837
Message: "DomainError with -1.0:\\nsqrt was called with a negative real argument but will only return a complex result if called with a complex argument. Try sqrt(Complex(x))."
# Check that the right type of exception was thrown
870
904
success =false
871
905
message_only =false
872
906
exc = result.exception
873
-
# NB: Throwing LoadError from macroexpands is deprecated, but in order to limit
874
-
# the breakage in package tests we add extra logic here.
875
-
from_macroexpand =
876
-
orig_expr isa Expr &&
877
-
orig_expr.head in (:call, :macrocall) &&
878
-
orig_expr.args[1] in MACROEXPAND_LIKE
879
-
ifisa(extype, Type)
880
-
success =
881
-
if from_macroexpand && extype == LoadError && exc isa Exception
882
-
Base.depwarn("macroexpand no longer throws a LoadError so `@test_throws LoadError ...` is deprecated and passed without checking the error type!", :do_test_throws)
if extype isa LoadError &&!(exc isa LoadError) &&typeof(extype.error) ==typeof(exc)
892
-
extype = extype.error # deprecated
907
+
908
+
# Handle three-argument form (type + pattern)
909
+
if pattern !==nothing
910
+
# In 3-arg form, first argument must be a type
911
+
if!isa(extype, Type)
912
+
testres =Fail(:test_throws_wrong, orig_expr, extype, exc, nothing, result.source, false, "First argument must be an exception type in three-argument form")
913
+
record(get_testset(), testres)
914
+
return
893
915
end
894
-
# Support `UndefVarError(:x)` meaning `UndefVarError(:x, scope)` for any `scope`.
895
-
# Retains the behaviour from pre-v1.11 when `UndefVarError` didn't have `scope`.
combined_expected =string(extype) *" with pattern "* pattern_str
922
+
923
+
# Check both type and pattern
924
+
type_success =isa(exc, extype)
925
+
if type_success
926
+
exc_msg =sprint(showerror, exc)
927
+
pattern_success =contains_warn(exc_msg, pattern)
928
+
success = pattern_success
929
+
else
930
+
success =false
900
931
end
932
+
extype = combined_expected # Use combined format for all results
901
933
else
902
-
message_only =true
903
-
exc =sprint(showerror, exc)
904
-
success =contains_warn(exc, extype)
905
-
exc =repr(exc)
906
-
ifisa(extype, AbstractString)
907
-
extype =repr(extype)
908
-
elseifisa(extype, Function)
909
-
extype ="< match function >"
934
+
# Original two-argument form logic
935
+
# NB: Throwing LoadError from macroexpands is deprecated, but in order to limit
936
+
# the breakage in package tests we add extra logic here.
937
+
from_macroexpand =
938
+
orig_expr isa Expr &&
939
+
orig_expr.head in (:call, :macrocall) &&
940
+
orig_expr.args[1] in MACROEXPAND_LIKE
941
+
ifisa(extype, Type)
942
+
success =
943
+
if from_macroexpand && extype == LoadError && exc isa Exception
944
+
Base.depwarn("macroexpand no longer throws a LoadError so `@test_throws LoadError ...` is deprecated and passed without checking the error type!", :do_test_throws)
0 commit comments