8000 Enable use 'Singleline,Multiline' option in split operator · PowerShell/PowerShell@75b0a41 · GitHub
[go: up one dir, main page]

Skip to content

Commit 75b0a41

Browse files
committed
Enable use 'Singleline,Multiline' option in split operator
1 parent fd047a8 commit 75b0a41

File tree

2 files changed

+236
-7
lines changed

2 files changed

+236
-7
lines changed

src/System.Management.Automation/engine/lang/parserutils.cs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -723,13 +723,6 @@ private static object SplitWithPattern(ExecutionContext context, IScriptExtent e
723723
}
724724
}
725725

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

0 commit comments

Comments
 (0)
0