Skip to content

Commit

Permalink
Use parantheses with type-objects in BeOfType assertions (#2608)
Browse files Browse the repository at this point in the history
  • Loading branch information
fflaten authored Jan 17, 2025
1 parent 0f15f22 commit e4a21df
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 7 deletions.
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-BeOfTypeAssertion($ActualValue, $ExpectedType, [switch] $Negate,
.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-BeOfTypeAssertion($ActualValue, $ExpectedType, [switch] $Negate,
$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 @@ -269,7 +269,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 @@ -320,7 +320,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 @@ -372,7 +372,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

0 comments on commit e4a21df

Please sign in to comment.