MegaBites-AI/Windows-powershell
0372
1# Copyright (c) Microsoft Corporation.2# Licensed under the MIT License.3Describe "Split Operator" -Tags CI {4 Context "Binary split operator" {5 It "Binary split operator can split array of value" {6 $res = "a b", "c d" -split " "7 $res.count | Should -Be 48 $res[0] | Should -Be "a"9 $res[1] | Should -Be "b"10 $res[2] | Should -Be "c"11 $res[3] | Should -Be "d"12 }13 14 It "Binary split operator can split a string" {15 $res = "a b c d" -split " "16 $res.count | Should -Be 417 $res[0] | Should -Be "a"18 $res[1] | Should -Be "b"19 $res[2] | Should -Be "c"20 $res[3] | Should -Be "d"21 }22 23 It "Binary split operator can works with max substring limit" {24 $res = "a b c d" -split " ", 225 $res.count | Should -Be 226 $res[0] | Should -Be "a"27 $res[1] | Should -Be "b c d"28 29 $res = "a b c d" -split " ", 030 $res.count | Should -Be 431 $res[0] | Should -Be "a"32 $res[1] | Should -Be "b"33 $res[2] | Should -Be "c"34 $res[3] | Should -Be "d"35 36 $res = "a b c d" -split " ", -237 $res.count | Should -Be 238 $res[0] | Should -Be "a b c"39 $res[1] | Should -Be "d"40 41 $res = "a b c d" -split " ", -142 $res.count | Should -Be 143 $res[0] | Should -Be "a b c d"44 }45 46 It "Binary split operator can work with different delimeter than split string" {47 $res = "a b c d" -split " ",848 $res.count | Should -Be 449 $res[0] | Should -Be "a"50 $res[1] | Should -Be "b"51 $res[2] | Should -Be "c"52 $res[3] | Should -Be "d"53 54 $res = "a b c d" -split " ",-855 $res.count | Should -Be 456 $res[0] | Should -Be "a"57 $res[1] | Should -Be "b"58 $res[2] | Should -Be "c"59 $res[3] | Should -Be "d"60 61 $res = " " -split " ",-262 $res.count | Should -Be 263 $res[0] | Should -Be ""64 $res[1] | Should -Be ""65 }66 67 It "Binary split operator with predicate can work with negative numbers" {68 $res = "a b c d" -split {$_ -like ' '},-269 $res.count | Should -Be 270 $res[0] | Should -Be "a b c"71 $res[1] | Should -Be "d"72 73 $res = "a b c d" -split {$_ -like ' '},-474 $res.count | Should -Be 475 $res[0] | Should -Be "a"76 $res[1] | Should -Be "b"77 $res[2] | Should -Be "c"78 $res[3] | Should -Be "d"79 80 $res = "a b c d" -split {$_ -like ' '},-881 $res.count | Should -Be 482 $res[0] | Should -Be "a"83 $res[1] | Should -Be "b"84 $res[2] | Should -Be "c"85 $res[3] | Should -Be "d"86 87 $res = " " -split {$_ -like ' '},-488 $res.count | Should -Be 289 $res[0] | Should -Be ""90 $res[1] | Should -Be ""91 92 $res = "folder/path/to/file" -split {$_ -like '/'}, -293 $res.count | Should -Be 294 $res[0] | Should -Be "folder/path/to"95 $res[1] | Should -Be "file"96 }97 98 It "Binary split operator can work with regex expression" {99 $res = "a2b3c4d" -split '\d+',2100 $res.count | Should -Be 2101 $res[0] | Should -Be "a"102 $res[1] | Should -Be "b3c4d"103 104 $res = "a2b3c4d" -split '\d+',-2105 $res.count | Should -Be 2106 $res[0] | Should -Be "a2b3c"107 $res[1] | Should -Be "d"108 }109 110 It "Binary split operator can works with freeform delimiter" {111 $res = "a::b::c::d" -split "::"112 $res.count | Should -Be 4113 $res[0] | Should -Be "a"114 $res[1] | Should -Be "b"115 $res[2] | Should -Be "c"116 $res[3] | Should -Be "d"117 }118 119 It "Binary split operator can preserve delimiter" {120 $res = "a1:b1:c1:d" -split "(1:)"121 $res.count | Should -Be 7122 $res[0] | Should -Be "a"123 $res[1] | Should -Be "1:"124 $res[2] | Should -Be "b"125 $res[3] | Should -Be "1:"126 $res[4] | Should -Be "c"127 $res[5] | Should -Be "1:"128 $res[6] | Should -Be "d"129 130 $res = "a1:b1:c1:d" -split "1(:)"131 $res.count | Should -Be 7132 $res[0] | Should -Be "a"133 $res[1] | Should -Be ":"134 $res[2] | Should -Be "b"135 $res[3] | Should -Be ":"136 $res[4] | Should -Be "c"137 $res[5] | Should -Be ":"138 $res[6] | Should -Be "d"139 }140 141 It "Binary split operator can be case-insensitive and case-sensitive" {142 $res = "abcBd" -split "B"143 $res.count | Should -Be 3144 $res[0] | Should -Be "a"145 $res[1] | Should -Be "c"146 $res[2] | Should -Be "d"147 148 $res = "abcBd" -isplit "B"149 $res.count | Should -Be 3150 $res[0] | Should -Be "a"151 $res[1] | Should -Be "c"152 $res[2] | Should -Be "d"153 154 $res = "abcBd" -csplit "B"155 $res.count | Should -Be 2156 $res[0] | Should -Be "abc"157 $res[1] | Should -Be "d"158 159 $res = "abcBd" -csplit "B", 0 , 'IgnoreCase'160 $res.count | Should -Be 3161 $res[0] | Should -Be "a"162 $res[1] | Should -Be "c"163 $res[2] | Should -Be "d"164 }165 166 It "Binary split operator can works with script block" {167 $res = "a::b::c::d" -split {$_ -eq "b" -or $_ -eq "C"}168 $res.count | Should -Be 3169 $res[0] | Should -Be "a::"170 $res[1] | Should -Be "::"171 $res[2] | Should -Be "::d"172 }173 174 }175 176 Context "Binary split operator options" {177 BeforeAll {178 # Add '%' in testText2 in order to second line doesn't start with 'b'.179 $testCases = @(180 @{ Name = '`n'; testText = "a12a`nb34b`nc56c`nd78d"; testText2 = "a12a`n%b34b`nc56c`nd78d"; newLine = "`n" }181 @{ Name = '`r`n'; testText = "a12a`r`nb34b`r`nc56c`r`nd78d"; testText2 = "a12a`r`n%b34b`r`nc56c`r`nd78d"; newLine = "`r`n" }182 )183 }184 185 It "Binary split operator has no Singleline and no Multiline by default (new line = '<Name>')" -TestCases $testCases {186 param($testText, $testText2, $newLine)187 # Multiline isn't default188 $res = $testText -split '^b'189 $res.count | Should -Be 1190 191 # Singleline isn't default192 $res = $testText -split 'b.+c'193 $res.count | Should -Be 1194 }195 196 It "Binary split operator works with Singleline (new line = '<Name>')" -TestCases $testCases {197 param($testText, $testText2, $newLine)198 $res = $testText -split 'b.+c', 0, 'Singleline'199 $res.count | Should -Be 2200 $res[0] | Should -Be "a12a$($newLine)"201 $res[1] | Should -Be "$($newLine)d78d"202 203 $res = $testText2 -split 'b.+c', 0, 'Singleline'204 $res.count | Should -Be 2205 $res[0] | Should -Be "a12a$($newLine)%"206 $res[1] | Should -Be "$($newLine)d78d"207 }208 209 It "Binary split operator works with Multiline (new line = '<Name>')" -TestCases $testCases {210 param($testText, $testText2, $newLine)211 $res = $testText -split '^b', 0, 'Multiline'212 $res.count | Should -Be 2213 $res[0] | Should -Be "a12a$($newLine)"214 $res[1] | Should -Be "34b$($newLine)c56c$($newLine)d78d"215 }216 217 It "Binary split operator works with Singleline,Multiline (new line = '<Name>')" -TestCases $testCases {218 param($testText, $testText2, $newLine)219 $res = $testText -split 'b.+c', 0, 'Singleline,Multiline'220 $res.count | Should -Be 2221 $res[0] | Should -Be "a12a$($newLine)"222 $res[1] | Should -Be "$($newLine)d78d"223 224 $res = $testText2 -split 'b.+c', 0, 'Singleline,Multiline'225 $res.count | Should -Be 2226 $res[0] | Should -Be "a12a$($newLine)%"227 $res[1] | Should -Be "$($newLine)d78d"228 229 $res = $testText -split '^b.+c', 0, 'Singleline,Multiline'230 $res.count | Should -Be 2231 $res[0] | Should -Be "a12a$($newLine)"232 $res[1] | Should -Be "$($newLine)d78d"233 234 $res = $testText2 -split '^b.+c', 0, 'Singleline,Multiline'235 $res.count | Should -Be 1236 }237 238 It "Binary split operator works with IgnorePatternWhitespace" {239 $res = "a: b:c" -split ': '240 $res.count | Should -Be 2241 $res[0] | Should -Be "a"242 $res[1] | Should -Be "b:c"243 244 $res = "a: b:c" -split ': ',0,'IgnorePatternWhitespace'245 $res.count | Should -Be 3246 $res[0] | Should -Be "a"247 $res[1] | Should -Be " b"248 $res[2] | Should -Be "c"249 }250 251 It "Binary split operator works with ExplicitCapture" {252 $res = "a:b" -split "(:)"253 $res.count | Should -Be 3254 $res[0] | Should -Be "a"255 $res[1] | Should -Be ":"256 $res[2] | Should -Be "b"257 258 $res = "a:b" -split "(:)", 0, 'ExplicitCapture'259 $res.count | Should -Be 2260 $res[0] | Should -Be "a"261 $res[1] | Should -Be "b"262 }263 264 It "Binary split operator works with SimpleMatch" {265 $res = "abc" -split "B", 0, 'SimpleMatch,IgnoreCase'266 $res.count | Should -Be 2267 $res[0] | Should -Be "a"268 $res[1] | Should -Be "c"269 }270 271 It "Binary split operator works with RegexMatch" {272 $res = "abc" -split "B", 0, 'RegexMatch,Singleline'273 $res.count | Should -Be 2274 $res[0] | Should -Be "a"275 $res[1] | Should -Be "c"276 }277 278 It "Binary split operator doesn't works with RegexMatch,SimpleMatch" {279 { "abc" -split "B", 0, 'RegexMatch,SimpleMatch' } | Should -Throw -ErrorId "InvalidSplitOptionCombination"280 }281 }282 283 Context "Unary split operator" {284 It "Unary split operator has higher precedence than a comma" {285 $res = -split "a b", "c d"286 $res.count | Should -Be 2287 $res[0][0] | Should -Be "a"288 $res[0][1] | Should -Be "b"289 $res[1] | Should -Be "c d"290 }291 292 It "Unary split operator can split array of values" {293 $res = -split ("a b", "c d")294 $res.count | Should -Be 4295 $res[0] | Should -Be "a"296 $res[1] | Should -Be "b"297 $res[2] | Should -Be "c"298 $res[3] | Should -Be "d"299 }300 301 It "Unary split operator can split a string" {302 $res = -split "a b c d"303 $res.count | Should -Be 4304 $res[0] | Should -Be "a"305 $res[1] | Should -Be "b"306 $res[2] | Should -Be "c"307 $res[3] | Should -Be "d"308 }309 }310}311 