diff --git a/src/functions/assert/String/Should-BeString.ps1 b/src/functions/assert/String/Should-BeString.ps1 index fedc3e097..8dc53d57f 100644 --- a/src/functions/assert/String/Should-BeString.ps1 +++ b/src/functions/assert/String/Should-BeString.ps1 @@ -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" } diff --git a/src/functions/assertions/Be.ps1 b/src/functions/assertions/Be.ps1 index 8e1356e08..891d405e7 100644 --- a/src/functions/assertions/Be.ps1 +++ b/src/functions/assertions/Be.ps1 @@ -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 @@ -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) } } diff --git a/tst/functions/assert/String/Should-BeString.Tests.ps1 b/tst/functions/assert/String/Should-BeString.Tests.ps1 index 4786c5b78..fdaaeabc4 100644 --- a/tst/functions/assert/String/Should-BeString.Tests.ps1 +++ b/tst/functions/assert/String/Should-BeString.Tests.ps1 @@ -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") } } diff --git a/tst/functions/assertions/Be.Tests.ps1 b/tst/functions/assertions/Be.Tests.ps1 index 4ff9de268..303294c28 100644 --- a/tst/functions/assertions/Be.Tests.ps1 +++ b/tst/functions/assertions/Be.Tests.ps1 @@ -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 ---^" } } }