Skip to content

Commit

Permalink
Fix escaping of base_name arguments in macros (#3631)
Browse files Browse the repository at this point in the history
  • Loading branch information
odow authored Dec 15, 2023
1 parent 65d12dc commit 27c701b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Containers/macro.jl
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@ function build_name_expr(
kwargs::Dict{Symbol,Any},
)
base_name = get(kwargs, :base_name, string(something(name, "")))
if base_name isa Expr
if !(base_name isa String)
base_name = esc(base_name)
end
if isempty(index_vars) || base_name == ""
Expand Down
18 changes: 18 additions & 0 deletions test/test_macros.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2167,4 +2167,22 @@ function test_expression_container_kwarg()
return
end

function test_base_name_escape()
model = Model()
x = @variable(model)
@test name(x) == ""
x = @variable(model, [1:2])
@test name.(x) == ["", ""]
x = @variable(model, base_name = "x")
@test name(x) == "x"
x = @variable(model, [1:2], base_name = "x")
@test name.(x) == ["x[1]", "x[2]"]
y = "abc"
x = @variable(model, base_name = y)
@test name(x) == "abc"
x = @variable(model, [1:2], base_name = y)
@test name.(x) == ["abc[1]", "abc[2]"]
return
end

end # module

0 comments on commit 27c701b

Please sign in to comment.