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
14 changes: 1 addition & 13 deletions src/functions/assert/String/Should-BeString.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -149,22 +149,10 @@ function Get-StringDifferenceMessage {
$expectedExpanded = Expand-SpecialCharacters -InputObject $Expected
$actualExpanded = Expand-SpecialCharacters -InputObject $Actual

# Recompute difference index on expanded strings
$maxLength = [Math]::Max($expectedExpanded.Length, $actualExpanded.Length)
$expandedDiffIndex = $null
for ($i = 0; $i -lt $maxLength -and ($null -eq $expandedDiffIndex); ++$i) {
if ($CaseSensitive) {
if ($expectedExpanded[$i] -cne $actualExpanded[$i]) { $expandedDiffIndex = $i }
}
else {
if ($expectedExpanded[$i] -ne $actualExpanded[$i]) { $expandedDiffIndex = $i }
}
}

$prefix = "Expected: '"
$lines += "$prefix$expectedExpanded'"
$lines += "But was: '$actualExpanded'"
$lines += (' ' * ($prefix.Length - 1)) + ('-' * $expandedDiffIndex) + '^'
$lines += (' ' * ($prefix.Length - 1)) + ('-' * $differenceIndex) + '^'

$lines -join "`n"
}
18 changes: 1 addition & 17 deletions src/functions/assertions/Be.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -207,24 +207,8 @@ function Get-CompareStringMessage {
"Strings differ at index $differenceIndex."
}

# find the difference in the string with expanded characters, this is the fastest and most foolproof way of
# getting the updated difference index. we could also inspect the new string and try to find every occurrence
# of special character before the difference index, but '\n' is valid piece of string
# or inspect the original string, but then we need to make sure that we look for all the special characters.
# instead we just compare it again.

$actualExpanded = Expand-SpecialCharacters -InputObject $actual
$expectedExpanded = Expand-SpecialCharacters -InputObject $ExpectedValue
$maxLength = if ($expectedExpanded.Length -gt $actualExpanded.Length) { $expectedExpanded.Length } else { $actualExpanded.Length }
$differenceIndex = $null
for ($i = 0; $i -lt $maxLength -and ($null -eq $differenceIndex); ++$i) {
$differenceIndex = if ($CaseSensitive -and ($expectedExpanded[$i] -cne $actualExpanded[$i])) {
$i
}
elseif ($expectedExpanded[$i] -ne $actualExpanded[$i]) {
$i
}
}

$ellipsis = "..."
# we will surround the output with Expected: '' and But was: '', from which the Expected: '' is longer
Expand Down Expand Up @@ -312,7 +296,7 @@ function Expand-SpecialCharacters {
[AllowEmptyString()]
[string[]]$InputObject)
process {
$InputObject -replace "`n", "\n" -replace "`r", "\r" -replace "`t", "\t" -replace "`0", "\0" -replace "`b", "\b"
[Pester.Formatter]::EscapeControlChars($InputObject)
}
}

Expand Down
6 changes: 3 additions & 3 deletions tst/functions/assert/String/Should-BeString.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,9 @@ Expected strings to be the same, but they were different.
Expected length: 8
Actual length: 7
Strings differ at index 3.
Expected: 'abc\r\ndef'
But was: 'abc\ndef'
----^
Expected: 'abc␍␊def'
But was: 'abc␊def'
---^
'@ -replace "`r`n", "`n")
}
}
6 changes: 3 additions & 3 deletions tst/functions/assertions/Be.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -151,15 +151,15 @@ InPesterModuleScope {
}

It "Replaces non-printable characters correctly" {
ShouldBeFailureMessage "`n`r`b`0`tx" "`n`r`b`0`ty" | Verify-Equal "Expected strings to be the same, but they were different.`nString lengths are both 6.`nStrings differ at index 5.`nExpected: '\n\r\b\0\ty'`nBut was: '\n\r\b\0\tx'`n ----------^"
ShouldBeFailureMessage "`n`r`b`0`tx" "`n`r`b`0`ty" | Verify-Equal "Expected strings to be the same, but they were different.`nString lengths are both 6.`nStrings differ at index 5.`nExpected: '␊␍␈␀␉y'`nBut was: '␊␍␈␀␉x'`n -----^"
}

It "The arrow points to the correct position when non-printable characters are replaced before the difference" {
ShouldBeFailureMessage "123`n456" "123`n789" | Verify-Equal "Expected strings to be the same, but they were different.`nString lengths are both 7.`nStrings differ at index 4.`nExpected: '123\n789'`nBut was: '123\n456'`n -----^"
ShouldBeFailureMessage "123`n456" "123`n789" | Verify-Equal "Expected strings to be the same, but they were different.`nString lengths are both 7.`nStrings differ at index 4.`nExpected: '123␊789'`nBut was: '123␊456'`n ----^"
}

It "The arrow points to the correct position when non-printable characters are replaced after the difference" {
ShouldBeFailureMessage "abcd`n123" "abc!`n123" | Verify-Equal "Expected strings to be the same, but they were different.`nString lengths are both 8.`nStrings differ at index 3.`nExpected: 'abc!\n123'`nBut was: 'abcd\n123'`n ---^"
ShouldBeFailureMessage "abcd`n123" "abc!`n123" | Verify-Equal "Expected strings to be the same, but they were different.`nString lengths are both 8.`nStrings differ at index 3.`nExpected: 'abc!␊123'`nBut was: 'abcd␊123'`n ---^"
}
}
}
Expand Down
Loading