Skip to content
This repository has been archived by the owner on Sep 3, 2023. It is now read-only.

Issue #7: recursiveEquality does not work for OrderedDictionaries #8

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion functional.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,22 @@ function recursiveEquality($a, $b) {
$inequalIndexes = 0..($a.Count - 1) | ? { -not (recursiveEquality $a[$_] $b[$_]) }
return $inequalIndexes.Count -eq 0
}
if ($a -is [hashtable] -and $b -is [hashtable]) {
if ($a -is [hashtable] -and $b -is [hashtable]){
Write-Debug "recursively test hashtable '$a' '$b'"
$inequalKeys = $a.Keys + $b.Keys `
| Sort-Object -Unique `
| ? { -not (recursiveEquality $a[$_] $b[$_]) }
return $inequalKeys.Count -eq 0
}

if ($a -is [System.Collections.IDictionary] -and $b -is [System.Collections.IDictionary]){
Write-Debug "recursively test Ordered Dictionary'$a' '$b'"
$inequalKeys = $a.Keys + $b.Keys `
| Sort-Object -Unique `
| ? { -not (recursiveEquality $a[$_] $b[$_]) }
return $inequalKeys.Count -eq 0
}

if ((isPsCustomObject $a) -and (isPsCustomObject $b)) {
Write-Debug "a is pscustomobject: $($a -is [psobject])"
Write-Debug "recursively test objects '$a' '$b'"
Expand Down
17 changes: 12 additions & 5 deletions functional.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -278,14 +278,21 @@ Describe "Test-Equality" {
@{a = 1; b = @{c = 2 } }, @{a = 1; b = @{c = 2 } } | Test-Equality | Should -BeTrue
}
}
Context "Given an array of PSCustomObject" {
$a = @([PSCustomObject]@{ 'Name' = 'Foo'; 'Value' = 'Foo' }, [PSCustomObject]@{ 'Name' = 'Baz'; 'Value' = 'Baz' } )
$b = @([PSCustomObject]@{ 'Name' = 'xxx'; 'Value' = 'Foo' }, [PSCustomObject]@{ 'Name' = 'Baz'; 'Value' = 'Baz' } )
Context "Given Ordered Dictionaries" {
It "Should be false for deep inequal values" {
$a, $b | Test-Equality | Should -BeFalse
[ordered]@{a = 1; b = @{c = 2 } }, [ordered]@{a = 1; b = [pscustomobject]@{c = 2 } } | Test-Equality | Should -BeFalse
}
It "Should be true for deep equal values" {
$a, $a | Test-Equality | Should -BeTrue
[ordered]@{a = 1; b = @{c = 2 } }, [ordered]@{a = 1; b = @{c = 2 } } | Test-Equality | Should -BeTrue
}
}
Context "Given an array of PSCustomObject" {
BeforeAll{
$a = @([PSCustomObject]@{ 'Name' = 'Foo'; 'Value' = 'Foo' }, [PSCustomObject]@{ 'Name' = 'Baz'; 'Value' = 'Baz' } )
$b = @([PSCustomObject]@{ 'Name' = 'xxx'; 'Value' = 'Foo' }, [PSCustomObject]@{ 'Name' = 'Baz'; 'Value' = 'Baz' } )
}
It "Should be false for deep inequal values" {
$a, $b | Test-Equality | Should -BeFalse
}
}
}