forked from PowerShell/PowerShell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSelect-String.Tests.ps1
More file actions
300 lines (244 loc) · 14.5 KB
/
Copy pathSelect-String.Tests.ps1
File metadata and controls
300 lines (244 loc) · 14.5 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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
Describe "Select-String" -Tags "CI" {
BeforeAll {
$nl = [Environment]::NewLine
$currentDirectory = $PWD.Path
$originalRendering = $PSStyle.OutputRendering
$PSStyle.OutputRendering = 'Ansi'
}
AfterAll {
$PSStyle.OutputRendering = $originalRendering
Push-Location $currentDirectory
}
Context "String actions" {
BeforeAll {
$testinputone = "hello","Hello","goodbye"
$testinputtwo = "hello","Hello"
}
It "Should be called without errors" {
{ $testinputone | Select-String -Pattern "hello" } | Should -Not -Throw
}
It "Should return an array data type when multiple matches are found" {
$result = $testinputtwo | Select-String -Pattern "hello"
,$result | Should -BeOfType System.Array
}
It "Should return an object type when one match is found" {
$result = $testinputtwo | Select-String -Pattern "hello" -CaseSensitive
,$result | Should -BeOfType System.Object
}
It "Should return matchinfo type" {
$result = $testinputtwo | Select-String -Pattern "hello" -CaseSensitive
,$result | Should -BeOfType Microsoft.PowerShell.Commands.MatchInfo
}
It "Should be called without an error using ca for casesensitive " {
{$testinputone | Select-String -Pattern "hello" -ca } | Should -Not -Throw
}
It "Should use the ca alias for casesensitive" {
$firstMatch = $testinputtwo | Select-String -Pattern "hello" -CaseSensitive
$secondMatch = $testinputtwo | Select-String -Pattern "hello" -ca
$equal = @(Compare-Object $firstMatch $secondMatch).Length -eq 0
$equal | Should -BeTrue
}
It "Should only return the case sensitive match when the casesensitive switch is used" {
$testinputtwo | Select-String -Pattern "hello" -CaseSensitive | Should -Be "hello"
}
It "Should accept a collection of strings from the input object" {
{ Select-String -InputObject "some stuff", "other stuff" -Pattern "other" } | Should -Not -Throw
}
It "Should return system.object when the input object switch is used on a collection" {
$result = Select-String -InputObject "some stuff", "other stuff" -Pattern "other"
,$result | Should -BeOfType System.Object
}
It "Should return null or empty when the input object switch is used on a collection and the pattern does not exist" {
Select-String -InputObject "some stuff", "other stuff" -Pattern "neither" | Should -BeNullOrEmpty
}
It "Should return a bool type when the quiet switch is used" {
,($testinputtwo | Select-String -Quiet "hello" -CaseSensitive) | Should -BeOfType System.Boolean
}
It "Should be true when select string returns a positive result when the quiet switch is used" {
($testinputtwo | Select-String -Quiet "hello" -CaseSensitive) | Should -BeTrue
}
It "Should be empty when select string does not return a result when the quiet switch is used" {
$testinputtwo | Select-String -Quiet "goodbye" | Should -BeNullOrEmpty
}
It "Should return an array of non matching strings when the switch of NotMatch is used and the string do not match" {
$testinputone | Select-String -Pattern "goodbye" -NotMatch | Should -BeExactly "hello", "Hello"
}
It "Should output a string with the first match highlighted" {
if ($Host.UI.SupportsVirtualTerminal -and !(Test-Path env:__SuppressAnsiEscapeSequences))
{
$result = $testinputone | Select-String -Pattern "l" | Out-String
$result | Should -Be "${nl}he`e[7ml`e[0mlo${nl}He`e[7ml`e[0mlo${nl}${nl}"
}
else
{
$result = $testinputone | Select-String -Pattern "l" | Out-String
$result | Should -Be "${nl}hello${nl}Hello${nl}${nl}"
}
}
It "Should output a string with all matches highlighted when AllMatch is used" {
if ($Host.UI.SupportsVirtualTerminal -and !(Test-Path env:__SuppressAnsiEscapeSequences))
{
$result = $testinputone | Select-String -Pattern "l" -AllMatch | Out-String
$result | Should -Be "${nl}he`e[7ml`e[0m`e[7ml`e[0mo${nl}He`e[7ml`e[0m`e[7ml`e[0mo${nl}${nl}"
}
else
{
$result = $testinputone | Select-String -Pattern "l" -AllMatch | Out-String
$result | Should -Be "${nl}hello${nl}Hello${nl}${nl}"
}
}
It "Should output a string with the first match highlighted when SimpleMatch is used" {
if ($Host.UI.SupportsVirtualTerminal -and !(Test-Path env:__SuppressAnsiEscapeSequences))
{
$result = $testinputone | Select-String -Pattern "l" -SimpleMatch | Out-String
$result | Should -Be "${nl}he`e[7ml`e[0mlo${nl}He`e[7ml`e[0mlo${nl}${nl}"
}
else
{
$result = $testinputone | Select-String -Pattern "l" -SimpleMatch | Out-String
$result | Should -Be "${nl}hello${nl}Hello${nl}${nl}"
}
}
It "Should output a string without highlighting when NoEmphasis is used" {
$result = $testinputone | Select-String -Pattern "l" -NoEmphasis | Out-String
$result | Should -Be "${nl}hello${nl}Hello${nl}${nl}"
}
It "Should return an array of matching strings without virtual terminal sequences" {
$testinputone | Select-String -Pattern "l" | Should -Be "hello", "hello"
}
It "Should return a string type when -Raw is used" {
$result = $testinputtwo | Select-String -Pattern "hello" -CaseSensitive -Raw
$result | Should -BeOfType System.String
}
It "Should return ParameterBindingException when -Raw and -Quiet are used together" {
{ $testinputone | Select-String -Pattern "hello" -Raw -Quiet -ErrorAction Stop } | Should -Throw -ExceptionType ([System.Management.Automation.ParameterBindingException])
}
}
Context "Filesystem actions" {
$testDirectory = $TestDrive
$testInputFile = Join-Path -Path $testDirectory -ChildPath testfile1.txt
BeforeEach {
New-Item $testInputFile -ItemType "file" -Force -Value "This is a text string, and another string${nl}This is the second line${nl}This is the third line${nl}This is the fourth line${nl}No matches"
}
AfterEach {
Remove-Item $testInputFile -Force
}
It "Should return an object when a match is found is the file on only one line" {
$result = Select-String $testInputFile -Pattern "string"
,$result | Should -BeOfType System.Object
}
It "Should return an array when a match is found is the file on several lines" {
$result = Select-String $testInputFile -Pattern "in"
,$result | Should -BeOfType System.Array
$result[0] | Should -BeOfType Microsoft.PowerShell.Commands.MatchInfo
}
It "Should return the name of the file and the string that 'string' is found if there is only one lines that has a match" {
$expected = $testInputFile + ":1:This is a text string, and another string"
Select-String $testInputFile -Pattern "string" | Should -BeExactly $expected
}
It "Should return all strings where 'second' is found in testfile1 if there is only one lines that has a match" {
$expected = $testInputFile + ":2:This is the second line"
Select-String $testInputFile -Pattern "second" | Should -BeExactly $expected
}
It "Should return all strings where 'in' is found in testfile1 pattern switch is not required" {
$expected1 = "This is a text string, and another string"
$expected2 = "This is the second line"
$expected3 = "This is the third line"
$expected4 = "This is the fourth line"
(Select-String in $testInputFile)[0].Line | Should -BeExactly $expected1
(Select-String in $testInputFile)[1].Line | Should -BeExactly $expected2
(Select-String in $testInputFile)[2].Line | Should -BeExactly $expected3
(Select-String in $testInputFile)[3].Line | Should -BeExactly $expected4
(Select-String in $testInputFile)[4].Line | Should -BeNullOrEmpty
}
It "Should return empty because 'for' is not found in testfile1 " {
Select-String for $testInputFile | Should -BeNullOrEmpty
}
It "Should return the third line in testfile1 and the lines above and below it " {
$expectedLine = "testfile1.txt:2:This is the second line"
$expectedLineBefore = "testfile1.txt:3:This is the third line"
$expectedLineAfter = "testfile1.txt:4:This is the fourth line"
Select-String third $testInputFile -Context 1 | Should -Match $expectedLine
Select-String third $testInputFile -Context 1 | Should -Match $expectedLineBefore
Select-String third $testInputFile -Context 1 | Should -Match $expectedLineAfter
}
It "Should return the number of matches for 'is' in textfile1 " {
(Select-String is $testInputFile -CaseSensitive).count | Should -Be 4
}
It "Should return the third line in testfile1 when a relative path is used" {
$expected = "testfile1.txt:3:This is the third line"
$relativePath = Join-Path -Path $testDirectory -ChildPath ".."
$relativePath = Join-Path -Path $relativePath -ChildPath $TestDirectory.Name
$relativePath = Join-Path -Path $relativePath -ChildPath testfile1.txt
Select-String third $relativePath | Should -Match $expected
}
It "Should return the fourth line in testfile1 when a relative path is used" {
$expected = "testfile1.txt:5:No matches"
Push-Location $testDirectory
Select-String matches (Join-Path -Path $testDirectory -ChildPath testfile1.txt) | Should -Match $expected
Pop-Location
}
It "Should return the fourth line in testfile1 when a regular expression is used" {
$expected = "testfile1.txt:5:No matches"
Select-String 'matc*' $testInputFile -CaseSensitive | Should -Match $expected
}
It "Should return the fourth line in testfile1 when a regular expression is used, using the alias for casesensitive" {
$expected = "testfile1.txt:5:No matches"
Select-String 'matc*' $testInputFile -ca | Should -Match $expected
}
It "Should return all strings where 'in' is found in testfile1, when -Raw is used." {
$expected1 = "This is a text string, and another string"
$expected2 = "This is the second line"
$expected3 = "This is the third line"
$expected4 = "This is the fourth line"
(Select-String in $testInputFile -Raw)[0] | Should -BeExactly $expected1
(Select-String in $testInputFile -Raw)[1] | Should -BeExactly $expected2
(Select-String in $testInputFile -Raw)[2] | Should -BeExactly $expected3
(Select-String in $testInputFile -Raw)[3] | Should -BeExactly $expected4
(Select-String in $testInputFile -Raw)[4] | Should -BeNullOrEmpty
}
It "Should ignore -Context parameter when -Raw is used." {
$expected = "This is the second line"
Select-String second $testInputFile -Raw -Context 2,2 | Should -BeExactly $expected
}
}
Context "Culture parameter" {
It "Should throw if -Culture parameter is used without -SimpleMatch parameter" {
{ "1" | Select-String -Pattern "hello" -Culture "ru-RU" } | Should -Throw -ErrorId "CannotSpecifyCultureWithoutSimpleMatch,Microsoft.PowerShell.Commands.SelectStringCommand"
}
It "Should accept a culture: '<culture>'" -TestCases: @(
@{ culture = "Ordinal"},
@{ culture = "Invariant"},
@{ culture = "Current"},
@{ culture = "ru-RU"}
) {
param ($culture)
{ "1" | Select-String -Pattern "hello" -Culture $culture -SimpleMatch } | Should -Not -Throw
}
It "Should works if -Culture parameter is a culture name: '<culture>'-'<pattern>'-'CaseSensitive:<casesensitive>'" -TestCases: @(
@{pattern = 'file'; culture = 'tr-TR'; expected = 'file'; casesensitive = $false }
@{pattern = 'fIle'; culture = 'tr-TR'; expected = $null; casesensitive = $false }
@{pattern = 'fIle'; culture = 'tr-TR'; expected = $null; casesensitive = $true }
@{pattern = "f`u{0130}le"; culture = 'tr-TR';expected = 'file'; casesensitive = $false }
@{pattern = 'file'; culture = 'en-US'; expected = 'file'; casesensitive = $false }
@{pattern = 'fIle'; culture = 'en-US'; expected = 'file'; casesensitive = $false }
@{pattern = 'fIle'; culture = 'en-US'; expected = $null; casesensitive = $true }
@{pattern = 'file'; culture = 'Ordinal'; expected = 'file'; casesensitive = $false }
@{pattern = 'fIle'; culture = 'Ordinal'; expected = 'file'; casesensitive = $false }
@{pattern = 'fIle'; culture = 'Ordinal'; expected = $null; casesensitive = $true }
@{pattern = 'file'; culture = 'Invariant'; expected = 'file'; casesensitive = $false }
@{pattern = 'fIle'; culture = 'Invariant'; expected = 'file'; casesensitive = $false }
@{pattern = 'fIle'; culture = 'Invariant'; expected = $null; casesensitive = $true }
@{pattern = 'file'; culture = 'Current'; expected = 'file'; casesensitive = $false }
@{pattern = 'fIle'; culture = 'Current'; expected = 'file'; casesensitive = $false }
@{pattern = 'fIle'; culture = 'Current'; expected = $null; casesensitive = $true }
) {
param ($pattern, $culture, $expected, $casesensitive)
if ($culture -ne 'Current' -or [CultureInfo]::CurrentCulture.Name -ne "tr-TR") {
'file' | Select-String -Pattern $pattern -Culture $culture -SimpleMatch -CaseSensitive:$casesensitive | Should -BeExactly $expected
}
}
}
}