Skip to content

Commit 0c9ba0f

Browse files
authored
[Test] refactor test/Test/Test.jl into functions (#2658)
1 parent 6869ad0 commit 0c9ba0f

File tree

1 file changed

+122
-78
lines changed

1 file changed

+122
-78
lines changed

test/Test/Test.jl

Lines changed: 122 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -4,87 +4,107 @@
44
# Use of this source code is governed by an MIT-style license that can be found
55
# in the LICENSE.md file or at https://opensource.org/licenses/MIT.
66

7-
using Test
7+
module TestTest
88

9+
using Test
910
import MathOptInterface as MOI
1011

11-
# Some tests are excluded because UniversalFallback accepts absolutely
12-
# everything.
13-
14-
MOI.Test.runtests(
15-
MOI.Utilities.MockOptimizer(
16-
MOI.Utilities.UniversalFallback(MOI.Utilities.Model{Float64}()),
17-
),
18-
MOI.Test.Config(),
19-
exclude = [
20-
r"^test_model_ScalarFunctionConstantNotZero$",
21-
"test_model_copy_to_UnsupportedAttribute",
22-
"test_model_copy_to_UnsupportedConstraint",
23-
"test_model_supports_constraint_ScalarAffineFunction_EqualTo",
24-
"test_model_supports_constraint_VariableIndex_EqualTo",
25-
"test_model_supports_constraint_VectorOfVariables_Nonnegatives",
26-
],
27-
warn_unsupported = true,
28-
)
29-
30-
# Run the previously excluded tests, this time without UniversalFallback.
31-
32-
MOI.Test.runtests(
33-
MOI.Utilities.MockOptimizer(
34-
MOI.Utilities.Model{Float64}(),
35-
scalar_function_constant_non_zero = true,
36-
),
37-
MOI.Test.Config();
38-
include = [
39-
r"^test_model_ScalarFunctionConstantNotZero$",
40-
"test_model_copy_to_UnsupportedAttribute",
41-
"test_model_copy_to_UnsupportedConstraint",
42-
"test_model_supports_constraint_ScalarAffineFunction_EqualTo",
43-
"test_model_supports_constraint_VariableIndex_EqualTo",
44-
"test_model_supports_constraint_VectorOfVariables_Nonnegatives",
45-
],
46-
verbose = true,
47-
)
48-
49-
# Test for Issue #1757
12+
function runtests()
13+
for name in names(@__MODULE__; all = true)
14+
if startswith("$name", "test_")
15+
@testset "$(name)" begin
16+
getfield(@__MODULE__, name)()
17+
end
18+
end
19+
end
20+
return
21+
end
5022

51-
MOI.Test.test_model_ScalarFunctionConstantNotZero(
52-
MOI.Utilities.MockOptimizer(
53-
MOI.Utilities.Model{Float64}(),
54-
scalar_function_constant_non_zero = false,
55-
),
56-
MOI.Test.Config(exclude = Any[MOI.ConstraintFunction]),
57-
)
23+
function test_runtests()
24+
# Some tests are excluded because UniversalFallback accepts absolutely
25+
# everything.
26+
MOI.Test.runtests(
27+
MOI.Utilities.MockOptimizer(
28+
MOI.Utilities.UniversalFallback(MOI.Utilities.Model{Float64}()),
29+
),
30+
MOI.Test.Config(),
31+
exclude = [
32+
r"^test_model_ScalarFunctionConstantNotZero$",
33+
"test_model_copy_to_UnsupportedAttribute",
34+
"test_model_copy_to_UnsupportedConstraint",
35+
"test_model_supports_constraint_ScalarAffineFunction_EqualTo",
36+
"test_model_supports_constraint_VariableIndex_EqualTo",
37+
"test_model_supports_constraint_VectorOfVariables_Nonnegatives",
38+
],
39+
warn_unsupported = true,
40+
verbose = true,
41+
)
42+
# Run the previously excluded tests, this time without UniversalFallback.
43+
MOI.Test.runtests(
44+
MOI.Utilities.MockOptimizer(
45+
MOI.Utilities.Model{Float64}(),
46+
scalar_function_constant_non_zero = true,
47+
),
48+
MOI.Test.Config();
49+
include = [
50+
r"^test_model_ScalarFunctionConstantNotZero$",
51+
"test_model_copy_to_UnsupportedAttribute",
52+
"test_model_copy_to_UnsupportedConstraint",
53+
"test_model_supports_constraint_ScalarAffineFunction_EqualTo",
54+
"test_model_supports_constraint_VariableIndex_EqualTo",
55+
"test_model_supports_constraint_VectorOfVariables_Nonnegatives",
56+
],
57+
verbose = true,
58+
)
59+
return
60+
end
5861

59-
# Test exclude_tests_after. This should work despite no methods being added for IncompleteOptimizer
60-
# because every test should get skipped.
62+
function test_issue_1757()
63+
MOI.Test.test_model_ScalarFunctionConstantNotZero(
64+
MOI.Utilities.MockOptimizer(
65+
MOI.Utilities.Model{Float64}(),
66+
scalar_function_constant_non_zero = false,
67+
),
68+
MOI.Test.Config(exclude = Any[MOI.ConstraintFunction]),
69+
)
70+
return
71+
end
6172

6273
struct IncompleteOptimizer <: MOI.AbstractOptimizer end
6374

64-
MOI.Test.runtests(
65-
IncompleteOptimizer(),
66-
MOI.Test.Config();
67-
exclude_tests_after = v"0.0.1",
68-
)
75+
# Test exclude_tests_after. This should work despite no methods being added for
76+
# IncompleteOptimizer because every test should get skipped.
77+
function test_exclude_tests_after()
78+
MOI.Test.runtests(
79+
IncompleteOptimizer(),
80+
MOI.Test.Config();
81+
exclude_tests_after = v"0.0.1",
82+
verbose = true,
83+
)
84+
return
85+
end
6986

7087
# Non-Float64 tests
7188

72-
MOI.Test.runtests(
73-
MOI.Utilities.MockOptimizer(
74-
MOI.Utilities.UniversalFallback(MOI.Utilities.Model{BigFloat}()),
75-
BigFloat,
76-
),
77-
MOI.Test.Config(BigFloat),
78-
exclude = [
79-
# ========================= Expected failures ==========================
80-
# UniversalFallback supports these tests.
81-
"test_model_copy_to_UnsupportedAttribute",
82-
"test_model_copy_to_UnsupportedConstraint",
83-
"test_model_supports_constraint_ScalarAffineFunction_EqualTo",
84-
"test_model_supports_constraint_VariableIndex_EqualTo",
85-
"test_model_supports_constraint_VectorOfVariables_Nonnegatives",
86-
],
87-
)
89+
function test_bigfloat_tests()
90+
MOI.Test.runtests(
91+
MOI.Utilities.MockOptimizer(
92+
MOI.Utilities.UniversalFallback(MOI.Utilities.Model{BigFloat}()),
93+
BigFloat,
94+
),
95+
MOI.Test.Config(BigFloat),
96+
exclude = [
97+
# ========================= Expected failures ======================
98+
# UniversalFallback supports these tests.
99+
"test_model_copy_to_UnsupportedAttribute",
100+
"test_model_copy_to_UnsupportedConstraint",
101+
"test_model_supports_constraint_ScalarAffineFunction_EqualTo",
102+
"test_model_supports_constraint_VariableIndex_EqualTo",
103+
"test_model_supports_constraint_VectorOfVariables_Nonnegatives",
104+
],
105+
)
106+
return
107+
end
88108

89109
# Special test for issue #2010
90110

@@ -98,21 +118,25 @@ function MOI.supports_constraint(
98118
return true
99119
end
100120

101-
MOI.Test.test_attribute_unsupported_constraint(
102-
_UnsupportedModel(),
103-
MOI.Test.Config(),
104-
)
121+
function test_issue_2010()
122+
MOI.Test.test_attribute_unsupported_constraint(
123+
_UnsupportedModel(),
124+
MOI.Test.Config(),
125+
)
126+
return
127+
end
105128

106-
@testset "test_attribute_unsupported_constraint_fallback" begin
129+
function test_attribute_unsupported_constraint_fallback()
107130
model = _UnsupportedModel()
108131
F, S = MOI.VariableIndex, MOI.ZeroOne
109132
attr = MOI.ListOfConstraintIndices{F,S}()
110133
@test_throws MOI.GetAttributeNotAllowed(attr) MOI.get(model, attr)
111134
attr = MOI.NumberOfConstraints{F,S}()
112135
@test_throws MOI.GetAttributeNotAllowed(attr) MOI.get(model, attr)
136+
return
113137
end
114138

115-
@testset "test_warning_on_ambiguous" begin
139+
function test_warning_on_ambiguous()
116140
model = MOI.Utilities.MockOptimizer(
117141
MOI.Utilities.UniversalFallback(MOI.Utilities.Model{Float64}()),
118142
)
@@ -123,18 +147,20 @@ end
123147
@test_logs (:warn,) MOI.Test.runtests(model, config; exclude = exclude)
124148
exclude = [r"^test_model_Name$", r"test_.+"]
125149
@test_logs MOI.Test.runtests(model, config; exclude = exclude)
150+
return
126151
end
127152

128-
@testset "test_FeasibilitySenseEvaluator" begin
153+
function test_FeasibilitySenseEvaluator()
129154
evaluator = MOI.Test.FeasibilitySenseEvaluator(true)
130155
@test MOI.features_available(evaluator) == [:Grad, :Jac, :Hess, :ExprGraph]
131156
@test MOI.hessian_lagrangian_structure(evaluator) == [(1, 1)]
132157
evaluator = MOI.Test.FeasibilitySenseEvaluator(false)
133158
@test MOI.features_available(evaluator) == [:Grad, :Jac, :ExprGraph]
134159
@test_throws AssertionError MOI.hessian_lagrangian_structure(evaluator)
160+
return
135161
end
136162

137-
@testset "test_HS071_evaluator" begin
163+
function test_HS071_evaluator()
138164
evaluator = MOI.Test.HS071(true, true)
139165
features = [:Grad, :Jac, :JacVec, :ExprGraph, :Hess, :HessVec]
140166
@test MOI.features_available(evaluator) == features
@@ -204,4 +230,22 @@ end
204230
:($(x[1]) * $(x[2]) * $(x[3]) * $(x[4]) >= 25.0)
205231
@test MOI.constraint_expr(evaluator, 2) ==
206232
:($(x[1])^2 + $(x[2])^2 + $(x[3])^2 + $(x[4])^2 == 40.0)
233+
return
207234
end
235+
236+
function test_error_handler()
237+
err = ErrorException("error")
238+
@test_throws err try
239+
@assert false
240+
catch
241+
MOI.Test._error_handler(err, "test_foo", true)
242+
end
243+
err = MOI.AddVariableNotAllowed()
244+
@test_logs (:warn,) MOI.Test._error_handler(err, "test_foo", true)
245+
@test_nowarn MOI.Test._error_handler(err, "test_foo", false)
246+
return
247+
end
248+
249+
end # module
250+
251+
TestTest.runtests()

0 commit comments

Comments
 (0)