Skip to content

Commit 513b04c

Browse files
committed
Tests for ProvideDefaultParameterValue rule
Getting "Object reference not set to an instance of an object error" when running test, but seems to complete successfully. Need to investigate error.
1 parent 44574b5 commit 513b04c

File tree

3 files changed

+41
-0
lines changed

3 files changed

+41
-0
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
function BadFunc
2+
{
3+
param(
4+
[Parameter()]
5+
[ValidateNotNullOrEmpty()]
6+
[string]
7+
$Param1
8+
)
9+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
Import-Module PSScriptAnalyzer
2+
$violationName = "PSProvideDefaultParameterValue"
3+
$violationMessage = "Parameter 'Param1' is not initialized. Non-global variables must be initialized. To fix a violation of this rule, please initialize non-global variables."
4+
$directory = Split-Path -Parent $MyInvocation.MyCommand.Path
5+
$violations = Invoke-ScriptAnalyzer $directory\ProvideDefaultParameterValue.ps1 | Where-Object {$_.RuleName -match $violationName}
6+
$noViolations = Invoke-ScriptAnalyzer $directory\ProvideDefaultParameterValueNoViolations.ps1
7+
8+
Describe "ProvideDefaultParameters" {
9+
Context "When there are violations" {
10+
It "has 1 provide default parameter value violation" {
11+
$violations.Count | Should Be 1
12+
}
13+
14+
It "has the correct description message" {
15+
$violations[0].Message | Should Match $violationMessage
16+
}
17+
}
18+
19+
Context "When there are no violations" {
20+
It "returns no violations" {
21+
$noViolations.Count | Should Be 0
22+
}
23+
}
24+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
function GoodFunc
2+
{
3+
param(
4+
[parmaeter(mandatory=$false)]
5+
[string]$Param1=$null
6+
)
7+
$Param1
8+
}

0 commit comments

Comments
 (0)