File size: 1,343 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
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
Describe "ScriptBlock.GetNewClosure()" -tags "CI" {

    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" {
        SimpleFunction_GetNewClosure | Should -BeExactly "OK"
    }

    It "Parameter attributes should not get evaluated again in GetNewClosure - ScriptCmdlet" {
        $result = ScriptCmdlet_GetNewClosure
        $result.Count | Should -Be 2
        $result[0] | Should -Be 4
        $result[1] | Should -BeNullOrEmpty
    }
}