Skip to content

Commit 8a6a88c

Browse files
🩹 [Patch]: Handle empty hashtable in Format-Hashtable function (#5)
## Description This pull request includes changes to the `Format-Hashtable` function and its corresponding tests to handle empty hashtables correctly. The most important changes are as follows: Improvements to `Format-Hashtable` function: * [`src/functions/public/Format-Hashtable.ps1`](diffhunk://#diff-7d90dfc5bc21b6d23ceed48105316975686cabff108964a18fa84407c1429f87R68-R72): Added a check to return '@{}' immediately if the hashtable is empty. Enhancements to tests: * [`tests/Hashtable.Tests.ps1`](diffhunk://#diff-0d9a4623edf86d4025f7602749bc2c1397bec49ddcbb1fa7b51fe34634049687R258-R267): Added a new test context to ensure that the `Format-Hashtable` function returns an empty hashtable string when given an empty hashtable. ## 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 63c76f7 commit 8a6a88c

2 files changed

Lines changed: 17 additions & 0 deletions

File tree

src/functions/public/Format-Hashtable.ps1

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@
6565
[int] $IndentLevel = 1
6666
)
6767

68+
# If the hashtable is empty, return '@{}' immediately.
69+
if ($Hashtable -is [System.Collections.IDictionary] -and $Hashtable.Count -eq 0) {
70+
return '@{}'
71+
}
72+
6873
$indent = ' '
6974
$lines = @()
7075
$lines += '@{'

tests/Hashtable.Tests.ps1

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,16 +255,28 @@
255255
}
256256

257257
Describe 'Format-Hashtable' {
258+
Context 'An empty hashtable' {
259+
It 'returns an empty hashtable string' {
260+
$ht = @{}
261+
$expected = '@{}'
262+
263+
$result = Format-Hashtable -Hashtable $ht
264+
$result | Should -Be $expected
265+
}
266+
}
267+
258268
Context 'Simple Hashtable' {
259269
It 'formats a simple hashtable correctly' {
260270
$ht = [ordered]@{
261271
Key1 = 'Value1'
262272
Key2 = 123
273+
Key3 = @{}
263274
}
264275
$expected = @'
265276
@{
266277
Key1 = 'Value1'
267278
Key2 = 123
279+
Key3 = @{}
268280
}
269281
'@.TrimEnd()
270282

0 commit comments

Comments
 (0)