From 29f5cb89300622d1fa481dd0ad99c920a272a221 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Jare=C5=A1?= Date: Mon, 20 May 2024 22:11:04 +0200 Subject: [PATCH] Revert test, fix safe commands etc. --- .gitignore | 1 - azure-pipelines.yml | 2 +- src/functions/assert/Common/Collect-Input.ps1 | 3 +- .../Equivalence/Should-BeEquivalent.ps1 | 36 ++++++------ .../assert/Exception/Should-Throw.ps1 | 6 +- test.ps1 | 45 ++------------- tst/Format2.Tests.ps1 | 8 +-- .../Common/Get-AssertionMessage.Tests.ps1 | 4 +- .../Should-BeEquivalent.Options.Tests.ps1 | 52 ++++++++--------- .../Equivalence/Should-BeEquivalent.Tests.ps1 | 56 +++++++++---------- .../assert/Exception/Should-Throw.Tests.ps1 | 4 +- 11 files changed, 91 insertions(+), 126 deletions(-) diff --git a/.gitignore b/.gitignore index 28591e1ea..e02bf5456 100644 --- a/.gitignore +++ b/.gitignore @@ -5,7 +5,6 @@ vendor/packages/ .vscode/ # But don't exclude settings.json !.vscode/settings.json -!.vscode/launch.json coverage.xml testResults.xml diff --git a/azure-pipelines.yml b/azure-pipelines.yml index 72162aff1..9e3f8f1a1 100644 --- a/azure-pipelines.yml +++ b/azure-pipelines.yml @@ -112,7 +112,7 @@ stages: targetType: inline pwsh: $(pwsh) script: | - & ./test.ps1 -CI -NoBuild + & ./test.ps1 -CI -PassThru -NoBuild workingDirectory: '$(Build.SourcesDirectory)' - task: PublishCodeCoverageResults@2 inputs: diff --git a/src/functions/assert/Common/Collect-Input.ps1 b/src/functions/assert/Common/Collect-Input.ps1 index d09285216..4180a4eb0 100644 --- a/src/functions/assert/Common/Collect-Input.ps1 +++ b/src/functions/assert/Common/Collect-Input.ps1 @@ -7,7 +7,8 @@ # It is always $null or object[] containing all the received items. $PipelineInput, # This tell us if we were called by | syntax or not. Caller needs to pass in $MyInvocation.ExpectingInput. - $IsPipelineInput, + [Parameter(Mandatory)] + [bool] $IsPipelineInput, # This unwraps input provided by |. The effect of this is that we get single item input directly, # and not wrapped in array. E.g. 1 | Should-Be -> 1, and not 1 | Should-Be -> @(1). # diff --git a/src/functions/assert/Equivalence/Should-BeEquivalent.ps1 b/src/functions/assert/Equivalence/Should-BeEquivalent.ps1 index aa4aa8ecb..f48e4eed9 100644 --- a/src/functions/assert/Equivalence/Should-BeEquivalent.ps1 +++ b/src/functions/assert/Equivalence/Should-BeEquivalent.ps1 @@ -179,7 +179,7 @@ function Compare-DataTableEquivalent ($Expected, $Actual, $Property, $Options) { } $Expected = Format-Nicely2 -Value $Expected $Actual = Format-Nicely2 -Value $Actual - $notFoundFormatted = Format-Nicely2 -Value ( $notFound | ForEach-Object { Format-Nicely2 -Value $_ } ) + $notFoundFormatted = Format-Nicely2 -Value ( $notFound | & $SafeCommands['ForEach-Object'] { Format-Nicely2 -Value $_ } ) if ($notFound) { $propertyMessage = if ($Property) { " in property $Property which is" } @@ -290,24 +290,24 @@ function Compare-HashtableEquivalent ($Actual, $Expected, $Property, $Options) { if (!$Options.ExcludePathsNotOnExpected) { # fix for powershell 2 where the array needs to be explicit - $keysNotInExpected = @( $actualKeys | Where-Object { $expectedKeys -notcontains $_ }) + $keysNotInExpected = @( $actualKeys | & $SafeCommands['Where-Object'] { $expectedKeys -notcontains $_ }) $filteredKeysNotInExpected = @( $keysNotInExpected | Test-IncludedPath -PathSelector Hashtable -Path $Property -Options $Options) # fix for powershell v2 where foreach goes once over null - if ($filteredKeysNotInExpected | Where-Object { $_ }) { + if ($filteredKeysNotInExpected | & $SafeCommands['Where-Object'] { $_ }) { v -Difference "`$Actual has $($filteredKeysNotInExpected.Count) keys that were not found on `$Expected: $(Format-Nicely2 @($filteredKeysNotInExpected))." } else { v "`$Actual has no keys that we did not find on `$Expected." } - foreach ($k in $filteredKeysNotInExpected | Where-Object { $_ }) { + foreach ($k in $filteredKeysNotInExpected | & $SafeCommands['Where-Object'] { $_ }) { $result += "Expected is missing key '$k' that the other object has." } } - if ($result | Where-Object { $_ }) { + if ($result | & $SafeCommands['Where-Object'] { $_ }) { v -Difference "Hashtables `$Actual and `$Expected are not equivalent." $expectedFormatted = Format-Nicely2 -Value $Expected $actualFormatted = Format-Nicely2 -Value $Actual @@ -355,18 +355,18 @@ function Compare-DictionaryEquivalent ($Actual, $Expected, $Property, $Options) } if (!$Options.ExcludePathsNotOnExpected) { # fix for powershell 2 where the array needs to be explicit - $keysNotInExpected = @( $actualKeys | Where-Object { $expectedKeys -notcontains $_ } ) + $keysNotInExpected = @( $actualKeys | & $SafeCommands['Where-Object'] { $expectedKeys -notcontains $_ } ) $filteredKeysNotInExpected = @( $keysNotInExpected | Test-IncludedPath -PathSelector Hashtable -Path $Property -Options $Options ) # fix for powershell v2 where foreach goes once over null - if ($filteredKeysNotInExpected | Where-Object { $_ }) { + if ($filteredKeysNotInExpected | & $SafeCommands['Where-Object'] { $_ }) { v -Difference "`$Actual has $($filteredKeysNotInExpected.Count) keys that were not found on `$Expected: $(Format-Nicely2 @($filteredKeysNotInExpected))." } else { v "`$Actual has no keys that we did not find on `$Expected." } - foreach ($k in $filteredKeysNotInExpected | Where-Object { $_ }) { + foreach ($k in $filteredKeysNotInExpected | & $SafeCommands['Where-Object'] { $_ }) { $result += "Expected is missing key '$k' that the other object has." } } @@ -403,7 +403,7 @@ function Compare-ObjectEquivalent ($Actual, $Expected, $Property, $Options) { } $propertyName = $p.Name - $actualProperty = $actualProperties | Where-Object { $_.Name -eq $propertyName } + $actualProperty = $actualProperties | & $SafeCommands['Where-Object'] { $_.Name -eq $propertyName } if (-not $actualProperty) { v -Difference "Property '$propertyName' was not found on `$Actual." "Expected has property '$PropertyName' that the other object does not have." @@ -424,7 +424,7 @@ function Compare-ObjectEquivalent ($Actual, $Expected, $Property, $Options) { #check if there are any extra actual object props $expectedPropertyNames = $expectedProperties | Select-Object -ExpandProperty Name - $propertiesNotInExpected = @( $actualProperties | Where-Object { $expectedPropertyNames -notcontains $_.name }) + $propertiesNotInExpected = @( $actualProperties | & $SafeCommands['Where-Object'] { $expectedPropertyNames -notcontains $_.name }) # fix for powershell v2 we need to make the array explicit $filteredPropertiesNotInExpected = $propertiesNotInExpected | @@ -438,7 +438,7 @@ function Compare-ObjectEquivalent ($Actual, $Expected, $Property, $Options) { } # fix for powershell v2 where foreach goes once over null - foreach ($p in $filteredPropertiesNotInExpected | Where-Object { $_ }) { + foreach ($p in $filteredPropertiesNotInExpected | & $SafeCommands['Where-Object'] { $_ }) { "Expected is missing property '$($p.Name)' that the other object has." } } @@ -456,12 +456,12 @@ function Compare-DataRowEquivalent ($Actual, $Expected, $Property, $Options) { return "Expected DataRow '$expectedFormatted', but got '$actualFormatted'." } - $actualProperties = $Actual.PsObject.Properties | Where-Object { 'RowError', 'RowState', 'Table', 'ItemArray', 'HasErrors' -notcontains $_.Name } - $expectedProperties = $Expected.PsObject.Properties | Where-Object { 'RowError', 'RowState', 'Table', 'ItemArray', 'HasErrors' -notcontains $_.Name } + $actualProperties = $Actual.PsObject.Properties | & $SafeCommands['Where-Object'] { 'RowError', 'RowState', 'Table', 'ItemArray', 'HasErrors' -notcontains $_.Name } + $expectedProperties = $Expected.PsObject.Properties | & $SafeCommands['Where-Object'] { 'RowError', 'RowState', 'Table', 'ItemArray', 'HasErrors' -notcontains $_.Name } foreach ($p in $expectedProperties) { $propertyName = $p.Name - $actualProperty = $actualProperties | Where-Object { $_.Name -eq $propertyName } + $actualProperty = $actualProperties | & $SafeCommands['Where-Object'] { $_.Name -eq $propertyName } if (-not $actualProperty) { "Expected has property '$PropertyName' that the other object does not have." continue @@ -473,10 +473,10 @@ function Compare-DataRowEquivalent ($Actual, $Expected, $Property, $Options) { #check if there are any extra actual object props $expectedPropertyNames = $expectedProperties | Select-Object -ExpandProperty Name - $propertiesNotInExpected = @($actualProperties | Where-Object { $expectedPropertyNames -notcontains $_.name }) + $propertiesNotInExpected = @($actualProperties | & $SafeCommands['Where-Object'] { $expectedPropertyNames -notcontains $_.name }) # fix for powershell v2 where foreach goes once over null - foreach ($p in $propertiesNotInExpected | Where-Object { $_ }) { + foreach ($p in $propertiesNotInExpected | & $SafeCommands['Where-Object'] { $_ }) { "Expected is missing property '$($p.Name)' that the other object has." } } @@ -750,7 +750,7 @@ function Test-IncludedPath { } function Format-EquivalencyOptions ($Options) { - $Options.ExcludedPaths | ForEach-Object { "Exclude path '$_'" } + $Options.ExcludedPaths | & $SafeCommands['ForEach-Object'] { "Exclude path '$_'" } if ($Options.ExcludePathsNotOnExpected) { "Excluding all paths not found on Expected" } } @@ -761,7 +761,7 @@ function Like-Any { [String] $Path ) process { - foreach ($pathFilter in $PathFilters | Where-Object { $_ }) { + foreach ($pathFilter in $PathFilters | & $SafeCommands['Where-Object'] { $_ }) { $r = $Path -like $pathFilter if ($r) { v -Skip "Path '$Path' matches filter '$pathFilter'." diff --git a/src/functions/assert/Exception/Should-Throw.ps1 b/src/functions/assert/Exception/Should-Throw.ps1 index d46e85105..a4b564a58 100644 --- a/src/functions/assert/Exception/Should-Throw.ps1 +++ b/src/functions/assert/Exception/Should-Throw.ps1 @@ -35,7 +35,7 @@ function Assert-Throw { .EXAMPLE ```powershell - $err = { throw 'error' } | Should-Throw -PassThru + $err = { throw 'error' } | Should-Throw $err.Exception.Message | Should-BeLike '*err*' ``` @@ -75,7 +75,7 @@ function Assert-Throw { } catch { $errorThrown = $true - $err = Get-Error $_ + $err = Get-ErrorObject $_ } $buts = @() @@ -127,7 +127,7 @@ function Assert-Throw { $err.ErrorRecord } -function Get-Error ($ErrorRecord) { +function Get-ErrorObject ($ErrorRecord) { if ($ErrorRecord.Exception -like '*"InvokeWithContext"*') { $e = $ErrorRecord.Exception.InnerException.ErrorRecord diff --git a/test.ps1 b/test.ps1 index be831ffb5..b5b4edbe3 100644 --- a/test.ps1 +++ b/test.ps1 @@ -12,12 +12,9 @@ so leaving it in code would run only one test from the file on the server. .PARAMETER SkipPTests - Skips P tests. Skip the tests written using the P module, Unit + Skips Passthrough P tests. Skip the tests written using the P module, Unit Tests for the Runtime, and Acceptance Tests for Pester - .PARAMETER SkipPesterTests - Skips Pester tests, but not P tests. - .PARAMETER NoBuild Skips running build.ps1. Do not build the underlying csharp components. Used in CI pipeline since a clean build has already been run prior to Test. @@ -32,22 +29,16 @@ done, but makes local debugging difficult. When -CI is used, inlining is forced. - .PARAMETER VSCode - Set when calling from VSCode laucher file so we automatically figure out - what to run or what to skip. .NOTES Tests are excluded with Tags VersionChecks, StyleRules, Help. #> -[CmdletBinding()] param ( # force P to fail when I leave `dt` in the tests [switch] $CI, [switch] $SkipPTests, - [switch] $SkipPesterTests, [switch] $NoBuild, [switch] $Inline, - [switch] $VSCode, - [string[]] $File = @() + [string[]] $File ) Set-StrictMode -Version Latest @@ -57,21 +48,6 @@ $ErrorView = "NormalView" "Using PS: $($PsVersionTable.PSVersion)" "In path: $($pwd.Path)" -if ($VSCode) { - # Detect which tests to skip from the filenames. - $anyFile = 0 -lt $File.Count - $anyPesterTests = [bool]@($File | Where-Object { $_ -like "*.Tests.ps1" }) - $anyPTests = [bool]@($File | Where-Object { $_ -like "*.ts.ps1" }) - - if ($SkipPTests -or ($anyFile -and -not $anyPTests)) { - $SkipPTests = $true - } - - if ($SkipPesterTests -or ($anyFile -and -not $anyPesterTests)) { - $SkipPesterTests = $true - } -} - if (-not $NoBuild) { if ($CI) { & "$PSScriptRoot/build.ps1" -Inline @@ -81,10 +57,6 @@ if (-not $NoBuild) { } } -# if ($CI -and ($SkipPTests -or $SkipPesterTests)) { -# throw "Cannot skip tests in CI mode!" -# } - # remove pester because we will be reimporting it in multiple other places Get-Module Pester | Remove-Module @@ -146,7 +118,7 @@ New-Module -Name TestHelpers -ScriptBlock { } function New-Dictionary ([hashtable]$Hashtable) { - $d = new-object "Collections.Generic.Dictionary[string,object]" + $d = [System.Collections.Generic.Dictionary[string, object]]::new() $Hashtable.GetEnumerator() | ForEach-Object { $d.Add($_.Key, $_.Value) } $d @@ -155,15 +127,8 @@ New-Module -Name TestHelpers -ScriptBlock { function Clear-WhiteSpace ($Text) { "$($Text -replace "(`t|`n|`r)"," " -replace "\s+"," ")".Trim() } - - function New-PSObject ([hashtable]$Property) { - New-Object -Type PSObject -Property $Property - } } | Out-Null -if ($SkipPesterTests) { - return -} $configuration = [PesterConfiguration]::Default @@ -189,8 +154,8 @@ if ($CI) { $configuration.Run.Exit = $true # not using code coverage, it is still very slow - $configuration.CodeCoverage.Enabled = $true - $configuration.CodeCoverage.Path = "$PSScriptRoot/bin/*" + $configuration.CodeCoverage.Enabled = $false + $configuration.CodeCoverage.Path = "$PSScriptRoot/src/*" # experimental, uses the Profiler based tracer to do code coverage without using breakpoints $configuration.CodeCoverage.UseBreakpoints = $false diff --git a/tst/Format2.Tests.ps1 b/tst/Format2.Tests.ps1 index 35c0065be..e6c935bbc 100644 --- a/tst/Format2.Tests.ps1 +++ b/tst/Format2.Tests.ps1 @@ -64,7 +64,7 @@ InPesterModuleScope { Describe "Format-Object2" { It "Formats object '' to ''" -TestCases @( - @{ Value = (New-PSObject @{Name = 'Jakub'; Age = 28 }); Expected = "PSObject{Age=28; Name='Jakub'}" }, + @{ Value = ([PSCustomObject]@{Name = 'Jakub'; Age = 28 }); Expected = "PSObject{Age=28; Name='Jakub'}" }, @{ Value = (New-Object -Type Assertions.TestType.Person -Property @{Name = 'Jakub'; Age = 28 }); Expected = "Assertions.TestType.Person{Age=28; Name='Jakub'}" } ) { param ($Value, $Expected) @@ -72,7 +72,7 @@ InPesterModuleScope { } It "Formats object '' with selected properties '' to ''" -TestCases @( - @{ Value = (New-PSObject @{Name = 'Jakub'; Age = 28 }); SelectedProperties = "Age"; Expected = "PSObject{Age=28}" }, + @{ Value = ([PSCustomObject]@{Name = 'Jakub'; Age = 28 }); SelectedProperties = "Age"; Expected = "PSObject{Age=28}" }, @{ Value = (New-Object -Type Assertions.TestType.Person -Property @{Name = 'Jakub'; Age = 28 }) SelectedProperties = 'Name' @@ -159,7 +159,7 @@ InPesterModuleScope { @{ Value = (1, 2, 3); Expected = '@(1, 2, 3)' }, @{ Value = 1.1; Expected = '1.1' }, @{ Value = [int]; Expected = '[int]' } - @{ Value = New-PSObject @{ Name = "Jakub" }; Expected = "PSObject{Name='Jakub'}" }, + @{ Value = [PSCustomObject]@{ Name = "Jakub" }; Expected = "PSObject{Name='Jakub'}" }, @{ Value = (New-Object -Type Assertions.TestType.Person -Property @{Name = 'Jakub'; Age = 28 }); Expected = "Assertions.TestType.Person{Age=28; Name='Jakub'}" } @{ Value = @{Name = 'Jakub'; Age = 28 }; Expected = "@{Age=28; Name='Jakub'}" } @{ Value = New-Dictionary @{Age = 28; Name = 'Jakub' }; Expected = "Dictionary{Age=28; Name='Jakub'}" } @@ -199,7 +199,7 @@ InPesterModuleScope { @{ Value = 1.1; Expected = '[double]' }, @{ Value = 'a' ; Expected = '[string]' }, @{ Value = $null ; Expected = '[null]' }, - @{ Value = New-PSObject @{Name = 'Jakub' } ; Expected = '[PSObject]' }, + @{ Value = [PSCustomObject]@{Name = 'Jakub' } ; Expected = '[PSObject]' }, @{ Value = [Object[]]1, 2, 3 ; Expected = '[collection]' } ) { param($Value, $Expected) diff --git a/tst/functions/assert/Common/Get-AssertionMessage.Tests.ps1 b/tst/functions/assert/Common/Get-AssertionMessage.Tests.ps1 index a65c5d650..22936417c 100644 --- a/tst/functions/assert/Common/Get-AssertionMessage.Tests.ps1 +++ b/tst/functions/assert/Common/Get-AssertionMessage.Tests.ps1 @@ -17,13 +17,13 @@ InPesterModuleScope { It "returns correct message when complex objects are provided" { $expected = "We expected string to be PSObject{Age=28; Name='Jakub'}, but got 2." $customMessage = "We expected string to be , but got ." - Get-AssertionMessage -CustomMessage $customMessage -Expected (New-PSObject @{Name = 'Jakub'; Age = 28 }) -Actual 2 | Verify-Equal $expected + Get-AssertionMessage -CustomMessage $customMessage -Expected ([PSCustomObject]@{Name = 'Jakub'; Age = 28 }) -Actual 2 | Verify-Equal $expected } It "returns correct message when type tokens are provided" { $expected = "We expected string to be [PSObject], but got [int]." $customMessage = "We expected string to be , but got ." - Get-AssertionMessage -CustomMessage $customMessage -Expected (New-PSObject @{Name = 'Jakub'; Age = 28 }) -Actual 2 | Verify-Equal $expected + Get-AssertionMessage -CustomMessage $customMessage -Expected ([PSCustomObject]@{Name = 'Jakub'; Age = 28 }) -Actual 2 | Verify-Equal $expected } It "returns correct type message when `$null is provided" { diff --git a/tst/functions/assert/Equivalence/Should-BeEquivalent.Options.Tests.ps1 b/tst/functions/assert/Equivalence/Should-BeEquivalent.Options.Tests.ps1 index 2f0520180..2a14c79d7 100644 --- a/tst/functions/assert/Equivalence/Should-BeEquivalent.Options.Tests.ps1 +++ b/tst/functions/assert/Equivalence/Should-BeEquivalent.Options.Tests.ps1 @@ -11,12 +11,12 @@ InPesterModuleScope { ) { param ($Path) - $expected = New-PSObject @{ + $expected = [PSCustomObject]@{ Name = "Jakub" Age = 30 } - $actual = New-PSObject @{ + $actual = [PSCustomObject]@{ Name = "Jakub" } @@ -30,11 +30,11 @@ InPesterModuleScope { @{ Path = "ParentProperty1.ParentProperty2" } ) { param ($Path) - $expected = New-PSObject @{ + $expected = [PSCustomObject]@{ Name = "Jakub" } - $actual = New-PSObject @{ + $actual = [PSCustomObject]@{ Name = "Jakub" Age = 30 } @@ -45,24 +45,24 @@ InPesterModuleScope { It "Given a full path to a property on object that is in collection it ignores it on the Expected object" { - $expected = New-PSObject @{ + $expected = [PSCustomObject]@{ ProgrammingLanguages = @( - (New-PSObject @{ + ([PSCustomObject]@{ Name = "C#" Type = "OO" }), - (New-PSObject @{ + ([PSCustomObject]@{ Name = "PowerShell" }) ) } - $actual = New-PSObject @{ + $actual = [PSCustomObject]@{ ProgrammingLanguages = @( - (New-PSObject @{ + ([PSCustomObject]@{ Name = "C#" }), - (New-PSObject @{ + ([PSCustomObject]@{ Name = "PowerShell" }) ) @@ -74,24 +74,24 @@ InPesterModuleScope { } It "Given a full path to a property on object that is in collection it ignores it on the Actual object" { - $expected = New-PSObject @{ + $expected = [PSCustomObject]@{ ProgrammingLanguages = @( - (New-PSObject @{ + ([PSCustomObject]@{ Name = "C#" }), - (New-PSObject @{ + ([PSCustomObject]@{ Name = "PowerShell" }) ) } - $actual = New-PSObject @{ + $actual = [PSCustomObject]@{ ProgrammingLanguages = @( - (New-PSObject @{ + ([PSCustomObject]@{ Name = "C#" Type = "OO" }), - (New-PSObject @{ + ([PSCustomObject]@{ Name = "PowerShell" }) ) @@ -103,24 +103,24 @@ InPesterModuleScope { } It "Given a full path to a property on object that is in hashtable it ignores it on the Expected object" { - $expected = New-PSObject @{ + $expected = [PSCustomObject]@{ ProgrammingLanguages = @{ - Language1 = (New-PSObject @{ + Language1 = ([PSCustomObject]@{ Name = "C#" Type = "OO" }); - Language2 = (New-PSObject @{ + Language2 = ([PSCustomObject]@{ Name = "PowerShell" }) } } - $actual = New-PSObject @{ + $actual = [PSCustomObject]@{ ProgrammingLanguages = @{ - Language1 = (New-PSObject @{ + Language1 = ([PSCustomObject]@{ Name = "C#" }); - Language2 = (New-PSObject @{ + Language2 = ([PSCustomObject]@{ Name = "PowerShell" }) } @@ -193,13 +193,13 @@ InPesterModuleScope { } It "Given options it passes them correctly from Should-BeEquivalent" { - $expected = New-PSObject @{ + $expected = [PSCustomObject]@{ Name = "Jakub" Location = "Prague" Age = 30 } - $actual = New-PSObject @{ + $actual = [PSCustomObject]@{ Name = "Jakub" } @@ -347,11 +347,11 @@ InPesterModuleScope { Describe "Compare-Equiavlent - equality comparison options" { It "Given objects that are equivalent and -Comparator Equality option it compares them as different" { - $expected = New-PsObject @{ + $expected = [PSCustomObject]@{ LikesIfsInMocks = $false } - $actual = New-PsObject @{ + $actual = [PSCustomObject]@{ LikesIfsInMocks = "False" } diff --git a/tst/functions/assert/Equivalence/Should-BeEquivalent.Tests.ps1 b/tst/functions/assert/Equivalence/Should-BeEquivalent.Tests.ps1 index ef65365d4..3a280638a 100644 --- a/tst/functions/assert/Equivalence/Should-BeEquivalent.Tests.ps1 +++ b/tst/functions/assert/Equivalence/Should-BeEquivalent.Tests.ps1 @@ -83,13 +83,13 @@ InPesterModuleScope { Describe "Get-ValueNotEquivalentMessage" { It "Returns correct message when comparing value to an object" { $e = 'abc' - $a = New-PSObject @{ Name = 'Jakub'; Age = 28 } + $a = [PSCustomObject]@{ Name = 'Jakub'; Age = 28 } Get-ValueNotEquivalentMessage -Actual $a -Expected $e | Verify-Equal "Expected 'abc' to be equivalent to the actual value, but got PSObject{Age=28; Name='Jakub'}." } It "Returns correct message when comparing object to a value" { - $e = New-PSObject @{ Name = 'Jakub'; Age = 28 } + $e = [PSCustomObject]@{ Name = 'Jakub'; Age = 28 } $a = 'abc' Get-ValueNotEquivalentMessage -Actual $a -Expected $e | Verify-Equal "Expected PSObject{Age=28; Name='Jakub'} to be equivalent to the actual value, but got 'abc'." @@ -166,7 +166,7 @@ InPesterModuleScope { @{ Actual = "abc"; Expected = "a b c"; Message = "Expected 'a b c' to be equivalent to the actual value, but got 'abc'." }, @{ Actual = @("abc", "bde"); Expected = "abc"; Message = "Expected 'abc' to be equivalent to the actual value, but got @('abc', 'bde')." }, @{ Actual = { def }; Expected = "abc"; Message = "Expected 'abc' to be equivalent to the actual value, but got { def }." }, - @{ Actual = (New-PSObject @{ Name = 'Jakub' }); Expected = "abc"; Message = "Expected 'abc' to be equivalent to the actual value, but got PSObject{Name='Jakub'}." }, + @{ Actual = ([PSCustomObject]@{ Name = 'Jakub' }); Expected = "abc"; Message = "Expected 'abc' to be equivalent to the actual value, but got PSObject{Name='Jakub'}." }, @{ Actual = (1, 2, 3); Expected = "abc"; Message = "Expected 'abc' to be equivalent to the actual value, but got @(1, 2, 3)." } ) { param($Actual, $Expected, $Message) @@ -190,7 +190,7 @@ InPesterModuleScope { It "Given collection '' on the expected side and non-collection '' on the actual side it prints the correct message ''" -TestCases @( @{ Actual = 3; Expected = (1, 2, 3, 4); Message = "Expected collection @(1, 2, 3, 4) with length 4, but got 3." }, - @{ Actual = (New-PSObject @{ Name = 'Jakub' }); Expected = (1, 2, 3, 4); Message = "Expected collection @(1, 2, 3, 4) with length 4, but got PSObject{Name='Jakub'}." } + @{ Actual = ([PSCustomObject]@{ Name = 'Jakub' }); Expected = (1, 2, 3, 4); Message = "Expected collection @(1, 2, 3, 4) with length 4, but got PSObject{Name='Jakub'}." } ) { param ($Actual, $Expected, $Message) Compare-CollectionEquivalent -Actual $Actual -Expected $Expected | Verify-Equal $Message @@ -234,7 +234,7 @@ InPesterModuleScope { } It "Given values '' and '' that are not equivalent it returns message ''." -TestCases @( - @{ Actual = 'a'; Expected = (New-PSObject @{ Name = 'Jakub' }); Message = "Expected object PSObject{Name='Jakub'}, but got 'a'." } + @{ Actual = 'a'; Expected = ([PSCustomObject]@{ Name = 'Jakub' }); Message = "Expected object PSObject{Name='Jakub'}, but got 'a'." } ) { param ($Actual, $Expected, $Message) Compare-ObjectEquivalent -Expected $Expected -Actual $Actual | Verify-Equal $Message @@ -319,9 +319,9 @@ InPesterModuleScope { @{ Actual = { abc }; Expected = { def }; Message = "Expected { def } to be equivalent to the actual value, but got { abc }." }, @{ Actual = (1, 2, 3); Expected = (1, 2, 3, 4); Message = "Expected collection @(1, 2, 3, 4) with length 4 to be the same size as the actual collection, but got @(1, 2, 3) with length 3." }, @{ Actual = 3; Expected = (1, 2, 3, 4); Message = "Expected collection @(1, 2, 3, 4) with length 4, but got 3." }, - @{ Actual = (New-PSObject @{ Name = 'Jakub' }); Expected = (1, 2, 3, 4); Message = "Expected collection @(1, 2, 3, 4) with length 4, but got PSObject{Name='Jakub'}." }, - @{ Actual = (New-PSObject @{ Name = 'Jakub' }); Expected = "a"; Message = "Expected 'a' to be equivalent to the actual value, but got PSObject{Name='Jakub'}." }, - @{ Actual = 'a'; Expected = (New-PSObject @{ Name = 'Jakub' }); Message = "Expected object PSObject{Name='Jakub'}, but got 'a'." } + @{ Actual = ([PSCustomObject]@{ Name = 'Jakub' }); Expected = (1, 2, 3, 4); Message = "Expected collection @(1, 2, 3, 4) with length 4, but got PSObject{Name='Jakub'}." }, + @{ Actual = ([PSCustomObject]@{ Name = 'Jakub' }); Expected = "a"; Message = "Expected 'a' to be equivalent to the actual value, but got PSObject{Name='Jakub'}." }, + @{ Actual = 'a'; Expected = ([PSCustomObject]@{ Name = 'Jakub' }); Message = "Expected object PSObject{Name='Jakub'}, but got 'a'." } @{ Actual = 'a'; Expected = @{ Name = 'Jakub' }; Message = "Expected hashtable @{Name='Jakub'}, but got 'a'." } @{ Actual = 'a'; Expected = New-Dictionary @{ Name = 'Jakub' }; Message = "Expected dictionary Dictionary{Name='Jakub'}, but got 'a'." } ) { @@ -330,7 +330,7 @@ InPesterModuleScope { } It "Comparing the same instance of a psObject returns null" { - $actual = $expected = New-PSObject @{ Name = 'Jakub' } + $actual = $expected = [PSCustomObject]@{ Name = 'Jakub' } Verify-Same -Expected $expected -Actual $actual Compare-Equivalent -Expected $expected -Actual $actual | Verify-Null @@ -338,16 +338,16 @@ InPesterModuleScope { It "Given PSObjects '' and ' that are different instances but have the same values it returns report with Equivalent set to `$true" -TestCases @( @{ - Expected = New-PSObject @{ Name = 'Jakub' } - Actual = New-PSObject @{ Name = 'Jakub' } + Expected = [PSCustomObject]@{ Name = 'Jakub' } + Actual = [PSCustomObject]@{ Name = 'Jakub' } }, @{ - Expected = New-PSObject @{ Name = 'Jakub' } - Actual = New-PSObject @{ Name = 'Jakub' } + Expected = [PSCustomObject]@{ Name = 'Jakub' } + Actual = [PSCustomObject]@{ Name = 'Jakub' } }, @{ - Expected = New-PSObject @{ Age = 28 } - Actual = New-PSObject @{ Age = '28' } + Expected = [PSCustomObject]@{ Age = 28 } + Actual = [PSCustomObject]@{ Age = '28' } } ) { param ($Expected, $Actual) @@ -358,18 +358,18 @@ InPesterModuleScope { It "Given PSObjects '' and ' that have different values in some of the properties it returns message ''" -TestCases @( @{ - Expected = New-PSObject @{ Name = 'Jakub'; Age = 28 } - Actual = New-PSObject @{ Name = 'Jakub'; Age = 19 } + Expected = [PSCustomObject]@{ Name = 'Jakub'; Age = 28 } + Actual = [PSCustomObject]@{ Name = 'Jakub'; Age = 19 } Message = "Expected property .Age with value 28 to be equivalent to the actual value, but got 19." }, @{ - Expected = New-PSObject @{ Name = 'Jakub'; Age = 28 } - Actual = New-PSObject @{ Name = 'Jakub' } + Expected = [PSCustomObject]@{ Name = 'Jakub'; Age = 28 } + Actual = [PSCustomObject]@{ Name = 'Jakub' } Message = "Expected has property 'Age' that the other object does not have." }, @{ - Expected = New-PSObject @{ Name = 'Jakub' } - Actual = New-PSObject @{ Name = 'Jakub'; Age = 28 } + Expected = [PSCustomObject]@{ Name = 'Jakub' } + Actual = [PSCustomObject]@{ Name = 'Jakub'; Age = 28 } Message = "Expected is missing property 'Age' that the other object has." } ) { @@ -382,7 +382,7 @@ InPesterModuleScope { It "Given PSObject '' and object ' that have the same values it returns `$null" -TestCases @( @{ Expected = New-Object -TypeName Assertions.TestType.Person2 -Property @{ Name = 'Jakub'; Age = 28 } - Actual = New-PSObject @{ Name = 'Jakub'; Age = 28 } + Actual = [PSCustomObject]@{ Name = 'Jakub'; Age = 28 } } ) { param ($Expected, $Actual) @@ -392,8 +392,8 @@ InPesterModuleScope { It "Given PSObjects '' and ' that contain different arrays in the same property returns the correct message" -TestCases @( @{ - Expected = New-PSObject @{ Numbers = 1, 2, 3 } - Actual = New-PSObject @{ Numbers = 3, 4, 5 } + Expected = [PSCustomObject]@{ Numbers = 1, 2, 3 } + Actual = [PSCustomObject]@{ Numbers = 3, 4, 5 } } ) { param ($Expected, $Actual) @@ -403,8 +403,8 @@ InPesterModuleScope { It "Comparing psObjects that have collections of objects returns `$null when the objects have the same value" -TestCases @( @{ - Expected = New-PSObject @{ Objects = (New-PSObject @{ Name = "Jan" }), (New-PSObject @{ Name = "Tomas" }) } - Actual = New-PSObject @{ Objects = (New-PSObject @{ Name = "Tomas" }), (New-PSObject @{ Name = "Jan" }) } + Expected = [PSCustomObject]@{ Objects = ([PSCustomObject]@{ Name = "Jan" }), ([PSCustomObject]@{ Name = "Tomas" }) } + Actual = [PSCustomObject]@{ Objects = ([PSCustomObject]@{ Name = "Tomas" }), ([PSCustomObject]@{ Name = "Jan" }) } } ) { param ($Expected, $Actual) @@ -413,8 +413,8 @@ InPesterModuleScope { It "Comparing psObjects that have collections of objects returns the correct message when the items in the collection differ" -TestCases @( @{ - Expected = New-PSObject @{ Objects = (New-PSObject @{ Name = "Jan" }), (New-PSObject @{ Name = "Petr" }) } - Actual = New-PSObject @{ Objects = (New-PSObject @{ Name = "Jan" }), (New-PSObject @{ Name = "Tomas" }) } + Expected = [PSCustomObject]@{ Objects = ([PSCustomObject]@{ Name = "Jan" }), ([PSCustomObject]@{ Name = "Petr" }) } + Actual = [PSCustomObject]@{ Objects = ([PSCustomObject]@{ Name = "Jan" }), ([PSCustomObject]@{ Name = "Tomas" }) } } ) { param ($Expected, $Actual) diff --git a/tst/functions/assert/Exception/Should-Throw.Tests.ps1 b/tst/functions/assert/Exception/Should-Throw.Tests.ps1 index ea8b192e6..6c8e75856 100644 --- a/tst/functions/assert/Exception/Should-Throw.Tests.ps1 +++ b/tst/functions/assert/Exception/Should-Throw.Tests.ps1 @@ -203,7 +203,7 @@ Describe "General try catch behavior" { } InPesterModuleScope { - Describe "Get-Error" { + Describe "Get-ErrorObject" { It 'Unwraps error from invoke with context' { $ErrorActionPreference = 'stop' try { @@ -218,7 +218,7 @@ InPesterModuleScope { $e = $_ } - $err = Get-Error $e + $err = Get-ErrorObject $e $err.ExceptionMessage | Verify-Like "Cannot find path*because it does not exist." $err.ExceptionType | Verify-Equal ([Management.Automation.ItemNotFoundException]) $err.FullyQualifiedErrorId | Verify-Equal 'PathNotFound,Microsoft.PowerShell.Commands.GetItemCommand'