From 30ce6e900267d98f94231a5e26cc5595b0298337 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Jare=C5=A1?= Date: Mon, 7 Oct 2024 21:22:52 +0200 Subject: [PATCH 1/2] Add -Count to Should-BeCollection --- .../assert/Collection/Should-BeCollection.ps1 | 25 ++++++++++++++----- .../Collection/Should-BeCollection.Tests.ps1 | 17 +++++++++++++ 2 files changed, 36 insertions(+), 6 deletions(-) diff --git a/src/functions/assert/Collection/Should-BeCollection.ps1 b/src/functions/assert/Collection/Should-BeCollection.ps1 index c6643dcce..7319bd5d5 100644 --- a/src/functions/assert/Collection/Should-BeCollection.ps1 +++ b/src/functions/assert/Collection/Should-BeCollection.ps1 @@ -9,6 +9,9 @@ .PARAMETER Actual A collection of items. + .PARAMETER Count + Checks if the collection has the expected number of items. + .PARAMETER Because The reason why the input should be the expected value. @@ -41,21 +44,31 @@ param ( [Parameter(Position = 1, ValueFromPipeline = $true)] $Actual, - [Parameter(Position = 0, Mandatory)] + [Parameter(Position = 0, Mandatory, ParameterSetName = 'Expected')] $Expected, - [String]$Because + [String]$Because, + [Parameter(ParameterSetName = 'Count')] + [int] $Count ) $collectedInput = Collect-Input -ParameterInput $Actual -PipelineInput $local:Input -IsPipelineInput $MyInvocation.ExpectingInput $Actual = $collectedInput.Actual - if (-not (Is-Collection -Value $Expected)) { - $Message = Get-AssertionMessage -Expected $Expected -Actual $Actual -Because $Because -DefaultMessage "Expected is not a collection." + if (-not (Is-Collection -Value $Actual)) { + $Message = Get-AssertionMessage -Expected $Expected -Actual $Actual -Because $Because -DefaultMessage "Actual is not a collection." throw [Pester.Factory]::CreateShouldErrorRecord($Message, $MyInvocation.ScriptName, $MyInvocation.ScriptLineNumber, $MyInvocation.Line.TrimEnd([System.Environment]::NewLine), $true) } - if (-not (Is-Collection -Value $Actual)) { - $Message = Get-AssertionMessage -Expected $Expected -Actual $Actual -Because $Because -DefaultMessage "Actual is not a collection." + if ($PSCmdlet.ParameterSetName -eq 'Count') { + if ($Count -ne $Actual.Count) { + $Message = Get-AssertionMessage -Expected $Count -Actual $Actual.Count -Because $Because -DefaultMessage "Expected items in , but it has items." + throw [Pester.Factory]::CreateShouldErrorRecord($Message, $MyInvocation.ScriptName, $MyInvocation.ScriptLineNumber, $MyInvocation.Line.TrimEnd([System.Environment]::NewLine), $true) + } + return + } + + if (-not (Is-Collection -Value $Expected)) { + $Message = Get-AssertionMessage -Expected $Expected -Actual $Actual -Because $Because -DefaultMessage "Expected is not a collection." throw [Pester.Factory]::CreateShouldErrorRecord($Message, $MyInvocation.ScriptName, $MyInvocation.ScriptLineNumber, $MyInvocation.Line.TrimEnd([System.Environment]::NewLine), $true) } diff --git a/tst/functions/assert/Collection/Should-BeCollection.Tests.ps1 b/tst/functions/assert/Collection/Should-BeCollection.Tests.ps1 index ef5dab0de..4219c1f44 100644 --- a/tst/functions/assert/Collection/Should-BeCollection.Tests.ps1 +++ b/tst/functions/assert/Collection/Should-BeCollection.Tests.ps1 @@ -24,4 +24,21 @@ Describe "Should-BeCollection" { $err = { $actual | Should-BeCollection $expected } | Verify-AssertionFailed $err.Exception.Message | Verify-Equal "Expected [Object[]] @(5, 6, 7, 8, 9) to be present in [Object[]] @(1, 2, 3, 4, 5) in any order, but some values were not.`nMissing in actual: '6 (index 1), 7 (index 2), 8 (index 3), 9 (index 4)'`nExtra in actual: '1 (index 0), 2 (index 1), 3 (index 2), 4 (index 3)'" } + + Describe "-Count" { + It "Counts empty collection @() correctly" { + @() | Should-BeCollection -Count 0 + } + + It "Counts collection with one item correctly" -ForEach @( + @(1), + (, @()), # array in array + @($null), + @(""), + # we also cannot distinguish between a single item and a single item array + 1 + ) { + $_ | Should-BeCollection -Count 1 + } + } } From 6fb23b02666b8a6111c5011e62e6b6fc5b900106 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Jare=C5=A1?= Date: Mon, 7 Oct 2024 21:30:38 +0200 Subject: [PATCH 2/2] Message fixes --- src/functions/assert/Collection/Should-BeCollection.ps1 | 4 ++-- .../assert/Collection/Should-BeCollection.Tests.ps1 | 5 +++++ 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/functions/assert/Collection/Should-BeCollection.ps1 b/src/functions/assert/Collection/Should-BeCollection.ps1 index 7319bd5d5..67af94ccd 100644 --- a/src/functions/assert/Collection/Should-BeCollection.ps1 +++ b/src/functions/assert/Collection/Should-BeCollection.ps1 @@ -61,7 +61,7 @@ if ($PSCmdlet.ParameterSetName -eq 'Count') { if ($Count -ne $Actual.Count) { - $Message = Get-AssertionMessage -Expected $Count -Actual $Actual.Count -Because $Because -DefaultMessage "Expected items in , but it has items." + $Message = Get-AssertionMessage -Expected $Count -Actual $Actual -Because $Because -Data @{ actualCount = $Actual.Count } -DefaultMessage "Expected items in , but it has items." throw [Pester.Factory]::CreateShouldErrorRecord($Message, $MyInvocation.ScriptName, $MyInvocation.ScriptLineNumber, $MyInvocation.Line.TrimEnd([System.Environment]::NewLine), $true) } return @@ -73,7 +73,7 @@ } if (-not (Is-CollectionSize -Expected $Expected -Actual $Actual)) { - $Message = Get-AssertionMessage -Expected $Expected -Actual $Actual -Because $Because -DefaultMessage "Expected to be present in , but they don't have the same number of items." + $Message = Get-AssertionMessage -Expected $Expected -Actual $Actual -Because $Because -DefaultMessage "Expected to be present in , but they don't have the same number of items." throw [Pester.Factory]::CreateShouldErrorRecord($Message, $MyInvocation.ScriptName, $MyInvocation.ScriptLineNumber, $MyInvocation.Line.TrimEnd([System.Environment]::NewLine), $true) } diff --git a/tst/functions/assert/Collection/Should-BeCollection.Tests.ps1 b/tst/functions/assert/Collection/Should-BeCollection.Tests.ps1 index 4219c1f44..7038df09c 100644 --- a/tst/functions/assert/Collection/Should-BeCollection.Tests.ps1 +++ b/tst/functions/assert/Collection/Should-BeCollection.Tests.ps1 @@ -40,5 +40,10 @@ Describe "Should-BeCollection" { ) { $_ | Should-BeCollection -Count 1 } + + It "Fails when collection does not have the expected number of items" { + $err = { @(1, 2) | Should-BeCollection -Count 3 } | Verify-AssertionFailed + $err.Exception.Message | Verify-Equal "Expected 3 items in [Object[]] @(1, 2), but it has 2 items." + } } }