From 3e70a1c137cda698ee2a7c693748948db40a3be9 Mon Sep 17 00:00:00 2001 From: JD Porter Date: Sat, 2 Feb 2019 21:47:22 -0700 Subject: [PATCH 1/9] adding -like operator for Should-Throw --- Functions/Assertions/PesterThrow.Tests.ps1 | 23 ++++++++++++++++++++++ Functions/Assertions/PesterThrow.ps1 | 18 +++++++++++------ 2 files changed, 35 insertions(+), 6 deletions(-) diff --git a/Functions/Assertions/PesterThrow.Tests.ps1 b/Functions/Assertions/PesterThrow.Tests.ps1 index ecd0d498b..0185140f9 100644 --- a/Functions/Assertions/PesterThrow.Tests.ps1 +++ b/Functions/Assertions/PesterThrow.Tests.ps1 @@ -80,6 +80,29 @@ InModuleScope Pester { } } + + Context "Matching error pattern" { + It "given scriptblock that throws exception with the expected pattern it passes" { + $expectedErrorMessage = "expected error message" + $expectedErrorPattern = "*error*" + { throw $expectedErrorMessage } | Should -Throw -like $expectedErrorPattern + } + + It "given scriptblock that throws exception with the expected pattern in UPPERCASE it passes" { + $expectedErrorMessage = "expected error message" + $expectedErrorPattern = "*error*" + $errorMessage = $expectedErrorMessage.ToUpperInvariant() + { throw $errorMessage } | Should -Throw -like $expectedErrorPattern + } + + It "given scriptblock that throws exception with a non-matching pattern it fails" { + $expectedErrorMessage = "expected error message" + $unexpectedErrorPattern = "*pass*" + $errorMessage = $expectedErrorMessage.ToUpperInvariant() + { { throw $errorMessage } | Should -Throw -like $unexpectedErrorPattern } | Verify-AssertionFailed + } + } + Context "Matching ErrorId (FullyQualifiedErrorId)" { It "given scriptblock that throws exception with FullyQualifiedErrorId with the expected ErrorId it passes" { $expectedErrorId = "expected error id" diff --git a/Functions/Assertions/PesterThrow.ps1 b/Functions/Assertions/PesterThrow.ps1 index c4aa752eb..96a27c8f5 100644 --- a/Functions/Assertions/PesterThrow.ps1 +++ b/Functions/Assertions/PesterThrow.ps1 @@ -1,4 +1,4 @@ -function Should-Throw([scriptblock] $ActualValue, $ExpectedMessage, $ErrorId, [type]$ExceptionType, [switch] $Negate, [string] $Because, [switch] $PassThru) { +function Should-Throw([scriptblock] $ActualValue, $ExpectedMessage, $ErrorId, [type]$ExceptionType, [switch] $Negate, [switch] $Like, [string] $Because, [switch] $PassThru) { <# .SYNOPSIS Checks if an exception was thrown. Enclose input in a script block. @@ -105,8 +105,13 @@ It does not throw an error, so the test passes. $filterOnMessage = -not [string]::IsNullOrEmpty($ExpectedMessage -replace "\s") if ($filterOnMessage) { $filters += "with message $(Format-Nicely $ExpectedMessage)" - if ($actualExceptionWasThrown -and (-not (Get-DoValuesMatch $actualExceptionMessage $ExpectedMessage))) { - $buts += "the message was $(Format-Nicely $actualExceptionMessage)" + if ($actualExceptionWasThrown ) { + if (-not $like -and -not (Get-DoValuesMatch $actualExceptionMessage $ExpectedMessage)) { + $buts += "the message was $(Format-Nicely $actualExceptionMessage)" + } + if ($Like -and -not ($actualExceptionMessage -like $ExpectedMessage)) { + $buts += "the message did not match the expression $(Format-Nicely $actualExceptionMessage)" + } } } @@ -116,9 +121,6 @@ It does not throw an error, so the test passes. if ($actualExceptionWasThrown -and (-not (Get-DoValuesMatch $actualErrorId $ErrorId))) { $buts += "the FullyQualifiedErrorId was $(Format-Nicely $actualErrorId)" } - } - - if (-not $actualExceptionWasThrown) { $buts += "no exception was thrown" } @@ -170,3 +172,7 @@ function NotShouldThrowFailureMessage { Add-AssertionOperator -Name Throw ` -InternalName Should-Throw ` -Test ${function:Should-Throw} + +Add-AssertionOperator -Name Throw ` + -InternalName Should-Throw ` + -Test ${function:Should-Throw} From 115349225c0438f2eac9d727c2216db28b60b344 Mon Sep 17 00:00:00 2001 From: JD Porter Date: Sat, 16 Feb 2019 10:26:50 -0700 Subject: [PATCH 2/9] displaying long strings as excerpts for issue #532 --- Functions/Assertions/Be.ps1 | 50 ++++++++++++++++++++++++++++++++++--- 1 file changed, 46 insertions(+), 4 deletions(-) diff --git a/Functions/Assertions/Be.ps1 b/Functions/Assertions/Be.ps1 index e7582b2b6..cf71c0f59 100644 --- a/Functions/Assertions/Be.ps1 +++ b/Functions/Assertions/Be.ps1 @@ -180,9 +180,10 @@ 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 ) + $ExpectedDisplayValue = $ExpectedValue | Expand-SpecialCharacters | Format-AsExcerpt -substringIndex $differenceIndex + $actualDisplayValue = $actual | Expand-SpecialCharacters | Format-AsExcerpt -substringIndex $differenceIndex + "Expected: '{0}'" -f ($ExpectedDisplayValue) + "But was: '{0}'" -f ($actualDisplayValue) $specialCharacterOffset = $null if ($differenceIndex -ne 0) { @@ -192,10 +193,51 @@ function Get-CompareStringMessage { & $SafeCommands['Measure-Object'] | & $SafeCommands['Select-Object'] -ExpandProperty Count) } + if ($differenceIndex -ge 13) { + $longStringOffset = 13; + } - '-' * ($differenceIndex + $specialCharacterOffset + 11) + '^' + '-' * ( $specialCharacterOffset + 11 + $longStringOffset) + '^' } } +function Format-AsExcerpt { + param ( + [Parameter(Mandatory = $true, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)] + [AllowEmptyString()] + [string]$InputObject, + + [Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true)] + [int]$substringIndex + ) + $ellipsis = "..." + $endEllipsis = "" ; + $displayDifferenceIndex = $substringIndex - 10; + $maximumStringLength = 40; + $maximumSubstringLength = 20; + $substringLength = $InputObject.Length - $displayDifferenceIndex; + if ($substringLength -gt $maximumSubstringLength) { + $substringLength = $maximumSubstringLength; + } + if ($displayDifferenceIndex + $substringLength -lt $InputObject.Length) { + $endEllipsis = $ellipsis + } + if ($displayDifferenceIndex -lt 0) { + $displayDifferenceIndex = 0; + } + if ($InputObject.length -ge $maximumStringLength) { + if ($displayDifferenceIndex -ne 0) { + + + $InputObjectDisplay = $ellipsis + } + $InputObjectDisplay += $InputObject.Substring($displayDifferenceIndex, $substringLength) + $endEllipsis + } + else { + $InputObjectDisplay = $InputObject + } + $InputObjectDisplay +} + function Expand-SpecialCharacters { param ( From 6ae8e78fe6ec21926e60fb9c6ded1d167a6d2dcf Mon Sep 17 00:00:00 2001 From: JD Porter Date: Sat, 16 Feb 2019 10:50:36 -0700 Subject: [PATCH 3/9] tidying code --- Functions/Assertions/Be.ps1 | 44 +++++++++++++++++++------------------ 1 file changed, 23 insertions(+), 21 deletions(-) diff --git a/Functions/Assertions/Be.ps1 b/Functions/Assertions/Be.ps1 index cf71c0f59..a5760a6ec 100644 --- a/Functions/Assertions/Be.ps1 +++ b/Functions/Assertions/Be.ps1 @@ -180,10 +180,10 @@ function Get-CompareStringMessage { "String lengths are both $ExpectedValueLength." "Strings differ at index $differenceIndex." } - $ExpectedDisplayValue = $ExpectedValue | Expand-SpecialCharacters | Format-AsExcerpt -substringIndex $differenceIndex - $actualDisplayValue = $actual | Expand-SpecialCharacters | Format-AsExcerpt -substringIndex $differenceIndex - "Expected: '{0}'" -f ($ExpectedDisplayValue) - "But was: '{0}'" -f ($actualDisplayValue) + $ellipsis = "..." + $excerptSize = 5; + "Expected: '{0}'" -f ( $ExpectedValue | Expand-SpecialCharacters | Format-AsExcerpt -startIndex $differenceIndex -excerptSize $excerptSize -excerptMarker $ellipsis) + "But was: '{0}'" -f ( $actual | Expand-SpecialCharacters | Format-AsExcerpt -startIndex $differenceIndex -excerptSize $excerptSize -excerptMarker $ellipsis) $specialCharacterOffset = $null if ($differenceIndex -ne 0) { @@ -193,8 +193,11 @@ function Get-CompareStringMessage { & $SafeCommands['Measure-Object'] | & $SafeCommands['Select-Object'] -ExpandProperty Count) } - if ($differenceIndex -ge 13) { - $longStringOffset = 13; + + # for excerpted strings, add in an additional length of arrow... + $excerptOffset = $ellipsis.Length + $excerptSize + if ($differenceIndex -ge $excerptOffset) { + $longStringOffset = $excerptOffset } '-' * ( $specialCharacterOffset + 11 + $longStringOffset) + '^' @@ -205,32 +208,31 @@ function Format-AsExcerpt { [Parameter(Mandatory = $true, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)] [AllowEmptyString()] [string]$InputObject, - [Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true)] - [int]$substringIndex + [int]$startIndex, + [Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true)] + [int]$excerptSize, + [Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true)] + [string]$excerptMarker ) - $ellipsis = "..." - $endEllipsis = "" ; - $displayDifferenceIndex = $substringIndex - 10; - $maximumStringLength = 40; - $maximumSubstringLength = 20; - $substringLength = $InputObject.Length - $displayDifferenceIndex; + $displayDifferenceIndex = $startIndex - $excerptSize + $maximumStringLength = 40 + $maximumSubstringLength = $excerptSize * 2 + $substringLength = $InputObject.Length - $displayDifferenceIndex if ($substringLength -gt $maximumSubstringLength) { - $substringLength = $maximumSubstringLength; + $substringLength = $maximumSubstringLength } if ($displayDifferenceIndex + $substringLength -lt $InputObject.Length) { - $endEllipsis = $ellipsis + $endExcerptMarker = $excerptMarker } if ($displayDifferenceIndex -lt 0) { - $displayDifferenceIndex = 0; + $displayDifferenceIndex = 0 } if ($InputObject.length -ge $maximumStringLength) { if ($displayDifferenceIndex -ne 0) { - - - $InputObjectDisplay = $ellipsis + $InputObjectDisplay = $excerptMarker } - $InputObjectDisplay += $InputObject.Substring($displayDifferenceIndex, $substringLength) + $endEllipsis + $InputObjectDisplay += $InputObject.Substring($displayDifferenceIndex, $substringLength) + $endExcerptMarker } else { $InputObjectDisplay = $InputObject From 1bdd5f941d12736fa0a9ac78cf4dbb428f9080d6 Mon Sep 17 00:00:00 2001 From: JD Porter <14062459+jd-porter@users.noreply.github.com> Date: Sat, 16 Feb 2019 10:26:50 -0700 Subject: [PATCH 4/9] displaying long strings as excerpts for issue #532 --- Functions/Assertions/Be.ps1 | 52 ++++++++++++++++++++-- Functions/Assertions/PesterThrow.Tests.ps1 | 23 ---------- Functions/Assertions/PesterThrow.ps1 | 18 +++----- 3 files changed, 54 insertions(+), 39 deletions(-) diff --git a/Functions/Assertions/Be.ps1 b/Functions/Assertions/Be.ps1 index e7582b2b6..a5760a6ec 100644 --- a/Functions/Assertions/Be.ps1 +++ b/Functions/Assertions/Be.ps1 @@ -180,9 +180,10 @@ 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; + "Expected: '{0}'" -f ( $ExpectedValue | Expand-SpecialCharacters | Format-AsExcerpt -startIndex $differenceIndex -excerptSize $excerptSize -excerptMarker $ellipsis) + "But was: '{0}'" -f ( $actual | Expand-SpecialCharacters | Format-AsExcerpt -startIndex $differenceIndex -excerptSize $excerptSize -excerptMarker $ellipsis) $specialCharacterOffset = $null if ($differenceIndex -ne 0) { @@ -193,9 +194,52 @@ 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 + } + + '-' * ( $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 + ) + $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 ( diff --git a/Functions/Assertions/PesterThrow.Tests.ps1 b/Functions/Assertions/PesterThrow.Tests.ps1 index 0185140f9..ecd0d498b 100644 --- a/Functions/Assertions/PesterThrow.Tests.ps1 +++ b/Functions/Assertions/PesterThrow.Tests.ps1 @@ -80,29 +80,6 @@ InModuleScope Pester { } } - - Context "Matching error pattern" { - It "given scriptblock that throws exception with the expected pattern it passes" { - $expectedErrorMessage = "expected error message" - $expectedErrorPattern = "*error*" - { throw $expectedErrorMessage } | Should -Throw -like $expectedErrorPattern - } - - It "given scriptblock that throws exception with the expected pattern in UPPERCASE it passes" { - $expectedErrorMessage = "expected error message" - $expectedErrorPattern = "*error*" - $errorMessage = $expectedErrorMessage.ToUpperInvariant() - { throw $errorMessage } | Should -Throw -like $expectedErrorPattern - } - - It "given scriptblock that throws exception with a non-matching pattern it fails" { - $expectedErrorMessage = "expected error message" - $unexpectedErrorPattern = "*pass*" - $errorMessage = $expectedErrorMessage.ToUpperInvariant() - { { throw $errorMessage } | Should -Throw -like $unexpectedErrorPattern } | Verify-AssertionFailed - } - } - Context "Matching ErrorId (FullyQualifiedErrorId)" { It "given scriptblock that throws exception with FullyQualifiedErrorId with the expected ErrorId it passes" { $expectedErrorId = "expected error id" diff --git a/Functions/Assertions/PesterThrow.ps1 b/Functions/Assertions/PesterThrow.ps1 index 96a27c8f5..c4aa752eb 100644 --- a/Functions/Assertions/PesterThrow.ps1 +++ b/Functions/Assertions/PesterThrow.ps1 @@ -1,4 +1,4 @@ -function Should-Throw([scriptblock] $ActualValue, $ExpectedMessage, $ErrorId, [type]$ExceptionType, [switch] $Negate, [switch] $Like, [string] $Because, [switch] $PassThru) { +function Should-Throw([scriptblock] $ActualValue, $ExpectedMessage, $ErrorId, [type]$ExceptionType, [switch] $Negate, [string] $Because, [switch] $PassThru) { <# .SYNOPSIS Checks if an exception was thrown. Enclose input in a script block. @@ -105,13 +105,8 @@ It does not throw an error, so the test passes. $filterOnMessage = -not [string]::IsNullOrEmpty($ExpectedMessage -replace "\s") if ($filterOnMessage) { $filters += "with message $(Format-Nicely $ExpectedMessage)" - if ($actualExceptionWasThrown ) { - if (-not $like -and -not (Get-DoValuesMatch $actualExceptionMessage $ExpectedMessage)) { - $buts += "the message was $(Format-Nicely $actualExceptionMessage)" - } - if ($Like -and -not ($actualExceptionMessage -like $ExpectedMessage)) { - $buts += "the message did not match the expression $(Format-Nicely $actualExceptionMessage)" - } + if ($actualExceptionWasThrown -and (-not (Get-DoValuesMatch $actualExceptionMessage $ExpectedMessage))) { + $buts += "the message was $(Format-Nicely $actualExceptionMessage)" } } @@ -121,6 +116,9 @@ It does not throw an error, so the test passes. if ($actualExceptionWasThrown -and (-not (Get-DoValuesMatch $actualErrorId $ErrorId))) { $buts += "the FullyQualifiedErrorId was $(Format-Nicely $actualErrorId)" } + } + + if (-not $actualExceptionWasThrown) { $buts += "no exception was thrown" } @@ -172,7 +170,3 @@ function NotShouldThrowFailureMessage { Add-AssertionOperator -Name Throw ` -InternalName Should-Throw ` -Test ${function:Should-Throw} - -Add-AssertionOperator -Name Throw ` - -InternalName Should-Throw ` - -Test ${function:Should-Throw} From 765772008ebc4c59c758c3f0025df39fc5a16835 Mon Sep 17 00:00:00 2001 From: JD Porter <14062459+jd-porter@users.noreply.github.com> Date: Sat, 16 Feb 2019 11:30:21 -0700 Subject: [PATCH 5/9] displaying long strings as excerpts for issue #532 --- Functions/Assertions/Be.ps1 | 52 ++++++++++++++++++++++++++++++++++--- 1 file changed, 48 insertions(+), 4 deletions(-) diff --git a/Functions/Assertions/Be.ps1 b/Functions/Assertions/Be.ps1 index e7582b2b6..a5760a6ec 100644 --- a/Functions/Assertions/Be.ps1 +++ b/Functions/Assertions/Be.ps1 @@ -180,9 +180,10 @@ 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; + "Expected: '{0}'" -f ( $ExpectedValue | Expand-SpecialCharacters | Format-AsExcerpt -startIndex $differenceIndex -excerptSize $excerptSize -excerptMarker $ellipsis) + "But was: '{0}'" -f ( $actual | Expand-SpecialCharacters | Format-AsExcerpt -startIndex $differenceIndex -excerptSize $excerptSize -excerptMarker $ellipsis) $specialCharacterOffset = $null if ($differenceIndex -ne 0) { @@ -193,9 +194,52 @@ 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 + } + + '-' * ( $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 + ) + $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 ( From aa9ae96c3bcaea0d7e2560898c822f150c60ee8d Mon Sep 17 00:00:00 2001 From: JD Porter <14062459+jd-porter@users.noreply.github.com> Date: Sat, 16 Feb 2019 13:16:27 -0700 Subject: [PATCH 6/9] correcting formatting for Get-CompareStringMessage pointer --- Functions/Assertions/Be.ps1 | 2 +- Functions/Context.ps1 | 3 +++ Functions/Output.Tests.ps1 | 2 +- 3 files changed, 5 insertions(+), 2 deletions(-) diff --git a/Functions/Assertions/Be.ps1 b/Functions/Assertions/Be.ps1 index a5760a6ec..a88f4f914 100644 --- a/Functions/Assertions/Be.ps1 +++ b/Functions/Assertions/Be.ps1 @@ -200,7 +200,7 @@ function Get-CompareStringMessage { $longStringOffset = $excerptOffset } - '-' * ( $specialCharacterOffset + 11 + $longStringOffset) + '^' + '-' * ( $differenceIndex + $specialCharacterOffset + 11 + $longStringOffset) + '^' } } function Format-AsExcerpt { diff --git a/Functions/Context.ps1 b/Functions/Context.ps1 index 1f3ac084a..914eeb4f9 100644 --- a/Functions/Context.ps1 +++ b/Functions/Context.ps1 @@ -40,6 +40,9 @@ Describe "Add-Numbers" { } } +.LINK +https://github.com/pester/Pester/wiki/Context + .LINK Describe It diff --git a/Functions/Output.Tests.ps1 b/Functions/Output.Tests.ps1 index ba97616c3..060fefb0f 100644 --- a/Functions/Output.Tests.ps1 +++ b/Functions/Output.Tests.ps1 @@ -370,4 +370,4 @@ InModuleScope -ModuleName Pester -ScriptBlock { } } } -} \ No newline at end of file +} From 4ddc514fc92b1eb6336cf61dec5969bde164f1e4 Mon Sep 17 00:00:00 2001 From: JD Porter <14062459+jd-porter@users.noreply.github.com> Date: Sat, 16 Feb 2019 13:34:56 -0700 Subject: [PATCH 7/9] initializing $longStringOffset in Be.ps1 to keep Powershell 2 happy. --- Functions/Assertions/Be.ps1 | 1 + 1 file changed, 1 insertion(+) diff --git a/Functions/Assertions/Be.ps1 b/Functions/Assertions/Be.ps1 index a88f4f914..bc90d39c9 100644 --- a/Functions/Assertions/Be.ps1 +++ b/Functions/Assertions/Be.ps1 @@ -182,6 +182,7 @@ function Get-CompareStringMessage { } $ellipsis = "..." $excerptSize = 5; + $longStringOffset = 0; "Expected: '{0}'" -f ( $ExpectedValue | Expand-SpecialCharacters | Format-AsExcerpt -startIndex $differenceIndex -excerptSize $excerptSize -excerptMarker $ellipsis) "But was: '{0}'" -f ( $actual | Expand-SpecialCharacters | Format-AsExcerpt -startIndex $differenceIndex -excerptSize $excerptSize -excerptMarker $ellipsis) From 72f943eab4f151b2dd404d343f9520c97759702c Mon Sep 17 00:00:00 2001 From: JD Porter <14062459+jd-porter@users.noreply.github.com> Date: Sat, 16 Mar 2019 00:27:48 -0600 Subject: [PATCH 8/9] updating formatting in Get-CompareStringMessage and adding tests --- Functions/Assertions/Be.Tests.ps1 | 17 +++++++++++++++++ Functions/Assertions/Be.ps1 | 8 +++----- 2 files changed, 20 insertions(+), 5 deletions(-) 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 bc90d39c9..c203f9f5b 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." @@ -182,9 +181,9 @@ function Get-CompareStringMessage { } $ellipsis = "..." $excerptSize = 5; - $longStringOffset = 0; - "Expected: '{0}'" -f ( $ExpectedValue | Expand-SpecialCharacters | Format-AsExcerpt -startIndex $differenceIndex -excerptSize $excerptSize -excerptMarker $ellipsis) - "But was: '{0}'" -f ( $actual | Expand-SpecialCharacters | Format-AsExcerpt -startIndex $differenceIndex -excerptSize $excerptSize -excerptMarker $ellipsis) + $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) { @@ -336,4 +335,3 @@ function ReplaceValueInArray { } } } - From 86ba938d014b83f19baf7cce6f78c8bdd0200145 Mon Sep 17 00:00:00 2001 From: JD Porter <14062459+jd-porter@users.noreply.github.com> Date: Sat, 16 Mar 2019 00:49:58 -0600 Subject: [PATCH 9/9] removing duplicate Format-AsExcerpt --- Functions/Assertions/Be.ps1 | 37 +------------------------------------ 1 file changed, 1 insertion(+), 36 deletions(-) diff --git a/Functions/Assertions/Be.ps1 b/Functions/Assertions/Be.ps1 index 6ea800ab8..5a34e9bb4 100644 --- a/Functions/Assertions/Be.ps1 +++ b/Functions/Assertions/Be.ps1 @@ -215,42 +215,7 @@ function Format-AsExcerpt { [Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true)] [string]$excerptMarker ) - $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 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