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

Update help and tests to use parantheses around type input in BeOfType assertions by @fflaten in #2608 (backport to rel/5.x.x) #2610

Merged
merged 1 commit into from
Jan 20, 2025
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/functions/assertions/BeOfType.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ function Should-BeOfType($ActualValue, $ExpectedType, [switch] $Negate, [string]
.SYNOPSIS
Asserts that the actual value should be an object of a specified type
(or a subclass of the specified type) using PowerShell's -is operator.
Expected type can be provided using full type name strings or a type wrapped in parantheses.
.EXAMPLE
$actual = Get-Item $env:SystemRoot
Expand All @@ -25,6 +26,11 @@ function Should-BeOfType($ActualValue, $ExpectedType, [switch] $Negate, [string]
$actual | Should -BeOfType System.IO.FileInfo
This test will fail, as FileInfo is not a base class of DirectoryInfo.
.EXAMPLE
$actual | Should -BeOfType ([System.IO.DirectoryInfo])
Test using a type-object. Remember to use parantheses for consistent behavior with PowerShell classes.
#>
if ($ExpectedType -is [string]) {
# parses type that is provided as a string in brackets (such as [int])
Expand Down
4 changes: 2 additions & 2 deletions tst/PesterConfiguration.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Describe "PesterConfiguration.Format.ps1xml" {
$formatData | Should -Not -BeNullOrEmpty
$formatData.FormatViewDefinition.Count | Should -Be 1
$formatData.FormatViewDefinition[0].Name | Should -BeExactly $section.FullName
$formatData.FormatViewDefinition[0].Control | Should -BeOfType [System.Management.Automation.ListControl]
$formatData.FormatViewDefinition[0].Control | Should -BeOfType ([System.Management.Automation.ListControl])
}

It 'View includes all options' {
Expand All @@ -35,7 +35,7 @@ Describe "PesterConfiguration.Format.ps1xml" {
$formatData | Should -Not -BeNullOrEmpty
$formatData.FormatViewDefinition.Count | Should -Be 1
$formatData.FormatViewDefinition[0].Name | Should -BeExactly 'Pester.Option'
$formatData.FormatViewDefinition[0].Control | Should -BeOfType [System.Management.Automation.TableControl]
$formatData.FormatViewDefinition[0].Control | Should -BeOfType ([System.Management.Automation.TableControl])
}

It 'View includes all options' {
Expand Down
2 changes: 1 addition & 1 deletion tst/functions/Get-ShouldOperator.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ InPesterModuleScope {
$BGT.PSTypeNames[0] | Should -BeExactly 'PesterAssertionOperatorHelp'
$BGT.Help.PSTypeNames[0] | Should -BeExactly 'MamlCommandHelpInfo#ExamplesView'
$BGT.Help.syntax.syntaxItem[0].name | Should -Be 'Should -BeGreaterThan'
$BGT.Help.syntax.syntaxItem[0].DisplayParameterSet | Should -BeOfType [string]
$BGT.Help.syntax.syntaxItem[0].DisplayParameterSet | Should -BeOfType ([string])
$BGT.Help.syntax.syntaxItem[0].DisplayParameterSet | Should -BeLike '*-ActualValue*'
}

Expand Down
2 changes: 1 addition & 1 deletion tst/functions/New-MockObject.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ Describe 'New-MockObject' {

it 'Should preserve types' {
$mockedProcess = New-MockObject -Type 'System.Diagnostics.Process' -Properties @{Id = 123 }
$mockedProcess.Id | Should -BeOfType [int]
$mockedProcess.Id | Should -BeOfType ([int])
}
}
}
6 changes: 3 additions & 3 deletions tst/functions/TestsRunningInCleanRunspace.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ Describe "Swallowing output" {
# note - the pipe command unrolls enumerable objects, so we have to wrap
# results in a sacrificial array to retain its original structure
# when passed to Should
@(, $results) | Should -BeOfType [PSCustomObject]
@(, $results) | Should -BeOfType ([PSCustomObject])
$results.TotalCount | Should -Be 1

# or, we could do this instead:
Expand Down Expand Up @@ -332,7 +332,7 @@ Describe "Swallowing output" {
# note - the pipe command unrolls enumerable objects, so we have to wrap
# results in a sacrificial array to retain its original structure
# when passed to Should
@(, $results) | Should -BeOfType [PSCustomObject]
@(, $results) | Should -BeOfType ([PSCustomObject])
$results.TotalCount | Should -Be 1

# or, we could do this instead:
Expand Down Expand Up @@ -384,7 +384,7 @@ Describe "Swallowing output" {
# note - the pipe command unrolls enumerable objects, so we have to wrap
# results in a sacrificial array to retain its original structure
# when passed to Should
@(, $results) | Should -BeOfType [PSCustomObject]
@(, $results) | Should -BeOfType ([PSCustomObject])
$results.TotalCount | Should -Be 1

# or, we could do this instead:
Expand Down