Skip to content

Commit a39d27a

Browse files
add test
1 parent fe079e8 commit a39d27a

File tree

12 files changed

+510
-0
lines changed

12 files changed

+510
-0
lines changed

stdlib/Test/test/runtests.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2033,3 +2033,6 @@ end
20332033
@test occursin("Error in testset", output)
20342034
@test occursin("1 == 2", output)
20352035
end
2036+
2037+
# Test testset filtering functionality
2038+
include("testset_filtering.jl")
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
# Test for testset filtering functionality
2+
using Test
3+
4+
# Helper function to collect all testset names recursively
5+
function collect_all_testset_names(testsets::Vector{Test.TestSetNode})
6+
names = String[]
7+
for testset in testsets
8+
push!(names, testset.name)
9+
append!(names, collect_all_testset_names(testset.children))
10+
end
11+
return names
12+
end
13+
14+
@testset "TestSet Filtering Tests" begin
15+
16+
@testset "testset discovery" begin
17+
# Test discovery of the rationalized test package
18+
test_file = joinpath(@__DIR__, "testset_filtering", "runtests.jl")
19+
@test isfile(test_file)
20+
21+
# Discover testsets
22+
discovered = Test.discover_testsets(test_file)
23+
@test isa(discovered, Vector{Test.TestSetNode})
24+
@test length(discovered) > 0
25+
26+
# Collect all testset names including nested ones
27+
all_names = collect_all_testset_names(discovered)
28+
29+
# Check for shared testsets (included multiple times via different patterns)
30+
@test count(contains("Shared Tests"), all_names) >= 2 # Should appear multiple times
31+
@test any(contains("basic math"), all_names)
32+
@test any(contains("string operations"), all_names)
33+
34+
# Check for advanced testsets (included via variable pattern)
35+
@test count(contains("Advanced Tests"), all_names) >= 1
36+
@test any(contains("boolean logic"), all_names)
37+
@test any(contains("comparison operators"), all_names)
38+
@test any(contains("array operations"), all_names)
39+
40+
# Check for variant testsets (string concatenation includes)
41+
@test any(contains("Math Variant Tests"), all_names)
42+
@test any(contains("String Variant Tests"), all_names)
43+
@test any(contains("Logic Variant Tests"), all_names)
44+
@test any(contains("arithmetic operations"), all_names)
45+
@test any(contains("string manipulation"), all_names)
46+
@test any(contains("logical operations"), all_names)
47+
48+
# Check for deep path includes
49+
@test any(contains("Deep Path Tests"), all_names)
50+
@test any(contains("path operations"), all_names)
51+
@test any(contains("nested path tests"), all_names)
52+
@test any(contains("very deep nesting"), all_names)
53+
54+
# Check for main testsets (defined inline)
55+
@test any(contains("Main Test Suite"), all_names)
56+
@test any(contains("Basic functionality"), all_names)
57+
@test any(contains("Nested tests"), all_names)
58+
@test any(contains("Level 1"), all_names)
59+
@test any(contains("Level 2"), all_names)
60+
@test any(contains("Level 3"), all_names)
61+
62+
# Check for loop testsets
63+
@test any(contains("Loop testsets"), all_names)
64+
@test any(startswith("Parameterized case_"), all_names)
65+
66+
println("Total testsets discovered (including nested): ", length(all_names))
67+
println("Rationalized structure demonstrates:")
68+
println(" - Shared files used by multiple include patterns: $(count(contains("Shared Tests"), all_names)) times")
69+
println(" - Advanced tests used by variable includes: $(count(contains("Advanced Tests"), all_names)) times")
70+
println(" - Variant tests via string concatenation: $(count(x -> contains("Variant Tests")(x), all_names)) variants")
71+
end
72+
73+
@testset "testset filtering" begin
74+
test_file = joinpath(@__DIR__, "testset_filtering", "runtests.jl")
75+
discovered = Test.discover_testsets(test_file)
76+
77+
# Test single pattern filtering
78+
filter1 = Test.create_testset_filter(discovered, ["Math Variant Tests"])
79+
@test length(filter1.enabled_testset_ids) >= 1
80+
81+
# Test multiple pattern filtering
82+
filter2 = Test.create_testset_filter(discovered, ["Shared Tests", "Advanced Tests"])
83+
@test length(filter2.enabled_testset_ids) >= 2
84+
85+
# Test regex pattern filtering
86+
filter3 = Test.create_testset_filter(discovered, [r"Variant Tests"])
87+
@test length(filter3.enabled_testset_ids) >= 3 # Should match Math, String, Logic Variant Tests
88+
89+
# Test no matches
90+
filter4 = Test.create_testset_filter(discovered, ["nonexistent test"])
91+
@test length(filter4.enabled_testset_ids) == 0
92+
end
93+
94+
@testset "tree structure" begin
95+
test_file = joinpath(@__DIR__, "testset_filtering", "runtests.jl")
96+
discovered = Test.discover_testsets(test_file)
97+
98+
# Find a nested testset and verify structure
99+
main_suite = nothing
100+
for node in discovered
101+
if node.name == "Main Test Suite"
102+
main_suite = node
103+
break
104+
end
105+
end
106+
107+
@test main_suite !== nothing
108+
@test length(main_suite.children) >= 2 # Should have "Basic functionality" and "Nested tests"
109+
110+
# Check nested structure
111+
nested_tests = nothing
112+
for child in main_suite.children
113+
if child.name == "Nested tests"
114+
nested_tests = child
115+
break
116+
end
117+
end
118+
119+
@test nested_tests !== nothing
120+
@test length(nested_tests.children) >= 1 # Should have "Level 1"
121+
122+
# Check deep nesting
123+
level1 = nested_tests.children[1]
124+
@test level1.name == "Level 1"
125+
@test length(level1.children) >= 1 # Should have "Level 2"
126+
127+
level2 = level1.children[1]
128+
@test level2.name == "Level 2"
129+
end
130+
131+
@testset "error handling" begin
132+
# Test with non-existent file - should return empty list with warning, not throw
133+
discovered = Test.discover_testsets("/nonexistent/file.jl")
134+
@test isa(discovered, Vector{Test.TestSetNode})
135+
@test length(discovered) == 0
136+
137+
# Test with empty file (should not error)
138+
empty_file = tempname()
139+
touch(empty_file)
140+
try
141+
discovered = Test.discover_testsets(empty_file)
142+
@test isa(discovered, Vector{Test.TestSetNode})
143+
@test length(discovered) == 0
144+
finally
145+
rm(empty_file, force=true)
146+
end
147+
end
148+
end
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Advanced test file used by variable-based includes
2+
# This file is included via:
3+
# - Variable include: for file in test_files; include(file); end
4+
5+
@testset "Advanced Tests" begin
6+
@testset "boolean logic" begin
7+
@test true && true == true
8+
@test false || true == true
9+
@test !false == true
10+
@test !(true && false) == true
11+
end
12+
13+
@testset "comparison operators" begin
14+
@test 5 > 3
15+
@test 2 <= 2
16+
@test 4 != 5
17+
@test 7 >= 7
18+
end
19+
20+
@testset "array operations" begin
21+
@test length([1,2,3]) == 3
22+
@test sum([1,2,3]) == 6
23+
@test [1,2] [2,3] == [1,2,3]
24+
end
25+
end
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
shared_tests.jl
2+
advanced_tests.jl
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Logic variant test - string concatenation include
2+
# This file is included via: include(variant * "_tests.jl") where variant = "logic"
3+
4+
@testset "Logic Variant Tests" begin
5+
@testset "logical operations" begin
6+
@test string(:gamma) == "gamma"
7+
@test startswith("gamma", "gam")
8+
@test !isempty("gamma")
9+
end
10+
11+
@testset "conditional logic" begin
12+
@test (true ? "yes" : "no") == "yes"
13+
@test (false ? "yes" : "no") == "no"
14+
@test all([true, true, true]) == true
15+
@test any([false, true, false]) == true
16+
end
17+
end
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Math variant test - string concatenation include
2+
# This file is included via: include(variant * "_tests.jl") where variant = "math"
3+
4+
@testset "Math Variant Tests" begin
5+
@testset "arithmetic operations" begin
6+
@test 2^3 == 8
7+
@test sqrt(16) == 4
8+
@test abs(-5) == 5
9+
end
10+
11+
@testset "mathematical functions" begin
12+
@test sin(0) == 0
13+
@test cos(0) == 1
14+
@test round(3.7) == 4
15+
end
16+
end
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Shared test file used by multiple include patterns
2+
# This file is included via:
3+
# - Static include: include("shared_tests.jl")
4+
# - Dynamic include: include(joinpath(TESTDIR, "shared_tests.jl"))
5+
# - Variable include: for file in test_files; include(file); end
6+
# - Readlines include: for file in readlines(...); include(file); end
7+
8+
@testset "Shared Tests" begin
9+
@testset "basic math" begin
10+
@test 1 + 1 == 2
11+
@test 2 * 3 == 6
12+
@test 10 ÷ 2 == 5
13+
end
14+
15+
@testset "string operations" begin
16+
@test length("hello") == 5
17+
@test uppercase("test") == "TEST"
18+
@test startswith("hello world", "hello")
19+
end
20+
end
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Deep nested path test - demonstrates joinpath() resolution
2+
# This file is included via: include(joinpath(@__DIR__, "nested", "deep_tests.jl"))
3+
4+
@testset "Deep Path Tests" begin
5+
@testset "path operations" begin
6+
@test joinpath("a", "b") == "a/b" || joinpath("a", "b") == "a\\b" # Handle both Unix and Windows
7+
@test dirname("/a/b/c.txt") == "/a/b"
8+
@test normpath("a/b/../c") == "a/c" || normpath("a/b/../c") == "a\\c"
9+
end
10+
11+
@testset "nested path tests" begin
12+
@testset "deep nesting" begin
13+
@test basename("/a/b/file.txt") == "file.txt"
14+
@test splitext("file.txt")[2] == ".txt"
15+
@testset "very deep nesting" begin
16+
@test isfile(@__FILE__)
17+
@test basename(@__FILE__) == "deep_tests.jl"
18+
end
19+
end
20+
end
21+
end
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# Test file that exercises all testset filtering functionality
2+
using Test
3+
4+
# ============================================================================
5+
# SHARED TEST FILES (used by multiple include patterns)
6+
# ============================================================================
7+
8+
# Test 1: Basic static include
9+
include("shared_tests.jl")
10+
11+
# Test 2: Const declaration with path function
12+
const TESTDIR = joinpath(@__DIR__, "modules")
13+
14+
# Test 3: Dynamic include with const variable (reuses shared_tests.jl)
15+
include(joinpath(TESTDIR, "shared_tests.jl"))
16+
17+
# Test 4: Variable-based includes (reuse the same files)
18+
test_files = ["shared_tests.jl", "advanced_tests.jl"]
19+
for file in test_files
20+
include(file)
21+
end
22+
23+
# Test 5: String concatenation includes (create variants that reuse content)
24+
test_variants = ["math", "string", "logic"]
25+
for variant in test_variants
26+
include(variant * "_tests.jl")
27+
end
28+
29+
# Test 6: Readlines-based includes (reuse shared files)
30+
for file in readlines(joinpath(@__DIR__, "file_list.txt"))
31+
include(file)
32+
end
33+
34+
# Test 7: Path function includes with deep nesting
35+
include(joinpath(@__DIR__, "nested", "deep_tests.jl"))
36+
37+
# ============================================================================
38+
# INLINE TESTSETS (demonstrate direct testset definition)
39+
# ============================================================================
40+
41+
@testset "Main Test Suite" begin
42+
@testset "Basic functionality" begin
43+
@test 1 + 1 == 2
44+
@test 5 > 3
45+
end
46+
47+
@testset "Nested tests" begin
48+
@testset "Level 1" begin
49+
@test true
50+
@testset "Level 2" begin
51+
@test 2 * 2 == 4
52+
@testset "Level 3" begin
53+
@test length("test") == 4
54+
end
55+
end
56+
end
57+
end
58+
end
59+
60+
# ============================================================================
61+
# LOOP TESTSETS (demonstrate parameterized testsets)
62+
# ============================================================================
63+
64+
@testset "Loop testsets" for n in 1:3
65+
@test n > 0
66+
@test n <= 3
67+
end
68+
69+
# Complex loop testsets with string interpolation
70+
test_cases = ["case_a", "case_b", "case_c"]
71+
@testset "Parameterized $case" for case in test_cases
72+
@test !isempty(case)
73+
@test startswith(case, "case_")
74+
end
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Shared test file used by multiple include patterns
2+
# This file is included via:
3+
# - Static include: include("shared_tests.jl")
4+
# - Dynamic include: include(joinpath(TESTDIR, "shared_tests.jl"))
5+
# - Variable include: for file in test_files; include(file); end
6+
# - Readlines include: for file in readlines(...); include(file); end
7+
8+
@testset "Shared Tests" begin
9+
@testset "basic math" begin
10+
@test 1 + 1 == 2
11+
@test 2 * 3 == 6
12+
@test 10 ÷ 2 == 5
13+
end
14+
15+
@testset "string operations" begin
16+
@test length("hello") == 5
17+
@test uppercase("test") == "TEST"
18+
@test startswith("hello world", "hello")
19+
end
20+
end

0 commit comments

Comments
 (0)