Skip to content

Commit

Permalink
Merge 9b975c8 into 24515f3
Browse files Browse the repository at this point in the history
  • Loading branch information
yashvardhan747 committed Apr 14, 2020
2 parents 24515f3 + 9b975c8 commit 7cde6c2
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/macros.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1142,8 +1142,15 @@ macro variable(args...)
anon_singleton = true
else
x = popfirst!(extra)
if x in [:Int,:Bin,:PSD]
_error("Ambiguous variable name $x detected. Use the \"category\" keyword argument to specify a category for an anonymous variable.")
if x == :Int
_error("Ambiguous variable name $x detected. To specify an anonymous integer " *
"variable, use `@variable(model, integer = true)` instead.")
elseif x == :Bin
_error("Ambiguous variable name $x detected. To specify an anonymous binary " *
"variable, use `@variable(model, binary = true)` instead.")
elseif x == :PSD
_error("Size of anonymous square matrix of positive semidefinite anonymous variables is not specified. To specify size of square matrix " *
"use `@variable(model, [1:n, 1:n], PSD)` instead.")
end
anon_singleton = false
end
Expand Down
20 changes: 20 additions & 0 deletions test/macros.jl
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,26 @@ end
@testset "Macros for JuMP.Model" begin
macros_test(Model, VariableRef)

@testset "Adding anonymous variable and specify required constraint on it" begin
model = Model()
@test_macro_throws ErrorException("In `@variable(m, Int)`: Ambiguous variable name Int detected." *
" To specify an anonymous integer variable, use `@variable(model, integer = true)` instead.") @variable(m, Int)
v = @variable(model, integer = true)
@test name(v) == ""
@test is_integer(v)

@test_macro_throws ErrorException("In `@variable(m, Bin)`: Ambiguous variable name Bin detected." *
" To specify an anonymous binary variable, use `@variable(model, binary = true)` instead.") @variable(m, Bin)
v = @variable(model, binary = true)
@test name(v) == ""
@test is_binary(v)

@test_macro_throws ErrorException("In `@variable(m, PSD)`: Size of anonymous square matrix of positive semidefinite anonymous variables is not specified." *
" To specify size of square matrix use `@variable(model, [1:n, 1:n], PSD)` instead.") @variable(m, PSD)
v = @variable(model, [1:1, 1:1], PSD)
@test name(v[1]) == ""
end

@testset "Nested tuple destructuring" begin
m = Model()
d = Dict((1,2) => 3)
Expand Down

0 comments on commit 7cde6c2

Please sign in to comment.