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

Quote input string in BeNullOrEmpty error #2329

Merged
merged 1 commit into from
Apr 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/functions/assertions/BeNullOrEmpty.ps1
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

function Should-BeNullOrEmpty([object[]] $ActualValue, [switch] $Negate, [string] $Because) {
function Should-BeNullOrEmpty($ActualValue, [switch] $Negate, [string] $Because) {
<#
.SYNOPSIS
Checks values for null or empty (strings).
Expand Down Expand Up @@ -30,6 +30,7 @@ function Should-BeNullOrEmpty([object[]] $ActualValue, [switch] $Negate, [string
}
elseif ($ActualValue.Count -eq 1) {
$expandedValue = $ActualValue[0]
$singleValue = $true
if ($expandedValue -is [hashtable]) {
$succeeded = $expandedValue.Count -eq 0
}
Expand All @@ -52,7 +53,8 @@ function Should-BeNullOrEmpty([object[]] $ActualValue, [switch] $Negate, [string
$failureMessage = NotShouldBeNullOrEmptyFailureMessage -Because $Because
}
else {
$failureMessage = ShouldBeNullOrEmptyFailureMessage -ActualValue $ActualValue -Because $Because
$valueToFormat = if ($singleValue) { $expandedValue } else { $ActualValue }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems to affect non empty hashtables too. Shall we add a unit tests to verify the exception message when the hast table is not empty? So it too returns the expected message? There is an existing test at line 22-24 but it does not verify the exception message.

Copy link
Collaborator Author

@fflaten fflaten Mar 29, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't do any special formatting for hashtables. It only drops the outer array, which in a single-item case should be the array Pester wrapped pipeline-input in.

@{ hello = "world" } | Should -BeNullOrEmpty
# Pester 5.3.3
Expected $null or empty, but got @(System.Collections.Hashtable).

# After PR
Expected $null or empty, but got System.Collections.Hashtable

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good.

$failureMessage = ShouldBeNullOrEmptyFailureMessage -ActualValue $valueToFormat -Because $Because
}
}

Expand Down
5 changes: 5 additions & 0 deletions tst/functions/assertions/BeNullOrEmpty.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ InPesterModuleScope {
$err = { 1 | Should -BeNullOrEmpty -Because 'reason' } | Verify-AssertionFailed
$err.Exception.Message | Verify-Equal 'Expected $null or empty, because reason, but got 1.'
}

It 'returns the correct assertion message for single string' {
$err = { 'empty' | Should -BeNullOrEmpty -Because 'reason' } | Verify-AssertionFailed
$err.Exception.Message | Verify-Equal 'Expected $null or empty, because reason, but got ''empty''.'
}
}

Describe "Should -Not -BeNullOrEmpty" {
Expand Down