Skip to content

Commit 2525021

Browse files
committed
Add tests for Private functions
- Add comprehensive unit tests for all Private functions
1 parent 72308d0 commit 2525021

File tree

7 files changed

+811
-1
lines changed

7 files changed

+811
-1
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
.claude/
2-
.vscode/launch.json
2+
.vscode/launch.json
3+
coverage.xml

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1212
- Add Pester tests and GitHub workflow
1313
- Add Dependabot configuration for automated dependency updates
1414
- Add Codecov integration for code coverage reporting
15+
- Add comprehensive unit tests for all Private functions
1516

1617
### Changed
1718
- Update GitHub Release action for new folder structures
Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
BeforeAll {
2+
# Import the module
3+
$ModulePath = Join-Path -Path $PSScriptRoot -ChildPath '..\..\AsBuiltReport.Core\AsBuiltReport.Core.psd1'
4+
Import-Module $ModulePath -Force
5+
}
6+
7+
Describe 'Draw-AsciiBanner Unit Tests' {
8+
Context 'Function Exists' {
9+
It 'Should have Draw-AsciiBanner function available' {
10+
InModuleScope 'AsBuiltReport.Core' {
11+
$Function = Get-Command -Name 'Draw-AsciiBanner' -ErrorAction SilentlyContinue
12+
$Function | Should -Not -BeNullOrEmpty
13+
}
14+
}
15+
}
16+
17+
Context 'Basic Functionality' {
18+
It 'Should call Write-Host when drawing banner' {
19+
InModuleScope 'AsBuiltReport.Core' {
20+
Mock Write-Host { }
21+
Draw-AsciiBanner -Lines @('Test') -TextColor 'Cyan' -SeparatorColor 'Cyan'
22+
Should -Invoke Write-Host -Times 3
23+
}
24+
}
25+
26+
It 'Should draw separators and text with single line' {
27+
InModuleScope 'AsBuiltReport.Core' {
28+
Mock Write-Host { }
29+
Draw-AsciiBanner -Lines @('Test Line') -TextColor 'Green' -SeparatorColor 'Yellow'
30+
Should -Invoke Write-Host -Times 3
31+
}
32+
}
33+
34+
It 'Should draw separators and text with multiple lines' {
35+
InModuleScope 'AsBuiltReport.Core' {
36+
Mock Write-Host { }
37+
Draw-AsciiBanner -Lines @('Line 1', 'Line 2', 'Line 3') -TextColor 'White' -SeparatorColor 'Cyan'
38+
Should -Invoke Write-Host -Times 5
39+
}
40+
}
41+
42+
It 'Should use specified text color' {
43+
InModuleScope 'AsBuiltReport.Core' {
44+
Mock Write-Host { }
45+
Draw-AsciiBanner -Lines @('Test') -TextColor 'Red' -SeparatorColor 'Blue'
46+
Should -Invoke Write-Host -ParameterFilter { $ForegroundColor -eq 'Red' } -Times 1
47+
}
48+
}
49+
50+
It 'Should use specified separator color' {
51+
InModuleScope 'AsBuiltReport.Core' {
52+
Mock Write-Host { }
53+
Draw-AsciiBanner -Lines @('Test') -TextColor 'Red' -SeparatorColor 'Blue'
54+
Should -Invoke Write-Host -ParameterFilter { $ForegroundColor -eq 'Blue' } -Times 2
55+
}
56+
}
57+
58+
It 'Should handle empty lines array' {
59+
InModuleScope 'AsBuiltReport.Core' {
60+
Mock Write-Host { }
61+
Draw-AsciiBanner -Lines @() -TextColor 'Cyan' -SeparatorColor 'Cyan'
62+
Should -Invoke Write-Host -Times 2
63+
}
64+
}
65+
66+
It 'Should accept custom separator length' {
67+
InModuleScope 'AsBuiltReport.Core' {
68+
Mock Write-Host { }
69+
Draw-AsciiBanner -Lines @('Test') -TextColor 'Cyan' -SeparatorColor 'Cyan' -SeparatorLength 80
70+
Should -Invoke Write-Host -Times 3
71+
}
72+
}
73+
}
74+
75+
Context 'Parameter Validation' {
76+
It 'Should accept Lines parameter as string array' {
77+
InModuleScope 'AsBuiltReport.Core' {
78+
Mock Write-Host { }
79+
{ Draw-AsciiBanner -Lines @('Test1', 'Test2') -TextColor 'Cyan' -SeparatorColor 'Cyan' } | Should -Not -Throw
80+
}
81+
}
82+
83+
It 'Should accept TextColor parameter' {
84+
InModuleScope 'AsBuiltReport.Core' {
85+
Mock Write-Host { }
86+
{ Draw-AsciiBanner -Lines @('Test') -TextColor 'Green' -SeparatorColor 'Cyan' } | Should -Not -Throw
87+
}
88+
}
89+
90+
It 'Should accept SeparatorColor parameter' {
91+
InModuleScope 'AsBuiltReport.Core' {
92+
Mock Write-Host { }
93+
{ Draw-AsciiBanner -Lines @('Test') -TextColor 'Cyan' -SeparatorColor 'Yellow' } | Should -Not -Throw
94+
}
95+
}
96+
97+
It 'Should accept SeparatorLength parameter' {
98+
InModuleScope 'AsBuiltReport.Core' {
99+
Mock Write-Host { }
100+
{ Draw-AsciiBanner -Lines @('Test') -TextColor 'Cyan' -SeparatorColor 'Cyan' -SeparatorLength 100 } | Should -Not -Throw
101+
}
102+
}
103+
104+
It 'Should use default SeparatorLength of 60 when not specified' {
105+
InModuleScope 'AsBuiltReport.Core' {
106+
Mock Write-Host { param($Object) $script:LastSeparator = $Object }
107+
Draw-AsciiBanner -Lines @() -TextColor 'Cyan' -SeparatorColor 'Cyan'
108+
$script:LastSeparator.Length | Should -Be 60
109+
}
110+
}
111+
}
112+
113+
Context 'Content Formatting' {
114+
It 'Should add padding to text lines' {
115+
InModuleScope 'AsBuiltReport.Core' {
116+
Mock Write-Host { param($Object) $script:Output += @($Object) }
117+
$script:Output = @()
118+
Draw-AsciiBanner -Lines @('Test') -TextColor 'Cyan' -SeparatorColor 'Cyan'
119+
$TextLine = $script:Output | Where-Object { $_ -match 'Test' }
120+
$TextLine | Should -BeLike ' Test'
121+
}
122+
}
123+
124+
It 'Should format multiple lines correctly' {
125+
InModuleScope 'AsBuiltReport.Core' {
126+
Mock Write-Host { param($Object) $script:Output += @($Object) }
127+
$script:Output = @()
128+
Draw-AsciiBanner -Lines @('Line1', 'Line2') -TextColor 'Cyan' -SeparatorColor 'Cyan'
129+
$script:Output[1] | Should -BeLike ' Line1'
130+
$script:Output[2] | Should -BeLike ' Line2'
131+
}
132+
}
133+
134+
It 'Should use horizontal line character for separator' {
135+
InModuleScope 'AsBuiltReport.Core' {
136+
Mock Write-Host { param($Object) $script:Output += @($Object) }
137+
$script:Output = @()
138+
Draw-AsciiBanner -Lines @() -TextColor 'Cyan' -SeparatorColor 'Cyan'
139+
$script:Output[0] | Should -Match ''
140+
}
141+
}
142+
}
143+
144+
Context 'Integration with Console Colors' {
145+
It 'Should accept standard console color for text' {
146+
InModuleScope 'AsBuiltReport.Core' {
147+
$Colors = @('Cyan', 'Yellow', 'Green', 'Red', 'Blue')
148+
Mock Write-Host { }
149+
foreach ($Color in $Colors) {
150+
{ Draw-AsciiBanner -Lines @('Test') -TextColor $Color -SeparatorColor 'Cyan' } | Should -Not -Throw
151+
}
152+
}
153+
}
154+
155+
It 'Should accept standard console color for separator' {
156+
InModuleScope 'AsBuiltReport.Core' {
157+
$Colors = @('Cyan', 'Yellow', 'Green', 'Red', 'Blue')
158+
Mock Write-Host { }
159+
foreach ($Color in $Colors) {
160+
{ Draw-AsciiBanner -Lines @('Test') -TextColor 'White' -SeparatorColor $Color } | Should -Not -Throw
161+
}
162+
}
163+
}
164+
}
165+
}
166+
167+
AfterAll {
168+
Remove-Module -Name 'AsBuiltReport.Core' -Force -ErrorAction SilentlyContinue
169+
}
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
BeforeAll {
2+
# Import the module
3+
$ModulePath = Join-Path -Path $PSScriptRoot -ChildPath '..\..\AsBuiltReport.Core\AsBuiltReport.Core.psd1'
4+
Import-Module $ModulePath -Force
5+
}
6+
7+
Describe 'Get-RequiredModule Unit Tests' {
8+
Context 'Function Exists' {
9+
It 'Should have Get-RequiredModule function available' {
10+
InModuleScope 'AsBuiltReport.Core' {
11+
$Function = Get-Command -Name 'Get-RequiredModule' -ErrorAction SilentlyContinue
12+
$Function | Should -Not -BeNullOrEmpty
13+
}
14+
}
15+
16+
It 'Should be a CmdletBinding function' {
17+
InModuleScope 'AsBuiltReport.Core' {
18+
$Function = Get-Command -Name 'Get-RequiredModule'
19+
$Function.CmdletBinding | Should -Be $true
20+
}
21+
}
22+
}
23+
24+
Context 'Parameter Validation' {
25+
It 'Should have Name parameter' {
26+
InModuleScope 'AsBuiltReport.Core' {
27+
$Command = Get-Command -Name 'Get-RequiredModule'
28+
$Command.Parameters.Keys | Should -Contain 'Name'
29+
}
30+
}
31+
32+
It 'Should have Version parameter' {
33+
InModuleScope 'AsBuiltReport.Core' {
34+
$Command = Get-Command -Name 'Get-RequiredModule'
35+
$Command.Parameters.Keys | Should -Contain 'Version'
36+
}
37+
}
38+
39+
It 'Should have Name as mandatory parameter' {
40+
InModuleScope 'AsBuiltReport.Core' {
41+
$Command = Get-Command -Name 'Get-RequiredModule'
42+
$Parameter = $Command.Parameters['Name']
43+
$Parameter.Attributes.Mandatory | Should -Contain $true
44+
}
45+
}
46+
47+
It 'Should have Version as mandatory parameter' {
48+
InModuleScope 'AsBuiltReport.Core' {
49+
$Command = Get-Command -Name 'Get-RequiredModule'
50+
$Parameter = $Command.Parameters['Version']
51+
$Parameter.Attributes.Mandatory | Should -Contain $true
52+
}
53+
}
54+
}
55+
56+
Context 'Module Version Checking' -Skip:($PSVersionTable.PSEdition -ne 'Core') {
57+
It 'Should pass when module version meets requirement' {
58+
InModuleScope 'AsBuiltReport.Core' {
59+
Mock Get-Module {
60+
[PSCustomObject]@{
61+
Name = 'TestModule'
62+
Version = [Version]'2.0.0'
63+
}
64+
}
65+
$global:translate = @{
66+
RequiredModuleNotInstalled = 'Module {0} version {1} or higher is required'
67+
RequiredModuleTooOld = 'Module {0} version {1} is installed, but version {2} or higher is required'
68+
}
69+
{ Get-RequiredModule -Name 'TestModule' -Version '1.0.0' } | Should -Not -Throw
70+
}
71+
}
72+
73+
It 'Should pass when module version exactly matches requirement' {
74+
InModuleScope 'AsBuiltReport.Core' {
75+
Mock Get-Module {
76+
[PSCustomObject]@{
77+
Name = 'TestModule'
78+
Version = [Version]'2.0.0'
79+
}
80+
}
81+
$global:translate = @{
82+
RequiredModuleNotInstalled = 'Module {0} version {1} or higher is required'
83+
RequiredModuleTooOld = 'Module {0} version {1} is installed, but version {2} or higher is required'
84+
}
85+
{ Get-RequiredModule -Name 'TestModule' -Version '2.0.0' } | Should -Not -Throw
86+
}
87+
}
88+
89+
It 'Should throw when module is not installed' {
90+
InModuleScope 'AsBuiltReport.Core' {
91+
Mock Get-Module { $null }
92+
$global:translate = @{
93+
RequiredModuleNotInstalled = 'Module {0} version {1} or higher is required'
94+
RequiredModuleTooOld = 'Module {0} version {1} is installed, but version {2} or higher is required'
95+
}
96+
{ Get-RequiredModule -Name 'NonExistentModule' -Version '1.0.0' } | Should -Throw
97+
}
98+
}
99+
100+
It 'Should throw when module version is too old' {
101+
InModuleScope 'AsBuiltReport.Core' {
102+
Mock Get-Module {
103+
[PSCustomObject]@{
104+
Name = 'TestModule'
105+
Version = [Version]'1.0.0'
106+
}
107+
}
108+
$global:translate = @{
109+
RequiredModuleNotInstalled = 'Module {0} version {1} or higher is required'
110+
RequiredModuleTooOld = 'Module {0} version {1} is installed, but version {2} or higher is required'
111+
}
112+
{ Get-RequiredModule -Name 'TestModule' -Version '2.0.0' } | Should -Throw
113+
}
114+
}
115+
116+
It 'Should call Get-Module with correct parameters' {
117+
InModuleScope 'AsBuiltReport.Core' {
118+
Mock Get-Module {
119+
[PSCustomObject]@{
120+
Name = 'TestModule'
121+
Version = [Version]'2.0.0'
122+
}
123+
}
124+
$global:translate = @{
125+
RequiredModuleNotInstalled = 'Module {0} version {1} or higher is required'
126+
RequiredModuleTooOld = 'Module {0} version {1} is installed, but version {2} or higher is required'
127+
}
128+
Get-RequiredModule -Name 'TestModule' -Version '1.0.0'
129+
Should -Invoke Get-Module -ParameterFilter {
130+
$ListAvailable -eq $true -and $Name -eq 'TestModule'
131+
}
132+
}
133+
}
134+
}
135+
}
136+
137+
AfterAll {
138+
Remove-Module -Name 'AsBuiltReport.Core' -Force -ErrorAction SilentlyContinue
139+
Remove-Variable -Name 'translate' -Scope Global -ErrorAction SilentlyContinue
140+
}

0 commit comments

Comments
 (0)