From 211dcb84b8f6b7cc64bbc119d4b41ca7f0562469 Mon Sep 17 00:00:00 2001 From: Jakub Jares Date: Thu, 18 Jun 2026 14:06:17 +0200 Subject: [PATCH] Use Unicode Control Pictures for special characters in assertions Replace \n, \r, \t etc. with visible Unicode glyphs (U+2400 block) in Expand-SpecialCharacters, reusing the existing C# implementation in [Pester.Formatter]::EscapeControlChars(). Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- src/functions/assertions/Be.ps1 | 2 +- .../assert/String/Should-BeString.Tests.ps1 | 12 +++--------- tst/functions/assertions/Be.Tests.ps1 | 6 +++--- 3 files changed, 7 insertions(+), 13 deletions(-) diff --git a/src/functions/assertions/Be.ps1 b/src/functions/assertions/Be.ps1 index 8e1356e08..ca0a61fe2 100644 --- a/src/functions/assertions/Be.ps1 +++ b/src/functions/assertions/Be.ps1 @@ -312,7 +312,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..6659404fa 100644 --- a/tst/functions/assert/String/Should-BeString.Tests.ps1 +++ b/tst/functions/assert/String/Should-BeString.Tests.ps1 @@ -155,14 +155,8 @@ But was: 'abc' It "Shows expanded whitespace characters in diff" { $err = { "abc`ndef" | Should-BeString "abc`r`ndef" } | Verify-AssertionFailed - $err.Exception.Message | Verify-Equal (@' -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' - ----^ -'@ -replace "`r`n", "`n") + $cr = [char]0x240D + $lf = [char]0x240A + $err.Exception.Message | Verify-Equal ("Expected strings to be the same, but they were different.`nExpected length: 8`nActual length: 7`nStrings differ at index 3.`nExpected: 'abc${cr}${lf}def'`nBut was: 'abc${lf}def'`n ---^" -replace "`r`n", "`n") } } diff --git a/tst/functions/assertions/Be.Tests.ps1 b/tst/functions/assertions/Be.Tests.ps1 index 4ff9de268..a72bd8e00 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: '$(([char]0x240A))$(([char]0x240D))$(([char]0x2408))$(([char]0x2400))$(([char]0x2409))y'`nBut was: '$(([char]0x240A))$(([char]0x240D))$(([char]0x2408))$(([char]0x2400))$(([char]0x2409))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$(([char]0x240A))789'`nBut was: '123$(([char]0x240A))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!$(([char]0x240A))123'`nBut was: 'abcd$(([char]0x240A))123'`n ---^" } } }