-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGet-MarkdownHelp.Tests.ps1
More file actions
164 lines (136 loc) · 4.36 KB
/
Get-MarkdownHelp.Tests.ps1
File metadata and controls
164 lines (136 loc) · 4.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#Requires -Modules @{ModuleName="Pester"; ModuleVersion="5.0.0"}
[Diagnostics.CodeAnalysis.SuppressMessage('PSUseDeclaredVarsMoreThanAssignments', 'ScriptFile', Justification = 'False positive')]
[Diagnostics.CodeAnalysis.SuppressMessage('PSUseDeclaredVarsMoreThanAssignments', 'ModuleFile', Justification = 'False positive')]
param ()
Describe 'Get-MarkdownHelp' {
BeforeAll {
# Set-StrictMode -Version Latest
$Value = $PSCommandPath -Replace '.Tests.ps1$', '.ps1'
$Name = [System.IO.Path]::GetFileNameWithoutExtension($Value)
Set-Alias -Name $Name -Value $Value
$ScriptName = 'TestFunction'
$ScriptFile = "$($Env:Temp)\$ScriptName"
$ModuleName = "Get-MarkdownHelp.TestScript.psm1"
$ModuleFile = "$($Env:Temp)\$ModuleName"
$Null, 1, 2 |Foreach-Object {
Set-Variable -Name TestHelp$_ -Value @"
<#
.SYNOPSIS
Test
.DESCRIPTION
This is a script with the code in the root
.PARAMETER InputObject$_
InputObject$_ parameter test
.PARAMETER Switch
Switch parameter test
#>
"@
Set-Variable -Name TestParam$_ -Value "
param(
[Parameter(Mandatory = `$True, ValueFromPipeLine = `$True)]`$InputObject$_,
[Switch]`$Switch$_
)
"
Set-Variable -Name TestFunction$_ -Value @"
function TestFunction$_ {
$TestHelp$_
$TestParam$_
}
"@
}
}
Context 'input' {
It 'ps1 file' {
$TestScript = @"
$TestHelp
$TestParam
"@
$ScriptBlock = [ScriptBlock]::Create($TestScript)
$ScriptBlock | Out-File $ScriptFile
$MarkdownHelp = Get-MarkdownHelp $ScriptFile
$MarkdownHelp | Should -Contain "# $ScriptName"
$MarkdownHelp | Should -Contain '## Description'
}
It 'psm1 file' {
$TestModule = @"
$TestHelp
function TestFunction {
$TestParam
}
Export-ModuleMember -Function TestFunction
"@
$ScriptBlock = [ScriptBlock]::Create($TestModule)
$ScriptBlock | Out-File $ModuleFile
$MarkdownHelp = Get-MarkdownHelp $ModuleFile
$MarkdownHelp | Should -Contain "# $ScriptName"
$MarkdownHelp | Should -Contain '## Description'
}
It 'ps1 file with embedded function' {
$TestScript = @"
$TestHelp
function TestFunction {
$TestParam
}
"@
$ScriptBlock = [ScriptBlock]::Create($TestScript)
$ScriptBlock | Out-File $ScriptFile
$MarkdownHelp = Get-MarkdownHelp $ScriptFile -Command TestFunction
$MarkdownHelp | Should -Contain '# TestFunction'
$MarkdownHelp | Should -Contain '## Description'
}
It 'ps1 file with embedded function and help' {
$TestScript = @"
function TestFunction {
$TestParam
$TestHelp
}
"@
$ScriptBlock = [ScriptBlock]::Create($TestScript)
$ScriptBlock | Out-File $ScriptFile
$MarkdownHelp = Get-MarkdownHelp $ScriptFile -Command TestFunction
$MarkdownHelp | Should -Contain '# TestFunction'
$MarkdownHelp | Should -Contain '## Description'
}
}
Context 'Github issues' {
It 'Issue #9 Add -Script parameter' {
$Script = {
<#
.SYNOPSIS
Test
.DESCRIPTION
Testing the -Script parameter
#>
function Test { }
}
$MarkdownHelp = Get-MarkdownHelp -Script $Script
$MarkdownHelp | Should -Contain '# Test'
$MarkdownHelp | Should -Contain '## Description'
}
It 'Issue #10 Embedded Here-Comment breaks parsing' {
$Script = {
# .SYNOPSIS
# Test
#
# .DESCRIPTION
# Testing the -Script parameter
#
# .EXAMPLE
# #Embedded Here-Comment:
#
# Write-Host 1
# <# 100 #> Write-Host 2
# Write-Host 3
function Test { }
}
$MarkdownHelp = Get-MarkdownHelp -Script $Script
$MarkdownHelp | Should -Contain 'Write-Host 1'
$MarkdownHelp | Should -Contain '<# 100 #> Write-Host 2'
$MarkdownHelp | Should -Contain 'Write-Host 3'
}
}
AfterAll {
# if (Test-Path -LiteralPath $ScriptFile) { Remove-Item -LiteralPath $ScriptFile }
# if (Test-Path -LiteralPath $ModuleFile) { Remove-Item -LiteralPath $ModuleFile }
}
}