Skip to content
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
10 changes: 5 additions & 5 deletions src/functions/assert/String/Should-BeString.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
[switch]$CaseSensitive,
[switch]$IgnoreWhitespace,
[switch]$TrimWhitespace,
[switch]$NormalizeNewline
[switch]$NormalizeLineEnding
)

if ($Actual -isnot [string]) {
return $false
}

if ($NormalizeNewline) {
if ($NormalizeLineEnding) {
$Expected = $Expected -replace '\r\n|\r|\p{Zl}', "`n"
$Actual = $Actual -replace '\r\n|\r|\p{Zl}', "`n"
}
Expand Down Expand Up @@ -58,7 +58,7 @@ function Should-BeString {
.PARAMETER TrimWhitespace
Trims whitespace at the start and end of the string.

.PARAMETER NormalizeNewline
.PARAMETER NormalizeLineEnding
Normalizes line endings before comparison, so that `\r\n`, `\r`, and the Unicode line separator are all treated as `\n`. Use this to compare multi-line strings across platforms without failing on line-ending style. Unlike `-IgnoreWhitespace`, the newlines and their positions are kept, so indentation and blank lines are still compared.

.PARAMETER Because
Expand Down Expand Up @@ -110,13 +110,13 @@ function Should-BeString {
[switch]$CaseSensitive,
[switch]$IgnoreWhitespace,
[switch]$TrimWhitespace,
[switch]$NormalizeNewline
[switch]$NormalizeLineEnding
)

$assert = New-ShouldAssertion -Caller $PSCmdlet -Actual $Actual -Buffer $local:Input
$Actual = $assert.Actual()

$stringsAreEqual = Test-StringEqual -Expected $Expected -Actual $Actual -CaseSensitive:$CaseSensitive -IgnoreWhitespace:$IgnoreWhiteSpace -TrimWhitespace:$TrimWhitespace -NormalizeNewline:$NormalizeNewline
$stringsAreEqual = Test-StringEqual -Expected $Expected -Actual $Actual -CaseSensitive:$CaseSensitive -IgnoreWhitespace:$IgnoreWhiteSpace -TrimWhitespace:$TrimWhitespace -NormalizeLineEnding:$NormalizeLineEnding
if (-not ($stringsAreEqual)) {
if ($Actual -is [string]) {
$assert.Fail((Get-StringDifferenceMessage -Expected $Expected -Actual $Actual -CaseSensitive:$CaseSensitive -Because $Because))
Expand Down
6 changes: 3 additions & 3 deletions src/functions/assert/String/Should-NotBeString.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ function Should-NotBeString {
.PARAMETER TrimWhitespace
Trims whitespace at the start and end of the string.

.PARAMETER NormalizeNewline
.PARAMETER NormalizeLineEnding
Normalizes line endings before comparison, so that `\r\n`, `\r`, and the Unicode line separator are all treated as `\n`. Use this to compare multi-line strings across platforms without failing on line-ending style. Unlike `-IgnoreWhitespace`, the newlines and their positions are kept, so indentation and blank lines are still compared.

.PARAMETER Because
Expand Down Expand Up @@ -69,7 +69,7 @@ function Should-NotBeString {
[switch]$CaseSensitive,
[switch]$IgnoreWhitespace,
[switch]$TrimWhitespace,
[switch]$NormalizeNewline
[switch]$NormalizeLineEnding
)

$assert = New-ShouldAssertion -Caller $PSCmdlet -Actual $Actual -Buffer $local:Input
Expand All @@ -79,7 +79,7 @@ function Should-NotBeString {
throw [ArgumentException]"Actual is expected to be string, to avoid confusing behavior that -ne operator exhibits with collections. To assert on collections use Should-Any, Should-All or some other collection assertion."
}

if (Test-StringEqual -Expected $Expected -Actual $Actual -CaseSensitive:$CaseSensitive -IgnoreWhitespace:$IgnoreWhiteSpace -TrimWhitespace:$TrimWhitespace -NormalizeNewline:$NormalizeNewline) {
if (Test-StringEqual -Expected $Expected -Actual $Actual -CaseSensitive:$CaseSensitive -IgnoreWhitespace:$IgnoreWhiteSpace -TrimWhitespace:$TrimWhitespace -NormalizeLineEnding:$NormalizeLineEnding) {
if (-not $CustomMessage) {
$formattedMessage = Get-StringNotEqualDefaultFailureMessage -Expected $Expected -Actual $Actual
}
Expand Down
10 changes: 5 additions & 5 deletions tst/functions/assert/String/Should-BeString.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,15 @@ InPesterModuleScope {
@{ l = "a`r`nb"; r = "a`rb"; lName = "CRLF"; rName = "CR" },
@{ l = "a$([char]0x2028)b"; r = "a`nb"; lName = "LS"; rName = "LF" }
) {
Test-StringEqual -Expected $l -Actual $r -NormalizeNewline | Verify-True
Test-StringEqual -Expected $l -Actual $r -NormalizeLineEnding | Verify-True
}

It "strings that differ in more than line ending style are not equal" {
Test-StringEqual -Expected "a`r`nb" -Actual "a`nc" -NormalizeNewline | Verify-False
Test-StringEqual -Expected "a`r`nb" -Actual "a`nc" -NormalizeLineEnding | Verify-False
}

It "keeps newline positions, so extra blank lines still differ" {
Test-StringEqual -Expected "a`r`nb" -Actual "a`r`n`r`nb" -NormalizeNewline | Verify-False
Test-StringEqual -Expected "a`r`nb" -Actual "a`r`n`r`nb" -NormalizeLineEnding | Verify-False
}
}
}
Expand Down Expand Up @@ -162,11 +162,11 @@ But was: 'abc'
}

It "Can compare multi-line strings ignoring line ending style" {
"line1`nline2" | Should-BeString -Expected "line1`r`nline2" -NormalizeNewline
"line1`nline2" | Should-BeString -Expected "line1`r`nline2" -NormalizeLineEnding
}

It "Normalizing newlines still compares indentation and blank lines" {
{ "a`nb" | Should-BeString -Expected "a`n`nb" -NormalizeNewline } | Verify-AssertionFailed
{ "a`nb" | Should-BeString -Expected "a`n`nb" -NormalizeLineEnding } | Verify-AssertionFailed
}
}

Expand Down
4 changes: 2 additions & 2 deletions tst/functions/assert/String/Should-NotBeString.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,11 @@ Describe "Should-NotBeString" {
}

It "Fails when strings differ only in line ending style" {
{ "line1`nline2" | Should-NotBeString -Expected "line1`r`nline2" -NormalizeNewline } | Verify-AssertionFailed
{ "line1`nline2" | Should-NotBeString -Expected "line1`r`nline2" -NormalizeLineEnding } | Verify-AssertionFailed
}

It "Passes when strings differ in more than line ending style" {
"a`nb" | Should-NotBeString -Expected "a`n`nb" -NormalizeNewline
"a`nb" | Should-NotBeString -Expected "a`n`nb" -NormalizeLineEnding
}
}

Expand Down
Loading