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 + } + } }