@@ -16,7 +16,7 @@ module Test
1616export @test , @test_throws , @test_broken , @test_skip , @test_warn , @test_nowarn
1717export @testset
1818# Legacy approximate testing functions, yet to be included
19- export @test_approx_eq_eps , @ inferred
19+ export @inferred
2020export detect_ambiguities
2121export GenericString
2222
@@ -203,29 +203,65 @@ end
203203
204204const comparison_prec = Base. operator_precedence (:(== ))
205205
206+ """
207+ test_expr!(ex, kws...)
208+
209+ Preprocess test expressions of function calls with trailing keyword arguments
210+ so that e.g. `@test a ≈ b atol=ε` means `@test ≈(a, b, atol=ε)`.
211+ """
212+ test_expr! (m, ex) = ex
213+
214+ function test_expr! (m, ex, kws... )
215+ ex isa Expr && ex. head == :call || @goto fail
216+ for kw in kws
217+ kw isa Expr && kw. head == :(= ) || @goto fail
218+ kw. head = :kw
219+ push! (ex. args, kw)
220+ end
221+ return ex
222+ @label fail
223+ error (" invalid test macro call: $m $ex $(join (kws," " )) " )
224+ end
225+
206226# @test - check if the expression evaluates to true
207227"""
208228 @test ex
229+ @test f(args...) key=val ...
209230
210231Tests that the expression `ex` evaluates to `true`.
211232Returns a `Pass` `Result` if it does, a `Fail` `Result` if it is
212233`false`, and an `Error` `Result` if it could not be evaluated.
234+
235+ The `@test f(args...) key=val...` form is equivalent to writing
236+ `@test f(args..., key=val...)` which can be useful when the expression
237+ is a call using infix syntax such as approximate comparisons:
238+
239+ @test a ≈ b atol=ε
240+
241+ This is equivalent to the uglier test `@test ≈(a, b, atol=ε)`.
242+ It is an error to supply more than one expression unless the first
243+ is a call expression and the rest are assignments (`k=v`).
213244"""
214- macro test (ex)
245+ macro test (ex, kws... )
246+ test_expr! (" @test" , ex, kws... )
215247 orig_ex = Expr (:inert , ex)
216248 result = get_test_result (ex)
217249 :(do_test ($ result, $ orig_ex))
218250end
219251
220252"""
221253 @test_broken ex
254+ @test_broken f(args...) key=val ...
222255
223256Indicates a test that should pass but currently consistently fails.
224257Tests that the expression `ex` evaluates to `false` or causes an
225258exception. Returns a `Broken` `Result` if it does, or an `Error` `Result`
226259if the expression evaluates to `true`.
260+
261+ The `@test_broken f(args...) key=val...` form works as for the `@test` macro.
227262"""
228- macro test_broken (ex)
263+ macro test_broken (ex, kws... )
264+ test_expr! (" @test_broken" , ex, kws... )
229265 orig_ex = Expr (:inert , ex)
230266 result = get_test_result (ex)
231267 # code to call do_test with execution result and original expr
@@ -234,12 +270,16 @@ end
234270
235271"""
236272 @test_skip ex
273+ @test_skip f(args...) key=val ...
237274
238275Marks a test that should not be executed but should be included in test
239276summary reporting as `Broken`. This can be useful for tests that intermittently
240277fail, or tests of not-yet-implemented functionality.
278+
279+ The `@test_skip f(args...) key=val...` form works as for the `@test` macro.
241280"""
242- macro test_skip (ex)
281+ macro test_skip (ex, kws... )
282+ test_expr! (" @test_skip" , ex, kws... )
243283 orig_ex = Expr (:inert , ex)
244284 testres = :(Broken (:skipped , $ orig_ex))
245285 :(record (get_testset (), $ testres))
326366 @test_throws extype ex
327367
328368Tests that the expression `ex` throws an exception of type `extype`.
369+ Note that `@test_throws` does not support a trailing keyword form.
329370"""
330371macro test_throws (extype, ex)
331372 orig_ex = Expr (:inert , ex)
@@ -960,71 +1001,6 @@ function get_testset_depth()
9601001 return length (testsets)
9611002end
9621003
963- # -----------------------------------------------------------------------
964- # Legacy approximate testing functions, yet to be included
965-
966- approx_full (x:: AbstractArray ) = x
967- approx_full (x:: Number ) = x
968- approx_full (x) = full (x)
969-
970- function test_approx_eq (va, vb, Eps, astr, bstr)
971- va = approx_full (va)
972- vb = approx_full (vb)
973- la, lb = length (linearindices (va)), length (linearindices (vb))
974- if la != lb
975- error (" lengths of " , astr, " and " , bstr, " do not match: " ,
976- " \n " , astr, " (length $la ) = " , va,
977- " \n " , bstr, " (length $lb ) = " , vb)
978- end
979- diff = real (zero (eltype (va)))
980- for (xa, xb) = zip (va, vb)
981- if isfinite (xa) && isfinite (xb)
982- diff = max (diff, abs (xa- xb))
983- elseif ! isequal (xa,xb)
984- error (" mismatch of non-finite elements: " ,
985- " \n " , astr, " = " , va,
986- " \n " , bstr, " = " , vb)
987- end
988- end
989-
990- if ! isnan (Eps) && ! (diff <= Eps)
991- sdiff = string (" |" , astr, " - " , bstr, " | <= " , Eps)
992- error (" assertion failed: " , sdiff,
993- " \n " , astr, " = " , va,
994- " \n " , bstr, " = " , vb,
995- " \n difference = " , diff, " > " , Eps)
996- end
997- end
998-
999- array_eps {T} (a:: AbstractArray{Complex{T}} ) = eps (float (maximum (x-> (isfinite (x) ? abs (x) : T (NaN )), a)))
1000- array_eps (a) = eps (float (maximum (x-> (isfinite (x) ? abs (x) : oftype (x,NaN )), a)))
1001-
1002- test_approx_eq (va, vb, astr, bstr) =
1003- test_approx_eq (va, vb, 1E4 * length (linearindices (va))* max (array_eps (va), array_eps (vb)), astr, bstr)
1004-
1005- """
1006- @test_approx_eq_eps(a, b, tol)
1007-
1008- Test two floating point numbers `a` and `b` for equality taking into account
1009- a margin of tolerance given by `tol`.
1010- """
1011- macro test_approx_eq_eps (a, b, c)
1012- :(test_approx_eq ($ (esc (a)), $ (esc (b)), $ (esc (c)), $ (string (a)), $ (string (b))))
1013- end
1014-
1015- """
1016- @test_approx_eq(a, b)
1017-
1018- Deprecated. Test two floating point numbers `a` and `b` for equality taking into
1019- account small numerical errors.
1020- """
1021- macro test_approx_eq (a, b)
1022- Base. depwarn (string (" @test_approx_eq is deprecated, use `@test " , a, " ≈ " , b, " ` instead" ),
1023- Symbol (" @test_approx_eq" ))
1024- :(test_approx_eq ($ (esc (a)), $ (esc (b)), $ (string (a)), $ (string (b))))
1025- end
1026- export @test_approx_eq
1027-
10281004_args_and_call (args... ; kwargs... ) = (args[1 : end - 1 ], kwargs, args[end ](args[1 : end - 1 ]. .. ; kwargs... ))
10291005"""
10301006 @inferred f(x)
@@ -1119,13 +1095,13 @@ end
11191095# Raises an error if any columnwise vector norm exceeds err. Otherwise, returns
11201096# nothing.
11211097function test_approx_eq_modphase {S<:Real,T<:Real} (
1122- a:: StridedVecOrMat{S} , b:: StridedVecOrMat{T} , err= nothing )
1098+ a:: StridedVecOrMat{S} , b:: StridedVecOrMat{T} ,
1099+ err = length (indices (a,1 ))^ 3 * (eps (S)+ eps (T))
1100+ )
11231101 @test indices (a,1 ) == indices (b,1 ) && indices (a,2 ) == indices (b,2 )
1124- m = length (indices (a,1 ))
1125- err === nothing && (err= m^ 3 * (eps (S)+ eps (T)))
11261102 for i in indices (a,2 )
11271103 v1, v2 = a[:, i], b[:, i]
1128- @test_approx_eq_eps min (abs (norm (v1- v2)), abs (norm (v1+ v2))) 0.0 err
1104+ @test min (abs (norm (v1- v2)),abs (norm (v1+ v2))) ≈ 0.0 atol = err
11291105 end
11301106end
11311107
0 commit comments