From 853b9d7bd33ff1b7e157a7db12a5dad46be630f2 Mon Sep 17 00:00:00 2001 From: odow Date: Mon, 18 Sep 2023 19:37:20 +1200 Subject: [PATCH] [Test] rename Test module to avoid conflict with Base.Test --- src/MathOptInterface.jl | 21 +++++++++++++++++++++ src/Test/Test.jl | 9 +++++++-- src/Test/test_nonlinear.jl | 26 +++++++++++++------------- src/Utilities/Utilities.jl | 1 + test/Bridges/Variable/vectorize.jl | 2 +- test/Bridges/Variable/zeros.jl | 2 +- 6 files changed, 44 insertions(+), 17 deletions(-) diff --git a/src/MathOptInterface.jl b/src/MathOptInterface.jl index b29360f256..c424f9eb3e 100644 --- a/src/MathOptInterface.jl +++ b/src/MathOptInterface.jl @@ -332,6 +332,27 @@ include("precompile.jl") # submodules include("Utilities/Utilities.jl") # MOI.Utilities include("Test/Test.jl") + +# 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 because 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 rename the module `_Test` and introduce this constant +# into the `MOI.` namespace for backwards compatibility. +const Test = _Test + include("Bridges/Bridges.jl") # MOI.Bridges include("Benchmarks/Benchmarks.jl") include("Nonlinear/Nonlinear.jl") diff --git a/src/Test/Test.jl b/src/Test/Test.jl index 2453297878..867af74547 100644 --- a/src/Test/Test.jl +++ b/src/Test/Test.jl @@ -4,7 +4,10 @@ # Use of this source code is governed by an MIT-style license that can be found # in the LICENSE.md file or at https://opensource.org/licenses/MIT. -module Test +# !!! warning +# To understand the naming of this module, see comment associated with the +# `const Test = _Test` line in MathOptInterface.jl +module _Test import LinearAlgebra @@ -201,7 +204,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) diff --git a/src/Test/test_nonlinear.jl b/src/Test/test_nonlinear.jl index 57d2509d47..87537deb12 100644 --- a/src/Test/test_nonlinear.jl +++ b/src/Test/test_nonlinear.jl @@ -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) @@ -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))] * @@ -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) @@ -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!) @@ -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!) @@ -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!) @@ -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!) @@ -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!) @@ -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!) @@ -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!) @@ -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!) @@ -1546,7 +1546,7 @@ so that the duals are easy to compute. """ function test_nonlinear_duals( model::MOI.ModelLike, - config::MOI.Test.Config{T}, + config::Config{T}, ) where {T} @requires T == Float64 @requires _supports(config, MOI.optimize!) @@ -1676,7 +1676,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!) diff --git a/src/Utilities/Utilities.jl b/src/Utilities/Utilities.jl index c553545f4f..7b9fce25a0 100644 --- a/src/Utilities/Utilities.jl +++ b/src/Utilities/Utilities.jl @@ -32,6 +32,7 @@ function replace_acronym(s::AbstractString) s = replace(s, "MathOptInterface.Utilities" => "MOIU") s = replace(s, "MathOptInterface.Bridges" => "MOIB") s = replace(s, "MathOptInterface.Test" => "MOIT") + s = replace(s, "MathOptInterface._Test" => "MOIT") s = replace(s, "MathOptInterface" => "MOI") return s end diff --git a/test/Bridges/Variable/vectorize.jl b/test/Bridges/Variable/vectorize.jl index 8fe459a906..fc6a9bdde4 100644 --- a/test/Bridges/Variable/vectorize.jl +++ b/test/Bridges/Variable/vectorize.jl @@ -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, diff --git a/test/Bridges/Variable/zeros.jl b/test/Bridges/Variable/zeros.jl index 06ad086f75..876189a88a 100644 --- a/test/Bridges/Variable/zeros.jl +++ b/test/Bridges/Variable/zeros.jl @@ -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,