Skip to content

Commit aa91b45

Browse files
authored
Merge pull request #502 from JuliaOpt/bl/fix499
Test InexactError thrown for bad objective function type
2 parents bc6bb2d + ad43d66 commit aa91b45

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

src/Test/UnitTests/objectives.jl

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,31 @@ function feasibility_sense(model::MOI.ModelLike, config::TestConfig)
5656
end
5757
unittests["feasibility_sense"] = feasibility_sense
5858

59+
"""
60+
get_objective_function(model::MOI.ModelLike, config::TestConfig)
61+
62+
Test get objective function.
63+
"""
64+
function get_objective_function(model::MOI.ModelLike, config::TestConfig)
65+
MOI.empty!(model)
66+
@test MOI.isempty(model)
67+
obj_attr = MOI.ObjectiveFunction{MOI.ScalarAffineFunction{Float64}}()
68+
@test MOI.supports(model, obj_attr)
69+
MOIU.loadfromstring!(model,"""
70+
variables: x
71+
minobjective: 2.0x + 1.0
72+
""")
73+
x = MOI.get(model, MOI.VariableIndex, "x")
74+
expected_obj_fun = MOI.ScalarAffineFunction([MOI.ScalarAffineTerm(2.0, x)],
75+
1.0)
76+
@test_throws InexactError begin
77+
MOI.get(model, MOI.ObjectiveFunction{MOI.SingleVariable}())
78+
end
79+
obj_fun = MOI.get(model, obj_attr)
80+
@test obj_fun expected_obj_fun
81+
end
82+
unittests["get_objective_function"] = get_objective_function
83+
5984
"""
6085
solve_constant_obj(model::MOI.ModelLike, config::TestConfig)
6186

src/Utilities/model.jl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,11 @@ function MOI.get(model::AbstractModel, ::MOI.ObjectiveFunctionType)
233233
end
234234
function MOI.get(model::AbstractModel, ::MOI.ObjectiveFunction{T})::T where T
235235
if typeof(model.objective) != T
236-
throw(InexactError())
236+
if VERSION >= v"0.7-"
237+
throw(InexactError(:get, T, model.objective))
238+
else
239+
throw(InexactError())
240+
end
237241
end
238242
model.objective
239243
end

0 commit comments

Comments
 (0)