File size: 5,960 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
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
using namespace System.Management.Automation.Language

Describe "StaticParameterBinder tests" -Tags "CI" {
    BeforeAll {
        $testCases = @(
            @{
                Source = 'Get-Alias abc'
                Description = 'string constant value'
                BoundParametersCount = 1
                ExceptionCount = 0
                ValidateScript = {
                    param ($result)
                    $result.BoundParameters.Name.ConstantValue | Should -Be 'abc'
                }
            },
            @{
                Source = 'Get-Alias {abc}'
                Description = 'script block value'
                BoundParametersCount = 1
                ExceptionCount = 0
                ValidateScript = {
                    param ($result)
                    $result.BoundParameters.Name.Value | Should -BeOfType ([ScriptBlockExpressionAst].FullName)
                    $result.BoundParameters.Name.Value.Extent.Text | Should -Be '{abc}'
                }
            },
            @{
                Source = 'Get-Alias -Path abc'
                Description = 'parameter -path not found'
                BoundParametersCount = 0
                ExceptionCount = 1
                ValidateScript = {
                    param ($result)
                    $result.BindingExceptions.Path.CommandElement.Extent.Text | Should -Be '-Path'
                    $result.BindingExceptions.Path.BindingException.ErrorId | Should -Be 'NamedParameterNotFound'
                }
            },
            @{
                Source = 'Get-Alias -Path -Name:abc'
                Description = 'parameter -name found while -path not found'
                BoundParametersCount = 1
                ExceptionCount = 1
                ValidateScript = {
                    param ($result)
                    $result.BoundParameters.Name.ConstantValue | Should -Be 'abc'
                    $result.BindingExceptions.Path.CommandElement.Extent.Text | Should -Be '-Path'
                    $result.BindingExceptions.Path.BindingException.ErrorId | Should -Be 'NamedParameterNotFound'
                }
            },
            @{
                Source = 'Get-Alias -Name -abc'
                Description = 'parameter -abc should be used as value'
                BoundParametersCount = 1
                ExceptionCount = 0
                ValidateScript = {
                    param ($result)
                    $result.BoundParameters.Name.Value | Should -BeOfType ([CommandParameterAst].FullName)
                    $result.BoundParameters.Name.Value.Extent.Text | Should -Be '-abc'
                }
            },
            @{
                Source = 'Get-Alias aa bb'
                Description = 'unbound positional parameter bb'
                BoundParametersCount = 1
                ExceptionCount = 1
                ValidateScript = {
                    param ($result)
                    $result.BoundParameters.Name.ConstantValue | Should -Be 'aa'
                    $result.BindingExceptions.bb.BindingException.ErrorId | Should -Be 'PositionalParameterNotFound'
                }
            },
            @{
                Source = 'Get-Alias aa,bb,cc'
                Description = 'array argument'
                BoundParametersCount = 1
                ExceptionCount = 0
                ValidateScript = {
                    param ($result)
                    $result.BoundParameters.Name.Value | Should -BeOfType ([ArrayLiteralAst].FullName)
                    $result.BoundParameters.Name.Value.Extent.Text | Should -Be 'aa,bb,cc'
                }
            },
            @{
                Source = 'Get-ChildItem -Name abc -rec'
                Description = 'switch params and positional param'
                BoundParametersCount = 3
                ExceptionCount = 0
                ValidateScript = {
                    param ($result)
                    $result.BoundParameters.Name.ConstantValue | Should -BeTrue
                    $result.BoundParameters.Recurse.ConstantValue | Should -BeTrue
                    $result.BoundParameters.Path.ConstantValue | Should -Be 'abc'
                }
            },
            @{
                Source = 'Get-ChildItem -Name -f'
                Description = 'switch parameter -name found while ambiguous parameter -f'
                BoundParametersCount = 1
                ExceptionCount = 1
                ValidateScript = {
                    param ($result)
                    $result.BoundParameters.Name.ConstantValue | Should -BeTrue
                    $result.BindingExceptions.f.CommandElement.Extent.Text | Should -Be '-f'
                    $result.BindingExceptions.f.BindingException.ErrorId | Should -Be 'AmbiguousParameter'
                }
            },
            @{
                Source = 'Get-ChildItem -Path -f'
                Description = 'non-switch parameter -path followed by ambiguous parameter -f'
                BoundParametersCount = 0
                ExceptionCount = 1
                ValidateScript = {
                    param ($result)
                    $result.BindingExceptions.f.CommandElement.Extent.Text | Should -Be '-f'
                    $result.BindingExceptions.f.BindingException.ErrorId | Should -Be 'AmbiguousParameter'
                }
            }
        )
    }

    It "<Description>: '<Source>'" -TestCases $testCases {
        param ($Source, $BoundParametersCount, $ExceptionCount, $ValidateScript)

        $ast = [Parser]::ParseInput($Source, [ref]$null, [ref]$null)
        $cmdAst = $ast.Find({$args[0] -is [CommandAst]}, $false)
        $result = [StaticParameterBinder]::BindCommand($cmdAst)

        $result.BoundParameters.Count | Should -Be $BoundParametersCount
        $result.BindingExceptions.Count | Should -Be $ExceptionCount
        . $ValidateScript $result
    }
}