@@ -86,8 +86,9 @@ struct Pass <: Result
86
86
data
87
87
value
88
88
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)
91
92
end
92
93
end
93
94
@@ -98,7 +99,11 @@ function Base.show(io::IO, t::Pass)
98
99
end
99
100
if t. test_type === :test_throws
100
101
# 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
102
107
elseif t. test_type === :test && t. data != = nothing
103
108
# The test was an expression, so display the term-by-term
104
109
# evaluated version as well
@@ -118,12 +123,14 @@ struct Fail <: Result
118
123
data:: Union{Nothing, String}
119
124
value:: String
120
125
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 )
122
128
return new (test_type,
123
129
string (orig_expr),
124
130
data === nothing ? nothing : string (data),
125
131
string (isa (data, Type) ? typeof (value) : value),
126
- source)
132
+ source,
133
+ message_only)
127
134
end
128
135
end
129
136
@@ -132,18 +139,24 @@ function Base.show(io::IO, t::Fail)
132
139
print (io, " at " )
133
140
printstyled (io, something (t. source. file, :none ), " :" , t. source. line, " \n " ; bold= true , color= :default )
134
141
print (io, " Expression: " , t. orig_expr)
142
+ value, data = t. value, t. data
135
143
if t. test_type === :test_throws_wrong
136
144
# 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
139
152
elseif t. test_type === :test_throws_nothing
140
153
# An exception was expected, but no exception was thrown
141
- print (io, " \n Expected: " , t . data)
154
+ print (io, " \n Expected: " , data)
142
155
print (io, " \n No exception thrown" )
143
- elseif t. test_type === :test && t . data != = nothing
156
+ elseif t. test_type === :test && data != = nothing
144
157
# The test was an expression, so display the term-by-term
145
158
# evaluated version as well
146
- print (io, " \n Evaluated: " , t . data)
159
+ print (io, " \n Evaluated: " , data)
147
160
end
148
161
end
149
162
@@ -238,6 +251,7 @@ function Serialization.serialize(s::Serialization.AbstractSerializer, t::Pass)
238
251
Serialization. serialize (s, t. data === nothing ? nothing : string (t. data))
239
252
Serialization. serialize (s, string (t. value))
240
253
Serialization. serialize (s, t. source === nothing ? nothing : t. source)
254
+ Serialization. serialize (s, t. message_only)
241
255
nothing
242
256
end
243
257
657
671
658
672
Tests that the expression `expr` throws `exception`.
659
673
The 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,
660
676
or a value (which will be tested for equality by comparing fields).
661
677
Note that `@test_throws` does not support a trailing keyword form.
662
678
@@ -671,7 +687,18 @@ julia> @test_throws DimensionMismatch [1, 2, 3] + [1, 2]
671
687
Test Passed
672
688
Expression: [1, 2, 3] + [1, 2]
673
689
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))."
674
695
```
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)
675
702
"""
676
703
macro test_throws (extype, ex)
677
704
orig_ex = Expr (:inert , ex)
@@ -697,6 +724,7 @@ function do_test_throws(result::ExecutionResult, orig_expr, extype)
697
724
if isa (result, Threw)
698
725
# Check that the right type of exception was thrown
699
726
success = false
727
+ message_only = false
700
728
exc = result. exception
701
729
# NB: Throwing LoadError from macroexpands is deprecated, but in order to limit
702
730
# the breakage in package tests we add extra logic here.
@@ -712,7 +740,7 @@ function do_test_throws(result::ExecutionResult, orig_expr, extype)
712
740
else
713
741
isa (exc, extype)
714
742
end
715
- else
743
+ elseif isa (extype, Exception) || ! isa (exc, Exception)
716
744
if extype isa LoadError && ! (exc isa LoadError) && typeof (extype. error) == typeof (exc)
717
745
extype = extype. error # deprecated
718
746
end
@@ -725,11 +753,21 @@ function do_test_throws(result::ExecutionResult, orig_expr, extype)
725
753
end
726
754
end
727
755
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
728
766
end
729
767
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 )
731
769
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 )
733
771
end
734
772
else
735
773
testres = Fail (:test_throws_nothing , orig_expr, extype, nothing , result. source)
0 commit comments