forked from pester/Pester
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Describe.ps1
203 lines (167 loc) · 5.74 KB
/
Describe.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
function Describe {
<#
.SYNOPSIS
Creates a logical group of tests.
.DESCRIPTION
Creates a logical group of tests. All Mocks and TestDrive contents
defined within a Describe block are scoped to that Describe; they
will no longer be present when the Describe block exits. A Describe
block may contain any number of Context and It blocks.
.PARAMETER Name
The name of the test group. This is often an expressive phrase describing
the scenario being tested.
.PARAMETER Fixture
The actual test script. If you are following the AAA pattern (Arrange-Act-Assert),
this typically holds the arrange and act sections. The Asserts will also lie
in this block but are typically nested each in its own It block. Assertions are
typically performed by the Should command within the It blocks.
.PARAMETER Tag
Optional parameter containing an array of strings. When calling Invoke-Pester,
it is possible to specify a -Tag parameter which will only execute Describe blocks
containing the same Tag.
.EXAMPLE
function Add-Numbers($a, $b) {
return $a + $b
}
Describe "Add-Numbers" {
It "adds positive numbers" {
$sum = Add-Numbers 2 3
$sum | Should -Be 5
}
It "adds negative numbers" {
$sum = Add-Numbers (-2) (-2)
$sum | Should -Be (-4)
}
It "adds one negative number to positive number" {
$sum = Add-Numbers (-2) 2
$sum | Should -Be 0
}
It "concatenates strings if given strings" {
$sum = Add-Numbers two three
$sum | Should -Be "twothree"
}
}
.LINK
It
Context
Invoke-Pester
about_Should
about_Mocking
about_TestDrive
#>
param(
[Parameter(Mandatory = $true, Position = 0)]
[string] $Name,
[Alias('Tags')]
[string[]] $Tag=@(),
[Parameter(Position = 1)]
[ValidateNotNull()]
[ScriptBlock] $Fixture = $(Throw "No test script block is provided. (Have you put the open curly brace on the next line?)")
)
if ($null -eq (& $SafeCommands['Get-Variable'] -Name Pester -ValueOnly -ErrorAction $script:IgnoreErrorPreference))
{
# User has executed a test script directly instead of calling Invoke-Pester
$Pester = New-PesterState -Path (& $SafeCommands['Resolve-Path'] .) -TestNameFilter $null -TagFilter @() -SessionState $PSCmdlet.SessionState
$script:mockTable = @{}
}
DescribeImpl @PSBoundParameters -CommandUsed 'Describe' -Pester $Pester -DescribeOutputBlock ${function:Write-Describe} -TestOutputBlock ${function:Write-PesterResult}
}
function DescribeImpl {
param(
[Parameter(Mandatory = $true, Position = 0)]
[string] $Name,
[Alias('Tags')]
$Tag=@(),
[Parameter(Position = 1)]
[ValidateNotNull()]
[ScriptBlock] $Fixture = $(Throw "No test script block is provided. (Have you put the open curly brace on the next line?)"),
[string] $CommandUsed = 'Describe',
$Pester,
[scriptblock] $DescribeOutputBlock,
[scriptblock] $TestOutputBlock,
[switch] $NoTestDrive
)
Assert-DescribeInProgress -CommandName $CommandUsed
if ($Pester.TestGroupStack.Count -eq 2)
{
if($Pester.TestNameFilter -and -not ($Pester.TestNameFilter | & $SafeCommands['Where-Object'] { $Name -like $_ }))
{
#skip this test
return
}
if($Pester.TagFilter -and @(& $SafeCommands['Compare-Object'] $Tag $Pester.TagFilter -IncludeEqual -ExcludeDifferent).count -eq 0) {return}
if($Pester.ExcludeTagFilter -and @(& $SafeCommands['Compare-Object'] $Tag $Pester.ExcludeTagFilter -IncludeEqual -ExcludeDifferent).count -gt 0) {return}
}
else
{
if ($PSBoundParameters.ContainsKey('Tag'))
{
Write-Warning "${CommandUsed} '$Name': Tags are only effective on the outermost test group, for now."
}
}
$Pester.EnterTestGroup($Name, $CommandUsed)
if ($null -ne $DescribeOutputBlock)
{
& $DescribeOutputBlock $Name $CommandUsed
}
$testDriveAdded = $false
try
{
try
{
if (-not $NoTestDrive)
{
if (-not (Test-Path TestDrive:\))
{
New-TestDrive
$testDriveAdded = $true
}
else
{
$TestDriveContent = Get-TestDriveChildItem
}
}
Add-SetupAndTeardown -ScriptBlock $Fixture
Invoke-TestGroupSetupBlocks
do
{
$null = & $Fixture
} until ($true)
}
finally
{
Invoke-TestGroupTeardownBlocks
if (-not $NoTestDrive)
{
if ($testDriveAdded)
{
Remove-TestDrive
}
else
{
Clear-TestDrive -Exclude ($TestDriveContent | & $SafeCommands['Select-Object'] -ExpandProperty FullName)
}
}
}
}
catch
{
$firstStackTraceLine = $_.InvocationInfo.PositionMessage.Trim() -split "$([System.Environment]::NewLine)" | & $SafeCommands['Select-Object'] -First 1
$Pester.AddTestResult("Error occurred in $CommandUsed block", "Failed", $null, $_.Exception.Message, $firstStackTraceLine, $null, $null, $_)
if ($null -ne $TestOutputBlock)
{
& $TestOutputBlock $Pester.TestResult[-1]
}
}
Exit-MockScope
$Pester.LeaveTestGroup($Name, $CommandUsed)
}
# Name is now misleading; rename later. (Many files touched to change this.)
function Assert-DescribeInProgress
{
param ($CommandName)
if ($null -eq $Pester)
{
throw "The $CommandName command may only be used from a Pester test script."
}
}