Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests for custom assertions #2552

Merged
merged 2 commits into from
Oct 1, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions tst/functions/Add-ShouldOperator.ts.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,78 @@ 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 }

# 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]@{
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 }

# 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]@{
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"
Expand Down