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
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.
        .Link
            AfterEachFeature
            BeforeEachScenario
            AfterEachScenario
    #>

    [CmdletBinding(DefaultParameterSetName="All")]
    param(
        # Optional tags. If set, this hook only runs for features with matching tags
        [Parameter(Mandatory=$True, Position=0, ParameterSetName="Tags")]
        [String[]]$Tags = @(),

        # The ScriptBlock to run for the hook
        [Parameter(Mandatory=$True, Position=1, ParameterSetName="Tags")]
        [Parameter(Mandatory=$True, Position=0, ParameterSetName="All")]
        [ScriptBlock]$Script
    )

    ${Script:GherkinHooks}.BeforeEachFeature += @( @{ Tags = $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.
        .Link
            BeforeEachFeature
            BeforeEachScenario
            AfterEachScenario
    #>

    [CmdletBinding(DefaultParameterSetName="All")]
    param(
        # Optional tags. If set, this hook only runs for features with matching tags
        [Parameter(Mandatory=$True, Position=0, ParameterSetName="Tags")]
        [String[]]$Tags = @(),

        # The ScriptBlock to run for the hook
        [Parameter(Mandatory=$True, Position=1, ParameterSetName="Tags")]
        [Parameter(Mandatory=$True, Position=0, ParameterSetName="All")]
        [ScriptBlock]$Script
    )

    ${Script:GherkinHooks}.AfterEachFeature += @( @{ Tags = $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.
        .Link
            AfterEachFeature
            BeforeEachScenario
            AfterEachScenario
    #>

    [CmdletBinding(DefaultParameterSetName="All")]
    param(
        # Optional tags. If set, this hook only runs for features with matching tags
        [Parameter(Mandatory=$True, Position=0, ParameterSetName="Tags")]
        [String[]]$Tags = @(),

        # The ScriptBlock to run for the hook
        [Parameter(Mandatory=$True, Position=1, ParameterSetName="Tags")]
        [Parameter(Mandatory=$True, Position=0, ParameterSetName="All")]
        [ScriptBlock]$Script
    )

    ${Script:GherkinHooks}.BeforeEachScenario += @( @{ Tags = $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.
        .Link
            BeforeEachFeature
            BeforeEachScenario
            AfterEachScenario
    #>

    [CmdletBinding(DefaultParameterSetName="All")]
    param(
        # Optional tags. If set, this hook only runs for features with matching tags
        [Parameter(Mandatory=$True, Position=0, ParameterSetName="Tags")]
        [String[]]$Tags = @(),

        # The ScriptBlock to run for the hook
        [Parameter(Mandatory=$True, Position=1, ParameterSetName="Tags")]
        [Parameter(Mandatory=$True, Position=0, ParameterSetName="All")]
        [ScriptBlock]$Script
    )

    ${Script:GherkinHooks}.AfterEachScenario += @( @{ Tags = $Tags; Script = $Script } )
}