Assert icon indicating copy to clipboard operation
Assert copied to clipboard

Moved StrictOrder, PowerShell 7.0.0-preview.2 DBNull support and StrictEquality

Open ili101 opened this issue 4 years ago • 1 comments

Hi This pull is to replace in part the outdated pull https://github.com/nohwnd/Assert/pull/34. There are 3 things covered here:

Commit https://github.com/nohwnd/Assert/commit/c7703405086e8b47f56c43da113ff1ffc748a405 - Moved StrictOrder to Get-EquivalencyOption from Assert-Equivalent so it will be in the same place with all the other configuration options.

Commit https://github.com/nohwnd/Assert/commit/6aea1e058bb2261f71f75b51a644284e73f2966f - Deserialize DBNull support added as Deserialize DataTable and DataRow are already supported in the existing version and it is missing to complete the set to make this usable.

Commit https://github.com/nohwnd/Assert/commit/959c9ca38ee7eccbe4d03fc3a485c44b10b2b426 - Added a new -Comparator Option 'StrictEquality' Use this code to illustrate the 3 modes ('Equivalency', 'Equality', 'StrictEquality')

$OptionsEquality = Get-EquivalencyOption -Comparator Equality
$OptionsStrictEquality = Get-EquivalencyOption -Comparator StrictEquality
$DBNull = [DBNull]::Value
$TestCases = @(
    #{ $Expected = 'Same'; $Actual = 'Same' }
    { $Expected = 'False'; $Actual = $false }
    { $Expected = $null; $Actual = $DBNull }
    { $Expected = 5; $Actual = '5' }
    { $Expected = { 'AAA' }; $Actual = { 'AAA' } }
)
$Results = @()
foreach ($TestCase in $TestCases) {
    . $TestCase
    $Results += [PSCustomObject]@{
        Test          = $TestCase.ToString()
        PsForward     = ($Expected -eq $Actual)
        PsReverse     = ($Actual -eq $Expected)
        AssEqualityF = . { try { Assert-Equivalent -Actual $Actual -Expected $Expected -Options $OptionsEquality ; $true } catch { $false } }
        AssEqualityR = . { try { Assert-Equivalent -Actual $Expected -Expected $Actual -Options $OptionsEquality ; $true } catch { $false } }
        AssertF = . { try { Assert-Equivalent -Actual $Actual -Expected $Expected ; $true } catch { $false } }
        AssertR = . { try { Assert-Equivalent -Actual $Expected -Expected $Actual ; $true } catch { $false } }
        AssSEqualityF = . { try { Assert-Equivalent -Actual $Actual -Expected $Expected -Options $OptionsStrictEquality ; $true } catch { $false } }
        AssSEqualityR = . { try { Assert-Equivalent -Actual $Expected -Expected $Actual -Options $OptionsStrictEquality ; $true } catch { $false } }
    }
}
$Results | Format-Table

PowerShell 7.0.0-preview.2

Test                                         PsForward PsReverse AssEqualityF AssEqualityR AssertF AssertR AssSEqualityF AssSEqualityR
----                                         --------- --------- ------------ ------------ ------- ------- ------------- -------------
 $Expected = 'False'; $Actual = $false            True     False         True        False    True    True         False         False
 $Expected = $null; $Actual = $DBNull             True      True         True         True    True    True         False         False
 $Expected = 5; $Actual = '5'                     True      True         True         True    True    True         False         False
 $Expected = { 'AAA' }; $Actual = { 'AAA' }      False     False        False        False    True    True         False         False

PowerShell 5.1

Test                                         PsForward PsReverse AssEqualityF AssEqualityR AssertF AssertR AssSEqualityF AssSEqualityR
----                                         --------- --------- ------------ ------------ ------- ------- ------------- -------------
 $Expected = 'False'; $Actual = $false            True     False         True        False    True    True         False         False
 $Expected = $null; $Actual = $DBNull            False     False        False        False    True    True         False         False
 $Expected = 5; $Actual = '5'                     True      True         True         True    True    True         False         False
 $Expected = { 'AAA' }; $Actual = { 'AAA' }      False     False        False        False    True    True         False         False

Equivalency - Return true in all the tested situations, using your existing equivalency code. StrictEquality - Return false in all the tested situations, it verifies that the Type is the same in addition to the value test. Equality - Exactly the same as doing $Expected -eq $Actual (Can return different results depending on the PS version and the side of the variable ('False' -eq $false vs $false -eq 'False')).

Since PowerShell 7.0.0-preview.2 DBNull -eq $null. So in Equality mode the result will depend on the PowerShell version as [DBNull]::Value -eq $null will depend on the PowerShell version. In Equivalency mode this inconsistency is fixed to reflect the spirit of PS7 and it will be true in all PS versions. In StrictEquality it will return false as in StrictEquality mode the Types need to be identical to pass.

ili101 avatar Aug 15 '19 12:08 ili101

Added commit https://github.com/nohwnd/Assert/pull/45/commits/6166abfdae360098c4c08a1988852e036f640ba4 as travis-ci was failing. The travis-ci test is running on powershell-6.0.0-beta.9 and it looks like Export-Clixml/Import-Clixml is bugged there (Also now it's working without needing a file). As PS 6.0 is already deprecated maybe it will be good to update the test to use newer version brew update? I'm not an Apple guy :-)

ili101 avatar Aug 15 '19 15:08 ili101