Skip to content

Commit

Permalink
Add -Count to Should-BeCollection
Browse files Browse the repository at this point in the history
  • Loading branch information
nohwnd committed Oct 7, 2024
1 parent 2238989 commit 30ce6e9
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 6 deletions.
25 changes: 19 additions & 6 deletions src/functions/assert/Collection/Should-BeCollection.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 <expectedType> <expected> is not a collection."
if (-not (Is-Collection -Value $Actual)) {
$Message = Get-AssertionMessage -Expected $Expected -Actual $Actual -Because $Because -DefaultMessage "Actual <actualType> <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 <actualType> <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 <expected> items in <actualType> <actual>, but it has <actualCount> 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 <expectedType> <expected> is not a collection."
throw [Pester.Factory]::CreateShouldErrorRecord($Message, $MyInvocation.ScriptName, $MyInvocation.ScriptLineNumber, $MyInvocation.Line.TrimEnd([System.Environment]::NewLine), $true)
}

Expand Down
17 changes: 17 additions & 0 deletions tst/functions/assert/Collection/Should-BeCollection.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}

0 comments on commit 30ce6e9

Please sign in to comment.