Functions/GherkinHook.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
function BeforeEachFeature {
    <#
        .SYNOPSIS
        Defines a ScriptBlock hook to run before each feature to set up the test environment
 
        .DESCRIPTION
        BeforeEachFeature hooks are run before each feature that is in (or above) the folder where the hook is defined.
 
        This is a convenience method, provided because unlike traditional RSpec Pester,
        there is not a simple test script where you can put setup and clean up.
 
        .PARAMETER Tags
        Optional tags. If set, this hook only runs for features with matching tags
 
        .PARAMETER Script
        The ScriptBlock to run for the hook
 
        .LINK
        AfterEachFeature
        BeforeEachScenario
        AfterEachScenario
    #>

    [CmdletBinding(DefaultParameterSetName="All")]
    param(

        [Parameter(Mandatory=$True, Position=0, ParameterSetName="Tags")]
        [String[]]$Tags = @(),

        [Parameter(Mandatory=$True, Position=1, ParameterSetName="Tags")]
        [Parameter(Mandatory=$True, Position=0, ParameterSetName="All")]
        [ScriptBlock]$Script
    )

    # This shouldn't be necessary, but PowerShell 2 is BAF
    if ($PSCmdlet.ParameterSetName -eq "Tags") {
        ${Script:GherkinHooks}.BeforeEachFeature += @( @{ Tags = $Tags; Script = $Script } )
    } else {
        ${Script:GherkinHooks}.BeforeEachFeature += @( @{ Tags = @(); Script = $Script } )
    }
}

function AfterEachFeature {
    <#
        .SYNOPSIS
        Defines a ScriptBlock hook to run at the very end of a test run
 
        .DESCRIPTION
        AfterEachFeature hooks are run after each feature that is in (or above) the folder where the hook is defined.
 
        This is a convenience method, provided because unlike traditional RSpec Pester,
        there is not a simple test script where you can put setup and clean up.
 
        .PARAMETER Tags
        Optional tags. If set, this hook only runs for features with matching tags.
 
        .PARAMETER Script
        The ScriptBlock to run for the hook
 
        .LINK
            BeforeEachFeature
            BeforeEachScenario
            AfterEachScenario
    #>

    [CmdletBinding(DefaultParameterSetName="All")]
    param(

        [Parameter(Mandatory=$True, Position=0, ParameterSetName="Tags")]
        [String[]]$Tags = @(),

        [Parameter(Mandatory=$True, Position=1, ParameterSetName="Tags")]
        [Parameter(Mandatory=$True, Position=0, ParameterSetName="All")]
        [ScriptBlock]$Script
    )

    # This shouldn't be necessary, but PowerShell 2 is BAF
    if ($PSCmdlet.ParameterSetName -eq "Tags") {
        ${Script:GherkinHooks}.AfterEachFeature += @( @{ Tags = $Tags; Script = $Script } )
    } else {
        ${Script:GherkinHooks}.AfterEachFeature += @( @{ Tags = @(); Script = $Script } )
    }
}

function BeforeEachScenario {
    <#
        .SYNOPSIS
        Defines a ScriptBlock hook to run before each scenario to set up the test environment
 
        .DESCRIPTION
        BeforeEachScenario hooks are run before each scenario that is in (or above) the folder where the hook is defined.
 
        You should not normally need this, because it overlaps significantly with the "Background" feature in the gherkin language.
 
        This is a convenience method, provided because unlike traditional RSpec Pester,
        there is not a simple test script where you can put setup and clean up.
 
        .PARAMETER Tags
        Optional tags. If set, this hook only runs for features with matching tags
 
        .PARAMETER Script
        The ScriptBlock to run for the hook
 
        .LINK
        AfterEachFeature
        BeforeEachScenario
        AfterEachScenario
    #>

    [CmdletBinding(DefaultParameterSetName="All")]
    param(

        [Parameter(Mandatory=$True, Position=0, ParameterSetName="Tags")]
        [String[]]$Tags = @(),

        [Parameter(Mandatory=$True, Position=1, ParameterSetName="Tags")]
        [Parameter(Mandatory=$True, Position=0, ParameterSetName="All")]
        [ScriptBlock]$Script
    )

    # This shouldn't be necessary, but PowerShell 2 is BAF
    if ($PSCmdlet.ParameterSetName -eq "Tags") {
        ${Script:GherkinHooks}.BeforeEachScenario += @( @{ Tags = $Tags; Script = $Script } )
    } else {
        ${Script:GherkinHooks}.BeforeEachScenario += @( @{ Tags = @(); Script = $Script } )
    }
}

function AfterEachScenario {
    <#
        .SYNOPSIS
        Defines a ScriptBlock hook to run after each scenario to set up the test environment
 
        .DESCRIPTION
        AfterEachScenario hooks are run after each Scenario that is in (or above) the folder where the hook is defined.
 
        This is a convenience method, provided because unlike traditional RSpec Pester,
        there is not a simple test script where you can put setup and clean up.
 
        .PARAMETER Tags
        Optional tags. If set, this hook only runs for features with matching tags
 
        .PARAMETER Script
        The ScriptBlock to run for the hook
 
        .LINK
            BeforeEachFeature
            BeforeEachScenario
            AfterEachScenario
    #>

    [CmdletBinding(DefaultParameterSetName="All")]
    param(
        [Parameter(Mandatory=$True, Position=0, ParameterSetName="Tags")]
        [String[]]$Tags = @(),

        [Parameter(Mandatory=$True, Position=1, ParameterSetName="Tags")]
        [Parameter(Mandatory=$True, Position=0, ParameterSetName="All")]
        [ScriptBlock]$Script
    )

    # This shouldn't be necessary, but PowerShell 2 is BAF
    if($PSCmdlet.ParameterSetName -eq "Tags") {
        ${Script:GherkinHooks}.AfterEachScenario += @( @{ Tags = $Tags; Script = $Script } )
    } else {
        ${Script:GherkinHooks}.AfterEachScenario += @( @{ Tags = @(); Script = $Script } )
    }
}