Skip to content

Commit 7a764e7

Browse files
author
Friedrich Weinmann
committed
New Template for Unit Tests
1 parent 7021586 commit 7a764e7

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

templates/CommandTest/PSMDInvoke.ps1

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
param (
2+
$Path
3+
)
4+
5+
New-PSMDTemplate -FilePath "$PSScriptRoot\þnameþ.Tests.ps1" -TemplateName CommandTest -OutPath $Path -Description "Testing template for a command unit test" -Author "Friedrich Weinmann" -Tags 'command', 'test', 'file'
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
Describe "þnameþ Unit Tests" -Tag "Unit" {
2+
BeforeAll {
3+
# Place here all things needed to prepare for the tests
4+
}
5+
AfterAll {
6+
# Here is where all the cleanup tasks go
7+
}
8+
9+
Describe "Ensuring unchanged command signature" {
10+
It "should have the expected parameter sets" {
11+
(Get-Command þnameþ).ParameterSets.Name | Should -Be þ{ ((Get-Command $Parameters.Name).ParameterSets.Name | ForEach-Object { "'{0}'" -f $_ }) -join ', '
12+
}
13+
14+
þ{
15+
$lines = @()
16+
$commonParameters = 'Verbose', 'Debug', 'ErrorAction', 'WarningAction', 'InformationAction', 'ErrorVariable', 'WarningVariable', 'InformationVariable', 'OutVariable', 'OutBuffer', 'PipelineVariable'
17+
foreach ($parameter in ((Get-Command $Parameters.Name).Parameters.Values | Where-Object Name -NotIn $commonParameters))
18+
{
19+
$lines += " It 'Should habe the expected parameter $($parameter.Name)' {"
20+
$lines += " `$parameter = (Get-Command $($Parameters.Name)).Parameters[$($parameter.Name)]"
21+
$lines += " `$parameter.Name | Should -Be '$($parameter.Name)'"
22+
$lines += " `$parameter.ParameterType.ToString() | Should -Be $($parameter.ParameterType.ToString())"
23+
$lines += " `$parameter.IsDynamic | Should -Be `$$($parameter.IsDynamic)"
24+
$lines += " `$parameter.ParameterSets.Keys | Should -Be $(($parameter.ParameterSets.Keys | ForEach-Object { "'{0}'" -f $_ }) -join ', ')"
25+
foreach ($key in $parameter.ParameterSets.Keys)
26+
{
27+
$lines += " `$parameter.ParameterSets.Keys | Should -Contain '$($key)'"
28+
$lines += " `$parameter.ParameterSets['$($key)'].IsMandatory | Should -Be `$$($parameter.ParameterSets[$key].IsMandatory)"
29+
$lines += " `$parameter.ParameterSets['$($key)'].Position | Should -Be $($parameter.ParameterSets[$key].Position)"
30+
$lines += " `$parameter.ParameterSets['$($key)'].ValueFromPipeline | Should -Be `$$($parameter.ParameterSets[$key].ValueFromPipeline)"
31+
$lines += " `$parameter.ParameterSets['$($key)'].ValueFromPipelineByPropertyName | Should -Be `$$($parameter.ParameterSets[$key].ValueFromPipelineByPropertyName)"
32+
$lines += " `$parameter.ParameterSets['$($key)'].ValueFromRemainingArguments | Should -Be `$$($parameter.ParameterSets[$key].ValueFromRemainingArguments)"
33+
}
34+
$lines += " }"
35+
}
36+
$lines -join "`n"
37+
38+
}
39+
40+
þ{
41+
$commonParameters = 'Verbose', 'Debug', 'ErrorAction', 'WarningAction', 'InformationAction', 'ErrorVariable', 'WarningVariable', 'InformationVariable', 'OutVariable', 'OutBuffer', 'PipelineVariable'
42+
foreach ($parameterSet in (Get-Command $Parameters.Name).ParameterSets)
43+
{
44+
$name = $parameterSet.Name
45+
$allParam = $parameterSet.Parameters | Where-Object Name -NotIn $commonParameters
46+
$mandatory = $allParam | Where-Object IsMandatory -EQ $true
47+
48+
@"
49+
Describe "Testing parameterset $($name)" {
50+
<#
51+
$($parameterSet.Name) -$($mandatory.Name -join " -")
52+
$($parameterSet.Name) -$($allParam.Name -join " -")
53+
#>
54+
}
55+
56+
"@
57+
}
58+
59+
}

0 commit comments

Comments
 (0)