forked from jonwagner/PShould
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PShould.psm1
517 lines (444 loc) · 12.1 KB
/
PShould.psm1
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
<#
PShould - Copyright(c) 2013 - Jon Wagner
See https://github.com/jonwagner/PShould for licensing and other information.
Version: $version$
Changeset: $changeset$
#>
# operators for the be case
$operators = @{
'GreaterThan' = '-gt'
'-gt' = '-gt'
'gt' = '-gt'
'>' = '-gt'
'LessThan' = '-lt'
'-lt' = '-lt'
'lt' = '-lt'
'<' = '-lt'
'GreaterThanOrEqualTo' = '-ge'
'-gte' = '-ge'
'-ge' = '-ge'
'gte' = '-ge'
'ge' = '-ge'
'>=' = '-ge'
'LessThanOrEqualTo' = '-le'
'-lte' = '-le'
'-le' = '-le'
'lte' = '-le'
'le' = '-le'
'<=' = '-le'
'EqualTo' = '-eq'
'-eq' = '-eq'
'eq' = '-eq'
'=' = '-eq'
'NotEqualTo' = '-ne'
'-ne' = '-ne'
'ne' = '-ne'
'!=' = '-ne'
'in' = '-in'
'-in' = '-in'
'notin' = '-notin'
'-notin' = '-notin'
}
$comparators = @(
'Be',
'Equal',
'Throw',
'Match',
'Contain',
'Count',
'Exist',
'ContainContent'
)
<#
.Synopsis
A fluent syntax to assert values.
.Description
A fluent syntax to assert values. If an assertion fails, an exception is thrown.
Syntax:
input | Should [not] [comparison] [expected] [and | -test]
The comparisons are:
Be - the input is tested against the expected value. See Get-Help ShouldBe for details.
Equal - the input is tested against the expected value. Arrays are tested for element equality.
Contain - the input is a collection, and must contain the given value.
Match - the input is a string, and must contain the given pattern.
Count - the input is a collection, and must contain the given number of elements.
Throw - the input is a script block, which is executed and the exception is tested.
Exist - the input is a Path, and Test-Path is called to check its existence.
ContainContent - the input is a Path, whose Get-Content is must contain a pattern.
[ScriptBlock] - the scriptblock is executed. The first parameter is the input.
If the command ends with 'and', then the inputs are copied to the output stream
for further testing or operation.
If the command ends with '-test', then the result of the test is output to the stream,
and an exception is not thrown.
For details on the individual operations, use Get-Help Should*
.Example
1 | Should Be 1
Tests that 1 is equal to 1.
.Example
5 | Should Not Be GTE 6
Tests that 5 is not greater than or equal to 6.
.Example
@(a,b,c) | Should Count 3 and | Should Contain b
Tests that the array is 3 elements long and contains the letter b.
.Example
{ Scary-Function } | Should Throw "bad mojo"
Tests that Scary-Function throws an exception containing "bad mojo".
.Example
5 | Should { param($value) $value -gt 4 }
Tests that the input passes the given function.
.Example
"abc" | Should Match '^ab" -test
Tests that "abc" starts with "ab" and returns the result rather than throwing an exception.f
.Link
Should*
#>
function Should {
# we are going to get a set of fluent arguments like "Not Be $null"
# parse them into a set of values
$i = 0
$not = $false
$comparator = $args[$i++]
$savedinput = @($input)
# handle not
if (('not' -eq $comparator) -or ('!' -eq $comparator)) {
$not = $true
$comparator = $args[$i++]
}
if ($comparator -match '^!') {
$not = $true
$comparator = $comparator -replace '^!',''
}
# handle be operators
if ($operators.Keys -contains $args[$i]) {
$operator = $args[$i++]
}
# call the comparator
if ($comparator -is [scriptblock]) {
# handle scriptblock tests
$result = & $comparator -Value:$savedinput
}
elseif ($comparators -contains $comparator) {
# handle value
$value = $args[$i++]
# call the assertion by name
$result = & "Should$comparator" $savedinput $value $operator
}
else {
throw "Comparison $comparator is not supported"
}
# handle the result
if ($not) {
$result = !$result
}
# if the should ends in 'and', then emit the input for chaining
if ($args[$i] -eq 'and') {
$savedinput
}
elseif ($args[$i] -eq '-test') {
# if -test is specified, then just output $true/$false
if ($result) {
$true
}
else {
$false
}
}
else {
if (!$result) {
if ($operator) { $operator += ' ' }
if ($not) { $not = 'not ' } else { $not = '' }
throw "Expected that (actual) ($savedinput) $not$comparator $operator($value)"
}
}
}
<#
.Synopsis
Tests whether two items are equal, with additional options.
.Description
Tests whether two items are equal, with additional options.
.Parameter Actual
The actual result.
.Parameter Expected
The expected value, or one of the following values:
Null - $null
Blank - ''
Empty - an empty array
.Parameter Operator
A comparison operator to use. Default is -eq.
This can be the PowerShell operators -eq -ne -gt -gte -lt -lte,
or the more readable versions GreaterThan, lt, ne, !=, =, etc.
.Example
ShouldBe 5 4 gt
Checks whether the actual value 5 is greater than the expected value 4.
.Example
7 | Should Not Be Null
Tests that the value is not null
.Example
$array.Count | Should Be GreaterThan 1
Tests that the array count is greater than 1
.Link
Should
#>
function ShouldBe {
param (
$Actual,
$Expected,
$Operator
)
# handle null by checking the value
if ('Null' -eq $Expected) {
return $($Actual) -eq $null
}
# handle blank by checking the string value
if ('Blank' -eq $Expected) {
[string] $s = $($Actual)
return $s -eq ''
}
# handle empty by checking the length of the array
if ('Empty' -eq $Expected) {
return @($Actual).Length -eq 0
}
# map the operator
if (!$Operator) {
$Operator = '-eq'
} else {
$Operator = $operators[$Operator]
}
# evaluate equality or another operator
if ($Operator -eq '-eq') {
return ShouldEqualEx $Actual $Expected
}
elseif ($Operator -in ('-in', '-notin')) {
# for in/notin, make sure all of the items are in/not in the expected array
foreach ($item in $Actual) {
if (!("`$item $Operator `$Expected" | iex)) {
return $false
}
}
return $true
}
else {
return "`$Actual $Operator `$Expected" | iex
}
}
<#
.Synopsis
Tests whether two items are equal.
.Description
Tests whether two items are equal.
.Parameter Actual
The actual result.
.Parameter Expected
The expected value.
.Example
ShouldEqual 1 1
Checks whether the actual value 1 is equal to the expected value 1.
.Example
1 | Should Equal 1
Checks whether the actual value 1 is equal to the expected value 1.
.Link
Should
#>
function ShouldEqual {
param (
$Actual,
$Expected
)
return ShouldEqualEx $Actual $Expected
}
# Internal function to test equality. Handles array equality by testing the equality of elements.
function ShouldEqualEx {
param (
$Actual,
$Expected
)
# array equality
if (($Actual -is [System.Array]) -and ($Expected -is [System.Array])) {
if ($Actual.Length -ne $Expected.Length) {
return $false
}
for($i = 0; $i -lt $actual.Length; $i++) {
if ($Actual[$i] -ne $Expected[$i]) {
return $false
}
}
return $true
}
# hashtable equality
if (($($Actual) -is [Hashtable]) -and ($Expected -is [Hashtable])) {
$actualHashtable = $($Actual)
if ($actualHashtable.Count -ne $Expected.Count) {
return $false
}
foreach ($key in $Expected.Keys) {
if ($actualHashtable[$key] -ne $Expected[$key]) {
return $false
}
}
return $true
}
return $Expected -eq $($Actual)
}
<#
.Synopsis
Tests whether a collection contains a given element.
.Description
Tests whether a collection contains a given element.
.Parameter Collection
The collection to test.
.Parameter Element
The expected element.
.Example
ShouldContain @(a,b,c) 'b'
Checks whether the given array contains the letter b.
.Example
@(a,b,c) | Should Contain 'b'
Checks whether the given array contains the letter b.
.Link
Should
#>
function ShouldContain {
param (
$Collection,
$Element
)
return $Collection -contains $Element
}
<#
.Synopsis
Tests whether a string matches a given pattern.
.Description
Tests whether a string matches a given pattern.
.Parameter String
The string to test.
.Parameter Match
The pattern to match.
.Example
ShouldMatch "Hi, Bob!" "Bob?$"
Checks whether the given string ends with Bob and another character.
.Example
"Hi, Bob!" | Should Match "Bob?$"
Checks whether the given string ends with Bob and another character.
.Link
Should
#>
function ShouldMatch {
param (
$String,
$Match
)
return $($String) -match $Match
}
<#
.Synopsis
Tests whether a collection has the given number of elements.
.Description
Tests whether a collection has the given number of elements.
.Parameter Collection
The collection to test.
.Parameter Count
The expected number of elements.
.Example
ShouldCount @(a,b,c) 3
Checks whether the given array contain 3 elements.
.Example
@(a,b,c) | Should Count 3
Checks whether the given array contain 3 elements.
.Link
Should
#>
function ShouldCount {
param (
$Collection,
$Count
)
return @($Collection).Count -eq $Count
}
<#
.Synopsis
Tests whether a scriptblock throws.
.Description
Tests whether a scriptblock throws. Optionally, this can also test that
the exception matches a given pattern.
.Parameter Script
The script block to test.
.Parameter Match
If specified, tests whether the thrown exception matches the pattern.
If Match is "-any", then any exception is matched.
.Example
ShouldThrow { throw "foo" }
Tests whether the given block throws.
.Example
{ throw "An error" } | Should Throw "Error"
Tests whether the given block throws an error that contains error
.Link
Should
#>
function ShouldThrow {
param (
$Script,
$Match
)
try {
# execute the script and eat the results
& $($Script) | Out-Null
return $false
}
catch {
if (!$Match -or ($Match -eq '-any')) {
return $true
}
return $_ -match $Match
}
}
<#
.Synopsis
Tests whether a file exists.
.Description
Tests whether a file exists. Internally, this calls Test-Path.
.Parameter Path
The name of the file to test.
.Example
ShouldExist 'PShould.ps1'
Tests whether the file PShould.ps1 exists.
.Example
'PShould.ps1' | Should Exist
Tests whether the file PShould.ps1 exists.
.Link
Should
#>
function ShouldExist {
param (
$Path
)
return Test-Path $Path
}
<#
.Synopsis
Tests whether a file exists and contains the given content.
.Description
Tests whether a file exists and contains the given content.
The content is a regex match.
.Parameter Path
The name of the file to test.
.Parameter Match
The regex match to check for.
.Example
ShouldContainContent 'PShould.ps1' '^function'
Tests whether the file PShould.ps1 contains the word function at the beginning of a line.
.Example
'PShould.ps1' | Should ContainContent '^function'
Tests whether the file PShould.ps1 contains the word function at the beginning of a line.
.Link
Should
#>
function ShouldContainContent {
param (
$Path,
$Match
)
return ((Get-Content $Path) -match $Match)
}
# export all of the functions so we can see help on all of them
Export-ModuleMember Should, ShouldBe, ShouldEqual, ShouldContain, ShouldMatch, ShouldCount,
ShouldThrow, ShouldExist, ShouldContainContent