Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/functions/assert/String/Should-BeString.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,13 @@ function Should-BeString {

This assertion will fail, because the actual value is not equal to the expected value.

.EXAMPLE
```powershell
"" | Should-BeString ""
```

This assertion will pass, because an empty string is allowed as the expected value.

.NOTES
The `Should-BeString` assertion is the opposite of the `Should-NotBeString` assertion.

Expand All @@ -88,6 +95,7 @@ function Should-BeString {
[Parameter(Position = 1, ValueFromPipeline = $true)]
$Actual,
[Parameter(Position = 0, Mandatory)]
[AllowEmptyString()]
[String]$Expected,
[String]$Because,
[switch]$CaseSensitive,
Expand Down
31 changes: 31 additions & 0 deletions tst/functions/assert/String/Should-BeString.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,37 @@ Describe "Should-BeString" {
"abc" | Should-BeString "abc"
}

Context "Empty expected string" {
It "Passes when both expected and actual are empty strings" {
Should-BeString -Expected "" -Actual ""
}

It "Passes when expected is empty and actual is passed by pipeline" {
"" | Should-BeString -Expected ""
}

It "Passes when empty expected is passed by position" {
"" | Should-BeString ""
}

It "Fails when expected is empty but actual is not" {
{ Should-BeString -Expected "" -Actual "abc" } | Verify-AssertionFailed
}

It "Throws with default message when expected is empty but actual is not" {
$err = { Should-BeString -Expected "" -Actual "abc" } | Verify-AssertionFailed
$err.Exception.Message | Verify-Equal (@'
Expected strings to be the same, but they were different.
Expected length: 0
Actual length: 3
Strings differ at index 0.
Expected: ''
But was: 'abc'
^
'@ -replace "`r`n", "`n")
}
}

It "Fails when collection of strings is passed in by pipeline, even if the last string is the same as the expected string" {
{ "bde", "abc" | Should-BeString -Expected "abc" } | Verify-AssertionFailed
}
Expand Down
Loading