Skip to content

Commit

Permalink
Shorten error message of string comparison when it is too long (peste…
Browse files Browse the repository at this point in the history
…r#1248)

Shortens error messages on long strings to only show the excerpt where the first difference is shown.
  • Loading branch information
jd-porter authored and nohwnd committed Mar 16, 2019
1 parent 9390079 commit f5d5256
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 6 deletions.
17 changes: 17 additions & 0 deletions Functions/Assertions/Be.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
57 changes: 51 additions & 6 deletions Functions/Assertions/Be.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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."

Expand All @@ -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) {
Expand All @@ -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 (
Expand Down Expand Up @@ -291,4 +337,3 @@ function ReplaceValueInArray {
}
}
}

0 comments on commit f5d5256

Please sign in to comment.