forked from PowerShell/PowerShell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSplitOperator.Tests.ps1
More file actions
242 lines (209 loc) · 8.54 KB
/
Copy pathSplitOperator.Tests.ps1
File metadata and controls
242 lines (209 loc) · 8.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
Describe "Split Operator" -Tags CI {
Context "Binary split operator" {
It "Binary split operator can split array of value" {
$res = "a b", "c d" -split " "
$res.count | Should Be 4
$res[0] | Should Be "a"
$res[1] | Should Be "b"
$res[2] | Should Be "c"
$res[3] | Should Be "d"
}
It "Binary split operator can split a string" {
$res = "a b c d" -split " "
$res.count | Should Be 4
$res[0] | Should Be "a"
$res[1] | Should Be "b"
$res[2] | Should Be "c"
$res[3] | Should Be "d"
}
It "Binary split operator can works with max substring limit" {
$res = "a b c d" -split " ", 2
$res.count | Should Be 2
$res[0] | Should Be "a"
$res[1] | Should Be "b c d"
$res = "a b c d" -split " ", 0
$res.count | Should Be 4
$res[0] | Should Be "a"
$res[1] | Should Be "b"
$res[2] | Should Be "c"
$res[3] | Should Be "d"
$res = "a b c d" -split " ", -1
$res.count | Should Be 4
$res[0] | Should Be "a"
$res[1] | Should Be "b"
$res[2] | Should Be "c"
$res[3] | Should Be "d"
}
It "Binary split operator can works with freeform delimiter" {
$res = "a::b::c::d" -split "::"
$res.count | Should Be 4
$res[0] | Should Be "a"
$res[1] | Should Be "b"
$res[2] | Should Be "c"
$res[3] | Should Be "d"
}
It "Binary split operator can preserve delimiter" {
$res = "a1:b1:c1:d" -split "(1:)"
$res.count | Should Be 7
$res[0] | Should Be "a"
$res[1] | Should Be "1:"
$res[2] | Should Be "b"
$res[3] | Should Be "1:"
$res[4] | Should Be "c"
$res[5] | Should Be "1:"
$res[6] | Should Be "d"
$res = "a1:b1:c1:d" -split "1(:)"
$res.count | Should Be 7
$res[0] | Should Be "a"
$res[1] | Should Be ":"
$res[2] | Should Be "b"
$res[3] | Should Be ":"
$res[4] | Should Be "c"
$res[5] | Should Be ":"
$res[6] | Should Be "d"
}
It "Binary split operator can be case-insensitive and case-sensitive" {
$res = "abcBd" -split "B"
$res.count | Should Be 3
$res[0] | Should Be "a"
$res[1] | Should Be "c"
$res[2] | Should Be "d"
$res = "abcBd" -isplit "B"
$res.count | Should Be 3
$res[0] | Should Be "a"
$res[1] | Should Be "c"
$res[2] | Should Be "d"
$res = "abcBd" -csplit "B"
$res.count | Should Be 2
$res[0] | Should Be "abc"
$res[1] | Should Be "d"
$res = "abcBd" -csplit "B", 0 , 'IgnoreCase'
$res.count | Should Be 3
$res[0] | Should Be "a"
$res[1] | Should Be "c"
$res[2] | Should Be "d"
}
It "Binary split operator can works with script block" {
$res = "a::b::c::d" -split {$_ -eq "b" -or $_ -eq "C"}
$res.count | Should Be 3
$res[0] | Should Be "a::"
$res[1] | Should Be "::"
$res[2] | Should Be "::d"
}
}
Context "Binary split operator options" {
BeforeAll {
# Add '%' in testText2 in order to second line doesn't start with 'b'.
$testCases = @(
@{ Name = '`n'; testText = "a12a`nb34b`nc56c`nd78d"; testText2 = "a12a`n%b34b`nc56c`nd78d"; newLine = "`n" }
@{ Name = '`r`n'; testText = "a12a`r`nb34b`r`nc56c`r`nd78d"; testText2 = "a12a`r`n%b34b`r`nc56c`r`nd78d"; newLine = "`r`n" }
)
}
It "Binary split operator has no Singleline and no Multiline by default (new line = '<Name>')" -TestCases $testCases {
param($testText, $testText2, $newLine)
# Multiline isn't default
$res = $testText -split '^b'
$res.count | Should Be 1
# Singleline isn't default
$res = $testText -split 'b.+c'
$res.count | Should Be 1
}
It "Binary split operator works with Singleline (new line = '<Name>')" -TestCases $testCases {
param($testText, $testText2, $newLine)
$res = $testText -split 'b.+c', 0, 'Singleline'
$res.count | Should Be 2
$res[0] | Should Be "a12a$($newLine)"
$res[1] | Should Be "$($newLine)d78d"
$res = $testText2 -split 'b.+c', 0, 'Singleline'
$res.count | Should Be 2
$res[0] | Should Be "a12a$($newLine)%"
$res[1] | Should Be "$($newLine)d78d"
}
It "Binary split operator works with Multiline (new line = '<Name>')" -TestCases $testCases {
param($testText, $testText2, $newLine)
$res = $testText -split '^b', 0, 'Multiline'
$res.count | Should Be 2
$res[0] | Should Be "a12a$($newLine)"
$res[1] | Should Be "34b$($newLine)c56c$($newLine)d78d"
}
It "Binary split operator works with Singleline,Multiline (new line = '<Name>')" -TestCases $testCases {
param($testText, $testText2, $newLine)
$res = $testText -split 'b.+c', 0, 'Singleline,Multiline'
$res.count | Should Be 2
$res[0] | Should Be "a12a$($newLine)"
$res[1] | Should Be "$($newLine)d78d"
$res = $testText2 -split 'b.+c', 0, 'Singleline,Multiline'
$res.count | Should Be 2
$res[0] | Should Be "a12a$($newLine)%"
$res[1] | Should Be "$($newLine)d78d"
$res = $testText -split '^b.+c', 0, 'Singleline,Multiline'
$res.count | Should Be 2
$res[0] | Should Be "a12a$($newLine)"
$res[1] | Should Be "$($newLine)d78d"
$res = $testText2 -split '^b.+c', 0, 'Singleline,Multiline'
$res.count | Should Be 1
}
It "Binary split operator works with IgnorePatternWhitespace" {
$res = "a: b:c" -split ': '
$res.count | Should Be 2
$res[0] | Should Be "a"
$res[1] | Should Be "b:c"
$res = "a: b:c" -split ': ',0,'IgnorePatternWhitespace'
$res.count | Should Be 3
$res[0] | Should Be "a"
$res[1] | Should Be " b"
$res[2] | Should Be "c"
}
It "Binary split operator works with ExplicitCapture" {
$res = "a:b" -split "(:)"
$res.count | Should Be 3
$res[0] | Should Be "a"
$res[1] | Should Be ":"
$res[2] | Should Be "b"
$res = "a:b" -split "(:)", 0, 'ExplicitCapture'
$res.count | Should Be 2
$res[0] | Should Be "a"
$res[1] | Should Be "b"
}
It "Binary split operator works with SimpleMatch" {
$res = "abc" -split "B", 0, 'SimpleMatch,IgnoreCase'
$res.count | Should Be 2
$res[0] | Should Be "a"
$res[1] | Should Be "c"
}
It "Binary split operator works with RegexMatch" {
$res = "abc" -split "B", 0, 'RegexMatch,Singleline'
$res.count | Should Be 2
$res[0] | Should Be "a"
$res[1] | Should Be "c"
}
It "Binary split operator doesn't works with RegexMatch,SimpleMatch" {
{ "abc" -split "B", 0, 'RegexMatch,SimpleMatch' } | ShouldBeErrorId "InvalidSplitOptionCombination"
}
}
Context "Unary split operator" {
It "Unary split operator has higher precedence than a comma" {
$res = -split "a b", "c d"
$res.count | Should Be 2
$res[0][0] | Should Be "a"
$res[0][1] | Should Be "b"
$res[1] | Should Be "c d"
}
It "Unary split operator can split array of values" {
$res = -split ("a b", "c d")
$res.count | Should Be 4
$res[0] | Should Be "a"
$res[1] | Should Be "b"
$res[2] | Should Be "c"
$res[3] | Should Be "d"
}
It "Unary split operator can split a string" {
$res = -split "a b c d"
$res.count | Should Be 4
$res[0] | Should Be "a"
$res[1] | Should Be "b"
$res[2] | Should Be "c"
$res[3] | Should Be "d"
}
}
}