|
| 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 |
0 commit comments