Tests/ConvertTo-Hash.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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 |
#$here = Split-Path -Parent $MyInvocation.MyCommand.Path #$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path) -replace '\.Tests\.', '.' #. "$here\$sut" #$path = "C:\Users\tore\Dropbox\SourceTreeRepros\PoshARM\PoshARM.psd1" #Import-Module $path $modulePath = Split-Path $PSScriptRoot -Parent $modulepath = Join-Path -Path $modulePath -ChildPath posharm.psd1 Import-Module $modulePath Describe "ConvertTo-Hash" { $simpleObject = [pscustomobject]@{ Name = "Tore" Age = 45 } $nestedObject = [pscustomobject]@{ Name = "Tore" Address = @{ Street = "TimesSquare 1" City = "New York" } } Context "Without pipeline" { $hash = ConvertTo-Hash -InputObject $simpleObject $nestedHash = ConvertTo-Hash -InputObject $nestedObject It "Should be a OrderedDictionary object type" { $hash.GetType().Name | should be "OrderedDictionary" } It "Should have a Name Key" { $hash.Name | Should be $simpleObject.Name } It "Should have a Age Key" { $hash.Age | Should be $simpleObject.Age } It "Should create a nested hashtable" { $nestedHash.Address | should not be $null } It "Should create a hashtable object" { $nestedHash.Address.GetType().Name | Should be "hashtable" } It "Should have a street address [$($nestedObject.Address.Street)]" { $nestedHash.Address.Street | should be $nestedObject.Address.Street } It "Should have a city address [$($nestedObject.Address.City)]" { $nestedHash.Address.City | should be $nestedObject.Address.City } } Context "With pipeline" { $hash = $null $nestedHash = $null $hash = $simpleObject | ConvertTo-Hash $nestedHash = $nestedObject | ConvertTo-Hash It "Should be a OrderedDictionary object type" { $hash.GetType().Name | should be "OrderedDictionary" } It "Should have a Name Key" { $hash.Name | Should be $simpleObject.Name } It "Should have a Age Key" { $hash.Age | Should be $simpleObject.Age } It "Should create a nested hashtable" { $nestedHash.Address | should not be $null } It "Should create a hashtable object" { $nestedHash.Address.GetType().Name | Should be "hashtable" } It "Should have a street address [$($nestedObject.Address.Street)]" { $nestedHash.Address.Street | should be $nestedObject.Address.Street } It "Should have a city address [$($nestedObject.Address.City)]" { $nestedHash.Address.City | should be $nestedObject.Address.City } } Context "Array handling with pipeline" { $array = @() foreach ($int in (1..2)) { $array += [PSCustomObject]@{ Number = $int Name = "My name is $int" } } $arrayObj = [pscustomobject]@{ArrayItem = $array} $assert = $arrayObj | ConvertTo-Hash It "arrayObj should be a PSCustomObject" { $arrayObj.GetType().Name | Should be "PSCustomObject" } It "Value of ArrayItem should be of type Array" { $arrayObj.ArrayItem -is [array] | Should be $true } it "Should convert the array to a collection of OrderedDictionary"{ $assert.GetType().Name | Should be "OrderedDictionary" } $index = 0 foreach ($hashItem in $assert.ArrayItem) { It "Should have a collection of OrderedDictionary values" { $hashItem.GetType().Name | Should Be "OrderedDictionary" } It "Should have Number key with value [$($arrayObj.ArrayItem[$index].Number)]" { $hashItem.Number | Should be $arrayObj.ArrayItem[$index].Number } $index++ } } Context "Array handling without pipeline" { $array = @() foreach ($int in (1..2)) { $array += [PSCustomObject]@{ Number = $int Name = "My name is $int" } } $arrayObj = [pscustomobject]@{ArrayItem = $array} $assert = ConvertTo-Hash -InputObject $arrayObj It "arrayObj should be a PSCustomObject" { $arrayObj.GetType().Name | Should be "PSCustomObject" } It "Value of ArrayItem should be of type Array" { $arrayObj.ArrayItem -is [array] | Should be $true } it "Should convert the array to a collection of OrderedDictionary"{ $assert.GetType().Name | Should be "OrderedDictionary" } $index = 0 foreach ($hashItem in $assert.ArrayItem) { It "Should have a collection of OrderedDictionary values" { $hashItem.GetType().Name | Should Be "OrderedDictionary" } It "Should have Number key with value [$($arrayObj.ArrayItem[$index].Number)]" { $hashItem.Number | Should be $arrayObj.ArrayItem[$index].Number } $index++ } } Context "Passthrou Hashtable with pipeline" { $hash = @{test=1} $assert = $hash | ConvertTo-Hash It "Should Passthrou inputobject if it is a hashtable" { $assert.GetType().Name | Should be "hashtable" } It "Should have a test key with value [1]" { $assert.test | should be 1 } } Context "Passthrou OrderedDictionary with pipeline" { $ordered = [ordered]@{test=1} $assert = $ordered | ConvertTo-Hash It "Should Passthrou inputobject if it is a OrderedDictionary" { $assert.GetType().Name | Should be "OrderedDictionary" } It "Should have a test key with value [1]" { $assert.test | should be 1 } } Context "Passthrou Hashtable without pipeline" { $hash = @{test=1} $assert = ConvertTo-Hash -InputObject $hash It "Should Passthrou inputobject if it is a hashtable" { $assert.GetType().Name | Should be "hashtable" } It "Should have a test key with value [1]" { $assert.test | should be 1 } } Context "Passthrou OrderedDictionary without pipeline" { $ordered = [ordered]@{test=1} $assert = ConvertTo-Hash -InputObject $ordered It "Should Passthrou inputobject if it is a OrderedDictionary" { $assert.GetType().Name | Should be "OrderedDictionary" } It "Should have a test key with value [1]" { $assert.test | should be 1 } } } Remove-Module -name posharm -ErrorAction SilentlyContinue |