Skip to content

Commit c05bc5b

Browse files
committed
Fix tests on Julia < 1.7
1 parent 558becb commit c05bc5b

1 file changed

Lines changed: 30 additions & 4 deletions

File tree

test/runtests.jl

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,32 @@ struct Struct{X,Y,Z}
2020
z::Z
2121
end
2222

23+
# Copied from ChainRulesCore: Test if a macro throws an error when it is expanded
24+
macro test_macro_throws(err_expr, expr)
25+
quote
26+
err = try
27+
@macroexpand($(esc(expr)))
28+
nothing
29+
catch _err
30+
# https://github.com/JuliaLang/julia/pull/38379
31+
if VERSION >= v"1.7.0-DEV.937"
32+
_err
33+
else
34+
# until Julia v1.7
35+
# all errors thrown at macro expansion time are LoadErrors, we need to unwrap
36+
@assert _err isa LoadError
37+
_err.error
38+
end
39+
end
40+
# Reuse `@test_throws` logic
41+
if err !== nothing
42+
@test_throws $(esc(err_expr)) ($(Meta.quot(expr)); throw(err))
43+
else
44+
@test_throws $(esc(err_expr)) $(Meta.quot(expr))
45+
end
46+
end
47+
end
48+
2349
@testset "SimpleUnPack.jl" begin
2450
@testset "Variable as RHS" begin
2551
d = (x=42, y=1.0, z="z")
@@ -64,9 +90,9 @@ end
6490

6591
@testset "Errors" begin
6692
d = (; x=42, y=1.0)
67-
@test_throws ArgumentError @macroexpand @unpack d
68-
@test_throws ArgumentError @macroexpand @unpack (; x=42, y=1.0)
69-
@test_throws ArgumentError @macroexpand @unpack x, y, (; x=42, y=1.0)
70-
@test_throws ArgumentError @macroexpand @unpack x, 1 = (; x=42, y=1.0)
93+
@test_macro_throws ArgumentError @unpack d
94+
@test_macro_throws ArgumentError @unpack (; x=42, y=1.0)
95+
@test_macro_throws ArgumentError @unpack x, y, (; x=42, y=1.0)
96+
@test_macro_throws ArgumentError @unpack x, 1 = (; x=42, y=1.0)
7197
end
7298
end

0 commit comments

Comments
 (0)