|
1 | | -# Test that the macro call `m` throws an error exception during pre-compilation |
2 | | -macro test_macro_throws(errortype, m) |
3 | | - # See https://discourse.julialang.org/t/test-throws-with-macros-after-pr-23533/5878 |
4 | | - return :(@test_throws $errortype try |
5 | | - @eval $m |
6 | | - catch err |
7 | | - throw(err.error) |
8 | | - end) |
| 1 | +# Copyright 2017, Iain Dunning, Joey Huchette, Miles Lubin, and contributors |
| 2 | +# This Source Code Form is subject to the terms of the Mozilla Public |
| 3 | +# License, v. 2.0. If a copy of the MPL was not distributed with this |
| 4 | +# file, You can obtain one at https://mozilla.org/MPL/2.0/. |
| 5 | +############################################################################# |
| 6 | + |
| 7 | +# Copy-pasted from JuMP.jl/test/utilities.jl |
| 8 | + |
| 9 | +function _strip_line_from_error(err::ErrorException) |
| 10 | + return ErrorException(replace(err.msg, r"^At.+\:[0-9]+\: `@" => "In `@")) |
| 11 | +end |
| 12 | + |
| 13 | +_strip_line_from_error(err::LoadError) = _strip_line_from_error(err.error) |
| 14 | + |
| 15 | +_strip_line_from_error(err) = err |
| 16 | + |
| 17 | +""" |
| 18 | + @test_throws_parsetime(error_type, expression) |
| 19 | +
|
| 20 | +Test that the macro call `expression` throws an `error_type` exception during |
| 21 | +the parsing of the macro. |
| 22 | +
|
| 23 | +See https://discourse.julialang.org/t/test-throws-with-macros-after-pr-23533 |
| 24 | +""" |
| 25 | +macro test_throws_parsetime(error_type, expression) |
| 26 | + return quote |
| 27 | + @test_throws( |
| 28 | + $(esc(_strip_line_from_error(error_type))), |
| 29 | + try |
| 30 | + @eval $expression |
| 31 | + catch err |
| 32 | + throw(_strip_line_from_error(err)) |
| 33 | + end |
| 34 | + ) |
| 35 | + end |
9 | 36 | end |
0 commit comments