Make Should-ContainCollection match a real sub-collection#2820
Merged
Conversation
Should-ContainCollection and Should-NotContainCollection used PowerShell's -contains operator, which only tests a single scalar value. The comment-based help, however, documented multi-item examples such as `1, 2, 3 | Should-ContainCollection @(1, 2)` and claimed they pass, when in fact they failed at runtime. The name and docs promised sub-collection containment that was never implemented. Implement an ordered-subsequence match instead: the expected items must appear in the actual collection in the same order, gaps are allowed, and each actual item is consumed at most once (so repeated expected items need at least as many matching actual items). A single value keeps the original one-item behaviour. The matching lives in a shared Is-CollectionSubsequence helper. The comment-based help for both assertions is rewritten to describe the real semantics, and the copy-paste error in Should-NotContainCollection's examples is fixed. Tests cover contiguous, gapped, out-of-order and duplicate cases. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Merged
nohwnd
added a commit
that referenced
this pull request
Jun 30, 2026
* Show real sub-collection matching in Should-ContainCollection release notes Should-ContainCollection now matches an ordered sub-collection (gaps allowed) as of #2820, but the 6.0.0 release notes still only showed the single-value case. Update the example and the family table to describe the real behaviour. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * Bump prerelease to rc4 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --------- Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Should-ContainCollection/Should-NotContainCollectionwere implemented with PowerShell's-containsoperator, which only tests whether a collection contains a single scalar value. When the right-hand side is itself a collection,-containscompares it by object identity and essentially never matches.The comment-based help (which generates the pester.dev pages), and the assertion's name, told a different story — they documented multi-item examples and claimed they pass:
So the docs and the behaviour disagreed, and the promised sub-collection containment was never actually implemented.
Should-NotContainCollection's help also had a copy-paste error referencingShould-ContainCollectionin its examples.Change
Implement true ordered-subsequence containment:
The matching logic lives in a new shared
Is-CollectionSubsequencehelper (greedy two-pointer). The comment-based help for both assertions is rewritten to describe the real semantics, and the copy-paste error inShould-NotContainCollectionis fixed.Behaviour now
1, 2, 3 | Should-ContainCollection @(1, 2)1, 2, 3 | Should-ContainCollection @(1, 3)1, 2, 3 | Should-ContainCollection @(3, 2, 1)1, 2, 3 | Should-ContainCollection @(3, 4)1, 1, 2 | Should-ContainCollection @(1, 1)1, 2 | Should-ContainCollection @(1, 1)@('a','b','c') | Should-ContainCollection 'b'Tests
Added contiguous, gapped, out-of-order and duplicate cases to both test files. All 868 assert tests pass, and PSScriptAnalyzer is clean on the changed files.
Note
This changes the runtime behaviour of these two assertions to match what their name and documentation always described. Single-item containment — the only thing that actually worked before — is preserved.