diff --git a/Functions/Assertions/Be.Tests.ps1 b/Functions/Assertions/Be.Tests.ps1 index 9ef3ae0f9..b17661b64 100644 --- a/Functions/Assertions/Be.Tests.ps1 +++ b/Functions/Assertions/Be.Tests.ps1 @@ -109,6 +109,23 @@ InModuleScope Pester { #are not tested here thoroughly, but the behaviour was visually checked and is #implicitly tested by using the whole output in the following tests + It "Shows excerpted error messages correctly" { + $expected = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb" + $actual = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb" + { $actual | Should Be $expected } | Should Throw "Expected: '...aaaaabbbbb...'" + } + + It "Shows excerpted error messages correctly" { + $expected = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb" + $actual = "abb" + { $actual | Should Be $expected } | Should Throw "Expected: 'aaaaaaaaaa...'" + } + + It "Shows excerpted 'actual values' correctly" { + $expected = "aaab" + $actual = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb" + { $actual | Should Be $expected } | Should Throw "But was: 'aaaaaaaaaa...'" + } It "Returns nothing for two identical strings" { #this situation should actually never happen, as the code is called diff --git a/Functions/Assertions/Be.ps1 b/Functions/Assertions/Be.ps1 index e7582b2b6..5a34e9bb4 100644 --- a/Functions/Assertions/Be.ps1 +++ b/Functions/Assertions/Be.ps1 @@ -167,7 +167,6 @@ function Get-CompareStringMessage { } } - [string]$output = $null if ($null -ne $differenceIndex) { "Expected strings to be the same,$(Format-Because $Because) but they were different." @@ -180,9 +179,11 @@ function Get-CompareStringMessage { "String lengths are both $ExpectedValueLength." "Strings differ at index $differenceIndex." } - - "Expected: '{0}'" -f ( $ExpectedValue | Expand-SpecialCharacters ) - "But was: '{0}'" -f ( $actual | Expand-SpecialCharacters ) + $ellipsis = "..." + $excerptSize = 5; + $longStringOffset = 0 + "Expected: '{0}'" -f ( $ExpectedValue | Format-AsExcerpt -startIndex $differenceIndex -excerptSize $excerptSize -excerptMarker $ellipsis | Expand-SpecialCharacters ) + "But was: '{0}'" -f ( $actual | Format-AsExcerpt -startIndex $differenceIndex -excerptSize $excerptSize -excerptMarker $ellipsis | Expand-SpecialCharacters ) $specialCharacterOffset = $null if ($differenceIndex -ne 0) { @@ -193,9 +194,54 @@ function Get-CompareStringMessage { & $SafeCommands['Select-Object'] -ExpandProperty Count) } - '-' * ($differenceIndex + $specialCharacterOffset + 11) + '^' + # for excerpted strings, add in an additional length of arrow... + $excerptOffset = $ellipsis.Length + $excerptSize + if ($differenceIndex -ge $excerptOffset) { + $longStringOffset = $excerptOffset + } + + '-' * ( $differenceIndex + $specialCharacterOffset + 11 + $longStringOffset) + '^' } } +function Format-AsExcerpt { + param ( + [Parameter(Mandatory = $true, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)] + [AllowEmptyString()] + [string]$InputObject, + [Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true)] + [int]$startIndex, + [Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true)] + [int]$excerptSize, + [Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true)] + [string]$excerptMarker + ) + $InputObjectDisplay="" + $displayDifferenceIndex = $startIndex - $excerptSize + $maximumStringLength = 40 + $maximumSubstringLength = $excerptSize * 2 + $substringLength = $InputObject.Length - $displayDifferenceIndex + if ($substringLength -gt $maximumSubstringLength) { + $substringLength = $maximumSubstringLength + } + if ($displayDifferenceIndex + $substringLength -lt $InputObject.Length) { + $endExcerptMarker = $excerptMarker + } + if ($displayDifferenceIndex -lt 0) { + $displayDifferenceIndex = 0 + } + if ($InputObject.length -ge $maximumStringLength) { + if ($displayDifferenceIndex -ne 0) { + $InputObjectDisplay = $excerptMarker + } + $InputObjectDisplay += $InputObject.Substring($displayDifferenceIndex, $substringLength) + $endExcerptMarker + } + else { + $InputObjectDisplay = $InputObject + } + $InputObjectDisplay +} + + function Expand-SpecialCharacters { param ( @@ -291,4 +337,3 @@ function ReplaceValueInArray { } } } -