Skip to content

Commit 6bab12d

Browse files
🩹 [Patch]: Update Format-Hashtable to handle objects (#11)
## Description This pull request includes updates to the `Format-Hashtable` function to improve type handling and formatting, as well as adding corresponding tests in `Hashtable.Tests.ps1`. ### Improvements to `Format-Hashtable`: * Changed the parameter type for `Hashtable` to `System.Collections.IDictionary` to ensure compatibility with different dictionary types. * Simplified the check for an empty hashtable by removing the redundant type check. * Unified the condition for nested hashtable formatting to use `System.Collections.IDictionary` for consistency. * Updated the handling of `PSCustomObject` and `PSObject` values to use `ConvertTo-Hashtable` for proper conversion before formatting. ### Enhancements to `Hashtable.Tests.ps1`: * Added comprehensive test cases to cover various scenarios including nested objects and arrays of hashtables. ## Type of change <!-- Use the check-boxes [x] on the options that are relevant. --> - [ ] 📖 [Docs] - [ ] 🪲 [Fix] - [x] 🩹 [Patch] - [ ] ⚠️ [Security fix] - [ ] 🚀 [Feature] - [ ] 🌟 [Breaking change] ## Checklist <!-- Use the check-boxes [x] on the options that are relevant. --> - [x] I have performed a self-review of my own code - [x] I have commented my code, particularly in hard-to-understand areas
1 parent 4894d92 commit 6bab12d

2 files changed

Lines changed: 56 additions & 5 deletions

File tree

src/functions/public/Format-Hashtable.ps1

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,15 @@
5656
ValueFromPipeline,
5757
ValueFromPipelineByPropertyName
5858
)]
59-
[object] $Hashtable,
59+
[System.Collections.IDictionary] $Hashtable,
6060

6161
# The indentation level for formatting nested structures.
6262
[Parameter()]
6363
[int] $IndentLevel = 1
6464
)
6565

6666
# If the hashtable is empty, return '@{}' immediately.
67-
if ($Hashtable -is [System.Collections.IDictionary] -and $Hashtable.Count -eq 0) {
67+
if ($Hashtable.Count -eq 0) {
6868
return '@{}'
6969
}
7070

@@ -88,14 +88,14 @@
8888
continue
8989
}
9090
Write-Verbose "Value type: [$($value.GetType().Name)]"
91-
if (($value -is [System.Collections.Hashtable]) -or ($value -is [System.Collections.Specialized.OrderedDictionary])) {
91+
if (($value -is [System.Collections.IDictionary])) {
9292
$nestedString = Format-Hashtable -Hashtable $value -IndentLevel ($IndentLevel + 1)
9393
$lines += "$levelIndent$paddedKey = $nestedString"
9494
} elseif ($value -is [System.Management.Automation.PSCustomObject]) {
95-
$nestedString = Format-Hashtable -Hashtable $value -IndentLevel ($IndentLevel + 1)
95+
$nestedString = $value | ConvertTo-Hashtable | Format-Hashtable -IndentLevel ($IndentLevel + 1)
9696
$lines += "$levelIndent$paddedKey = $nestedString"
9797
} elseif ($value -is [System.Management.Automation.PSObject]) {
98-
$nestedString = Format-Hashtable -Hashtable $value -IndentLevel ($IndentLevel + 1)
98+
$nestedString = $value | ConvertTo-Hashtable | Format-Hashtable -IndentLevel ($IndentLevel + 1)
9999
$lines += "$levelIndent$paddedKey = $nestedString"
100100
} elseif ($value -is [bool]) {
101101
$lines += "$levelIndent$paddedKey = `$$($value.ToString().ToLower())"

tests/Hashtable.Tests.ps1

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,29 @@
402402
}
403403
)
404404
}
405+
Run = [ordered]@{
406+
ABoolean = $true
407+
AString = 'Hello'
408+
AnArray = @('One', 'Two', 'Three')
409+
AnObject = [ordered]@{
410+
NestedKey1 = 'NestedValue1'
411+
NestedKey2 = 'NestedValue2'
412+
}
413+
AnArrayOfHashtables = @(
414+
[ordered]@{
415+
Path = 'C:\Repos\GitHub\PSModule\Action\Invoke-Pester\tests\3-Advanced\Planets\Planets.Tests.ps1'
416+
Data = [pscustomobject]@{
417+
Path = 'C:\Repos\GitHub\PSModule\Action\Invoke-Pester\tests\3-Advanced\Planets\Planets.Data.ps1'
418+
}
419+
},
420+
[ordered]@{
421+
Path = 'C:\Repos\GitHub\PSModule\Action\Invoke-Pester\tests\3-Advanced\Sheep\Sheep.Tests.ps1'
422+
Data = [pscustomobject]@{
423+
Path = 'C:\Repos\GitHub\PSModule\Action\Invoke-Pester\tests\3-Advanced\Sheep\Sheep.Data.ps1'
424+
}
425+
}
426+
)
427+
}
405428
}
406429

407430
# Act - Run the function
@@ -441,7 +464,35 @@
441464
}
442465
)
443466
}
467+
Run = @{
468+
ABoolean = $true
469+
AString = 'Hello'
470+
AnArray = @(
471+
'One'
472+
'Two'
473+
'Three'
474+
)
475+
AnObject = @{
476+
NestedKey1 = 'NestedValue1'
477+
NestedKey2 = 'NestedValue2'
478+
}
479+
AnArrayOfHashtables = @(
480+
@{
481+
Path = 'C:\Repos\GitHub\PSModule\Action\Invoke-Pester\tests\3-Advanced\Planets\Planets.Tests.ps1'
482+
Data = @{
483+
Path = 'C:\Repos\GitHub\PSModule\Action\Invoke-Pester\tests\3-Advanced\Planets\Planets.Data.ps1'
484+
}
485+
}
486+
@{
487+
Path = 'C:\Repos\GitHub\PSModule\Action\Invoke-Pester\tests\3-Advanced\Sheep\Sheep.Tests.ps1'
488+
Data = @{
489+
Path = 'C:\Repos\GitHub\PSModule\Action\Invoke-Pester\tests\3-Advanced\Sheep\Sheep.Data.ps1'
490+
}
491+
}
492+
)
493+
}
444494
}
495+
445496
'@.Trim() # Trim to remove any unintended whitespace
446497

447498
# Compare function output to expected output

0 commit comments

Comments
 (0)