|
| 1 | +using namespace System.Management.Automation.Language |
| 2 | + |
| 3 | +Describe "StaticParameterBinder tests" -Tags "CI" { |
| 4 | + BeforeAll { |
| 5 | + $testCases = @( |
| 6 | + @{ |
| 7 | + Source = 'Get-Alias abc' |
| 8 | + Description = 'string constant value' |
| 9 | + BoundParametersCount = 1 |
| 10 | + ExceptionCount = 0 |
| 11 | + ValidateScript = { |
| 12 | + param ($result) |
| 13 | + $result.BoundParameters.Name.ConstantValue | Should Be 'abc' |
| 14 | + } |
| 15 | + }, |
| 16 | + @{ |
| 17 | + Source = 'Get-Alias {abc}' |
| 18 | + Description = 'script block value' |
| 19 | + BoundParametersCount = 1 |
| 20 | + ExceptionCount = 0 |
| 21 | + ValidateScript = { |
| 22 | + param ($result) |
| 23 | + $result.BoundParameters.Name.Value | Should BeOfType ([ScriptBlockExpressionAst].FullName) |
| 24 | + $result.BoundParameters.Name.Value.Extent.Text | Should Be '{abc}' |
| 25 | + } |
| 26 | + }, |
| 27 | + @{ |
| 28 | + Source = 'Get-Alias -Path abc' |
| 29 | + Description = 'parameter -path not found' |
| 30 | + BoundParametersCount = 0 |
| 31 | + ExceptionCount = 1 |
| 32 | + ValidateScript = { |
| 33 | + param ($result) |
| 34 | + $result.BindingExceptions.Path.CommandElement.Extent.Text | Should Be '-Path' |
| 35 | + $result.BindingExceptions.Path.BindingException.ErrorId | Should Be 'NamedParameterNotFound' |
| 36 | + } |
| 37 | + }, |
| 38 | + @{ |
| 39 | + Source = 'Get-Alias -Path -Name:abc' |
| 40 | + Description = 'parameter -name found while -path not found' |
| 41 | + BoundParametersCount = 1 |
| 42 | + ExceptionCount = 1 |
| 43 | + ValidateScript = { |
| 44 | + param ($result) |
| 45 | + $result.BoundParameters.Name.ConstantValue | Should Be 'abc' |
| 46 | + $result.BindingExceptions.Path.CommandElement.Extent.Text | Should Be '-Path' |
| 47 | + $result.BindingExceptions.Path.BindingException.ErrorId | Should Be 'NamedParameterNotFound' |
| 48 | + } |
| 49 | + }, |
| 50 | + @{ |
| 51 | + Source = 'Get-Alias -Name -abc' |
| 52 | + Description = 'parameter -abc should be used as value' |
| 53 | + BoundParametersCount = 1 |
| 54 | + ExceptionCount = 0 |
| 55 | + ValidateScript = { |
| 56 | + param ($result) |
| 57 | + $result.BoundParameters.Name.Value | Should BeOfType ([CommandParameterAst].FullName) |
| 58 | + $result.BoundParameters.Name.Value.Extent.Text | Should Be '-abc' |
| 59 | + } |
| 60 | + }, |
| 61 | + @{ |
| 62 | + Source = 'Get-Alias aa bb' |
| 63 | + Description = 'unbound positional parameter bb' |
| 64 | + BoundParametersCount = 1 |
| 65 | + ExceptionCount = 1 |
| 66 | + ValidateScript = { |
| 67 | + param ($result) |
| 68 | + $result.BoundParameters.Name.ConstantValue | Should Be 'aa' |
| 69 | + $result.BindingExceptions.bb.BindingException.ErrorId | Should Be 'PositionalParameterNotFound' |
| 70 | + } |
| 71 | + }, |
| 72 | + @{ |
| 73 | + Source = 'Get-Alias aa,bb,cc' |
| 74 | + Description = 'array argument' |
| 75 | + BoundParametersCount = 1 |
| 76 | + ExceptionCount = 0 |
| 77 | + ValidateScript = { |
| 78 | + param ($result) |
| 79 | + $result.BoundParameters.Name.Value | Should BeOfType ([ArrayLiteralAst].FullName) |
| 80 | + $result.BoundParameters.Name.Value.Extent.Text | Should Be 'aa,bb,cc' |
| 81 | + } |
| 82 | + }, |
| 83 | + @{ |
| 84 | + Source = 'Get-ChildItem -Name abc -rec' |
| 85 | + Description = 'switch params and positional param' |
| 86 | + BoundParametersCount = 3 |
| 87 | + ExceptionCount = 0 |
| 88 | + ValidateScript = { |
| 89 | + param ($result) |
| 90 | + $result.BoundParameters.Name.ConstantValue | Should Be $true |
| 91 | + $result.BoundParameters.Recurse.ConstantValue | Should Be $true |
| 92 | + $result.BoundParameters.Path.ConstantValue | Should Be 'abc' |
| 93 | + } |
| 94 | + }, |
| 95 | + @{ |
| 96 | + Source = 'Get-ChildItem -Name -f' |
| 97 | + Description = 'switch parameter -name found while ambiguous parameter -f' |
| 98 | + BoundParametersCount = 1 |
| 99 | + ExceptionCount = 1 |
| 100 | + ValidateScript = { |
| 101 | + param ($result) |
| 102 | + $result.BoundParameters.Name.ConstantValue | Should Be $true |
| 103 | + $result.BindingExceptions.f.CommandElement.Extent.Text | Should Be '-f' |
| 104 | + $result.BindingExceptions.f.BindingException.ErrorId | Should Be 'AmbiguousParameter' |
| 105 | + } |
| 106 | + }, |
| 107 | + @{ |
| 108 | + Source = 'Get-ChildItem -Path -f' |
| 109 | + Description = 'non-switch parameter -path followed by ambiguous parameter -f' |
| 110 | + BoundParametersCount = 0 |
| 111 | + ExceptionCount = 1 |
| 112 | + ValidateScript = { |
| 113 | + param ($result) |
| 114 | + $result.BindingExceptions.f.CommandElement.Extent.Text | Should Be '-f' |
| 115 | + $result.BindingExceptions.f.BindingException.ErrorId | Should Be 'AmbiguousParameter' |
| 116 | + } |
| 117 | + } |
| 118 | + ) |
| 119 | + } |
| 120 | + |
| 121 | + It "<Description>: '<Source>'" -TestCases $testCases { |
| 122 | + param ($Source, $BoundParametersCount, $ExceptionCount, $ValidateScript) |
| 123 | + |
| 124 | + $ast = [Parser]::ParseInput($Source, [ref]$null, [ref]$null) |
| 125 | + $cmdAst = $ast.Find({$args[0] -is [CommandAst]}, $false) |
| 126 | + $result = [StaticParameterBinder]::BindCommand($cmdAst) |
| 127 | + |
| 128 | + $result.BoundParameters.Count | Should Be $BoundParametersCount |
| 129 | + $result.BindingExceptions.Count | Should Be $ExceptionCount |
| 130 | + . $ValidateScript $result |
| 131 | + } |
| 132 | +} |
0 commit comments