Skip to content

Commit 916440d

Browse files
nohwndgaelcolas
authored andcommitted
Add New assertions in Pester
1 parent 2b2a0f2 commit 916440d

7 files changed

+151
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# pipeline syntax
2+
1 | Should-Be -Expected 1 # pass
3+
@(1) | Should-Be -Expected 1 # pass
4+
5+
2 | Should-Be -Expected 1
6+
7+
1 | Should-Be -Expected 1, 2
8+
9+
1, 2 | Should-Be -Expected 1
10+
11+
$null | Should-Be 1
12+
13+
# parameter syntax
14+
Should-Be -Actual 1 -Expected 1 # pass
15+
16+
Should-Be -Actual @(1) -Expected 1
17+
18+
# special cases
19+
'$null' | Should-Be $null
20+
'' | Should-Be 'Jakub'
21+
22+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# boolean
2+
$true | Should-BeTrue # pass
3+
4+
1 | Should-BeTrue
5+
6+
"false" | Should-BeTrue
7+
8+
$null | Should-BeTrue
9+
10+
# vs
11+
# pass
12+
1 | Should-BeTruthy
13+
# pass
14+
1 | Should-Be $true
15+
16+
17+
# string
18+
"abc" | Should-BeString "abc" # pass
19+
"ABC" | Should-BeString "abc" # pass
20+
21+
"ABC" | Should-BeString "abc" -CaseSensitive
22+
23+
" a b c " | Should-BeString "abc" -IgnoreWhitespace # pass
24+
" a b c " | Should-BeString "abc" -TrimWhitespace
25+
26+
1 | Should-BeString "abc"
27+
28+
"abc" | Should-BeLikeString "*bc" # pass
29+
"def" | Should-BeLikeString "*bc"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# pass
2+
@(1) | Should-BeCollection -Expected @(1)
3+
# pass
4+
1 | Should-BeCollection -Expected @(1)
5+
6+
@(1, 2, 3) | Should-BeCollection @(3, 4, 5)
7+
8+
9+
10+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# pass
2+
@(1, 2, 3) | Should-All { $_ -gt 0 }
3+
4+
@(1, 2, 3) | Should-All { $_ -lt 2 }
5+
6+
@(1, 2, 3) | Should-All { $_ | Should-BeLessThan 2 }
7+
8+
# pass
9+
@(1, 2, 3) | Should-Any { $_ | Should-BeLessThan 2 }
10+
11+
@(1, 2, 3) | Should-Any { $_ | Should-BeGreaterThan 4 }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
2+
# What is equivalency?
3+
4+
# false, not the same instance
5+
# even though the shape is the same
6+
([PSCustomObject]@{ Name = 'Jakub' }) -eq ([PSCustomObject]@{ Name = 'Jakub' })
7+
8+
# if there was -equivalent operator then
9+
# true, different instances but same shape
10+
([PSCustomObject]@{ Name = 'Jakub' }) -equivalent ([PSCustomObject]@{ Name = 'Jakub' })
11+
12+
13+
# DEMO 1 - pass
14+
$expected = [PSCustomObject] @{
15+
Name = "Jakub"
16+
Age = 36
17+
Languages = "PowerShell", "C#"
18+
}
19+
20+
$actual = [PSCustomObject] @{
21+
Name = "Jakub"
22+
Age = 36
23+
Languages = "C#", "PowerShell"
24+
}
25+
26+
$actual | Should-BeEquivalent $expected
27+
28+
# DEMO 2 - fail
29+
$expected = [PSCustomObject] @{
30+
Name = "Jakub"
31+
Age = 36
32+
Languages = "PowerShell", "C#", "TypeScript"
33+
34+
DrinksCoffee = $true
35+
}
36+
37+
$actual = [PSCustomObject] @{
38+
Name = "Jakub"
39+
Age = 36
40+
Languages = "PowerShell", "C#"
41+
42+
HasSmallKeyboard = $true
43+
}
44+
45+
$actual | Should-BeEquivalent $expected
46+
47+
# DEMO 3 - pass, exclude paths not on expected
48+
$expected = [PSCustomObject] @{
49+
Name = "Jakub"
50+
Age = 36
51+
Languages = @(
52+
[PSCustomObject]@{ Name = "PowerShell" }
53+
[PSCustomObject]@{ Name = "C#" }
54+
)
55+
}
56+
57+
$actual = [PSCustomObject] @{
58+
Name = "Jakub"
59+
Age = 36
60+
Languages = @(
61+
[PSCustomObject]@{ Name = "PowerShell"; Paradigm = "Scripting" <# <-- #> }
62+
[PSCustomObject]@{ Name = "C#"; Paradigm = "OOP" <# <-- #> }
63+
)
64+
65+
HasSmallKeyboard = $true <# <-- #>
66+
HasBigDisplay = $true <# <-- #>
67+
}
68+
69+
Should-BeEquivalent -Actual $actual -Expected $Expected -ExcludePathsNotOnExpected
70+
71+
72+
73+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
(Get-Item "C:\Windows\System32\notepad.exe").CreationTime |
2+
Should-BeAfter 1week -Ago
3+
4+
5+
{ Start-Sleep -Second 1 }
6+
| Should-BeFasterThan 1s
Binary file not shown.

0 commit comments

Comments
 (0)