Skip to content

Commit ad6c689

Browse files
authored
[Bridges] improve test coverage (#2672)
1 parent acb56d4 commit ad6c689

File tree

2 files changed

+38
-7
lines changed

2 files changed

+38
-7
lines changed

src/Bridges/Bridges.jl

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,18 @@ function _test_structural_identical(
222222
return
223223
end
224224

225+
_runtests_error_handler(err, ::Bool) = rethrow(err)
226+
227+
function _runtests_error_handler(
228+
err::MOI.GetAttributeNotAllowed{MOI.ConstraintFunction},
229+
cannot_unbridge::Bool,
230+
)
231+
if cannot_unbridge
232+
return # This error is expected. Do nothing.
233+
end
234+
return rethrow(err)
235+
end
236+
225237
"""
226238
runtests(
227239
Bridge::Type{<:AbstractBridge},
@@ -305,13 +317,8 @@ function runtests(
305317
set = try
306318
MOI.get(model, MOI.ConstraintSet(), ci)
307319
catch err
308-
# Could be thrown by `unbridged_function`
309-
if cannot_unbridge &&
310-
err isa MOI.GetAttributeNotAllowed{MOI.ConstraintFunction}
311-
continue
312-
else
313-
rethrow(err)
314-
end
320+
_runtests_error_handler(err, cannot_unbridge)
321+
continue
315322
end
316323
for attr in (MOI.ConstraintPrimalStart(), MOI.ConstraintDualStart())
317324
if MOI.supports(model, attr, MOI.ConstraintIndex{F,S})

test/Bridges/Variable/zeros.jl

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,30 @@ function test_runtests()
4646
return
4747
end
4848

49+
function test_bridge_error_handler()
50+
for (err, flag) in (
51+
ErrorException("abc") => false,
52+
MOI.GetAttributeNotAllowed(MOI.ObjectiveSense()) => false,
53+
MOI.GetAttributeNotAllowed(MOI.ConstraintFunction()) => true,
54+
)
55+
@test_throws err try
56+
@assert false
57+
catch
58+
MOI.Bridges._runtests_error_handler(err, false)
59+
end
60+
if flag
61+
@test MOI.Bridges._runtests_error_handler(err, true) === nothing
62+
else
63+
@test_throws err try
64+
@assert false
65+
catch
66+
MOI.Bridges._runtests_error_handler(err, true)
67+
end
68+
end
69+
end
70+
return
71+
end
72+
4973
function test_zeros()
5074
mock = MOI.Utilities.MockOptimizer(MOI.Utilities.Model{Float64}())
5175
bridged_mock = MOI.Bridges.Variable.Zeros{Float64}(mock)

0 commit comments

Comments
 (0)