Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
hdavid16 committed Oct 16, 2023
1 parent fae47b1 commit e40f340
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 11 deletions.
11 changes: 1 addition & 10 deletions src/macros.jl
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ function _name_call(base_name, idxvars)
return ex
end

# Process macro arugments
# Process macro arguments
function _extract_kwargs(args)
arg_list = collect(args)
if !isempty(args) && isexpr(args[1], :parameters)
Expand Down Expand Up @@ -89,15 +89,6 @@ function _add_positional_args(call, args)
return
end

# Determine if an expression contains any index variable symbols
function _has_idxvars(expr, idxvars)
expr in idxvars && return true
if expr isa Expr
return any(_has_idxvars(a, idxvars) for a in expr.args)
end
return false
end

# Ensure a model argument is valid
# Inspired from https://github.com/jump-dev/JuMP.jl/blob/d9cd5fb16c2d0a7e1c06aa9941923492fc9a28b5/src/macros.jl#L38-L44
function _valid_model(_error::Function, model, name)
Expand Down
13 changes: 13 additions & 0 deletions test/disjunction.jl → test/constraints/disjunction.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
function test_macro_helpers()
@test DP._esc_non_constant(1) == 1
@test DP._get_name(:x) == :x
@test DP._get_name("x") == "x"
@test DP._get_name(Expr(:string,"x")) == Expr(:string,"x")
@test DP._name_call("",[]) == ""
@test DP._name_call("name",[]) == "name"
end

function test_disjunction_add_fail()
model = GDPModel()
@variable(model, x)
Expand All @@ -21,6 +30,7 @@ function test_disjunction_add_fail()
@constraint(model, x == 10, DisjunctConstraint(y[2]))
@disjunction(model, disj, y)
@test_macro_throws UndefVarError @disjunction(model, disj, y) #duplicate name
@test_throws ErrorException DP._error_if_cannot_register(error, model, :disj) #duplicate name

@test_macro_throws ErrorException @disjunction(model, "bad"[i=1:2], y) #wrong expression for disjunction name
@test_macro_throws ErrorException @disjunction(model, [model=1:2], y) #index name can't be same as model name
Expand Down Expand Up @@ -209,6 +219,9 @@ function test_disjunction_function_nested()
end

@testset "Disjunction" begin
@testset "Macro Helpers" begin
test_macro_helpers()
end
@testset "Add Disjunction" begin
test_disjunction_add_fail()
test_disjunction_add_success()
Expand Down
2 changes: 1 addition & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,5 @@ include("constraints/indicator.jl")
include("constraints/bigm.jl")
include("constraints/hull.jl")
include("constraints/fallback.jl")
include("disjunction.jl")
include("constraints/disjunction.jl")
include("solve.jl")

0 comments on commit e40f340

Please sign in to comment.