diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/FormatAndOutput/format-hex/Format-Hex.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/FormatAndOutput/format-hex/Format-Hex.cs index 9a6fae44665..09df27f2a92 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/FormatAndOutput/format-hex/Format-Hex.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/FormatAndOutput/format-hex/Format-Hex.cs @@ -403,6 +403,7 @@ private byte[] ConvertToBytes(object inputObject) if (_lastInputType != null && baseType != _lastInputType) { _groupInput = false; + FlushInputBuffer(); } _lastInputType = baseType; diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Format-Hex.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Format-Hex.Tests.ps1 index 875273acac0..c0f3e65fb30 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Format-Hex.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Format-Hex.Tests.ps1 @@ -63,6 +63,12 @@ public enum TestSByteEnum : sbyte { } $testCases = @( + @{ + Name = "Can process bool type 'fhx -InputObject `$true'" + InputObject = $true + Count = 1 + ExpectedResult = "00000000 01 00 00 00" + } @{ Name = "Can process byte type 'fhx -InputObject [byte]5'" InputObject = [byte]5 @@ -159,6 +165,12 @@ public enum TestSByteEnum : sbyte { } $testCases = @( + @{ + Name = "Can process bool type '`$true | fhx'" + InputObject = $true + Count = 1 + ExpectedResult = "0000000000000000 01" + } @{ Name = "Can process byte type '[byte]5 | fhx'" InputObject = [byte]5 @@ -227,6 +239,13 @@ public enum TestSByteEnum : sbyte { ExpectedResult = "0000000000000000 F1 12 15 FB ñ��û" ExpectedSecondResult = "0000000000000000 01 02 03 04 05 06 ������" } + @{ + Name = "Can process jagged array type '[bool[]](`$true, `$false), [int[]](1, 2, 3, 4) | fhx'" + InputObject = [bool[]]($true, $false), [int[]](1, 2, 3, 4) + Count = 2 + ExpectedResult = "0000000000000000 01 00 00 00 00 00 00 00 �" + ExpectedSecondResult = "0000000000000000 01 00 00 00 02 00 00 00 03 00 00 00 04 00 00 00 � � � �" + } @{ Name = "Can process PS-native enum array '[TestEnum[]]('TestOne', 'TestTwo', 'TestThree', 'TestFour') | fhx'" InputObject = [TestEnum[]]('TestOne', 'TestTwo', 'TestThree', 'TestFour') @@ -291,6 +310,19 @@ public enum TestSByteEnum : sbyte { "System.UInt16[]" ).ForEach{ [regex]::Escape($_) } -join '|' } + @{ + InputScript = { $true, $false, $true, 123, 100, 76, $true, $false } + Count = 3 + ExpectedResults = @( + "0000000000000000 01 00 00 00 00 00 00 00 01 00 00 00 � �" + "0000000000000000 7B 00 00 00 64 00 00 00 4C 00 00 00 { d L" + "0000000000000000 01 00 00 00 00 00 00 00 �" + ) + ExpectedLabels = @( + "System.Boolean" + "System.Int32" + ).ForEach{ [regex]::Escape($_) } -join '|' + } ) It 'can process jagged input: ' -TestCases $heterogenousInputCases { @@ -356,8 +388,7 @@ public enum TestSByteEnum : sbyte { if ($PathCase) { $result = Format-Hex -Path $Path - } - else { + } else { # LiteralPath $result = Format-Hex -LiteralPath $Path } @@ -380,8 +411,7 @@ public enum TestSByteEnum : sbyte { $Result.Bytes[-1] | Should -Be 0x0A $Result.Bytes[-2] | Should -Be 0x0D $Result.Bytes.Length | Should -Be 14 - } - else { + } else { $Result.Bytes[-1] | Should -Be 0x0A $Result.Bytes.Length | Should -Be 13 } @@ -507,8 +537,7 @@ public enum TestSByteEnum : sbyte { if ($PathCase) { $output = Format-Hex -Path $InvalidPath, $inputFile1 -ErrorVariable errorThrown -ErrorAction SilentlyContinue - } - else { + } else { # LiteralPath $output = Format-Hex -LiteralPath $InvalidPath, $inputFile1 -ErrorVariable errorThrown -ErrorAction SilentlyContinue }