@@ -86,8 +86,9 @@ struct Pass <: Result
8686 data
8787 value
8888 source:: Union{Nothing,LineNumberNode}
89- function Pass (test_type:: Symbol , orig_expr, data, thrown, source= nothing )
90- return new (test_type, orig_expr, data, thrown isa String ? " String" : thrown, source)
89+ message_only:: Bool
90+ function Pass (test_type:: Symbol , orig_expr, data, thrown, source= nothing , message_only= false )
91+ return new (test_type, orig_expr, data, thrown, source, message_only)
9192 end
9293end
9394
@@ -98,7 +99,11 @@ function Base.show(io::IO, t::Pass)
9899 end
99100 if t. test_type === :test_throws
100101 # The correct type of exception was thrown
101- print (io, " \n Thrown: " , t. value isa String ? t. value : typeof (t. value))
102+ if t. message_only
103+ print (io, " \n Message: " , t. value)
104+ else
105+ print (io, " \n Thrown: " , typeof (t. value))
106+ end
102107 elseif t. test_type === :test && t. data != = nothing
103108 # The test was an expression, so display the term-by-term
104109 # evaluated version as well
@@ -118,12 +123,14 @@ struct Fail <: Result
118123 data:: Union{Nothing, String}
119124 value:: String
120125 source:: LineNumberNode
121- function Fail (test_type:: Symbol , orig_expr, data, value, source:: LineNumberNode )
126+ message_only:: Bool
127+ function Fail (test_type:: Symbol , orig_expr, data, value, source:: LineNumberNode , message_only:: Bool = false )
122128 return new (test_type,
123129 string (orig_expr),
124130 data === nothing ? nothing : string (data),
125131 string (isa (data, Type) ? typeof (value) : value),
126- source)
132+ source,
133+ message_only)
127134 end
128135end
129136
@@ -132,18 +139,24 @@ function Base.show(io::IO, t::Fail)
132139 print (io, " at " )
133140 printstyled (io, something (t. source. file, :none ), " :" , t. source. line, " \n " ; bold= true , color= :default )
134141 print (io, " Expression: " , t. orig_expr)
142+ value, data = t. value, t. data
135143 if t. test_type === :test_throws_wrong
136144 # An exception was thrown, but it was of the wrong type
137- print (io, " \n Expected: " , t. data)
138- print (io, " \n Thrown: " , t. value)
145+ if t. message_only
146+ print (io, " \n Expected: " , data)
147+ print (io, " \n Message: " , value)
148+ else
149+ print (io, " \n Expected: " , data)
150+ print (io, " \n Thrown: " , value)
151+ end
139152 elseif t. test_type === :test_throws_nothing
140153 # An exception was expected, but no exception was thrown
141- print (io, " \n Expected: " , t . data)
154+ print (io, " \n Expected: " , data)
142155 print (io, " \n No exception thrown" )
143- elseif t. test_type === :test && t . data != = nothing
156+ elseif t. test_type === :test && data != = nothing
144157 # The test was an expression, so display the term-by-term
145158 # evaluated version as well
146- print (io, " \n Evaluated: " , t . data)
159+ print (io, " \n Evaluated: " , data)
147160 end
148161end
149162
@@ -238,6 +251,7 @@ function Serialization.serialize(s::Serialization.AbstractSerializer, t::Pass)
238251 Serialization. serialize (s, t. data === nothing ? nothing : string (t. data))
239252 Serialization. serialize (s, string (t. value))
240253 Serialization. serialize (s, t. source === nothing ? nothing : t. source)
254+ Serialization. serialize (s, t. message_only)
241255 nothing
242256end
243257
657671
658672Tests that the expression `expr` throws `exception`.
659673The exception may specify either a type,
674+ a string, regular expression, or list of strings occurring in the displayed error message,
675+ a matching function,
660676or a value (which will be tested for equality by comparing fields).
661677Note that `@test_throws` does not support a trailing keyword form.
662678
@@ -671,7 +687,18 @@ julia> @test_throws DimensionMismatch [1, 2, 3] + [1, 2]
671687Test Passed
672688 Expression: [1, 2, 3] + [1, 2]
673689 Thrown: DimensionMismatch
690+
691+ julia> @test_throws "Try sqrt(Complex" sqrt(-1)
692+ Test Passed
693+ Expression: sqrt(-1)
694+ Message: "DomainError with -1.0:\\ nsqrt will only return a complex result if called with a complex argument. Try sqrt(Complex(x))."
674695```
696+
697+ In the final example, instead of matching a single string it could alternatively have been performed with:
698+
699+ - `["Try", "Complex"]` (a list of strings)
700+ - `r"Try sqrt\\ ([Cc]omplex"` (a regular expression)
701+ - `str -> occursin("complex", str)` (a matching function)
675702"""
676703macro test_throws (extype, ex)
677704 orig_ex = Expr (:inert , ex)
@@ -697,6 +724,7 @@ function do_test_throws(result::ExecutionResult, orig_expr, extype)
697724 if isa (result, Threw)
698725 # Check that the right type of exception was thrown
699726 success = false
727+ message_only = false
700728 exc = result. exception
701729 # NB: Throwing LoadError from macroexpands is deprecated, but in order to limit
702730 # the breakage in package tests we add extra logic here.
@@ -712,7 +740,7 @@ function do_test_throws(result::ExecutionResult, orig_expr, extype)
712740 else
713741 isa (exc, extype)
714742 end
715- else
743+ elseif isa (extype, Exception) || ! isa (exc, Exception)
716744 if extype isa LoadError && ! (exc isa LoadError) && typeof (extype. error) == typeof (exc)
717745 extype = extype. error # deprecated
718746 end
@@ -725,11 +753,21 @@ function do_test_throws(result::ExecutionResult, orig_expr, extype)
725753 end
726754 end
727755 end
756+ else
757+ message_only = true
758+ exc = sprint (showerror, exc)
759+ success = contains_warn (exc, extype)
760+ exc = repr (exc)
761+ if isa (extype, AbstractString)
762+ extype = repr (extype)
763+ elseif isa (extype, Function)
764+ extype = " < match function >"
765+ end
728766 end
729767 if success
730- testres = Pass (:test_throws , orig_expr, extype, exc, result. source)
768+ testres = Pass (:test_throws , orig_expr, extype, exc, result. source, message_only )
731769 else
732- testres = Fail (:test_throws_wrong , orig_expr, extype, exc, result. source)
770+ testres = Fail (:test_throws_wrong , orig_expr, extype, exc, result. source, message_only )
733771 end
734772 else
735773 testres = Fail (:test_throws_nothing , orig_expr, extype, nothing , result. source)
0 commit comments