From b4d2df0231efbfb4673d34a1424623b5d3558caa Mon Sep 17 00:00:00 2001 From: Frode Flaten <3436158+fflaten@users.noreply.github.com> Date: Wed, 31 Jul 2024 12:18:05 +0000 Subject: [PATCH 1/2] Add tests for custom assertions --- tst/functions/Add-ShouldOperator.ts.ps1 | 78 +++++++++++++++++++++++++ 1 file changed, 78 insertions(+) diff --git a/tst/functions/Add-ShouldOperator.ts.ps1 b/tst/functions/Add-ShouldOperator.ts.ps1 index 3fb327bf2..46681724d 100644 --- a/tst/functions/Add-ShouldOperator.ts.ps1 +++ b/tst/functions/Add-ShouldOperator.ts.ps1 @@ -70,6 +70,84 @@ i -PassThru:$PassThru { } } + b 'Executing custom Should assertions' { + # Testing paramter and output syntax described in docs (https://pester.dev/docs/assertions/custom-assertions) + Get-Module Pester | Remove-Module + Import-Module "$PSScriptRoot\..\..\bin\Pester.psd1" + + t 'Works for simple functions with expected output' { + function CustomSimple ([string] $ActualValue, [switch] $Negate) { + $succeeded = 'A' -eq $ActualValue + if ($Negate) { $succeeded = -not $succeeded } + + if (-not $succeeded) { + if ($Negate) { + $failureMessage = "Expected anything but 'A', but got '$ActualValue'." + } + else { + $failureMessage = "Expected 'A', but got '$ActualValue'." + } + } + + return [pscustomobject]@{ + Succeeded = $succeeded + FailureMessage = $failureMessage + } + } + + Add-ShouldOperator -Name CustomSimple -Test $function:CustomSimple + 'A' | Should -CustomSimple + 'B' | Should -CustomSimple -Not + $e = { 'B' | Should -CustomSimple } | Verify-AssertionFailed + $e.Exception.Message | Should -Be "Expected 'A', but got 'B'." + } + + t 'Works for advanced function with expected output' { + function CustomAdvanced { + [CmdletBinding()] + # Requires all mandatory parameters below as $args is disabled by default in advanced functions + param( + [string] $ActualValue, + [switch] $Negate, + $CallerSessionState + ) + $succeeded = 'A' -eq $ActualValue + if ($Negate) { $succeeded = -not $succeeded } + + if (-not $succeeded) { + if ($Negate) { + $failureMessage = "Expected anything but 'A', but got '$ActualValue'." + } + else { + $failureMessage = "Expected 'A', but got '$ActualValue'." + } + } + + return [pscustomobject]@{ + Succeeded = $succeeded + FailureMessage = $failureMessage + } + } + + Add-ShouldOperator -Name CustomAdvanced -Test $function:CustomAdvanced + 'A' | Should -CustomAdvanced + 'B' | Should -CustomAdvanced -Not + $e = { 'B' | Should -CustomAdvanced } | Verify-AssertionFailed + $e.Exception.Message | Should -Be "Expected 'A', but got 'B'." + } + + t 'Assertion exceptions are surfaced to user' { + function CustomException ([string] $ActualValue, [switch] $Negate) { + throw [System.InvalidOperationException]'something went wrong' + } + + Add-ShouldOperator -Name CustomException -Test $function:CustomException + $e = { 'A' | Should -CustomException } | Verify-Throw + $e.Exception | Verify-Type -Expected ([System.InvalidOperationException]) + $e.Exception.Message | Should -Be "something went wrong" + } + } + b 'HelpMessage for built-in Should operators' { Get-Module Pester | Remove-Module Import-Module "$PSScriptRoot\..\..\bin\Pester.psd1" From e555cb341df499df922dc384c85b03eaf5d07238 Mon Sep 17 00:00:00 2001 From: Frode Flaten <3436158+fflaten@users.noreply.github.com> Date: Wed, 31 Jul 2024 12:36:19 +0000 Subject: [PATCH 2/2] Fix test for strict mode --- tst/functions/Add-ShouldOperator.ts.ps1 | 22 ++++++++-------------- 1 file changed, 8 insertions(+), 14 deletions(-) diff --git a/tst/functions/Add-ShouldOperator.ts.ps1 b/tst/functions/Add-ShouldOperator.ts.ps1 index 46681724d..71c4143c9 100644 --- a/tst/functions/Add-ShouldOperator.ts.ps1 +++ b/tst/functions/Add-ShouldOperator.ts.ps1 @@ -80,13 +80,10 @@ i -PassThru:$PassThru { $succeeded = 'A' -eq $ActualValue if ($Negate) { $succeeded = -not $succeeded } - if (-not $succeeded) { - if ($Negate) { - $failureMessage = "Expected anything but 'A', but got '$ActualValue'." - } - else { - $failureMessage = "Expected 'A', but got '$ActualValue'." - } + # Default failure message. Not used by Should unless succeeded is $false + $failureMessage = "Expected 'A', but got '$ActualValue'." + if (-not $succeeded -and $Negate) { + $failureMessage = "Expected anything but 'A', but got '$ActualValue'." } return [pscustomobject]@{ @@ -114,13 +111,10 @@ i -PassThru:$PassThru { $succeeded = 'A' -eq $ActualValue if ($Negate) { $succeeded = -not $succeeded } - if (-not $succeeded) { - if ($Negate) { - $failureMessage = "Expected anything but 'A', but got '$ActualValue'." - } - else { - $failureMessage = "Expected 'A', but got '$ActualValue'." - } + # Default failure message. Not used by Should unless succeeded is $false + $failureMessage = "Expected 'A', but got '$ActualValue'." + if (-not $succeeded -and $Negate) { + $failureMessage = "Expected anything but 'A', but got '$ActualValue'." } return [pscustomobject]@{