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-02-17 06:25:23 +01:00
|
|
|
Describe "ScriptBlock.GetNewClosure()" -tags "CI" {
|
2020-03-24 19:08:37 +01:00
|
|
|
|
2017-02-17 06:25:23 +01:00
|
|
|
BeforeAll {
|
|
|
|
|
|
|
|
## No error should occur when calling GetNewClosure because:
|
|
|
|
## 1. ValidateAttributes are not evaluated on parameter default values
|
|
|
|
## 2. GetNewClosure no longer forces validation on existing variables
|
|
|
|
function SimpleFunction_GetNewClosure
|
|
|
|
{
|
|
|
|
param([ValidateNotNull()] $Name)
|
|
|
|
|
|
|
|
& { 'OK' }.GetNewClosure()
|
|
|
|
}
|
|
|
|
|
|
|
|
function ScriptCmdlet_GetNewClosure
|
|
|
|
{
|
|
|
|
[CmdletBinding()]
|
|
|
|
param(
|
|
|
|
[Parameter()]
|
|
|
|
[ValidateNotNullOrEmpty()]
|
|
|
|
[string] $Name = "",
|
|
|
|
|
|
|
|
[Parameter()]
|
|
|
|
[ValidateRange(1,3)]
|
|
|
|
[int] $Value = 4
|
|
|
|
)
|
|
|
|
|
|
|
|
& { $Value; $Name }.GetNewClosure()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
It "Parameter attributes should not get evaluated again in GetNewClosure - SimpleFunction" {
|
2018-03-14 20:13:32 +01:00
|
|
|
SimpleFunction_GetNewClosure | Should -BeExactly "OK"
|
2017-02-17 06:25:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
It "Parameter attributes should not get evaluated again in GetNewClosure - ScriptCmdlet" {
|
|
|
|
$result = ScriptCmdlet_GetNewClosure
|
2018-03-14 20:13:32 +01:00
|
|
|
$result.Count | Should -Be 2
|
|
|
|
$result[0] | Should -Be 4
|
|
|
|
$result[1] | Should -BeNullOrEmpty
|
2017-02-17 06:25:23 +01:00
|
|
|
}
|
|
|
|
}
|