File size: 5,984 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
using namespace System.Management.Automation
using namespace System.Collections.ObjectModel

Describe 'ProxyCommand Tests' -Tags "CI" {
    BeforeAll {
        function NormalizeCRLF {
            param ($helpObj)

            if($helpObj.Synopsis.Contains("`r`n"))
            {
                $helpObjText = ($helpObj.Synopsis).replace("`r`n", [System.Environment]::NewLine).trim()
            }
            else
            {
                $helpObjText = ($helpObj.Synopsis).replace("`n", [System.Environment]::NewLine).trim()
            }

            return $helpObjText
        }

        function GetSectionText {
            param ($object)
            $texts = $object | Out-String -Stream | ForEach-Object {
                if (![string]::IsNullOrWhiteSpace($_)) { $_.Trim() }
            }
            $texts -join ""
        }

        function ProxyTest {
            [CmdletBinding(DefaultParameterSetName='Name')]
            param (
                [Parameter(ParameterSetName="Name", Mandatory=$true)]
                [ValidateSet("Orange", "Apple")]
                [string] $Name,

                [Parameter(ParameterSetName="Id", Mandatory=$true)]
                [ValidateRange(1,5)]
                [int] $Id,

                [Parameter(ValueFromPipeline)]
                [string] $Message
            )

            DynamicParam {
                $ParamAttrib  = [parameter]::new()
                $ParamAttrib.Mandatory  = $true

                $AttribColl = [Collection[System.Attribute]]::new()
                $AttribColl.Add($ParamAttrib)

                $RuntimeParam  = [RuntimeDefinedParameter]::new('LastName', [string], $AttribColl)
                $RuntimeParamDic  = [RuntimeDefinedParameterDictionary]::new()
                $RuntimeParamDic.Add('LastName',  $RuntimeParam)
                return  $RuntimeParamDic
            }

            Begin {
                $AllMessages = @()
                if ($PSCmdlet.ParameterSetName -eq "Name") {
                    $MyString = $Name, $PSBoundParameters['LastName'] -join ","
                } else {
                    $MyString = $Id, $PSBoundParameters['LastName'] -join ","
                }
            }

            Process {
                if ($Message) {
                    $AllMessages += $Message
                }
            }

            End {
                $MyString + " - " + ($AllMessages -join ";")
            }
        }
    }

    It "Test ProxyCommand.GetHelpComments" {
        $helpObj = Get-Help Get-Alias -Full
        $helpContent = [System.Management.Automation.ProxyCommand]::GetHelpComments($helpObj)

        $funcBody = @"
    <#
    {0}
    #>

    param({1})
"@
        $params = $helpObj.parameters.parameter
        $paramString = '${0}' -f $params[0].name
        for ($i = 1; $i -lt $params.Length; $i++) {
            $paramString += ',${0}' -f $params[$i].name
        }

        $bodyToUse = $funcBody -f $helpContent, $paramString
        $bodySB = [scriptblock]::Create($bodyToUse)
        Set-Item -Path function:\TestHelpComment -Value $bodySB
        $newHelpObj = Get-Help TestHelpComment -Full

        $helpObjText = NormalizeCRLF -helpObj $helpObj
        $newHelpObjText = NormalizeCRLF -helpObj $newHelpObj

        $helpObjText | Should -Be $newHelpObjText
        $oldDespText = GetSectionText $helpObj.description
        $newDespText = GetSectionText $newHelpObj.description
        $oldDespText | Should -Be $newDespText

        $oldParameters = @($helpObj.parameters.parameter)
        $newParameters = @($newHelpObj.parameters.parameter)
        $oldParameters.Length | Should -Be $newParameters.Length
        $oldParameters.name -join "," | Should -Be ($newParameters.name -join ",")

        $oldExamples = @($helpObj.examples.example)
        $newExamples = @($newHelpObj.examples.example)
        $oldExamples.Length | Should -Be $newExamples.Length
    }

    It "Test generate proxy command" {
        $cmdInfo = Get-Command -Name Get-Content
        $cmdMetadata = [CommandMetadata]::new($cmdInfo)
        $proxyBody = [ProxyCommand]::Create($cmdMetadata, "--DummyHelpContent--")
        $proxyBody | Should -Match '--DummyHelpContent--'

        $proxyBodySB = [scriptblock]::Create($proxyBody)
        Set-Item -Path function:\MyGetContent -Value $proxyBodySB

        $expectedContent = "Hello World"
        Set-Content -Path $TestDrive\content.txt -Value $expectedContent -Encoding Unicode
        $myContent = MyGetContent -Path $TestDrive\content.txt -Encoding Unicode
        $myContent | Should -Be $expectedContent
    }

    It "Test generate individual components" {
        $cmdInfo = Get-Command -Name ProxyTest
        $cmdMetadata = [CommandMetadata]::new($cmdInfo)
        $template = @"
{0}
param(
{1}
)

DynamicParam {{
{2}
}}

Begin {{
{3}
}}

Process {{
{4}
}}

End {{
{5}
}}
"@

        $cmdletBindig = [ProxyCommand]::GetCmdletBindingAttribute($cmdMetadata)
        $params = [ProxyCommand]::GetParamBlock($cmdMetadata)
        $dynamicParams = [ProxyCommand]::GetDynamicParam($cmdMetadata)
        $begin = [ProxyCommand]::GetBegin($cmdMetadata)
        $process = [ProxyCommand]::GetProcess($cmdMetadata)
        $end = [ProxyCommand]::GetEnd($cmdMetadata)

        $funcBody = $template -f $cmdletBindig, $params, $dynamicParams, $begin, $process, $end
        $bodySB = [scriptblock]::Create($funcBody)
        Set-Item -Path function:\MyProxyTest -Value $bodySB

        $cmdMyProxyTest = Get-Command MyProxyTest
        $dyParam = $cmdMyProxyTest.Parameters.GetEnumerator() | Where-Object { $_.Value.IsDynamic }
        $dyParam.Key | Should -Be 'LastName'

        $result = "Msg1", "Msg2" | MyProxyTest -Name Apple -LastName Last
        $result | Should -Be "Apple,Last - Msg1;Msg2"

        $result = "Msg1", "Msg2" | MyProxyTest -Id 3 -LastName Last
        $result | Should -Be "3,Last - Msg1;Msg2"
    }
}