Skip to content

Commit 08998af

Browse files
authored
Add -InParameterSet in Should -HaveParameter (#2273)
1 parent d318bdd commit 08998af

File tree

2 files changed

+83
-12
lines changed

2 files changed

+83
-12
lines changed

src/functions/assertions/HaveParameter.ps1

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@
22
$ActualValue,
33
[String] $ParameterName,
44
$Type,
5-
[String]$DefaultValue,
6-
[Switch]$Mandatory,
7-
[Switch]$HasArgumentCompleter,
8-
[String[]]$Alias,
9-
[Switch]$Negate,
10-
[String]$Because ) {
5+
[String] $DefaultValue,
6+
[Switch] $Mandatory,
7+
[String] $InParameterSet,
8+
[Switch] $HasArgumentCompleter,
9+
[String[]] $Alias,
10+
[Switch] $Negate,
11+
[String] $Because ) {
1112
<#
1213
.SYNOPSIS
1314
Asserts that a command has the expected parameter.
@@ -30,6 +31,7 @@
3031
throw "The ParameterName can't be empty"
3132
}
3233

34+
#region HelperFunctions
3335
function Get-ParameterInfo {
3436
param (
3537
[Parameter(Mandatory = $true)]
@@ -149,6 +151,7 @@
149151
}
150152
}
151153
}
154+
#endregion HelperFunctions
152155

153156
if ($Type -is [string]) {
154157
# parses type that is provided as a string in brackets (such as [int])
@@ -160,7 +163,6 @@
160163

161164
$Type = $parsedType
162165
}
163-
#endregion HelperFunctions
164166

165167
$buts = @()
166168
$filters = @()
@@ -181,22 +183,38 @@
181183
}
182184

183185
$hasKey = $ActualValue.Parameters.PSBase.ContainsKey($ParameterName)
184-
$filters += "to$(if ($Negate) {" not"}) have a parameter $ParameterName"
186+
$filters += "to$(if ($Negate) {' not'}) have a parameter $ParameterName$(if ($InParameterSet) { " in parameter set $InParameterSet" })"
185187

