From d6b32b2a262fe412b785a3131afebc44b88e576f Mon Sep 17 00:00:00 2001 From: Frode Flaten <3436158+fflaten@users.noreply.github.com> Date: Tue, 28 Mar 2023 19:31:51 +0000 Subject: [PATCH] Format single item arrays as an array --- src/Format.ps1 | 9 +++++---- tst/Format.Tests.ps1 | 3 ++- tst/functions/assertions/HaveCount.Tests.ps1 | 2 +- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/src/Format.ps1 b/src/Format.ps1 index 761cc4e20..7112ba300 100644 --- a/src/Format.ps1 +++ b/src/Format.ps1 @@ -121,6 +121,11 @@ function Format-Nicely ($Value, [switch]$Pretty) { return Format-ScriptBlock -Value $Value } + # Check collection before Is-Value to format single item arrays + if (Is-Collection -Value $Value) { + return Format-Collection -Value $Value -Pretty:$Pretty + } + if (Is-Value -Value $Value) { return $Value } @@ -137,10 +142,6 @@ function Format-Nicely ($Value, [switch]$Pretty) { #return Format-Dictionary -Value $Value } - if (Is-Collection -Value $Value) { - return Format-Collection -Value $Value -Pretty:$Pretty - } - # no advanced formatting of objects in the first version, till I balance it return [string]$Value # Format-Object -Value $Value -Property (Get-DisplayProperty $Value) -Pretty:$Pretty diff --git a/tst/Format.Tests.ps1 b/tst/Format.Tests.ps1 index 8c11484d7..f48916da3 100644 --- a/tst/Format.Tests.ps1 +++ b/tst/Format.Tests.ps1 @@ -148,7 +148,8 @@ Describe "Format-Nicely" { @{ Value = 1; Expected = '1' }, @{ Value = (1, 2, 3); Expected = '@(1, 2, 3)' }, @{ Value = 1.1; Expected = '1.1' }, - @{ Value = [int]; Expected = '[int]' } + @{ Value = [int]; Expected = '[int]' }, + @{ Value = @('a'); Expected = "@('a')" } # @{ Value = [PSCustomObject] @{ Name = "Jakub" }; Expected = 'PSObject{Name=Jakub}' }, #@{ Value = (Get-Process Idle); Expected = 'Diagnostics.Process{Id=0; Name=Idle}'}, #@{ Value = (New-Object -Type Assertions.TestType.Person -Property @{Name = 'Jakub'; Age = 28}); Expected = "Assertions.TestType.Person{Age=28; Name=Jakub}"} diff --git a/tst/functions/assertions/HaveCount.Tests.ps1 b/tst/functions/assertions/HaveCount.Tests.ps1 index e87329261..eb6c84f9e 100644 --- a/tst/functions/assertions/HaveCount.Tests.ps1 +++ b/tst/functions/assertions/HaveCount.Tests.ps1 @@ -35,7 +35,7 @@ InPesterModuleScope { It "returns the correct assertion message when collection is not empty" { $err = { @(1) | Should -HaveCount 0 -Because 'reason' } | Verify-AssertionFailed - $err.Exception.Message | Verify-Equal 'Expected an empty collection, because reason, but got collection with size 1 1.' + $err.Exception.Message | Verify-Equal 'Expected an empty collection, because reason, but got collection with size 1 @(1).' } It "validates the expected size to be bigger than 0" {