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

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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

ili101
Copy link
Contributor

@ili101 ili101 commented Aug 15, 2019

Hi
This pull is to replace in part the outdated pull #34.
There are 3 things covered here:

Commit c770340 - Moved StrictOrder to Get-EquivalencyOption from Assert-Equivalent so it will be in the same place with all the other configuration options.

Commit 6aea1e0 - 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 959c9ca - 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.

…: in mode "Equivalency" it will pass and fail for "Equality" in all PS versions.

Also Deserialize DBNull support added as Deserialize DataTable and DataRow are already supported and it was missing.
@ili101
Copy link
Contributor Author

ili101 commented Aug 15, 2019

Added commit 6166abf 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 :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant