MegaBites-AI/Windows-powershell
0372
1# Copyright (c) Microsoft Corporation.2# Licensed under the MIT License.3 4# This is a Pester test suite which validate the Json cmdlets.5#6 7#8# This 'Describe' is for tests that were converted from utscripts (SDXROOT/admin/monad/tests/monad/DRT/utscripts)9# and C# tests (SDXROOT/admin/monad/tests/monad/DRT/commands/utility/UnitTests) to Pester.10#11Describe "Json Tests" -Tags "Feature" {12 13 BeforeAll {14 15 function ValidateSampleObject16 {17 param ($result, [switch]$hasEmbeddedSampleObject )18 19 Write-Verbose "validating deserialized SampleObject" -Verbose20 $result.SampleInt | Should -Be 9876521 $result.SampleString | Should -Match "stringVal"22 $result.SampleArray.Count | Should -Be 223 $result.SampleTrue | Should -BeTrue24 $result.SampleFalse | Should -BeFalse25 $result.SampleNull | Should -BeNullOrEmpty26 $result.SampleFloat | Should -Be 9.8765E+4327 28 if ($hasEmbeddedSampleObject)29 {30 Write-Verbose "validating deserialized Embedded SampleObject" -Verbose31 ValidateSampleObject -result $result.SampleObject32 }33 }34 35 }36 37 Context "ConvertTo-Json Bug Fixes" {38 39 It "ConvertTo-JSON should not have hard coded english error message" {40 41 # Test follow-up for bug WinBlue: 163372 - ConvertTo-JSON has hard coded english error message.42 $process = Get-Process -Id $PID43 $hash = @{ $process = "def" }44 $expectedFullyQualifiedErrorId = "NonStringKeyInDictionary,Microsoft.PowerShell.Commands.ConvertToJsonCommand"45 46 { ConvertTo-Json -InputObject $hash } | Should -Throw -ErrorId $expectedFullyQualifiedErrorId47 }48 49 It "ConvertTo-Json should handle terms with double quotes" {50 51 # Test follow-up for bug WinBlue: 11484 - ConvertTo-Json can't handle terms with double quotes.52 53 $notcompressed = ConvertTo-Json @{ FirstName = 'Hello " World' }54 $compressed = ConvertTo-Json @{ FirstName = 'Hello " World' } -Compress55 $valueFromNotCompressedResult = ConvertFrom-Json -InputObject $notcompressed56 $valueFromCompressedResult = ConvertFrom-Json -InputObject $compressed57 58 $valueFromNotCompressedResult.FirstName | Should -Match $valueFromCompressedResult.FirstName59 }60 61 It "Convertto-Json should handle Enum based on Int64" -Skip:(62 [ExperimentalFeature]::IsEnabled("PSSerializeJSONLongEnumAsNumber")63 ) {64 65 # Test follow-up for bug Win8: 378368 Convertto-Json problems with Enum based on Int64.66 if ( $null -eq ("JsonEnumTest" -as "Type")) {67 $enum1 = "TestEnum" + (Get-Random)68 $enum2 = "TestEnum" + (Get-Random)69 $enum3 = "TestEnum" + (Get-Random)70 71 $jsontype = Add-Type -pass -TypeDef "72 public enum $enum1 : ulong { One = 1, Two = 2 };73 public enum $enum2 : long { One = 1, Two = 2 };74 public enum $enum3 : int { One = 1, Two = 2 };75 public class JsonEnumTest {76 public $enum1 TestEnum1 = ${enum1}.One;77 public $enum2 TestEnum2 = ${enum2}.Two;78 public $enum3 TestEnum3 = ${enum3}.One;79 }"80 }81 $op = [JsonEnumTest]::New() | ConvertTo-Json | ConvertFrom-Json82 $op.TestEnum1 | Should -BeExactly "One"83 $op.TestEnum2 | Should -BeExactly "Two"84 $op.TestEnum3 | Should -Be 185 }86 87 It "Test followup for Windows 8 bug 121627" {88 89 $JsonString = Get-Command Get-help |Select-Object Name, Noun, Verb| ConvertTo-Json90 $actual = ConvertFrom-Json $JsonString91 92 $actual.Name | Should -BeExactly "Get-Help"93 $actual.Noun | Should -BeExactly "Help"94 $actual.Verb | Should -BeExactly "Get"95 }96 }97 98 Context "ConvertFrom and ConvertTo on JsonObject Tests" {99 100 It "Convert dictionary to PSObject" {101 102 $response = ConvertFrom-Json '{"d":{"__type":"SimpleJsonObject","Name":{"First":"Joel","Last":"Wood"},"Greeting":"Hello"}}'103 $response.d.Name.First | Should -Match "Joel"104 }105 106 It "Convert to Json using PSObject" -Pending:($IsCoreCLR) {107 108 $response = ConvertFrom-Json '{"d":{"__type":"SimpleJsonObject","Name":{"First":"Joel","Last":"Wood"},"Greeting":"Hello"}}'109 110 $response2 = ConvertTo-Json -InputObject $response -ErrorAction Continue111 $response2 = ConvertTo-Json -InputObject $response -ErrorAction Inquire112 $response2 = ConvertTo-Json -InputObject $response -ErrorAction SilentlyContinue113 $response2 = ConvertTo-Json -InputObject $response -Depth 2 -Compress114 $response2 | Should -Be '{"d":{"Name":{"First":"Joel","Last":"Wood"},"Greeting":"Hello"}}'115 116 $response2 = ConvertTo-Json -InputObject $response -Depth 1 -Compress117 $nameString = [System.Management.Automation.LanguagePrimitives]::ConvertTo($response.d.Name, [string])118 $response2 | Should -BeExactly "{`"d`":{`"Name`":`"$nameString`",`"Greeting`":`"Hello`"}}"119 120 $result1 = @"121{122 "d": {123 "Name": {124 "First": "Joel",125 "Last": "Wood"126 },127 "Greeting": "Hello"128 }129}130"@131 $response2 = ConvertTo-Json -InputObject $response -Depth 2132 $response2 | Should -Match $result1133 134 $result2 = @"135{136 "d": {137 "Name": "$nameString",138 "Greeting": "Hello"139 }140}141"@142 $response2 = ConvertTo-Json -InputObject $response -Depth 1143 $response2 | Should -Match $result2144 145 $arraylist = New-Object System.Collections.ArrayList146 [void]$arraylist.Add("one")147 [void]$arraylist.Add("two")148 [void]$arraylist.Add("three")149 $response2 = ConvertTo-Json -InputObject $arraylist -Compress150 $response2 | Should -Be '["one","two","three"]'151 152 $result3 = @"153[154 "one",155 "two",156 "three"157]158"@159 $response2 = ConvertTo-Json -InputObject $arraylist160 $response2 | Should -Be $result3161 162 $response2 = $arraylist | ConvertTo-Json163 $response2 | Should -Be $result3164 }165 166 It "Convert to Json using hashtable" -Pending:($IsCoreCLR) {167 168 $nameHash = @{First="Joe1";Last="Wood"}169 $dHash = @{Name=$nameHash; Greeting="Hello"}170 $rootHash = @{d=$dHash}171 $response3 = ConvertTo-Json -InputObject $rootHash -Depth 2 -Compress172 $response3 | Should -Be '{"d":{"Greeting":"Hello","Name":{"Last":"Wood","First":"Joe1"}}}'173 174 $response3 = ConvertTo-Json -InputObject $rootHash -Depth 1 -Compress175 $response3 | Should -Be '{"d":{"Greeting":"Hello","Name":"System.Collections.Hashtable"}}'176 177 $result4 = @"178{179 "d": {180 "Greeting": "Hello",181 "Name": {182 "Last": "Wood",183 "First": "Joe1"184 }185 }186}187"@188 $response3 = ConvertTo-Json -InputObject $rootHash -Depth 2189 $response3 | Should -Be $result4190 191 $result5 = @"192{193 "d": {194 "Greeting": "Hello",195 "Name": "System.Collections.Hashtable"196 }197}198"@199 $response3 = ConvertTo-Json -InputObject $rootHash -Depth 1200 $response3 | Should -Be $result5201 }202 203 It "Convert from Json allows an empty string" {204 205 $emptyStringResult = ConvertFrom-Json ""206 $emptyStringResult | Should -BeNullOrEmpty207 }208 209 It "Convert enumerated values to Json" {210 211 $sampleObject = [pscustomobject]@{212 PSTypeName = 'Test.EnumSample'213 SampleSimpleEnum = [System.Management.Automation.ActionPreference]::Ignore214 SampleBitwiseEnum = [System.Management.Automation.CommandTypes]'Alias,Function,Cmdlet'215 }216 217 $response4 = ConvertTo-Json -InputObject $sampleObject -Compress218 $response4 | Should -Be '{"SampleSimpleEnum":4,"SampleBitwiseEnum":11}'219 220 $response4 = ConvertTo-Json -InputObject $sampleObject -Compress -EnumsAsStrings221 $response4 | Should -Be '{"SampleSimpleEnum":"Ignore","SampleBitwiseEnum":"Alias, Function, Cmdlet"}'222 223 }224 225 }226 227 Context "JsonObject Tests" {228 229 It "AddMember on JsonObject" {230 231 # create a Version object232 $versionObject = New-Object System.Version 2, 3, 4, 14233 234 # add a NoteProperty member called Note with a text note235 $versionObject | Add-Member -MemberType NoteProperty -Name Note -Value "a version object"236 237 # add an AliasProperty called Rev as an alias to the Revision property238 $versionObject | Add-Member -MemberType AliasProperty -Name Rev -Value Revision239 240 # add a ScriptProperty called IsOld which returns whether the version is an older version241 $versionObject | Add-Member -MemberType ScriptProperty -Name IsOld -Value { ($this.Major -le 3) }242 $jstr = ConvertTo-Json $versionObject243 244 # convert the JSON string to a JSON object245 $json = ConvertFrom-Json $jstr246 247 # Check the basic properties248 $json.Major | Should -Be 2249 $json.Minor | Should -Be 3250 $json.Build | Should -Be 4251 $json.Revision | Should -Be 14252 $json.Note | Should -Match "a version object"253 254 # Check the AliasProperty255 $json.Rev | Should -Be $json.Revision256 257 # Check the ScriptProperty258 $json.IsOld | Should -BeTrue259 }260 261 It "ConvertFrom-Json with a key value pair" {262 263 $json = "{name:1}"264 $result = ConvertFrom-Json $json265 $result.name | Should -Be 1266 }267 268 It "ConvertFrom-Json with a simple array" {269 270 $json = "[1,2,3,4,5,6]"271 $result = ConvertFrom-Json $json272 $result.Count | Should -Be 6273 ,$result | Should -BeOfType System.Array274 }275 276 It "ConvertFrom-Json with a float value" {277 278 $json = '{"SampleFloat1":1.2345E67, "SampleFloat2":-7.6543E-12}'279 $result = ConvertFrom-Json $json280 281 $sampleFloat1 = Invoke-Expression 1.2345E67282 $result.SampleFloat1 | Should -Be $sampleFloat1283 284 $sampleFloat2 = Invoke-Expression -7.6543E-12285 $result.SampleFloat2 | Should -Be $sampleFloat2286 }287 288 It "ConvertFrom-Json hash table nested in array" {289 290 $json = "['one', 'two', {'First':1,'Second':2,'Third':['Five','Six', 'Seven']}, 'four']"291 $result = ConvertFrom-Json $json292 293 $result.Count | Should -Be 4294 $result[0] | Should -BeExactly "one"295 $result[1] | Should -BeExactly "two"296 $result[3] | Should -BeExactly "four"297 298 $hash = $result[2]299 $hash.First | Should -Be 1300 $hash.Second | Should -Be 2301 $hash.Third.Count | Should -Be 3302 $hash.Third[0] | Should -BeExactly "Five"303 $hash.Third[1] | Should -BeExactly "Six"304 $hash.Third[2] | Should -BeExactly "Seven"305 }306 307 It "ConvertFrom-Json array nested in hash table" {308 309 $json = '{"First":["one", "two", "three"], "Second":["four", "five"], "Third": {"blah": 4}}'310 $result = ConvertFrom-Json $json311 312 $result.First.Count | Should -Be 3313 $result.First[0] | Should -BeExactly "one"314 $result.First[1] | Should -BeExactly "two"315 $result.First[2] | Should -BeExactly "three"316 317 $result.Second.Count | Should -Be 2318 $result.Second[0] | Should -BeExactly "four"319 $result.Second[1] | Should -BeExactly "five"320 321 $result.Third.blah | Should -BeExactly "4"322 }323 324 It "ConvertFrom-Json case insensitive test" {325 326 $json = '{"sAMPleValUE":12345}'327 $result = ConvertFrom-Json $json328 329 $result.SampleValue | Should -Be 12345330 }331 332 It "ConvertFrom-Json sample values" {333 334 $json = '{"SampleInt":98765, "SampleString":"stringVal","SampleArray":[2,"two"], "SampleTrue":true, "SampleFalse":false,"SampleNull":null, "SampleFloat":9.8765E43}'335 $result = ConvertFrom-Json $json336 337 # Validate the result object338 ValidateSampleObject -result $result339 340 $json = '{"SampleInt":98765, "SampleString":"stringVal","SampleArray":[2,"two"], "SampleTrue":true, ' +341 '"SampleFalse":false,"SampleNull":null, "SampleFloat":9.8765E43, "SampleObject":'+342 '{"SampleInt":98765, "SampleString":"stringVal","SampleArray":[2,"two"], '+343 '"SampleTrue":true, "SampleFalse":false,"SampleNull":null, "SampleFloat":9.8765E43}}'344 345 # Validate the result object346 $result = ConvertFrom-Json $json347 ValidateSampleObject -result $result -hasEmbeddedSampleObject348 }349 350 It "ConvertFrom-Json correctly processes datetimes vs strings using newtonsoft conventions" {351 # Create all the various "standard" date formats from a source date 2008-09-22T14:01:54.9571247Z352 # Then we deserialize them and verify which ones newtonsoft decides to make into datetimes and which ones newtonsoft keeps as strings353 # For the ones that are datetimes, make sure they retain all of the various formats correctly354 # for the ones that are strings just verify that they are actually strings.355 356 $json = @"357{358 "date-s-should-parse-as-datetime": "2008-09-22T14:01:54",359 "date-upperO-should-parse-as-datetime": "2008-09-22T14:01:54.9571247Z",360 361 "date-o-should-parse-as-string": "2019-12-17T06:14:06 +06:00",362 "date-upperD-should-parse-as-string": "Monday, September 22, 2008",363 "date-f-should-parse-as-string": "Monday, September 22, 2008 2:01 PM",364 "date-upperF-should-parse-as-string": "Monday, September 22, 2008 2:01:54 PM",365 "date-g-should-parse-as-string": "9/22/2008 2:01 PM",366 "date-upperG-should-parse-as-string": "9/22/2008 2:01:54 PM",367 "date-m-should-parse-as-string": "September 22",368 "date-upperM-should-parse-as-string": "September 22",369 "date-upperR-should-parse-as-string": "Mon, 22 Sep 2008 14:01:54 GMT",370 "date-t-should-parse-as-string": "2:01 PM",371 "date-upperT-should-parse-as-string": "2:01:54 PM",372 "date-u-should-parse-as-string": "2008-09-22 14:01:54Z",373 "date-upperU-should-parse-as-string": "Monday, September 22, 2008 2:01:54 PM",374 "date-upperY-should-parse-as-string": "September 2008",375 "date-y-should-parse-as-string": "September 2008"376}377"@378 foreach ($asHashtableOption in @($True, $False)) {379 $result = ConvertFrom-Json $json -AsHashtable:$asHashtableOption380 381 # If the timezone is not specified in the input string newtonsoft assumes the system timezone.382 # this means that some tests will fail locally but work on the build farm and vice-versa.383 # this is used to force the datetime into UTC without changing the value so 4PM Central becomes 4PM UTC instead of becoming 10PM UTC384 $result."date-s-should-parse-as-datetime" = [datetime]::SpecifyKind($result."date-s-should-parse-as-datetime", [System.DateTimeKind]::Utc)385 $result."date-s-should-parse-as-datetime".ToString("d") | Should -Be "9/22/2008"386 $result."date-s-should-parse-as-datetime".ToString("D") | Should -Be "Monday, September 22, 2008"387 $result."date-s-should-parse-as-datetime".ToString("f") | Should -Be "Monday, September 22, 2008 2:01 PM"388 $result."date-s-should-parse-as-datetime".ToString("F") | Should -Be "Monday, September 22, 2008 2:01:54 PM"389 $result."date-s-should-parse-as-datetime".ToString("g") | Should -Be "9/22/2008 2:01 PM"390 $result."date-s-should-parse-as-datetime".ToString("G") | Should -Be "9/22/2008 2:01:54 PM"391 $result."date-s-should-parse-as-datetime".ToString("m") | Should -Be "September 22"392 $result."date-s-should-parse-as-datetime".ToString("M") | Should -Be "September 22"393 $result."date-s-should-parse-as-datetime".ToString("o") | Should -Be "2008-09-22T14:01:54.0000000Z"394 $result."date-s-should-parse-as-datetime".ToString("O") | Should -Be "2008-09-22T14:01:54.0000000Z"395 $result."date-s-should-parse-as-datetime".ToString("R") | Should -Be "Mon, 22 Sep 2008 14:01:54 GMT"396 $result."date-s-should-parse-as-datetime".ToString("s") | Should -Be "2008-09-22T14:01:54"397 $result."date-s-should-parse-as-datetime".ToString("t") | Should -Be "2:01 PM"398 $result."date-s-should-parse-as-datetime".ToString("T") | Should -Be "2:01:54 PM"399 $result."date-s-should-parse-as-datetime".ToString("u") | Should -Be "2008-09-22 14:01:54Z"400 $result."date-s-should-parse-as-datetime".ToString("U") | Should -Be "Monday, September 22, 2008 2:01:54 PM"401 $result."date-s-should-parse-as-datetime".ToString("Y") | Should -Be "September 2008"402 $result."date-s-should-parse-as-datetime".ToString("y") | Should -Be "September 2008"403 $result."date-s-should-parse-as-datetime" | Should -BeOfType [DateTime]404 405 $result."date-upperO-should-parse-as-datetime" = [datetime]::SpecifyKind($result."date-upperO-should-parse-as-datetime", [System.DateTimeKind]::Utc)406 $result."date-upperO-should-parse-as-datetime".ToString("d") | Should -Be "9/22/2008"407 $result."date-upperO-should-parse-as-datetime".ToString("D") | Should -Be "Monday, September 22, 2008"408 $result."date-upperO-should-parse-as-datetime".ToString("f") | Should -Be "Monday, September 22, 2008 2:01 PM"409 $result."date-upperO-should-parse-as-datetime".ToString("F") | Should -Be "Monday, September 22, 2008 2:01:54 PM"410 $result."date-upperO-should-parse-as-datetime".ToString("g") | Should -Be "9/22/2008 2:01 PM"411 $result."date-upperO-should-parse-as-datetime".ToString("G") | Should -Be "9/22/2008 2:01:54 PM"412 $result."date-upperO-should-parse-as-datetime".ToString("m") | Should -Be "September 22"413 $result."date-upperO-should-parse-as-datetime".ToString("M") | Should -Be "September 22"414 $result."date-upperO-should-parse-as-datetime".ToString("o") | Should -Be "2008-09-22T14:01:54.9571247Z"415 $result."date-upperO-should-parse-as-datetime".ToString("O") | Should -Be "2008-09-22T14:01:54.9571247Z"416 $result."date-upperO-should-parse-as-datetime".ToString("R") | Should -Be "Mon, 22 Sep 2008 14:01:54 GMT"417 $result."date-upperO-should-parse-as-datetime".ToString("s") | Should -Be "2008-09-22T14:01:54"418 $result."date-upperO-should-parse-as-datetime".ToString("t") | Should -Be "2:01 PM"419 $result."date-upperO-should-parse-as-datetime".ToString("T") | Should -Be "2:01:54 PM"420 $result."date-upperO-should-parse-as-datetime".ToString("u") | Should -Be "2008-09-22 14:01:54Z"421 $result."date-upperO-should-parse-as-datetime".ToString("U") | Should -Be "Monday, September 22, 2008 2:01:54 PM"422 $result."date-upperO-should-parse-as-datetime".ToString("Y") | Should -Be "September 2008"423 $result."date-upperO-should-parse-as-datetime".ToString("y") | Should -Be "September 2008"424 $result."date-upperO-should-parse-as-datetime" | Should -BeOfType [DateTime]425 426 $result."date-o-should-parse-as-string" | Should -Be "2019-12-17T06:14:06 +06:00"427 $result."date-o-should-parse-as-string" | Should -BeOfType [String]428 $result."date-f-should-parse-as-string" | Should -Be "Monday, September 22, 2008 2:01 PM"429 $result."date-f-should-parse-as-string" | Should -BeOfType [String]430 $result."date-g-should-parse-as-string" | Should -Be "9/22/2008 2:01 PM"431 $result."date-g-should-parse-as-string" | Should -BeOfType [String]432 $result."date-m-should-parse-as-string" | Should -Be "September 22"433 $result."date-m-should-parse-as-string" | Should -BeOfType [String]434 $result."date-upperM-should-parse-as-string" | Should -Be "September 22"435 $result."date-upperM-should-parse-as-string" | Should -BeOfType [String]436 $result."date-t-should-parse-as-string" | Should -Be "2:01 PM"437 $result."date-t-should-parse-as-string" | Should -BeOfType [String]438 $result."date-u-should-parse-as-string" | Should -Be "2008-09-22 14:01:54Z"439 $result."date-u-should-parse-as-string" | Should -BeOfType [String]440 $result."date-upperD-should-parse-as-string" | Should -Be "Monday, September 22, 2008"441 $result."date-upperD-should-parse-as-string" | Should -BeOfType [String]442 $result."date-upperF-should-parse-as-string" | Should -Be "Monday, September 22, 2008 2:01:54 PM"443 $result."date-upperF-should-parse-as-string" | Should -BeOfType [String]444 $result."date-upperG-should-parse-as-string" | Should -Be "9/22/2008 2:01:54 PM"445 $result."date-upperG-should-parse-as-string" | Should -BeOfType [String]446 $result."date-upperR-should-parse-as-string" | Should -Be "Mon, 22 Sep 2008 14:01:54 GMT"447 $result."date-upperR-should-parse-as-string" | Should -BeOfType [String]448 $result."date-upperT-should-parse-as-string" | Should -Be "2:01:54 PM"449 $result."date-upperT-should-parse-as-string" | Should -BeOfType [String]450 $result."date-upperU-should-parse-as-string" | Should -Be "Monday, September 22, 2008 2:01:54 PM"451 $result."date-upperU-should-parse-as-string" | Should -BeOfType [String]452 $result."date-upperY-should-parse-as-string" | Should -Be "September 2008"453 $result."date-upperY-should-parse-as-string" | Should -BeOfType [String]454 $result."date-y-should-parse-as-string" | Should -Be "September 2008"455 $result."date-y-should-parse-as-string" | Should -BeOfType [String]456 }457 }458 459 It "ConvertFrom-Json properly parses complex objects" {460 $json = @"461{462"_id": "60dd3ea9253016932039a0a2",463"index": 0,464"guid": "429b96a7-24e3-47de-a93b-f44a346c5ac9",465"isActive": false,466"balance": "$2,039.72",467"picture": "http://placehold.it/32x32",468"age": 35,469"eyeColor": "green",470"name": "Rhodes Roberson",471"gender": "male",472"company": "INSECTUS",473"email": "rhodesroberson@insectus.com",474"phone": "+1 (883) 561-3999",475"address": "931 Kings Place, Hartsville/Hartley, Federated States Of Micronesia, 9344",476"about": "Ipsum pariatur nisi eiusmod aliquip in cupidatat. Deserunt non sit anim consectetur consectetur incididunt elit qui id proident nostrud. Consectetur pariatur et adipisicing aliquip fugiat fugiat Lorem reprehenderit laboris magna. Duis veniam irure amet ex minim eiusmod et laborum non elit. Dolor enim Lorem occaecat nisi consectetur mollit laborum anim velit et. Irure aliquip eiusmod anim proident ex ea duis deserunt aute amet adipisicing nisi nostrud. Minim ipsum fugiat consequat mollit fugiat tempor fugiat.",477"registered": "2019-12-17T06:14:06 +06:00",478"latitude": 51.890798,479"longitude": -47.522764,480"tags": [481 "laboris",482 "voluptate",483 "amet",484 "ad",485 "velit",486 "ipsum",487 "do"488],489"friends": [490 {491 "id": 0,492 "name": "Renee Holden"493 },494 {495 "id": 1,496 "name": "Bennett Dixon"497 },498 {499 "id": 2,500 "name": "Emilia Holder"501 }502],503"greeting": "Hello, Rhodes Roberson! You have 9 unread messages.",504"favoriteFruit": "banana"505}506"@507 $result = ConvertFrom-Json $Json508 $result."about"| Should -BeExactly "Ipsum pariatur nisi eiusmod aliquip in cupidatat. Deserunt non sit anim consectetur consectetur incididunt elit qui id proident nostrud. Consectetur pariatur et adipisicing aliquip fugiat fugiat Lorem reprehenderit laboris magna. Duis veniam irure amet ex minim eiusmod et laborum non elit. Dolor enim Lorem occaecat nisi consectetur mollit laborum anim velit et. Irure aliquip eiusmod anim proident ex ea duis deserunt aute amet adipisicing nisi nostrud. Minim ipsum fugiat consequat mollit fugiat tempor fugiat."509 $result."about" | Should -BeOfType [String]510 $result."address"| Should -BeExactly "931 Kings Place, Hartsville/Hartley, Federated States Of Micronesia, 9344"511 $result."address" | Should -BeOfType [String]512 $result."age" | Should -BeOfType [Int64]513 $result."balance"| Should -BeExactly ",039.72"514 $result."balance" | Should -BeOfType [String]515 $result."company"| Should -BeExactly "INSECTUS"516 $result."company" | Should -BeOfType [String]517 $result."email"| Should -BeExactly "rhodesroberson@insectus.com"518 $result."email" | Should -BeOfType [String]519 $result."eyeColor"| Should -BeExactly "green"520 $result."eyeColor" | Should -BeOfType [String]521 $result."favoriteFruit"| Should -BeExactly "banana"522 $result."favoriteFruit" | Should -BeOfType [String]523 $result."gender"| Should -BeExactly "male"524 $result."gender" | Should -BeOfType [String]525 $result."greeting"| Should -BeExactly "Hello, Rhodes Roberson! You have 9 unread messages."526 $result."greeting" | Should -BeOfType [String]527 $result."guid"| Should -BeExactly "429b96a7-24e3-47de-a93b-f44a346c5ac9"528 $result."guid" | Should -BeOfType [String]529 $result."index" | Should -BeOfType [Int64]530 $result."isActive"| Should -Be False531 $result."isActive" | Should -BeOfType [Boolean]532 $result."latitude"| Should -Be 51.890798533 $result."latitude" | Should -BeOfType [Double]534 $result."longitude"| Should -Be -47.522764535 $result."longitude" | Should -BeOfType [Double]536 $result."name"| Should -BeExactly "Rhodes Roberson"537 $result."name" | Should -BeOfType [String]538 $result."phone"| Should -BeExactly "+1 (883) 561-3999"539 $result."phone" | Should -BeOfType [String]540 $result."picture"| Should -BeExactly "http://placehold.it/32x32"541 $result."picture" | Should -BeOfType [String]542 $result."registered"| Should -BeExactly "2019-12-17T06:14:06 +06:00"543 $result."registered" | Should -BeOfType [String]544 $result."_id"| Should -BeExactly "60dd3ea9253016932039a0a2"545 $result."_id" | Should -BeOfType [String]546 547 $result.Tags | Should -BeOfType [string]548 549 $result.Tags.count | Should -Be 7550 $result.Tags[0] | Should -BeExactly "laboris"551 $result.Tags | Should -Be @("laboris", "voluptate", "amet", "ad", "velit", "ipsum", "do")552 553 $result.Friends | Should -BeOfType [pscustomobject]554 $result.Friends[0].id | Should -Be 0555 $result.Friends[0].name | Should -BeExactly "Renee Holden"556 $result.Friends[1].id | Should -Be 1557 $result.Friends[1].name | Should -BeExactly "Bennett Dixon"558 $result.Friends[2].id | Should -Be 2559 $result.Friends[2].name | Should -BeExactly "Emilia Holder"560 }561 562 It "ConvertFrom-Json chooses the appropriate number type" {563 ConvertFrom-Json -InputObject "5" | should -Be 5564 ConvertFrom-Json -InputObject 5 | should -Be 5565 ConvertFrom-Json -InputObject "5" | should -BeOfType [int64]566 ConvertFrom-Json -InputObject 5 | should -BeOfType [int64]567 ConvertFrom-Json -InputObject 5000000000000 | should -Be 5000000000000568 ConvertFrom-Json -InputObject "5000000000000" | should -Be "5000000000000"569 ConvertFrom-Json -InputObject "5000000000000" | should -BeOfType [int64]570 ConvertFrom-Json -InputObject 5000000000000 | should -BeOfType [int64]571 ConvertFrom-Json -InputObject "5.0" | should -Be 5.0572 ConvertFrom-Json -InputObject 5.0 | should -Be 5.0573 ConvertFrom-Json -InputObject "5.0" | should -BeOfType [double]574 ConvertFrom-Json -InputObject 5.0 | should -BeOfType [double]575 576 # The decimal is lost but only when this is quoted577 ConvertFrom-Json -InputObject "500000000000.0000000000000001" | should -Be "500000000000"578 579 # Counter intuitively all four of these tests pass because precision is lost on both sides of the test, likely due to powershell number handling580 ConvertFrom-Json -InputObject 500000000000.0000000000000001 | should -Be 500000000000581 ConvertFrom-Json -InputObject 500000000000.0000000000000001 | should -Be 500000000000.0000000000000001582 ConvertFrom-Json -InputObject 500000000000 | should -Be 500000000000.0000000000000001583 ConvertFrom-Json -InputObject 500000000000 | should -Be 500000000000584 585 ConvertFrom-Json -InputObject "500000000000.0000000000000001" | should -BeOfType [double]586 ConvertFrom-Json -InputObject 500000000000.0000000000000001 | should -BeOfType [double]587 588 # these tests also pass because precision is lost during conversion/powershell handling589 ConvertFrom-Json -InputObject "50000000000000000000000000000000000.0000000000000001" | should -Be "5E+34"590 ConvertFrom-Json -InputObject 50000000000000000000000000000000000.0000000000000001 | should -Be "5E+34"591 592 ConvertFrom-Json -InputObject "50000000000000000000000000000000000.0000000000000001" | should -BeOfType [double]593 ConvertFrom-Json -InputObject 50000000000000000000000000000000000.0000000000000001 | should -BeOfType [double]594 595 596 ConvertFrom-Json -InputObject "50000000000000000000000000000000000" | should -Be 50000000000000000000000000000000000597 ConvertFrom-Json -InputObject 50000000000000000000000000000000000 | should -Be 50000000000000000000000000000000000598 ConvertFrom-Json -InputObject "50000000000000000000000000000000000" | should -BeOfType [BigInt]599 ConvertFrom-Json -InputObject 50000000000000000000000000000000000 | should -BeOfType [BigInt]600 }601 602 It "ConvertFrom-Json with special characters" {603 604 $json = '{"SampleValue":"\"\\\b\f\n\r\t\u4321\uD7FF"}'605 $result = ConvertFrom-Json $json606 $result.SampleValue[0] | Should -Be '"'607 $result.SampleValue[1] | Should -Be '\'608 $result.SampleValue[2] | Should -Be 0x8609 $result.SampleValue[3] | Should -Be 0xC610 $result.SampleValue[4] | Should -Be 0xA611 $result.SampleValue[5] | Should -Be 0xD612 $result.SampleValue[6] | Should -Be 0x9613 $result.SampleValue[7] | Should -Be 0x4321614 $result.SampleValue[8] | Should -Be 0xD7FF615 }616 }617}618 619# This Describe is for new Json tests620#621Describe "Validate Json serialization" -Tags "CI" {622 623 Context "Validate Json serialization ascii values" {624 625 $testCases = @(626 @{627 TestInput = 0628 ToJson = if ( $IsCoreCLR ) { '"\u0000"' } else { 'null' }629 FromJson = ''630 }631 @{632 TestInput = 1633 ToJson = '"\u0001"'634 FromJson = ''635 }636 @{637 TestInput = 2638 ToJson = '"\u0002"'639 FromJson = ''640 }641 @{642 TestInput = 3643 ToJson = '"\u0003"'644 FromJson = ''645 }646 @{647 TestInput = 4648 ToJson = '"\u0004"'649 FromJson = ''650 }651 @{652 TestInput = 5653 ToJson = '"\u0005"'654 FromJson = ''655 }656 @{657 TestInput = 6658 ToJson = '"\u0006"'659 FromJson = ''660 }661 @{662 TestInput = 7663 ToJson = '"\u0007"'664 FromJson = ''665 }666 @{667 TestInput = 8668 ToJson = '"\b"'669 FromJson = ''670 }671 @{672 TestInput = 9673 ToJson = '"\t"'674 FromJson = ' '675 }676 @{677 TestInput = 10678 ToJson = '"\n"'679 FromJson = "`n"680 }681 @{682 TestInput = 11683 ToJson = '"\u000b"'684 FromJson = ''685 }686 @{687 TestInput = 12688 ToJson = '"\f"'689 FromJson = ''690 }691 @{692 TestInput = 13693 ToJson = '"\r"'694 FromJson = ''695 }696 @{697 TestInput = 14698 ToJson = '"\u000e"'699 FromJson = ''700 }701 @{702 TestInput = 15703 ToJson = '"\u000f"'704 FromJson = ''705 }706 @{707 TestInput = 16708 ToJson = '"\u0010"'709 FromJson = ''710 }711 @{712 TestInput = 17713 ToJson = '"\u0011"'714 FromJson = ''715 }716 @{717 TestInput = 18718 ToJson = '"\u0012"'719 FromJson = ''720 }721 @{722 TestInput = 19723 ToJson = '"\u0013"'724 FromJson = ''725 }726 @{727 TestInput = 20728 ToJson = '"\u0014"'729 FromJson = ''730 }731 @{732 TestInput = 21733 ToJson = '"\u0015"'734 FromJson = ''735 }736 @{737 TestInput = 22738 ToJson = '"\u0016"'739 FromJson = ''740 }741 @{742 TestInput = 23743 ToJson = '"\u0017"'744 FromJson = ''745 }746 @{747 TestInput = 24748 ToJson = '"\u0018"'749 FromJson = ''750 }751 @{752 TestInput = 25753 ToJson = '"\u0019"'754 FromJson = ''755 }756 @{757 TestInput = 26758 ToJson = '"\u001a"'759 FromJson = ''760 }761 @{762 TestInput = 27763 ToJson = '"\u001b"'764 FromJson = ''765 }766 @{767 TestInput = 28768 ToJson = '"\u001c"'769 FromJson = ''770 }771 @{772 TestInput = 29773 ToJson = '"\u001d"'774 FromJson = ''775 }776 @{777 TestInput = 30778 ToJson = '"\u001e"'779 FromJson = ''780 }781 @{782 TestInput = 31783 ToJson = '"\u001f"'784 FromJson = ''785 }786 @{787 TestInput = 32788 ToJson = '" "'789 FromJson = ' '790 }791 @{792 TestInput = 33793 ToJson = '"!"'794 FromJson = '!'795 }796 @{797 TestInput = 34798 ToJson = '"\""'799 FromJson = '"'800 }801 @{802 TestInput = 35803 ToJson = '"#"'804 FromJson = '#'805 }806 @{807 TestInput = 36808 ToJson = '"$"'809 FromJson = '$'810 }811 @{812 TestInput = 37813 ToJson = '"%"'814 FromJson = '%'815 }816 @{817 TestInput = 38818 ToJson = if ( $IsCoreCLR ) { '"&"' } else { '"\u0026"' }819 FromJson = '&'820 }821 @{822 TestInput = 39823 ToJson = if ( $IsCoreCLR ) { '"''"' } else { '"\u0027"' }824 FromJson = "'"825 }826 @{827 TestInput = 40828 ToJson = '"("'829 FromJson = '('830 }831 @{832 TestInput = 41833 ToJson = '")"'834 FromJson = ')'835 }836 @{837 TestInput = 42838 ToJson = '"*"'839 FromJson = '*'840 }841 @{842 TestInput = 43843 ToJson = '"+"'844 FromJson = '+'845 }846 @{847 TestInput = 44848 ToJson = '","'849 FromJson = ','850 }851 @{852 TestInput = 45853 ToJson = '"-"'854 FromJson = '-'855 }856 @{857 TestInput = 46858 ToJson = '"."'859 FromJson = '.'860 }861 @{862 TestInput = 47863 ToJson = '"/"'864 FromJson = '/'865 }866 @{867 TestInput = 48868 ToJson = '"0"'869 FromJson = '0'870 }871 @{872 TestInput = 49873 ToJson = '"1"'874 FromJson = '1'875 }876 @{877 TestInput = 50878 ToJson = '"2"'879 FromJson = '2'880 }881 @{882 TestInput = 51883 ToJson = '"3"'884 FromJson = '3'885 }886 @{887 TestInput = 52888 ToJson = '"4"'889 FromJson = '4'890 }891 @{892 TestInput = 53893 ToJson = '"5"'894 FromJson = '5'895 }896 @{897 TestInput = 54898 ToJson = '"6"'899 FromJson = '6'900 }901 @{902 TestInput = 55903 ToJson = '"7"'904 FromJson = '7'905 }906 @{907 TestInput = 56908 ToJson = '"8"'909 FromJson = '8'910 }911 @{912 TestInput = 57913 ToJson = '"9"'914 FromJson = '9'915 }916 @{917 TestInput = 58918 ToJson = '":"'919 FromJson = ':'920 }921 @{922 TestInput = 59923 ToJson = '";"'924 FromJson = ';'925 }926 @{927 TestInput = 60928 ToJson = if ( $IsCoreCLR ) { '"<"' } else { '"\u003c"' }929 FromJson = '<'930 }931 @{932 TestInput = 61933 ToJson = '"="'934 FromJson = '='935 }936 @{937 TestInput = 62938 ToJson = if ( $IsCoreCLR ) { '">"' } else { '"\u003e"' }939 FromJson = '>'940 }941 @{942 TestInput = 63943 ToJson = '"?"'944 FromJson = '?'945 }946 @{947 TestInput = 64948 ToJson = '"@"'949 FromJson = '@'950 }951 @{952 TestInput = 65953 ToJson = '"A"'954 FromJson = 'A'955 }956 @{957 TestInput = 66958 ToJson = '"B"'959 FromJson = 'B'960 }961 @{962 TestInput = 67963 ToJson = '"C"'964 FromJson = 'C'965 }966 @{967 TestInput = 68968 ToJson = '"D"'969 FromJson = 'D'970 }971 @{972 TestInput = 69973 ToJson = '"E"'974 FromJson = 'E'975 }976 @{977 TestInput = 70978 ToJson = '"F"'979 FromJson = 'F'980 }981 @{982 TestInput = 71983 ToJson = '"G"'984 FromJson = 'G'985 }986 @{987 TestInput = 72988 ToJson = '"H"'989 FromJson = 'H'990 }991 @{992 TestInput = 73993 ToJson = '"I"'994 FromJson = 'I'995 }996 @{997 TestInput = 74998 ToJson = '"J"'999 FromJson = 'J'1000 }1001 @{1002 TestInput = 751003 ToJson = '"K"'1004 FromJson = 'K'1005 }1006 @{1007 TestInput = 761008 ToJson = '"L"'1009 FromJson = 'L'1010 }1011 @{1012 TestInput = 771013 ToJson = '"M"'1014 FromJson = 'M'1015 }1016 @{1017 TestInput = 781018 ToJson = '"N"'1019 FromJson = 'N'1020 }1021 @{1022 TestInput = 791023 ToJson = '"O"'1024 FromJson = 'O'1025 }1026 @{1027 TestInput = 801028 ToJson = '"P"'1029 FromJson = 'P'1030 }1031 @{1032 TestInput = 811033 ToJson = '"Q"'1034 FromJson = 'Q'1035 }1036 @{1037 TestInput = 821038 ToJson = '"R"'1039 FromJson = 'R'1040 }1041 @{1042 TestInput = 831043 ToJson = '"S"'1044 FromJson = 'S'1045 }1046 @{1047 TestInput = 841048 ToJson = '"T"'1049 FromJson = 'T'1050 }1051 @{1052 TestInput = 851053 ToJson = '"U"'1054 FromJson = 'U'1055 }1056 @{1057 TestInput = 861058 ToJson = '"V"'1059 FromJson = 'V'1060 }1061 @{1062 TestInput = 871063 ToJson = '"W"'1064 FromJson = 'W'1065 }1066 @{1067 TestInput = 881068 ToJson = '"X"'1069 FromJson = 'X'1070 }1071 @{1072 TestInput = 891073 ToJson = '"Y"'1074 FromJson = 'Y'1075 }1076 @{1077 TestInput = 901078 ToJson = '"Z"'1079 FromJson = 'Z'1080 }1081 @{1082 TestInput = 911083 ToJson = '"["'1084 FromJson = '['1085 }1086 @{1087 TestInput = 921088 ToJson = '"\\"'1089 FromJson = '\'1090 }1091 @{1092 TestInput = 931093 ToJson = '"]"'1094 FromJson = ']'1095 }1096 @{1097 TestInput = 941098 ToJson = '"^"'1099 FromJson = '^'1100 }1101 @{1102 TestInput = 951103 ToJson = '"_"'1104 FromJson = '_'1105 }1106 @{1107 TestInput = 961108 ToJson = '"`"'1109 FromJson = '`'1110 }1111 @{1112 TestInput = 971113 ToJson = '"a"'1114 FromJson = 'a'1115 }1116 @{1117 TestInput = 981118 ToJson = '"b"'1119 FromJson = 'b'1120 }1121 @{1122 TestInput = 991123 ToJson = '"c"'1124 FromJson = 'c'1125 }1126 @{1127 TestInput = 1001128 ToJson = '"d"'1129 FromJson = 'd'1130 }1131 @{1132 TestInput = 1011133 ToJson = '"e"'1134 FromJson = 'e'1135 }1136 @{1137 TestInput = 1021138 ToJson = '"f"'1139 FromJson = 'f'1140 }1141 @{1142 TestInput = 1031143 ToJson = '"g"'1144 FromJson = 'g'1145 }1146 @{1147 TestInput = 1041148 ToJson = '"h"'1149 FromJson = 'h'1150 }1151 @{1152 TestInput = 1051153 ToJson = '"i"'1154 FromJson = 'i'1155 }1156 @{1157 TestInput = 1061158 ToJson = '"j"'1159 FromJson = 'j'1160 }1161 @{1162 TestInput = 1071163 ToJson = '"k"'1164 FromJson = 'k'1165 }1166 @{1167 TestInput = 1081168 ToJson = '"l"'1169 FromJson = 'l'1170 }1171 @{1172 TestInput = 1091173 ToJson = '"m"'1174 FromJson = 'm'1175 }1176 @{1177 TestInput = 1101178 ToJson = '"n"'1179 FromJson = 'n'1180 }1181 @{1182 TestInput = 1111183 ToJson = '"o"'1184 FromJson = 'o'1185 }1186 @{1187 TestInput = 1121188 ToJson = '"p"'1189 FromJson = 'p'1190 }1191 @{1192 TestInput = 1131193 ToJson = '"q"'1194 FromJson = 'q'1195 }1196 @{1197 TestInput = 1141198 ToJson = '"r"'1199 FromJson = 'r'1200 }