File size: 4,106 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 | # Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
Describe "Error position Tests" -Tags "CI" {
BeforeAll {
$switch_condition_script = @'
$test = 1
switch ($null[0]) {
"a" {};
}
'@
$for_expression_initializer_script = @'
$test = 1
for ($null[0];
$test -gt 1;) { }
'@
$for_pipeline_initializer_script = @'
$test = 1
for (Test-Path $null -ErrorAction Stop;
$test -gt 1;) { }
'@
$for_expression_condition_script = @'
$test = 1
for (;$null[0];)
{ }
'@
$for_pipeline_condition_script = @'
$test = 1
for (;Test-Path $null -ErrorAction Stop;)
{ }
'@
$do_while_expression_condition_script = @'
$test = 1
do {}
while ($null[0])
'@
$do_while_pipeline_condition_script = @'
$test = 1
do {}
while (Test-Path $null -ErrorAction Stop)
'@
$do_until_expression_condition_script = @'
$test = 1
do {}
until ($null[0])
'@
$do_until_pipeline_condition_script = @'
$test = 1
do {}
until (Test-Path $null -ErrorAction Stop)
'@
$testCases = @(
@{ Name = "switch condition"; FileName = "SwitchError2.ps1"; Script = $switch_condition_script; MatchText = "SwitchError2.ps1: line 2" }
@{ Name = "for statement expression initializer"; FileName = "ForError1.ps1"; Script = $for_expression_initializer_script; MatchText = "ForError1.ps1: line 2" }
@{ Name = "for statement pipeline initializer"; FileName = "ForError2.ps1"; Script = $for_pipeline_initializer_script; MatchText = "ForError2.ps1: line 2" }
@{ Name = "for statement expression condition"; FileName = "ForError3.ps1"; Script = $for_expression_condition_script; MatchText = "ForError3.ps1: line 2" }
@{ Name = "for statement pipeline condition"; FileName = "ForError4.ps1"; Script = $for_pipeline_condition_script; MatchText = "ForError4.ps1: line 2" }
@{ Name = "do-while statement expression condition"; FileName = "DoWhileError1.ps1"; Script = $do_while_expression_condition_script; MatchText = "DoWhileError1.ps1: line 3" }
@{ Name = "do-while statement pipeline condition"; FileName = "DoWhileError2.ps1"; Script = $do_while_pipeline_condition_script; MatchText = "DoWhileError2.ps1: line 3" }
@{ Name = "do-until statement expression condition"; FileName = "DoUntilError1.ps1"; Script = $do_until_expression_condition_script; MatchText = "DoUntilError1.ps1: line 3" }
@{ Name = "do-until statement pipeline condition"; FileName = "DoUntilError2.ps1"; Script = $do_until_pipeline_condition_script; MatchText = "DoUntilError2.ps1: line 3" }
)
}
It "<Name> evaluation failure should report correct error position" -TestCases $testCases {
param($FileName, $Script, $MatchText)
$testFile = Join-Path $TestDrive $FileName
Set-Content -Path $testFile -Encoding Ascii -Value $Script
try { & $testFile } catch { $errorRecord = $_ }
$errorRecord | Should -Not -BeNullOrEmpty
$errorRecord.ScriptStackTrace | Should -Match $MatchText
}
It "switch condition MoveNext failure should report correct error position" {
$code = @'
using System;
using System.Collections.Generic;
namespace SwitchTest
{
public class Test
{
public static IEnumerable<string> GetName()
{
yield return "Hello world";
throw new ArgumentException();
}
}
}
'@
$testFile = Join-Path $TestDrive "SwitchError1.ps1"
Set-Content -Path $testFile -Encoding Ascii -Value @'
$test = 1
$enumerable = [SwitchTest.Test]::GetName()
switch ($enumerable) {
"hello world" { $test = 1; $_ }
"Yay" { $test = 2; $_ }
}
'@
if (-not ("SwitchTest.Test" -as [type])) {
Add-Type -TypeDefinition $code
}
try { & $testFile > $null } catch { $errorRecord = $_ }
$errorRecord | Should -Not -BeNullOrEmpty
$errorRecord.ScriptStackTrace | Should -Match "SwitchError1.ps1: line 3"
}
}
|