Skip to content

Commit d810194

Browse files
committed
Update test_macro_throws
1 parent 033869e commit d810194

3 files changed

Lines changed: 42 additions & 15 deletions

File tree

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ SemialgebraicSets = "8e049039-38e8-557d-ae3a-bc521ccf6204"
1919
[compat]
2020
DataStructures = "0.18, 0.19"
2121
DynamicPolynomials = "0.5, 0.6"
22-
IntervalArithmetic = "0.22"
22+
IntervalArithmetic = "0.22, 1"
2323
JuMP = "1"
2424
MathOptInterface = "1"
2525
MultivariateBases = "0.2"

test/constraint.jl

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -120,14 +120,14 @@ function test_errors(var)
120120
@variable m β
121121
p = α * x * y + β * x^2
122122
q = α * x^2 + β * x * y + α * y^2
123-
@test_macro_throws ErrorException @constraint(m, p)
124-
@test_macro_throws ErrorException @constraint(m, begin
123+
@test_throws_parsetime ErrorException @constraint(m, p)
124+
@test_throws_parsetime ErrorException @constraint(m, begin
125125
p >= 0
126126
end)
127-
@test_macro_throws ErrorException @constraint(m, +(p, p, p))
128-
@test_macro_throws ErrorException @constraint(m, p >= 0, 1)
129-
#@test_macro_throws ErrorException @constraint(m, p >= 0, domain = (@set x >= -1 && x <= 1, domain = y >= -1 && y <= 1))
130-
@test_macro_throws ErrorException @constraint(
127+
@test_throws_parsetime ErrorException @constraint(m, +(p, p, p))
128+
@test_throws_parsetime ErrorException @constraint(m, p >= 0, 1)
129+
#@test_throws_parsetime ErrorException @constraint(m, p >= 0, domain = (@set x >= -1 && x <= 1, domain = y >= -1 && y <= 1))
130+
@test_throws_parsetime ErrorException @constraint(
131131
m,
132132
p + 0,
133133
domain = (@set x >= -1 && x <= 1)

test/utilities.jl

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,36 @@
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
936
end

0 commit comments

Comments
 (0)