PowerShell/test/powershell/Language/Parser/Ast.Tests.ps1
Ilya 409ab7443f Fix GetType() bad pattern and related issues in tests (#3134)
* Fix GetType() bad pattern and related issues in tests

$var.GetType() can raise an exception in tests so we should check $var
before make the call. A large part of the tests does not make this
check.
I start with searching ".GetType()" but discovered many related issues
in tests (reduntant and unneeded tests, "throw" bad pattens, bugs,
formattings (sorry!) and so on) - I had to fix them too.

* Fix after code review
* Second wave of migration GetType() -> BeOfType
Removed 'GetType().Name' patterns.
2017-02-15 16:40:51 -08:00

40 lines
1.4 KiB
PowerShell

using Namespace System.Management.Automation.Language
Describe "The SafeGetValue method on AST returns safe values" -Tags "CI" {
It "A hashtable is returned from a HashtableAst" {
$HashtableAstType = [HashtableAst]
$HtAst = {
@{ one = 1 }
}.ast.Find({$args[0] -is $HashtableAstType}, $true)
$HtAst | Should Not BeNullOrEmpty
$HtAst.SafeGetValue() | Should BeOfType "Hashtable"
}
It "An Array is returned from a LiteralArrayAst" {
$ArrayAstType = [ArrayLiteralAst]
$ArrayAst = {
@( 1,2,3,4)
}.ast.Find({$args[0] -is $ArrayAstType}, $true)
$ArrayAst | Should Not BeNullOrEmpty
,$ArrayAst.SafeGetValue() | Should BeOfType "Object[]"
}
It "The proper error is returned when a variable is referenced" {
$ast = { $a }.Ast.Find({$args[0] -is "VariableExpressionAst"},$true)
try {
$ast.SafeGetValue() | out-null
throw "No Exception!"
}
catch {
$_.FullyQualifiedErrorId | Should be "InvalidOperationException"
$_.ToString() | Should Match '\$a'
}
}
It "A ScriptBlock AST fails with the proper error" {
try {
{ 1 }.Ast.SafeGetValue()
throw "No Exception!"
}
catch {
$_.FullyQualifiedErrorId | Should be "InvalidOperationException"
}
}
}