Skip to content

Commit dc166f6

Browse files
authored
[Test] wrap Test module to avoid conflict with MOI.Test (#2267)
1 parent e98bee2 commit dc166f6

File tree

4 files changed

+42
-19
lines changed

4 files changed

+42
-19
lines changed

src/Test/Test.jl

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,31 @@ import LinearAlgebra
1111
import MathOptInterface as MOI
1212
import MathOptInterface.Utilities as MOIU
1313

14+
#=
15+
We made a bit of a mistake calling the `Test/Test.jl` submodule "Test" because
16+
it conflicts with the standard library "Test" which is imported by MOI.Test.
17+
18+
In present (and previous) versions of Julia, this has never been a problem.
19+
But every module `Foo` has a self-referential global constant `Foo`:
20+
```julia
21+
julia> module Foo end
22+
Main.Foo
23+
24+
julia> Foo.Foo
25+
Main.Foo
26+
```
27+
MOI has the problematic feature that MOI.Test.Test is not self-referential,
28+
and JET.jl appropriately complains with "invalid redefinition of constant
29+
Test."
30+
31+
The work-around is to wrap `Test` in a module so that `MOI.Test.Test` is
32+
`MOI.Test`.
33+
=#
34+
module _BaseTest
1435
using Test
36+
end
37+
38+
using ._BaseTest: @testset, @test, @test_throws, @inferred
1539

1640
# Be wary of adding new fields to this Config struct. Always think: can it be
1741
# achieved a different way?
@@ -201,7 +225,9 @@ function runtests(
201225
warn_unsupported::Bool = false,
202226
exclude_tests_after::VersionNumber = v"999.0.0",
203227
)
204-
tests = filter(n -> startswith("$n", "test_"), names(MOI.Test; all = true))
228+
tests = filter(names(@__MODULE__; all = true)) do name
229+
return startswith("$name", "test_")
230+
end
205231
tests = string.(tests)
206232
for ex in exclude
207233
if ex in tests && any(t -> ex != t && occursin(ex, t), tests)

src/Test/test_nonlinear.jl

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -484,7 +484,7 @@ function test_nonlinear_hs071_NLPBlockDual(
484484
cu = MOI.add_constraint.(model, v, MOI.LessThan.(u))
485485
MOI.set.(model, MOI.VariablePrimalStart(), v, start)
486486
lb, ub = [25.0, 40.0], [Inf, 40.0]
487-
evaluator = MOI.Test.HS071(true)
487+
evaluator = HS071(true)
488488
block_data = MOI.NLPBlockData(MOI.NLPBoundsPair.(lb, ub), evaluator, true)
489489
MOI.set(model, MOI.NLPBlock(), block_data)
490490

@@ -931,7 +931,7 @@ This is mainly for the internal purpose of checking their correctness as
931931
written. External solvers can exclude this test without consequence.
932932
"""
933933
function test_nonlinear_HS071_internal(::MOI.ModelLike, ::Config)
934-
d = MOI.Test.HS071(true, true)
934+
d = HS071(true, true)
935935
@test MOI.objective_expr(d) == :(
936936
x[$(MOI.VariableIndex(1))] *
937937
x[$(MOI.VariableIndex(4))] *
@@ -994,7 +994,7 @@ This is mainly for the internal purpose of checking their correctness as
994994
written. External solvers can exclude this test without consequence.
995995
"""
996996
function test_nonlinear_Feasibility_internal(::MOI.ModelLike, ::Config)
997-
d = MOI.Test.FeasibilitySenseEvaluator(true)
997+
d = FeasibilitySenseEvaluator(true)
998998
@test MOI.objective_expr(d) == :()
999999
@test MOI.constraint_expr(d, 1) == :(x[$(MOI.VariableIndex(1))]^2 == 1.0)
10001000
@test_throws AssertionError MOI.constraint_expr(d, 2)
@@ -1118,7 +1118,7 @@ end
11181118

11191119
function test_nonlinear_expression_hs071(
11201120
model::MOI.ModelLike,
1121-
config::MOI.Test.Config{T},
1121+
config::Config{T},
11221122
) where {T}
11231123
@requires T == Float64
11241124
@requires _supports(config, MOI.optimize!)
@@ -1173,7 +1173,7 @@ end
11731173

11741174
function test_nonlinear_expression_hs071_epigraph(
11751175
model::MOI.ModelLike,
1176-
config::MOI.Test.Config{T},
1176+
config::Config{T},
11771177
) where {T}
11781178
@requires T == Float64
11791179
@requires _supports(config, MOI.optimize!)
@@ -1229,7 +1229,7 @@ end
12291229

12301230
function test_nonlinear_expression_hs109(
12311231
model::MOI.ModelLike,
1232-
config::MOI.Test.Config{T},
1232+
config::Config{T},
12331233
) where {T}
12341234
@requires T == Float64
12351235
@requires _supports(config, MOI.optimize!)
@@ -1296,7 +1296,7 @@ end
12961296

12971297
function test_nonlinear_expression_hs110(
12981298
model::MOI.ModelLike,
1299-
config::MOI.Test.Config{T},
1299+
config::Config{T},
13001300
) where {T}
13011301
@requires T == Float64
13021302
@requires _supports(config, MOI.optimize!)
@@ -1358,7 +1358,7 @@ end
13581358

13591359
function test_nonlinear_expression_quartic(
13601360
model::MOI.ModelLike,
1361-
config::MOI.Test.Config{T},
1361+
config::Config{T},
13621362
) where {T}
13631363
@requires T == Float64
13641364
@requires _supports(config, MOI.optimize!)
@@ -1406,7 +1406,7 @@ end
14061406

14071407
function test_nonlinear_expression_overrides_objective(
14081408
model::MOI.ModelLike,
1409-
config::MOI.Test.Config{T},
1409+
config::Config{T},
14101410
) where {T}
14111411
@requires T == Float64
14121412
@requires _supports(config, MOI.optimize!)
@@ -1457,7 +1457,7 @@ end
14571457

14581458
function test_nonlinear_expression_univariate_function(
14591459
model::MOI.ModelLike,
1460-
config::MOI.Test.Config{T},
1460+
config::Config{T},
14611461
) where {T}
14621462
@requires T == Float64
14631463
@requires _supports(config, MOI.optimize!)
@@ -1493,7 +1493,7 @@ end
14931493

14941494
function test_nonlinear_expression_multivariate_function(
14951495
model::MOI.ModelLike,
1496-
config::MOI.Test.Config{T},
1496+
config::Config{T},
14971497
) where {T}
14981498
@requires T == Float64
14991499
@requires _supports(config, MOI.optimize!)
@@ -1544,10 +1544,7 @@ end
15441544
Tests dual solutions with `ScalarNonlinearFunction`. We use a linear program
15451545
so that the duals are easy to compute.
15461546
"""
1547-
function test_nonlinear_duals(
1548-
model::MOI.ModelLike,
1549-
config::MOI.Test.Config{T},
1550-
) where {T}
1547+
function test_nonlinear_duals(model::MOI.ModelLike, config::Config{T}) where {T}
15511548
@requires T == Float64
15521549
@requires _supports(config, MOI.optimize!)
15531550
@requires _supports(config, MOI.ConstraintDual)
@@ -1676,7 +1673,7 @@ end
16761673

16771674
function test_nonlinear_vector_complements(
16781675
model::MOI.ModelLike,
1679-
config::MOI.Test.Config{T},
1676+
config::Config{T},
16801677
) where {T}
16811678
@requires T == Float64
16821679
@requires _supports(config, MOI.optimize!)

test/Bridges/Variable/vectorize.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ function test_exp3_with_add_constrained_variable_y()
145145

146146
err = ArgumentError(
147147
"Variable bridge of type `$(MOI.Bridges.Variable.VectorizeBridge{Float64,MOI.Nonpositives})`" *
148-
" does not support accessing the attribute `MathOptInterface.Test.UnknownVariableAttribute()`.",
148+
" does not support accessing the attribute `$(MOI.Test.UnknownVariableAttribute())`.",
149149
)
150150
@test_throws err MOI.get(
151151
bridged_mock,

test/Bridges/Variable/zeros.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ function test_zeros()
9797

9898
err = ArgumentError(
9999
"Variable bridge of type `MathOptInterface.Bridges.Variable.ZerosBridge{Float64}`" *
100-
" does not support accessing the attribute `MathOptInterface.Test.UnknownVariableAttribute()`.",
100+
" does not support accessing the attribute `$(MOI.Test.UnknownVariableAttribute())`.",
101101
)
102102
@test_throws err MOI.get(
103103
bridged_mock,

0 commit comments

Comments
 (0)