2016-07-25 23:01:41 +02:00
|
|
|
Describe 'Break statements with classes' -Tags "CI" {
|
2017-01-16 22:31:14 +01:00
|
|
|
|
2016-07-19 22:52:03 +02:00
|
|
|
function Get-Errors([string]$sourceCode) {
|
2017-01-16 22:31:14 +01:00
|
|
|
$tokens = $null
|
2016-07-19 22:52:03 +02:00
|
|
|
$errors = $null
|
|
|
|
$ast = [System.Management.Automation.Language.Parser]::ParseInput($sourceCode, [ref] $tokens, [ref] $errors)
|
|
|
|
return $errors
|
|
|
|
}
|
2017-01-16 22:31:14 +01:00
|
|
|
|
2016-07-19 22:52:03 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
2017-01-16 22:31:14 +01:00
|
|
|
'@
|
2016-07-19 22:52:03 +02:00
|
|
|
$errors.Count | Should be 1
|
|
|
|
$errors[0].ErrorId | Should be 'LabelNotFound'
|
|
|
|
}
|
|
|
|
|
|
|
|
It 'reports parse error for break outside of loop' {
|
|
|
|
$errors = Get-Errors @'
|
|
|
|
class A
|
|
|
|
{
|
|
|
|
static [int] foo()
|
|
|
|
{
|
|
|
|
break some_label
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
}
|
2017-01-16 22:31:14 +01:00
|
|
|
'@
|
2016-07-19 22:52:03 +02:00
|
|
|
$errors.Count | Should be 1
|
|
|
|
$errors[0].ErrorId | Should be 'LabelNotFound'
|
|
|
|
}
|
|
|
|
|
2016-08-25 19:49:32 +02:00
|
|
|
It 'work fine, when break is legit' {
|
2016-07-19 22:52:03 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
2017-01-16 22:31:14 +01:00
|
|
|
'@
|
2016-07-19 22:52:03 +02:00
|
|
|
$errors.Count | Should be 1
|
|
|
|
$errors[0].ErrorId | Should be 'LabelNotFound'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Context 'break is in called function' {
|
|
|
|
It 'doesn''t terminate caller method' -Skip {
|
2017-01-16 22:31:14 +01:00
|
|
|
|
2016-07-19 22:52:03 +02:00
|
|
|
function ImBreak() {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
class C
|
|
|
|
{
|
|
|
|
static [int] getInt()
|
|
|
|
{
|
|
|
|
ImBreak
|
|
|
|
return 123
|
|
|
|
}
|
|
|
|
}
|
2017-01-16 22:31:14 +01:00
|
|
|
|
2016-07-19 22:52:03 +02:00
|
|
|
$canary = $false
|
|
|
|
try {
|
|
|
|
[C]::getInt() | Should Be 123
|
|
|
|
$canary = $true
|
|
|
|
} finally {
|
|
|
|
$canary | Should be $true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
It 'doesn''t allow goto outside of function with break' -Skip {
|
2017-01-16 22:31:14 +01:00
|
|
|
|
2016-07-19 22:52:03 +02:00
|
|
|
function ImBreak() {
|
|
|
|
break label1
|
|
|
|
}
|
|
|
|
|
|
|
|
class C
|
|
|
|
{
|
|
|
|
static [int] getInt()
|
|
|
|
{
|
|
|
|
$count = 123
|
|
|
|
:label1
|
2017-01-16 22:31:14 +01:00
|
|
|
foreach ($i in 0..3) {
|
|
|
|
foreach ($i in 0..3) {
|
2016-07-19 22:52:03 +02:00
|
|
|
ImBreak
|
|
|
|
$count++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $count
|
|
|
|
}
|
|
|
|
}
|
2017-01-16 22:31:14 +01:00
|
|
|
|
2016-07-19 22:52:03 +02:00
|
|
|
$canary = $false
|
|
|
|
try {
|
|
|
|
[C]::getInt() | Should Be (123 + 4*4)
|
|
|
|
$canary = $true
|
|
|
|
} finally {
|
|
|
|
$canary | Should be $true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Context 'no classes involved' {
|
2017-01-16 22:31:14 +01:00
|
|
|
|
2016-07-19 22:52:03 +02:00
|
|
|
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
|
|
|
|
}
|
2017-01-16 22:31:14 +01:00
|
|
|
'@
|
2016-07-19 22:52:03 +02:00
|
|
|
$errors.Count | Should be 0
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|