File size: 3,479 Bytes
8c763fb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
Describe 'Break statements with classes' -Tags "CI" {

    function Get-Errors([string]$sourceCode) {
        $tokens = $null
        $errors = $null
        $ast = [System.Management.Automation.Language.Parser]::ParseInput($sourceCode, [ref] $tokens, [ref] $errors)
        return $errors
    }

    Context 'break is inside a class method' {
        It 'reports parse error for break on non-existing label' {
            $errors = Get-Errors @'
class A
{
    static [int] foo()
    {
        while (1) { break some_label }
        return 1
    }
}
'@
            $errors.Count | Should -Be 1
            $errors[0].ErrorId | Should -BeExactly 'LabelNotFound'
        }

        It 'reports parse error for break outside of loop' {
            $errors = Get-Errors @'
class A
{
    static [int] foo()
    {
        break some_label
        return 1
    }
}
'@
            $errors.Count | Should -Be 1
            $errors[0].ErrorId | Should -BeExactly 'LabelNotFound'
        }

        It 'work fine, when break is legit' {
            class C
            {
                static [int] foo()
                {
                    foreach ($i in 101..102) {
                        break
                    }
                    return $i
                }
            }
            [C]::foo() | Should -Be 101
        }
    }

    Context 'continue inside a class method' {
        It 'reports parse error for continue on non-existing label' {
            $errors = Get-Errors @'
class A
{
    static [int] foo()
    {
        while (1) { continue some_label }
        return 1
    }
}
'@
            $errors.Count | Should -Be 1
            $errors[0].ErrorId | Should -BeExactly 'LabelNotFound'
        }
    }

    Context 'break is in called function'  {
        It 'doesn''t terminate caller method' -Skip {

            function ImBreak() {
                break
            }

            class C
            {
                static [int] getInt()
                {
                    ImBreak
                    return 123
                }
            }

            $canary = $false
            try {
                [C]::getInt() | Should -Be 123
                $canary = $true
            } finally {
                $canary | Should -BeTrue
            }
        }

        It 'doesn''t allow goto outside of function with break' -Skip {

            function ImBreak() {
                break label1
            }

            class C
            {
                static [int] getInt()
                {
                    $count = 123
                    :label1
                    foreach ($i in 0..3) {
                        foreach ($i in 0..3) {
                            ImBreak
                            $count++
                        }
                    }
                    return $count
                }
            }

            $canary = $false
            try {
                [C]::getInt() | Should -Be (123 + 4*4)
                $canary = $true
            } finally {
                $canary | Should -BeTrue
            }
        }
    }

    Context 'no classes involved' {

         It 'doesn''t report parse error for non-existing label' {
            $errors = Get-Errors @'
function foo()
{
    while (1) { break some_label }
    while (1) { continue another_label }
    return 1
}
'@
            $errors.Count | Should -Be 0
        }

    }
}