Skip to content

Commit

Permalink
Add -TrimWhitespace to Should-BeString (#2501)
Browse files Browse the repository at this point in the history
  • Loading branch information
nohwnd authored Jun 15, 2024
1 parent f6ce6dd commit 9b11cad
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
16 changes: 13 additions & 3 deletions src/functions/assert/String/Should-BeString.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
[String]$Expected,
$Actual,
[switch]$CaseSensitive,
[switch]$IgnoreWhitespace
[switch]$IgnoreWhitespace,
[switch]$TrimWhitespace
)

if ($Actual -isnot [string]) {
Expand All @@ -15,6 +16,11 @@
$Actual = $Actual -replace '\s'
}

if ($TrimWhitespace) {
$Expected = $Expected -replace '^\s+|\s+$'
$Actual = $Actual -replace '^\s+|\s+$'
}

if (-not $CaseSensitive) {
$Expected -eq $Actual
}
Expand Down Expand Up @@ -43,6 +49,9 @@ function Should-BeString {
.PARAMETER IgnoreWhitespace
Indicates that the comparison should ignore whitespace.
.PARAMETER TrimWhitespace
Trims whitespace at the start and end of the string.
.PARAMETER Because
The reason why the actual value should be equal to the expected value.
Expand Down Expand Up @@ -77,13 +86,14 @@ function Should-BeString {
[String]$Expected,
[String]$Because,
[switch]$CaseSensitive,
[switch]$IgnoreWhitespace
[switch]$IgnoreWhitespace,
[switch]$TrimWhitespace
)

$collectedInput = Collect-Input -ParameterInput $Actual -PipelineInput $local:Input -IsPipelineInput $MyInvocation.ExpectingInput -UnrollInput
$Actual = $collectedInput.Actual

$stringsAreEqual = Test-StringEqual -Expected $Expected -Actual $Actual -CaseSensitive:$CaseSensitive -IgnoreWhitespace:$IgnoreWhiteSpace
$stringsAreEqual = Test-StringEqual -Expected $Expected -Actual $Actual -CaseSensitive:$CaseSensitive -IgnoreWhitespace:$IgnoreWhiteSpace -TrimWhitespace:$TrimWhitespace
if (-not ($stringsAreEqual)) {
$Message = Get-AssertionMessage -Expected $Expected -Actual $Actual -Because $Because -DefaultMessage "Expected <expectedType> <expected>, but got <actualType> <actual>."
throw [Pester.Factory]::CreateShouldErrorRecord($Message, $MyInvocation.ScriptName, $MyInvocation.ScriptLineNumber, $MyInvocation.Line.TrimEnd([System.Environment]::NewLine), $true)
Expand Down
13 changes: 13 additions & 0 deletions tst/functions/assert/String/Should-BeString.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,19 @@ Describe "Should-BeString" {
It "Can compare strings without whitespace" {
Should-BeString -Expected " a b c " -Actual "abc" -IgnoreWhitespace
}

It "Can compare strings without whitespace at the start or end" -ForEach @(
@{ Value = " abc" }
@{ Value = "abc " }
@{ Value = "abc`t" }
@{ Value = "`tabc" }
) {
" abc " | Should-BeString -Expected "abc" -TrimWhitespace
}

It "Trimming whitespace does not remove it from inside of the string" {
{ "a bc" | Should-BeString -Expected "abc" -TrimWhitespace } | Verify-AssertionFailed
}
}

It "Can be called with positional parameters" {
Expand Down

0 comments on commit 9b11cad

Please sign in to comment.