Functions/Assertions/Should.Tests.ps1

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
Set-StrictMode -Version Latest

InModuleScope Pester {
    Describe "Parse-ShouldArgs" {
        It "sanitizes assertions functions" {
            $parsedArgs = Parse-ShouldArgs TestFunction
            $parsedArgs.AssertionMethod | Should Be PesterTestFunction
        }

        It "works with strict mode when using 'switch' style tests" {
            Set-StrictMode -Version Latest
            { throw 'Test' } | Should Throw
        }

        Context "for positive assertions" {

            $parsedArgs = Parse-ShouldArgs testMethod, 1

            It "gets the expected value from the 2nd argument" {
                $ParsedArgs.ExpectedValue | Should Be 1
            }

            It "marks the args as a positive assertion" {
                $ParsedArgs.PositiveAssertion | Should Be $true
            }
        }

        Context "for negative assertions" {

            $parsedArgs = Parse-ShouldArgs Not, testMethod, 1

            It "gets the expected value from the third argument" {
                $ParsedArgs.ExpectedValue | Should Be 1
            }

            It "marks the args as a negative assertion" {
                $ParsedArgs.PositiveAssertion | Should Be $false
            }
        }

        Context "for the throw assertion" {

            $parsedArgs = Parse-ShouldArgs Throw

            It "translates the Throw assertion to PesterThrow" {
                $ParsedArgs.AssertionMethod | Should Be PesterThrow
            }

        }
    }

    Describe "Get-TestResult" {
        Context "for positive assertions" {
            function PesterTest { return $true }
            $shouldArgs = Parse-ShouldArgs Test

            It "returns false if the test returns true" {
                Get-TestResult $shouldArgs | Should Be $false
            }
        }

        Context "for negative assertions" {
            function PesterTest { return $false }
            $shouldArgs = Parse-ShouldArgs Not, Test

            It "returns false if the test returns false" {
                Get-TestResult $shouldArgs | Should Be $false
            }
        }
    }

    Describe "Get-FailureMessage" {
        Context "for positive assertions" {
            function PesterTestFailureMessage($v, $e) { return "slime $e $v" }
            $shouldArgs = Parse-ShouldArgs Test, 1

            It "should return the positive assertion failure message" {
                Get-FailureMessage $shouldArgs 2 | Should Be "slime 1 2"
            }
        }

        Context "for negative assertions" {
            function NotPesterTestFailureMessage($v, $e) { return "not slime $e $v" }
            $shouldArgs = Parse-ShouldArgs Not, Test, 1

            It "should return the negative assertion failure message" {
              Get-FailureMessage $shouldArgs 2 | Should Be "not slime 1 2"
            }
        }

    }

    Describe -Tag "Acceptance" "Should" {
        It "can use the Be assertion" {
            1 | Should Be 1
        }

        It "can use the Not Be assertion" {
            1 | Should Not Be 2
        }

        It "can use the BeNullOrEmpty assertion" {
            $null | Should BeNullOrEmpty
            @()   | Should BeNullOrEmpty
            ""    | Should BeNullOrEmpty
        }

        It "can use the Not BeNullOrEmpty assertion" {
            @("foo") | Should Not BeNullOrEmpty
            "foo"    | Should Not BeNullOrEmpty
            " "    | Should Not BeNullOrEmpty
            @(1,2,3) | Should Not BeNullOrEmpty
            12345    | Should Not BeNullOrEmpty
            $item1 = New-Object PSObject -Property @{Id=1; Name="foo"}
            $item2 = New-Object PSObject -Property @{Id=2; Name="bar"}
            @($item1, $item2) | Should Not BeNullOrEmpty
        }

        It "can handle exception thrown assertions" {
            { foo } | Should Throw
        }

        It "can handle exception should not be thrown assertions" {
            { $foo = 1 } | Should Not Throw
        }

        It "can handle Exist assertion" {
            $TestDrive | Should Exist
        }

        It "can handle the Match assertion" {
            "abcd1234" | Should Match "d1"
        }

        It "can test for file contents" {
            Setup -File "test.foo" "expected text"
            "$TestDrive\test.foo" | Should Contain "expected text"
        }

        It "ensures all assertion functions provide failure messages" {
            $assertionFunctions = @("PesterBe", "PesterThrow", "PesterBeNullOrEmpty", "PesterExist",
                "PesterMatch", "PesterContain")
            $assertionFunctions | % {
                "function:$($_)FailureMessage" | Should Exist
                "function:Not$($_)FailureMessage" | Should Exist
            }
        }

        # TODO understand the purpose of this test, perhaps some better wording
        It "can process functions with empty output as input" {
            function ReturnNothing {}

            # TODO figure out why this is the case
            if ($PSVersionTable.PSVersion -eq "2.0") {
                { $(ReturnNothing) | Should Not BeNullOrEmpty } | Should Not Throw
            } else {
                { $(ReturnNothing) | Should Not BeNullOrEmpty } | Should Throw
            }
        }

        It 'All failure message functions are present' {
            $assertionFunctions = Get-Command -CommandType Function -Module Pester |
                                  Select-Object -ExpandProperty Name |
                                  Where-Object { $_ -like 'Pester*' -and $_ -notlike '*FailureMessage' }

            $missingFunctions = @(
                foreach ($assertionFunction in $assertionFunctions)
                {
                    $positiveFailureMessage = "${assertionFunction}FailureMessage"
                    $negativeFailureMessage = "Not${assertionFunction}FailureMessage"

                    if (-not (Test-Path function:$positiveFailureMessage)) { $positiveFailureMessage }
                    if (-not (Test-Path function:$negativeFailureMessage)) { $negativeFailureMessage }
                }
            )

            [string]$missingFunctions | Should BeNullOrEmpty
        }
    }
}