186188
if (-not $Negate -and -not $hasKey) {
187189
$buts += "the parameter is missing"
188190
}
189191
elseif ($Negate -and -not $hasKey) {
190192
return & $SafeCommands['New-Object'] PSObject -Property @{ Succeeded = $true }
191193
}
192-
elseif ($Negate -and $hasKey -and -not ($Mandatory -or $Type -or $DefaultValue -or $HasArgumentCompleter)) {
194+
elseif ($Negate -and $hasKey -and -not ($InParameterSet -or $Mandatory -or $Type -or $DefaultValue -or $HasArgumentCompleter)) {
193195
$buts += "the parameter exists"
194196
}
195197
else {
196198
$attributes = $ActualValue.Parameters[$ParameterName].Attributes
199+
$parameterAttributes = $attributes | & $SafeCommands['Where-Object'] { $_ -is [System.Management.Automation.ParameterAttribute] }
200+
201+
if ($InParameterSet) {
202+
$parameterAttributes = $parameterAttributes | & $SafeCommands['Where-Object'] { $_.ParameterSetName -eq $InParameterSet }
203+
204+
if (-not $Negate -and -not $parameterAttributes) {
205+
$buts += 'the parameter is missing'
206+
}
207+
elseif ($Negate -and $parameterAttributes) {
208+
$buts += 'the parameter exists'
209+
}
210+
}
211+
}
212+
213+
if ($buts.Count -eq 0) {
214+
# Parameter exists (in set if specified), assert remaining requirements
197215

198216
if ($Mandatory) {
199-
$testMandatory = $attributes | & $SafeCommands['Where-Object'] { $_ -is [System.Management.Automation.ParameterAttribute] -and $_.Mandatory }
217+
$testMandatory = $parameterAttributes | & $SafeCommands['Where-Object'] { $_.Mandatory }
200218
$filters += "which is$(if ($Negate) {" not"}) mandatory"
201219

202220
if (-not $Negate -and -not $testMandatory) {

tst/functions/assertions/HaveParameter.Tests.ps1

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@ InPesterModuleScope {
77
if ($PSVersionTable.PSVersion.Major -ge 5) {
88
function Invoke-DummyFunction {
99
param(
10-
[Parameter(Mandatory = $true)]
10+
[Parameter(Mandatory = $true, ParameterSetName = 'PrimarySet')]
1111
[Alias('First', 'Another')]
1212
$MandatoryParam,
1313

14+
[Parameter(ParameterSetName = 'PrimarySet')]
1415
[ValidateNotNullOrEmpty()]
1516
[DateTime]$ParamWithNotNullOrEmptyValidation = (Get-Date),
1617

@@ -62,10 +63,11 @@ InPesterModuleScope {
6263
else {
6364
function Invoke-DummyFunction {
6465
param(
65-
[Parameter(Mandatory = $true)]
66+
[Parameter(Mandatory = $true, ParameterSetName = 'PrimarySet')]
6667
[Alias('First', 'Another')]
6768
$MandatoryParam,
6869

70+
[Parameter(ParameterSetName = 'PrimarySet')]
6971
[ValidateNotNullOrEmpty()]
7072
[DateTime]$ParamWithNotNullOrEmptyValidation = (Get-Date),
7173

@@ -323,6 +325,38 @@ InPesterModuleScope {
323325
$err.Exception.Message | Verify-Equal "Expected command Invoke-DummyFunction to have a parameter ParamWithNotNullOrEmptyValidation, which is mandatory, of type [System.TimeSpan], the default value to be 'wrong value' and has ArgumentCompletion, because of reasons, but it wasn't mandatory, it was of type [System.DateTime], the default value was '(Get-Date)' and has no ArgumentCompletion."
324326
}
325327
}
328+
329+
Context 'Using InParameterSet' {
330+
It "passes if parameter <ParameterName> exist in parameter set <ParameterSetName>" -TestCases @(
331+
@{ParameterName = 'ParamWithNotNullOrEmptyValidation'; ParameterSetName = 'PrimarySet' }
332+
) {
333+
Get-Command 'Invoke-DummyFunction' | Should -HaveParameter $ParameterName -InParameterSet $ParameterSetName
334+
}
335+
336+
It 'passes if parameter <ParameterName> exist in parameter set <ParameterSetName> and is mandatory' -TestCases @(
337+
@{ParameterName = 'MandatoryParam'; ParameterSetName = 'PrimarySet' }
338+
) {
339+
Get-Command 'Invoke-DummyFunction' | Should -HaveParameter $ParameterName -InParameterSet $ParameterSetName -Mandatory
340+
}
341+
342+
It 'fails if parameter <ParameterName> does not exist at all or not in parameter set <ParameterSetName>' -TestCases @(
343+
@{ParameterName = 'NonExistingParam'; ParameterSetName = 'PrimarySet' }
344+
@{ParameterName = 'ParamWithNotNullOrEmptyValidation'; ParameterSetName = 'NonExistingSet' }
345+
@{ParameterName = 'ParamWithScriptValidation'; ParameterSetName = 'PrimarySet' }
346+
) {
347+
$err = { Get-Command 'Invoke-DummyFunction' | Should -HaveParameter $ParameterName -InParameterSet $ParameterSetName } | Verify-AssertionFailed
348+
$err.Exception.Message | Verify-Equal "Expected command Invoke-DummyFunction to have a parameter $ParameterName in parameter set $ParameterSetName, but the parameter is missing."
349+
}
350+
351+
It 'fails if parameter <ParameterName> exists in parameter set <ParameterSetName> but is not mandatory' -TestCases @(
352+
@{ParameterName = 'ParamWithNotNullOrEmptyValidation'; ParameterSetName = 'PrimarySet' }
353+
) {
354+
$err = { Get-Command 'Invoke-DummyFunction' | Should -HaveParameter $ParameterName -InParameterSet $ParameterSetName -Mandatory } | Verify-AssertionFailed
355+
$err.Exception.Message | Verify-Equal "Expected command Invoke-DummyFunction to have a parameter $ParameterName in parameter set $ParameterSetName, which is mandatory, but it wasn't mandatory."
356+
}
357+
358+
# -InParameterSet only affects if parameter exist and -Mandatory atm. Only appends a filter in the error for the remaining options
359+
}
326360
}
327361

328362
Describe "Should -Not -HaveParameter" {
@@ -492,6 +526,25 @@ InPesterModuleScope {
492526
$err.Exception.Message | Verify-Equal "Expected command Invoke-DummyFunction to not have a parameter $ParameterName, not of type [$ExpectedType], the default value not to be '$ExpectedValue' and has ArgumentCompletion, because of reasons, but it was of type [$ExpectedType], the default value was '$ExpectedValue' and has ArgumentCompletion."
493527
}
494528
}
529+
530+
Context 'Using InParameterSet' {
531+
It 'passes if parameter <ParameterName> does not exist at all or not in parameter set <ParameterSetName>' -TestCases @(
532+
@{ParameterName = 'NonExistingParam'; ParameterSetName = 'PrimarySet' }
533+
@{ParameterName = 'ParamWithScriptValidation'; ParameterSetName = 'PrimarySet' }
534+
@{ParameterName = 'ParamWithNotNullOrEmptyValidation'; ParameterSetName = 'NonExistingSet' }
535+
) {
536+
Get-Command 'Invoke-DummyFunction' | Should -Not -HaveParameter $ParameterName -InParameterSet $ParameterSetName
537+
}
538+
539+
It 'fails if parameter <ParameterName> exist in parameter set <ParameterSetName>' -TestCases @(
540+
@{ParameterName = 'ParamWithNotNullOrEmptyValidation'; ParameterSetName = 'PrimarySet' }
541+
) {
542+
$err = { Get-Command 'Invoke-DummyFunction' | Should -Not -HaveParameter $ParameterName -InParameterSet $ParameterSetName } | Verify-AssertionFailed
543+
$err.Exception.Message | Verify-Equal "Expected command Invoke-DummyFunction to not have a parameter $ParameterName in parameter set $ParameterSetName, but the parameter exists."
544+
}
545+
546+
# -Not -HaveParameter only supports parameter existing atm. Extend when not mandatory etc is possible.
547+
}
495548
}
496549
}
497550

0 commit comments

Comments
 (0)