Repository: PowerShell/PowerShell
File: test/powershell/engine/WildcardPattern.Tests.ps1 (PowerShell)

Code before:
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.

Describe "WildcardPattern.ToRegex Tests" -Tags "CI" {
    Context "Basic wildcard to regex conversion" {
        It "Converts asterisk wildcard to regex" {
            $pattern = [System.Management.Automation.WildcardPattern]::new("*.txt")
            $regex = $pattern.ToRegex()
            $regex | Should -BeExactly '\.txt$'
        }

        It "Converts question mark wildcard to regex" {
            $pattern = [System.Management.Automation.WildcardPattern]::new("test?.log")
            $regex = $pattern.ToRegex()
            # ? converts to . (matches exactly one character), not .*
            $regex | Should -BeExactly '^test.\.log$'
        }

        It "Converts bracket expression to regex" {
            $pattern = [System.Management.Automation.WildcardPattern]::new("file[0-9].txt")
            $regex = $pattern.ToRegex()
            $regex | Should -BeExactly '^file[0-9]\.txt$'
        }

        It "Escapes regex special characters" {
            $pattern = [System.Management.Automation.WildcardPattern]::new("test.log")
            $regex = $pattern.ToRegex()
            $regex | Should -BeExactly '^test\.log$'
        }
    }

Review comment (bug):
It seems most of new tests could be in TestCases excluding `Context "Edge cases"`

Code after:
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.

Describe "WildcardPattern.ToRegex Tests" -Tags "CI" {
    It "Converts '<Pattern>' to regex pattern '<Expected>'" -TestCases @(
        @{ Pattern = '*.txt'; Expected = '\.txt$' }
        @{ Pattern = 'test?.log'; Expected = '^test.\.log$' }
        @{ Pattern = 'file[0-9].txt'; Expected = '^file[0-9]\.txt$' }
        @{ Pattern = 'test.log'; Expected = '^test\.log$' }
        @{ Pattern = '*test*file*.txt'; Expected = 'test.*file.*\.txt$' }
        @{ Pattern = 'file[0-9][a-z].txt'; Expected = '^file[0-9][a-z]\.txt$' }
        @{ Pattern = 'test*'; Expected = '^test' }
        @{ Pattern = '*test*'; Expected = 'test' }
    ) {
        param($Pattern, $Expected)
        $wildcardPattern = [System.Management.Automation.WildcardPattern]::new($Pattern)
        $regex = $wildcardPattern.ToRegex()
        $regex | Should -BeOfType ([regex])
        $regex.ToString() | Should -BeExactly $Expected
    }

    It "Converts '<Pattern>' with <OptionName> option" -TestCases @(
        @{ Pattern = 'TEST'; OptionName = 'IgnoreCase'; Option = [System.Management.Automation.WildcardOptions]::IgnoreCase; Expected = '^TEST$'; ExpectedRegexOptions = 'IgnoreCase, Singleline'; TestString = 'test'; ExpectedMatch = $true }
        @{ Pattern = 'test'; OptionName = 'CultureInvariant'; Option = [System.Management.Automation.WildcardOptions]::CultureInvariant; Expected = '^test$'; ExpectedRegexOptions = 'Singleline, CultureInvariant'; TestString = 'test'; ExpectedMatch = $true }
        @{ Pattern = 'test*'; OptionName = 'Compiled'; Option = [System.Management.Automation.WildcardOptions]::Compiled; Expected = '^test'; ExpectedRegexOptions = 'Compiled, Singleline'; TestString = 'testing'; ExpectedMatch = $true }
    ) {
        param($Pattern, $OptionName, $Option, $Expected, $ExpectedRegexOptions, $TestString, $ExpectedMatch)
        $wildcardPattern = [System.Management.Automation.WildcardPattern]::new($Pattern, $Option)
        $regex = $wildcardPattern.ToRegex()
        $regex | Should -BeOfType ([regex])