Skip to content

Allow empty string for Should-BeString -Expected#2863

Open
nohwnd wants to merge 1 commit into
mainfrom
nohwnd-should-bestring-allow-empty-expected
Open

Allow empty string for Should-BeString -Expected#2863
nohwnd wants to merge 1 commit into
mainfrom
nohwnd-should-bestring-allow-empty-expected

Conversation

@nohwnd

@nohwnd nohwnd commented Jul 10, 2026

Copy link
Copy Markdown
Member

Fix #2857

Problem

Should-BeString rejects an empty string for -Expected:

"" | Should-BeString ""
# Cannot bind argument to parameter 'Expected' because it is an empty string.

This forces users porting v5 -ForEach tests to branch between Should-BeString and Should-BeEmptyString depending on whether the expected value is empty.

Root cause

The rejection is not a deliberate assertion rule — there's no validation in the function body checking for empty. It's an implicit side effect of declaring the parameter as [Parameter(Mandatory)][String]$Expected: a mandatory, typed [String] parameter automatically refuses "" during parameter binding.

Everything downstream already handles empty strings:

  • Test-StringEqual compares empty strings correctly.
  • Get-StringDifferenceMessage already declares [AllowEmptyString()] on $Expected/$Actual.
  • The sibling Should-NotBeString doesn't mark Expected as Mandatory, so it already accepts empty strings — Should-BeString was the inconsistent one.

Change

  • Add [AllowEmptyString()] to $Expected (keeping Mandatory, so the argument is still required — it may just be empty).
  • Add a comment-based-help example.
  • Add tests covering: both empty (named, positional, pipeline) and the failure/diff message when expected is "" but actual is non-empty.

All 42 tests in Should-BeString.Tests.ps1 pass.

The -Expected parameter was declared Mandatory with a [String] type, which
implicitly rejects empty strings during parameter binding. There is no
assertion-level reason to reject an empty expected value: Test-StringEqual and
Get-StringDifferenceMessage already handle empty strings, and Should-NotBeString
already accepts them. Add [AllowEmptyString()] so the parameter stays required
but may be empty.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@nohwnd nohwnd added this to the 6.1.0 milestone Jul 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Should-BeString should allow empty strings for the -Expected parameter

1 participant