|
| 1 | +# Copyright (c) Microsoft Corporation. |
| 2 | +# Licensed under the MIT License. |
| 3 | + |
| 4 | +Describe "WildcardPattern.ToRegex Tests" -Tags "CI" { |
| 5 | + It "Converts '<Pattern>' to regex pattern '<Expected>'" -TestCases @( |
| 6 | + @{ Pattern = '*.txt'; Expected = '\.txt$' } |
| 7 | + @{ Pattern = 'test?.log'; Expected = '^test.\.log$' } |
| 8 | + @{ Pattern = 'file[0-9].txt'; Expected = '^file[0-9]\.txt$' } |
| 9 | + @{ Pattern = 'test.log'; Expected = '^test\.log$' } |
| 10 | + @{ Pattern = '*test*file*.txt'; Expected = 'test.*file.*\.txt$' } |
| 11 | + @{ Pattern = 'file[0-9][a-z].txt'; Expected = '^file[0-9][a-z]\.txt$' } |
| 12 | + @{ Pattern = 'test*'; Expected = '^test' } |
| 13 | + @{ Pattern = '*test*'; Expected = 'test' } |
| 14 | + ) { |
| 15 | + param($Pattern, $Expected) |
| 16 | + $wildcardPattern = [System.Management.Automation.WildcardPattern]::new($Pattern) |
| 17 | + $regex = $wildcardPattern.ToRegex() |
| 18 | + $regex | Should -BeOfType ([regex]) |
| 19 | + $regex.ToString() | Should -BeExactly $Expected |
| 20 | + } |
| 21 | + |
| 22 | + It "Converts '<Pattern>' with <OptionName> option" -TestCases @( |
| 23 | + @{ Pattern = 'TEST'; OptionName = 'IgnoreCase'; Option = [System.Management.Automation.WildcardOptions]::IgnoreCase; Expected = '^TEST$'; ExpectedRegexOptions = 'IgnoreCase, Singleline'; TestString = 'test'; ExpectedMatch = $true } |
| 24 | + @{ Pattern = 'test'; OptionName = 'CultureInvariant'; Option = [System.Management.Automation.WildcardOptions]::CultureInvariant; Expected = '^test$'; ExpectedRegexOptions = 'Singleline, CultureInvariant'; TestString = 'test'; ExpectedMatch = $true } |
| 25 | + @{ Pattern = 'test*'; OptionName = 'Compiled'; Option = [System.Management.Automation.WildcardOptions]::Compiled; Expected = '^test'; ExpectedRegexOptions = 'Compiled, Singleline'; TestString = 'testing'; ExpectedMatch = $true } |
| 26 | + ) { |
| 27 | + param($Pattern, $OptionName, $Option, $Expected, $ExpectedRegexOptions, $TestString, $ExpectedMatch) |
| 28 | + $wildcardPattern = [System.Management.Automation.WildcardPattern]::new($Pattern, $Option) |
| 29 | + $regex = $wildcardPattern.ToRegex() |
| 30 | + $regex | Should -BeOfType ([regex]) |
| 31 | + $regex.ToString() | Should -BeExactly $Expected |
| 32 | + $regex.Options.ToString() | Should -BeExactly $ExpectedRegexOptions |
| 33 | + $regex.IsMatch($TestString) | Should -Be $ExpectedMatch |
| 34 | + } |
| 35 | + |
| 36 | + It "Regex from '<Pattern>' matches '<TestString>': <ShouldMatch>" -TestCases @( |
| 37 | + @{ Pattern = '*test*file*.txt'; TestString = 'mytestmyfile123.txt'; ShouldMatch = $true } |
| 38 | + @{ Pattern = 'file[0-9][a-z].txt'; TestString = 'file5a.txt'; ShouldMatch = $true } |
| 39 | + @{ Pattern = 'file[0-9][a-z].txt'; TestString = 'file55.txt'; ShouldMatch = $false } |
| 40 | + ) { |
| 41 | + param($Pattern, $TestString, $ShouldMatch) |
| 42 | + $regex = [System.Management.Automation.WildcardPattern]::new($Pattern).ToRegex() |
| 43 | + $regex.IsMatch($TestString) | Should -Be $ShouldMatch |
| 44 | + } |
| 45 | + |
| 46 | + Context "Edge cases" { |
| 47 | + It "Handles empty pattern" { |
| 48 | + $pattern = [System.Management.Automation.WildcardPattern]::new("") |
| 49 | + $regex = $pattern.ToRegex() |
| 50 | + $regex | Should -BeOfType ([regex]) |
| 51 | + $regex.ToString() | Should -Be "^$" |
| 52 | + } |
| 53 | + |
| 54 | + It "Handles pattern with only asterisk" { |
| 55 | + $pattern = [System.Management.Automation.WildcardPattern]::new("*") |
| 56 | + $regex = $pattern.ToRegex() |
| 57 | + $regex | Should -BeOfType ([regex]) |
| 58 | + $regex.ToString() | Should -BeExactly "" |
| 59 | + $regex.IsMatch("anything") | Should -BeTrue |
| 60 | + $regex.IsMatch("") | Should -BeTrue |
| 61 | + } |
| 62 | + |
| 63 | + It "Handles escaped '<Char>' wildcard character" -TestCases @( |
| 64 | + @{ Char = '*'; Pattern = 'file`*.txt'; Expected = '^file\*\.txt$' } |
| 65 | + @{ Char = '?'; Pattern = 'file`?.txt'; Expected = '^file\?\.txt$' } |
| 66 | + @{ Char = '['; Pattern = 'file`[.txt'; Expected = '^file\[\.txt$' } |
| 67 | + @{ Char = ']'; Pattern = 'file`].txt'; Expected = '^file]\.txt$' } |
| 68 | + ) { |
| 69 | + param($Char, $Pattern, $Expected) |
| 70 | + $wildcardPattern = [System.Management.Automation.WildcardPattern]::new($Pattern) |
| 71 | + $regex = $wildcardPattern.ToRegex() |
| 72 | + $regex | Should -BeOfType ([regex]) |
| 73 | + $regex.ToString() | Should -BeExactly $Expected |
| 74 | + } |
| 75 | + |
| 76 | + } |
| 77 | +} |
0 commit comments