Skip to content

Commit

Permalink
[Test] wrap Test module to avoid conflict with MOI.Test (#2267)
Browse files Browse the repository at this point in the history
  • Loading branch information
odow authored Sep 18, 2023
1 parent e98bee2 commit dc166f6
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 19 deletions.
28 changes: 27 additions & 1 deletion src/Test/Test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,31 @@ import LinearAlgebra
import MathOptInterface as MOI
import MathOptInterface.Utilities as MOIU

#=
We made a bit of a mistake calling the `Test/Test.jl` submodule "Test" because
it conflicts with the standard library "Test" which is imported by MOI.Test.
In present (and previous) versions of Julia, this has never been a problem.
But every module `Foo` has a self-referential global constant `Foo`:
```julia
julia> module Foo end
Main.Foo
julia> Foo.Foo
Main.Foo
```
MOI has the problematic feature that MOI.Test.Test is not self-referential,
and JET.jl appropriately complains with "invalid redefinition of constant
Test."
The work-around is to wrap `Test` in a module so that `MOI.Test.Test` is
`MOI.Test`.
=#
module _BaseTest
using Test
end

using ._BaseTest: @testset, @test, @test_throws, @inferred

# Be wary of adding new fields to this Config struct. Always think: can it be
# achieved a different way?
Expand Down Expand Up @@ -201,7 +225,9 @@ function runtests(
warn_unsupported::Bool = false,
exclude_tests_after::VersionNumber = v"999.0.0",
)
tests = filter(n -> startswith("$n", "test_"), names(MOI.Test; all = true))
tests = filter(names(@__MODULE__; all = true)) do name
return startswith("$name", "test_")
end
tests = string.(tests)
for ex in exclude
if ex in tests && any(t -> ex != t && occursin(ex, t), tests)
Expand Down
29 changes: 13 additions & 16 deletions src/Test/test_nonlinear.jl
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,7 @@ function test_nonlinear_hs071_NLPBlockDual(
cu = MOI.add_constraint.(model, v, MOI.LessThan.(u))
MOI.set.(model, MOI.VariablePrimalStart(), v, start)
lb, ub = [25.0, 40.0], [Inf, 40.0]
evaluator = MOI.Test.HS071(true)
evaluator = HS071(true)
block_data = MOI.NLPBlockData(MOI.NLPBoundsPair.(lb, ub), evaluator, true)
MOI.set(model, MOI.NLPBlock(), block_data)

Expand Down Expand Up @@ -931,7 +931,7 @@ This is mainly for the internal purpose of checking their correctness as
written. External solvers can exclude this test without consequence.
"""
function test_nonlinear_HS071_internal(::MOI.ModelLike, ::Config)
d = MOI.Test.HS071(true, true)
d = HS071(true, true)
@test MOI.objective_expr(d) == :(
x[$(MOI.VariableIndex(1))] *
x[$(MOI.VariableIndex(4))] *
Expand Down Expand Up @@ -994,7 +994,7 @@ This is mainly for the internal purpose of checking their correctness as
written. External solvers can exclude this test without consequence.
"""
function test_nonlinear_Feasibility_internal(::MOI.ModelLike, ::Config)
d = MOI.Test.FeasibilitySenseEvaluator(true)
d = FeasibilitySenseEvaluator(true)
@test MOI.objective_expr(d) == :()
@test MOI.constraint_expr(d, 1) == :(x[$(MOI.VariableIndex(1))]^2 == 1.0)
@test_throws AssertionError MOI.constraint_expr(d, 2)
Expand Down Expand Up @@ -1118,7 +1118,7 @@ end

function test_nonlinear_expression_hs071(
model::MOI.ModelLike,
config::MOI.Test.Config{T},
config::Config{T},
) where {T}
@requires T == Float64
@requires _supports(config, MOI.optimize!)
Expand Down Expand Up @@ -1173,7 +1173,7 @@ end

function test_nonlinear_expression_hs071_epigraph(
model::MOI.ModelLike,
config::MOI.Test.Config{T},
config::Config{T},
) where {T}
@requires T == Float64
@requires _supports(config, MOI.optimize!)
Expand Down Expand Up @@ -1229,7 +1229,7 @@ end

function test_nonlinear_expression_hs109(
model::MOI.ModelLike,
config::MOI.Test.Config{T},
config::Config{T},
) where {T}
@requires T == Float64
@requires _supports(config, MOI.optimize!)
Expand Down Expand Up @@ -1296,7 +1296,7 @@ end

function test_nonlinear_expression_hs110(
model::MOI.ModelLike,
config::MOI.Test.Config{T},
config::Config{T},
) where {T}
@requires T == Float64
@requires _supports(config, MOI.optimize!)
Expand Down Expand Up @@ -1358,7 +1358,7 @@ end

function test_nonlinear_expression_quartic(
model::MOI.ModelLike,
config::MOI.Test.Config{T},
config::Config{T},
) where {T}
@requires T == Float64
@requires _supports(config, MOI.optimize!)
Expand Down Expand Up @@ -1406,7 +1406,7 @@ end

function test_nonlinear_expression_overrides_objective(
model::MOI.ModelLike,
config::MOI.Test.Config{T},
config::Config{T},
) where {T}
@requires T == Float64
@requires _supports(config, MOI.optimize!)
Expand Down Expand Up @@ -1457,7 +1457,7 @@ end

function test_nonlinear_expression_univariate_function(
model::MOI.ModelLike,
config::MOI.Test.Config{T},
config::Config{T},
) where {T}
@requires T == Float64
@requires _supports(config, MOI.optimize!)
Expand Down Expand Up @@ -1493,7 +1493,7 @@ end

function test_nonlinear_expression_multivariate_function(
model::MOI.ModelLike,
config::MOI.Test.Config{T},
config::Config{T},
) where {T}
@requires T == Float64
@requires _supports(config, MOI.optimize!)
Expand Down Expand Up @@ -1544,10 +1544,7 @@ end
Tests dual solutions with `ScalarNonlinearFunction`. We use a linear program
so that the duals are easy to compute.
"""
function test_nonlinear_duals(
model::MOI.ModelLike,
config::MOI.Test.Config{T},
) where {T}
function test_nonlinear_duals(model::MOI.ModelLike, config::Config{T}) where {T}
@requires T == Float64
@requires _supports(config, MOI.optimize!)
@requires _supports(config, MOI.ConstraintDual)
Expand Down Expand Up @@ -1676,7 +1673,7 @@ end

function test_nonlinear_vector_complements(
model::MOI.ModelLike,
config::MOI.Test.Config{T},
config::Config{T},
) where {T}
@requires T == Float64
@requires _supports(config, MOI.optimize!)
Expand Down
2 changes: 1 addition & 1 deletion test/Bridges/Variable/vectorize.jl
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ function test_exp3_with_add_constrained_variable_y()

err = ArgumentError(
"Variable bridge of type `$(MOI.Bridges.Variable.VectorizeBridge{Float64,MOI.Nonpositives})`" *
" does not support accessing the attribute `MathOptInterface.Test.UnknownVariableAttribute()`.",
" does not support accessing the attribute `$(MOI.Test.UnknownVariableAttribute())`.",
)
@test_throws err MOI.get(
bridged_mock,
Expand Down
2 changes: 1 addition & 1 deletion test/Bridges/Variable/zeros.jl
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ function test_zeros()

err = ArgumentError(
"Variable bridge of type `MathOptInterface.Bridges.Variable.ZerosBridge{Float64}`" *
" does not support accessing the attribute `MathOptInterface.Test.UnknownVariableAttribute()`.",
" does not support accessing the attribute `$(MOI.Test.UnknownVariableAttribute())`.",
)
@test_throws err MOI.get(
bridged_mock,
Expand Down

0 comments on commit dc166f6

Please sign in to comment.