PowerShell/test/powershell/Language/Scripting/Trap.Tests.ps1
Dongbo Wang bbf1766ac7 Fix try/catch to choose the more specific exception handler (#2429)
* Refactor 'FindMatchingHandler' to get rid of recursion
Search state can be saved and used throughout the searching process without the recursion.

* Rank the exception types based on how specific they are

* Refactor code to find handler based on the rank of given handlers
2016-12-09 12:37:08 -08:00

29 lines
1.3 KiB
PowerShell

Describe "Test trap" -Tags "CI" {
Context "Trap with flow control" {
It "Line after exception should NOT be continued when it's from a nested script block" {
$a = . {trap {"trapped"; continue;}; . {"hello"; throw "exception"; "world"}}
$a.Length | Should Be 2
$a -join "," | Should Be "hello,trapped"
}
It "Line after exception should NOT be continued and both inner and outter traps should be triggered" {
$a = . {trap {"outer trap"; continue;}; . {trap {"inner trap"; break;}; "hello"; throw "exception"; "world"}}
$a.Length | Should Be 3
$a -join "," | Should Be "hello,inner trap,outer trap"
}
It "Line after exception should be invoked after continue" {
$a = . {trap {"outer trap"; continue;} "hello"; throw "exception"; "world"}
$a.Length | Should Be 3
$a -join "," | Should Be "hello,outer trap,world"
}
It "Line after exception should NOT be invoked and inner trap should not be triggered" {
$a = . {trap {"outer trap"; continue;}; . {trap [system.Argumentexception] {"inner trap"; continue;}; "hello"; throw "exception"; "world"}}
$a.Length | Should Be 2
$a -join "," | Should Be "hello,outer trap"
}
}
}