Examples/Gherkin/VariableScope.Steps.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
When 'I (?:set|initialize) the variable (?<Name>[\w:]+) to "(?<Value>[^"]+)"' {
    param($Name, $Value)

    switch($Name)
    {
        "One" { $One = $Value }
        "Two" { $Two = $Value }
        "Script:Two" { $Script:Two = $Value }
    }
}

Given 'I initialize variables One and (?<Script>.+:)?Two to "Uno" and "Dos"' {
    param($Script)
    $One = "Uno"

    if($Script) {
        $Script:Two = "Dos"
    } else {
        $Two = "Dos"
    }
}


Then 'the variable ([\w:]+) should be "([^"]+)"' {
    param($Name, $Value)

    $Result = switch($Name)
    {
        "One" { $One }
        "Two" { $Two  }
        "Script:Two" { $Script:Two }
    }
    $Result | Should Be $Value
}

Then "the variable ([\w:]+) should not exist" {
    param($Name)

    switch($Name)
    {
        "One" {
            Test-Path Variable:One | Should Be $False
        }
        "Two" {
            Test-Path Variable:Two | Should Be $False
        }
        "Script:Two" {
            Test-Path Variable:Script:Two | Should Be $False
        }
    }
}

BeforeEachFeature {
    Remove-Variable One -ErrorAction SilentlyContinue
    Remove-Variable Two -ErrorAction SilentlyContinue
    Remove-Variable Two -Scope Script -ErrorAction SilentlyContinue
}

# Not using this BACKGROUND Given anymore, we're using a BeforeEachFeature instead
# That way we only clear the variable at the beginning of the test
Given "I ensure variables ([\w:]+) and ([\w:]+) are not set" {
    param(
        [Parameter(ValueFromRemainingArguments=$True)]
        [string[]]$names
    )

    foreach($name in $Names) {
        Remove-Variable -Name $Name -ErrorAction SilentlyContinue
    }
}