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

recursiveEquality does not work for OrderedDictionaries #7

Open
dendy7h opened this issue Jul 28, 2020 · 1 comment
Open

recursiveEquality does not work for OrderedDictionaries #7

dendy7h opened this issue Jul 28, 2020 · 1 comment

Comments

@dendy7h
Copy link

dendy7h commented Jul 28, 2020

Comparing Ordered-dictionaries using recursiveEquality does not work.

@dendy7h
Copy link
Author

dendy7h commented Jul 28, 2020

I am not sure how to contribute, do not have the permissions needed for creating a new branch. This is a quick fix, just adding another if statement in the recursiveEquality function:

function recursiveEquality($a, $b) {                                                                
  if ($a -is [array] -and $b -is [array]) {                                                         
    Write-Debug "recursively test arrays '$a' '$b'"                                                 
    if ($a.Count -ne $b.Count) {                                                                    
      return $false                                                                                 
    }                                                                                               
    $inequalIndexes = 0..($a.Count - 1) | ? { -not (recursiveEquality $a[$_] $b[$_]) }              
    return $inequalIndexes.Count -eq 0                                                              
  }                                                                                                 
  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'"                                                
    $inequalKeys = $a.psobject.Properties + $b.psobject.Properties `                                
    | % Name `                                                                                      
    | Sort-Object -Unique `                                                                         
    | ? { -not (recursiveEquality $a.$_ $b.$_) }                                                    
    return $inequalKeys.Count -eq 0                                                                 
  }                                                                                                 
  Write-Debug "test leaves '$a' '$b'"                                                               
  return $a.GetType() -eq $b.GetType() -and $a -eq $b                                               
}                                                                                                                                                                                                  

functions.test.ps1

Describe "Test-Equality" {                                                                                                                 
  Context "Given leaves" {                                                                                                                 
    It "Should be true for equal values of the same type" {                                                                                
      3, 3 | Test-Equality | Should  -BeTrue                                                                                               
    }                                                                                                                                      
    It "Should be false for equal values of different types" {                                                                             
      3, "3" | Test-Equality | Should -BeFalse                                                                                             
    }                                                                                                                                      
  }                                                                                                                                        
  Context "Given arrays" {                                                                                                                 
    It "Should be false for deep inequal values" {                                                                                         
      @(1, 2, @{a = 1 }, 3), @(1, 2, @{a = 2 }, 3) | Test-Equality | Should -BeFalse                                                       
    }                                                                                                                                      
    It "Should be true for deep equal values" {                                                                                            
      @(1, 2, @{a = 1 }, 3), @(1, 2, @{a = 1 }, 3) | Test-Equality | Should -BeTrue                                                        
    }                                                                                                                                      
  }                                                                                                                                        
  Context "Given hashtables" {                                                                                                             
    It "Should be false for deep inequal values" {                                                                                         
      @{a = 1; b = @{c = 2 } }, @{a = 1; b = [pscustomobject]@{c = 2 } } | Test-Equality | Should -BeFalse                                 
    }                                                                                                                                      
    It "Should be true for deep equal values" {                                                                                            
      @{a = 1; b = @{c = 2 } }, @{a = 1; b = @{c = 2 } } | Test-Equality | Should -BeTrue                                                  
    }                                                                                                                                      
  }                                                                                                                                        
  Context "Given Ordered Dictionaries" {                                                                                                   
    It "Should be false for deep inequal values" {                                                                                         
      [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" {                                                                                            
      [ordered]@{a = 1; b = @{c = 2 } }, [ordered]@{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' } )                    
    It "Should be false for deep inequal values" {                                                                                         
      $a, $b | Test-Equality | Should -BeFalse                                                                                             
    }                                                                                                                                      
  }                                                                                                                                        
}                                                                                                                                          

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

No branches or pull requests

1 participant