Skip to content

Commit 48446f3

Browse files
authored
Add Should-HaveParameter (#2621)
* Add Should-HaveParameter * Help * Fix
1 parent c5646c8 commit 48446f3

File tree

6 files changed

+166
-0
lines changed

6 files changed

+166
-0
lines changed

src/Module.ps1

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,9 @@ $script:SafeCommands['Set-DynamicParameterVariable'] = $ExecutionContext.Session
9292
'Should-BeBefore'
9393
'Should-BeAfter'
9494

95+
'Should-HaveParameter'
96+
'Should-NotHaveParameter'
97+
9598
# export
9699
'Export-NUnitReport'
97100
'ConvertTo-NUnitReport'

src/Pester.psd1

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,9 @@
114114
'Should-BeBefore'
115115
'Should-BeAfter'
116116

117+
'Should-HaveParameter'
118+
'Should-NotHaveParameter'
119+
117120
# helpers
118121
'New-MockObject'
119122
'New-Fixture'
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
function Should-HaveParameter {
2+
<#
3+
.SYNOPSIS
4+
Asserts that a command has the expected parameter.
5+
6+
.PARAMETER ParameterName
7+
The name of the parameter to check. E.g. Uri
8+
9+
.PARAMETER Type
10+
The type of the parameter to check. E.g. [string]
11+
12+
.PARAMETER DefaultValue
13+
The default value of the parameter to check. E.g. "https://example.com"
14+
15+
.PARAMETER Mandatory
16+
Whether the parameter is mandatory or not.
17+
18+
.PARAMETER InParameterSet
19+
The parameter set that the parameter belongs to.
20+
21+
.PARAMETER HasArgumentCompleter
22+
Whether the parameter has an argument completer or not.
23+
24+
.PARAMETER Alias
25+
The alias of the parameter to check.
26+
27+
.PARAMETER Actual
28+
The actual command to check. E.g. Get-Command "Invoke-WebRequest"
29+
30+
.PARAMETER Because
31+
The reason why the input should be the expected value.
32+
33+
.EXAMPLE
34+
```powershell
35+
Get-Command "Invoke-WebRequest" | Should -HaveParameter Uri -Mandatory
36+
```
37+
38+
This test passes, because it expected the parameter URI to exist and to
39+
be mandatory.
40+
41+
42+
.NOTES
43+
The attribute [ArgumentCompleter] was added with PSv5. Previously this
44+
assertion will not be able to use the -HasArgumentCompleter parameter
45+
if the attribute does not exist.
46+
47+
.LINK
48+
https://pester.dev/docs/commands/Should-HaveParameter
49+
50+
.LINK
51+
https://pester.dev/docs/assertions
52+
#>
53+
54+
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseProcessBlockForPipelineCommand', '')]
55+
param(
56+
[String] $ParameterName,
57+
$Type,
58+
[String] $DefaultValue,
59+
[Switch] $Mandatory,
60+
[String] $InParameterSet,
61+
[Switch] $HasArgumentCompleter,
62+
[String[]] $Alias,
63+
[Parameter(ValueFromPipeline = $true)]
64+
$Actual,
65+
[String] $Because
66+
)
67+
68+
$collectedInput = Collect-Input -ParameterInput $Actual -PipelineInput $local:Input -IsPipelineInput $MyInvocation.ExpectingInput -UnrollInput
69+
$Actual = $collectedInput.Actual
70+
71+
$PSBoundParameters["ActualValue"] = $Actual
72+
$PSBoundParameters.Remove("Actual")
73+
74+
$testResult = Should-HaveParameterAssertion @PSBoundParameters
75+
76+
Test-AssertionResult $testResult
77+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
function Should-NotHaveParameter {
2+
<#
3+
.SYNOPSIS
4+
Asserts that a command has does not have the parameter.
5+
6+
.PARAMETER ParameterName
7+
The name of the parameter to check. E.g. Uri
8+
9+
.PARAMETER Actual
10+
The actual command to check. E.g. Get-Command "Invoke-WebRequest"
11+
12+
.PARAMETER Because
13+
The reason why the input should be the expected value.
14+
15+
.EXAMPLE
16+
```powershell
17+
Get-Command "Invoke-WebRequest" | Should -NotHaveParameter Uri
18+
```
19+
20+
This test fails, because it expected the parameter URI to not exist.
21+
22+
.NOTES
23+
The attribute [ArgumentCompleter] was added with PSv5. Previously this
24+
assertion will not be able to use the -HasArgumentCompleter parameter
25+
if the attribute does not exist.
26+
27+
.LINK
28+
https://pester.dev/docs/commands/Should-NotHaveParameter
29+
30+
.LINK
31+
https://pester.dev/docs/assertions
32+
#>
33+
34+
35+
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseProcessBlockForPipelineCommand', '')]
36+
param(
37+
[String] $ParameterName,
38+
[Parameter(ValueFromPipeline = $true)]
39+
$Actual,
40+
[String] $Because
41+
)
42+
43+
$collectedInput = Collect-Input -ParameterInput $Actual -PipelineInput $local:Input -IsPipelineInput $MyInvocation.ExpectingInput -UnrollInput
44+
$Actual = $collectedInput.Actual
45+
46+
$PSBoundParameters["ActualValue"] = $Actual
47+
$PSBoundParameters.Remove("Actual")
48+
$PSBoundParameters["Negate"] = $true
49+
50+
$testResult = Should-HaveParameterAssertion @PSBoundParameters
51+
52+
Test-AssertionResult $testResult
53+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
Set-StrictMode -Version Latest
2+
3+
Describe "Should-HaveParameter" {
4+
It "Passes when function has a parameter" {
5+
function f ($a) { }
6+
7+
Get-Command f | Should-HaveParameter a
8+
}
9+
10+
It "Fails when function does not have a parameter" {
11+
function f () { }
12+
13+
{ Get-Command f | Should-HaveParameter a } | Verify-Throw
14+
}
15+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
Set-StrictMode -Version Latest
2+
3+
Describe "Should-HaveParameter" {
4+
It "Passes when function does not have a parameter" {
5+
function f () { }
6+
7+
Get-Command f | Should-NotHaveParameter a
8+
}
9+
10+
It "Fails when function has a parameter" {
11+
function f ($a) { }
12+
13+
{ Get-Command f | Should-NotHaveParameter a } | Verify-Throw
14+
}
15+
}

0 commit comments

Comments
 (0)