From 0230d8cfda6537dfb2e5fb7bf943c8929f399b2f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Jare=C5=A1?= Date: Sun, 7 Apr 2024 22:58:47 +0200 Subject: [PATCH] Remove Compatibility.ps1 --- build.ps1 | 2 -- src/Format2.ps1 | 4 ++-- src/functions/assert/Compatibility.ps1 | 21 ------------------- .../assert/Equivalence/Assert-Equivalent.ps1 | 18 ++++++++-------- .../assert/Exception/Assert-Throw.ps1 | 4 ++-- 5 files changed, 13 insertions(+), 36 deletions(-) delete mode 100644 src/functions/assert/Compatibility.ps1 diff --git a/build.ps1 b/build.ps1 index 0f48d5791..19743608f 100644 --- a/build.ps1 +++ b/build.ps1 @@ -282,8 +282,6 @@ $script = @( "$PSScriptRoot/src/Pester.RSpec.ps1" "$PSScriptRoot/src/Main.ps1" - # TODO: Imports Compatibility.ps1, remove the code that is there, because it is for PowerShell 2.0 compatibility! - "$PSScriptRoot/src/functions/assert/*" "$PSScriptRoot/src/functions/assert/*/*" "$PSScriptRoot/src/functions/assertions/*" "$PSScriptRoot/src/functions/*" diff --git a/src/Format2.ps1 b/src/Format2.ps1 index 0c8c08197..a6cfa50fd 100644 --- a/src/Format2.ps1 +++ b/src/Format2.ps1 @@ -112,7 +112,7 @@ function Format-Nicely2 ($Value, [switch]$Pretty) { return Format-Collection2 -Value $Value -Pretty:$Pretty } - Format-Object2 -Value $Value -Property (Get-DisplayProperty2 (Get-Type $Value)) -Pretty:$Pretty + Format-Object2 -Value $Value -Property (Get-DisplayProperty2 $Value.GetType()) -Pretty:$Pretty } function Get-DisplayProperty2 ([Type]$Type) { @@ -139,7 +139,7 @@ function Get-DisplayProperty2 ([Type]$Type) { function Get-ShortType2 ($Value) { if ($null -ne $value) { - Format-Type2 (Get-Type $Value) + Format-Type2 $Value.GetType() } else { Format-Type2 $null diff --git a/src/functions/assert/Compatibility.ps1 b/src/functions/assert/Compatibility.ps1 deleted file mode 100644 index 9b03aaf45..000000000 --- a/src/functions/assert/Compatibility.ps1 +++ /dev/null @@ -1,21 +0,0 @@ - -function Test-NullOrWhiteSpace ($Value) { - # psv2 compatibility, on newer .net we would simply use - # [string]::isnullorwhitespace - $null -eq $Value -or $Value -match "^\s*$" -} - -function Get-Type ($InputObject) { - try { - $ErrorActionPreference = 'Stop' - # normally this would not ever throw - # but in psv2 when datatable is deserialized then - # [Deserialized.System.Data.DataTable] does not contain - # .GetType() - $InputObject.GetType() - } - catch [Exception] { - return [Object] - } - -} diff --git a/src/functions/assert/Equivalence/Assert-Equivalent.ps1 b/src/functions/assert/Equivalence/Assert-Equivalent.ps1 index e03d4db24..a3b2f39f5 100644 --- a/src/functions/assert/Equivalence/Assert-Equivalent.ps1 +++ b/src/functions/assert/Equivalence/Assert-Equivalent.ps1 @@ -56,7 +56,7 @@ function Compare-CollectionEquivalent ($Expected, $Actual, $Property, $Options) } if (-not (Is-Collection -Value $Actual)) { - v -Difference "`$Actual is not a collection it is a $(Format-Nicely2 (Get-Type $Actual)), so they are not equivalent." + v -Difference "`$Actual is not a collection it is a $(Format-Nicely2 $Actual.GetType()), so they are not equivalent." $expectedFormatted = Format-Collection2 -Value $Expected $expectedLength = $expected.Length $actualFormatted = Format-Nicely2 -Value $actual @@ -240,9 +240,9 @@ function Compare-ValueEquivalent ($Actual, $Expected, $Property, $Options) { v "Equality comparator is used, values will be compared for equality." } - v "Comparing values as $(Format-Nicely2 (Get-Type $Expected)) because `$Expected has that type." + v "Comparing values as $(Format-Nicely2 $Expected.GetType()) because `$Expected has that type." # todo: shorter messages when both sides have the same type (do not compare by using -is, instead query the type and compare it) because -is is true even for parent types - $type = Get-Type $Expected + $type = $Expected.GetType() $coalescedActual = $Actual -as $type if ($Expected -ne $Actual) { v -Difference "`$Actual is not equivalent to $(Format-Nicely2 $Expected) because it is $(Format-Nicely2 $Actual), and $(Format-Nicely2 $Actual) coalesced to $(Format-Nicely2 $type) is $(Format-Nicely2 $coalescedActual)." @@ -257,7 +257,7 @@ function Compare-HashtableEquivalent ($Actual, $Expected, $Property, $Options) { } if (-not (Is-Hashtable -Value $Actual)) { - v -Difference "`$Actual is not a hashtable it is a $(Format-Nicely2 (Get-Type $Actual)), so they are not equivalent." + v -Difference "`$Actual is not a hashtable it is a $(Format-Nicely2 $Actual.GetType()), so they are not equivalent." $expectedFormatted = Format-Nicely2 -Value $Expected $actualFormatted = Format-Nicely2 -Value $Actual return "Expected hashtable '$expectedFormatted', but got '$actualFormatted'." @@ -323,7 +323,7 @@ function Compare-DictionaryEquivalent ($Actual, $Expected, $Property, $Options) } if (-not (Is-Dictionary -Value $Actual)) { - v -Difference "`$Actual is not a dictionary it is a $(Format-Nicely2 (Get-Type $Actual)), so they are not equivalent." + v -Difference "`$Actual is not a dictionary it is a $(Format-Nicely2 $Actual.GetType()), so they are not equivalent." $expectedFormatted = Format-Nicely2 -Value $Expected $actualFormatted = Format-Nicely2 -Value $Actual return "Expected dictionary '$expectedFormatted', but got '$actualFormatted'." @@ -387,7 +387,7 @@ function Compare-ObjectEquivalent ($Actual, $Expected, $Property, $Options) { } if (-not (Is-Object -Value $Actual)) { - v -Difference "`$Actual is not an object it is a $(Format-Nicely2 (Get-Type $Actual)), so they are not equivalent." + v -Difference "`$Actual is not an object it is a $(Format-Nicely2 $Actual.GetType()), so they are not equivalent." $expectedFormatted = Format-Nicely2 -Value $Expected $actualFormatted = Format-Nicely2 -Value $Actual return "Expected object '$expectedFormatted', but got '$actualFormatted'." @@ -553,11 +553,11 @@ function Compare-Equivalent { } if ($null -eq $Actual) { - v -Difference "`$Actual is $(Format-Nicely2), but `$Expected has value of type $(Format-Nicely2 (Get-Type $Expected)), so they are not equivalent." + v -Difference "`$Actual is $(Format-Nicely2), but `$Expected has value of type $(Format-Nicely2 $Expected.GetType()), so they are not equivalent." return Get-ValueNotEquivalentMessage -Expected $Expected -Actual $Actual -Property $Path } - v "`$Expected has type $(Get-Type $Expected), `$Actual has type $(Get-Type $Actual), they are both non-null." + v "`$Expected has type $$Expected.GetType(), `$Actual has type $$Actual.GetType(), they are both non-null." # test value types, strings, and single item arrays with values in them as values # expand the single item array to get to the value in it @@ -609,7 +609,7 @@ function Compare-Equivalent { return } - v "`$Expected is an object of type $(Get-Type $Expected), we will be comparing `$Actual to objects." + v "`$Expected is an object of type $$Expected.GetType(), we will be comparing `$Actual to objects." Compare-ObjectEquivalent -Expected $Expected -Actual $Actual -Property $Path -Options $Options } diff --git a/src/functions/assert/Exception/Assert-Throw.ps1 b/src/functions/assert/Exception/Assert-Throw.ps1 index 023e74329..438900903 100644 --- a/src/functions/assert/Exception/Assert-Throw.ps1 +++ b/src/functions/assert/Exception/Assert-Throw.ps1 @@ -45,7 +45,7 @@ function Assert-Throw { } } - $filterOnMessage = -not (Test-NullOrWhiteSpace $ExceptionMessage) + $filterOnMessage = -not ([string]::IsNullOrWhiteSpace($ExceptionMessage)) if ($filterOnMessage) { $filters += "with message '$ExceptionMessage'" if ($err.ExceptionMessage -notlike $ExceptionMessage) { @@ -53,7 +53,7 @@ function Assert-Throw { } } - $filterOnId = -not (Test-NullOrWhiteSpace $FullyQualifiedErrorId) + $filterOnId = -not ([string]::IsNullOrWhiteSpace($FullyQualifiedErrorId)) if ($filterOnId) { $filters += "with FullyQualifiedErrorId '$FullyQualifiedErrorId'" if ($err.FullyQualifiedErrorId -notlike $FullyQualifiedErrorId) {