2020-03-24 19:08:37 +01:00
|
|
|
# Copyright (c) Microsoft Corporation.
|
2018-02-13 18:23:53 +01:00
|
|
|
# Licensed under the MIT License.
|
2017-09-30 01:28:15 +02:00
|
|
|
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)
|
2018-03-21 18:47:08 +01:00
|
|
|
$HtAst | Should -Not -BeNullOrEmpty
|
2020-01-24 20:00:37 +01:00
|
|
|
$HtAst.SafeGetValue() | Should -BeOfType Hashtable
|
2017-09-30 01:28:15 +02:00
|
|
|
}
|
|
|
|
It "An Array is returned from a LiteralArrayAst" {
|
|
|
|
$ArrayAstType = [ArrayLiteralAst]
|
|
|
|
$ArrayAst = {
|
|
|
|
@( 1,2,3,4)
|
|
|
|
}.ast.Find({$args[0] -is $ArrayAstType}, $true)
|
2018-03-21 18:47:08 +01:00
|
|
|
$ArrayAst | Should -Not -BeNullOrEmpty
|
2020-01-24 20:00:37 +01:00
|
|
|
,$ArrayAst.SafeGetValue() | Should -BeOfType Object[]
|
2017-09-30 01:28:15 +02:00
|
|
|
}
|
|
|
|
It "The proper error is returned when a variable is referenced" {
|
|
|
|
$ast = { $a }.Ast.Find({$args[0] -is "VariableExpressionAst"},$true)
|
2018-03-21 18:47:08 +01:00
|
|
|
{ $ast.SafeGetValue() } | Should -Throw -ErrorId "InvalidOperationException"
|
2017-09-30 01:28:15 +02:00
|
|
|
}
|
|
|
|
It "A ScriptBlock AST fails with the proper error" {
|
2018-03-21 18:47:08 +01:00
|
|
|
{ { 1 }.Ast.SafeGetValue() } | Should -Throw -ErrorId "InvalidOperationException"
|
2017-09-30 01:28:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|