Skip to content

Commit

Permalink
Merge branch 'master' into od/error_fn
Browse files Browse the repository at this point in the history
  • Loading branch information
odow authored Dec 7, 2023
2 parents a5a729b + 59cc031 commit 839981c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 14 deletions.
22 changes: 8 additions & 14 deletions src/macros.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1777,30 +1777,24 @@ In the last case, the expression throws an error using the `error_fn`
function in case the value is not an `MOI.OptimizationSense`.
"""
function _moi_sense(error_fn::Function, sense)
if sense == :Min
expr = MIN_SENSE
expr = if sense == :Min
MIN_SENSE
elseif sense == :Max
expr = MAX_SENSE
MAX_SENSE
else
# Refers to a variable that holds the sense.
# TODO: Better document this behavior
expr = esc(sense)
esc(sense)
end
return :(_throw_error_for_invalid_sense($error_fn, $expr))
end

function _throw_error_for_invalid_sense(error_fn::Function, sense)
return error_fn(
"Unexpected sense `$value`. The sense must be an",
" `MOI.OptimizatonSense`, `Min` or `Max`.",
"unexpected sense `$sense`. The sense must be an " *
"`::MOI.OptimizatonSense`, or the symbol `:Min` or `:Max`.",
)
end
function _throw_error_for_invalid_sense(
error_fn::Function,
sense::MOI.OptimizationSense,
)
return sense
end

_throw_error_for_invalid_sense(::Function, sense::MOI.OptimizationSense) = sense

"""
_replace_zero(model::M, x) where {M<:AbstractModel}
Expand Down
14 changes: 14 additions & 0 deletions test/test_macros.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2131,4 +2131,18 @@ function test_issue_3514()
return
end

function test_bad_objective_sense()
model = Model()
@variable(model, x)
@test_throws_strip(
ErrorException(
"In `@objective(model, :MinMax, x)`: unexpected sense `MinMax`. " *
"The sense must be an `::MOI.OptimizatonSense`, or the symbol " *
"`:Min` or `:Max`.",
),
@objective(model, :MinMax, x),
)
return
end

end # module

0 comments on commit 839981c

Please sign in to comment.