Tests/Convert-ObjectToHashtable.Tests.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
gci "$PSScriptRoot\..\*.psm1" | Import-Module  -force


Describe 'Convert-ObjectToHashtable' {
    Context 'when converting a JSON file to a hashtable' {

        $obj = Get-Content "$PSScriptRoot\TestData\template_properties.json" | ConvertFrom-Json
        
        $result = Convert-ObjectToHashtable $obj
        $resultObj = Convert-HashTableToObject $result

        It 'has all of the same values as the orginal JSON file' {       
            Compare-FullObject -referenceObject $resultObj -differenceObject $obj | Should BeNullOrempty 
        }
        
        It 'had the same order as the input object' {
            $propNames = $obj.psobject.properties.name
            $hashKeys = $result.GetEnumerator().Name
            for($i = 0; $i -lt $propNames.Count;$i++) {
                $propNames[$i] | Should Match $hashKeys[$i]
            }
            
            for($i = 0; $i -lt $hashKeys.Count;$i++) {
                $hashKeys[$i] | Should Match $propNames[$i]
            }   
            
        }
    }
}