Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add -TrimWhitespace to Should-BeString #2501

Merged
merged 1 commit into from
Jun 15, 2024
Merged
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
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+$'
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Regex over Trim() because $Actual might not be string?

}

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