Team Ai
Datasetpublic

MegaBites-AI/Windows-powershell

sourceHugging Facemitupdated 6mo agoView on Hugging Face
0likes372downloads
JsonObject.Tests.ps1120 linesDownload Raw Back to Microsoft.PowerShell.Utility
1# Copyright (c) Microsoft Corporation.2# Licensed under the MIT License.3Describe 'Unit tests for JsonObject' -tags "CI" {4 5    BeforeAll {6        $jsonWithEmptyKey = '{"": "Value"}'7        $jsonContainingKeysWithDifferentCasing = '{"key1": "Value1", "Key1": "Value2"}'8 9        $testCasesJsonDepthWithAndWithoutReturnHashTable = @(10            @{ Depth = 2;    ReturnHashTable = $true  }11            @{ Depth = 2;    ReturnHashTable = $false }12            @{ Depth = 200;  ReturnHashTable = $true  }13            @{ Depth = 200;  ReturnHashTable = $false }14            @{ Depth = 2000; ReturnHashTable = $true  }15            @{ Depth = 2000; ReturnHashTable = $false }16        )17 18        function New-NestedJson {19            Param(20                [ValidateRange(1, 2048)]21                [int]22                $Depth23            )24 25            $nestedJson = "true"26 27            $Depth..1 | ForEach-Object {28                $nestedJson = '{"' + $_ + '":' + $nestedJson + '}'29            }30 31            return $nestedJson32        }33    }34 35    It 'No error for valid string ''<name>'' with -ReturnHashTable:<ReturnHashTable>' -TestCases @(36        @{ name = "null";   str = $null;   ReturnHashTable = $true  }37        @{ name = "empty";  str = "";      ReturnHashTable = $true  }38        @{ name = "spaces"; str = "  ";    ReturnHashTable = $true  }39        @{ name = "object"; str = "{a:1}"; ReturnHashTable = $true  }40        @{ name = "null";   str = $null;   ReturnHashTable = $false }41        @{ name = "empty";  str = "";      ReturnHashTable = $false }42        @{ name = "spaces"; str = "  ";    ReturnHashTable = $false }43        @{ name = "object"; str = "{a:1}"; ReturnHashTable = $false }44    ) {45        param ($str, $ReturnHashTable)46        $errRecord = $null47        [Microsoft.PowerShell.Commands.JsonObject]::ConvertFromJson($str, $ReturnHashTable, [ref]$errRecord)48        $errRecord | Should -BeNullOrEmpty49    }50 51    It 'Throw ArgumentException for invalid string ''<name>'' with -ReturnHashTable:<ReturnHashTable>' -TestCases @(52        @{ name = "plain text"; str = "plaintext"; ReturnHashTable = $true  }53        @{ name = "part";       str = '{"a" :';    ReturnHashTable = $true  }54        @{ name = "plain text"; str = "plaintext"; ReturnHashTable = $false }55        @{ name = "part";       str = '{"a" :';    ReturnHashTable = $false }56    )  {57        param ($str, $ReturnHashTable)58        $errRecord = $null59        { [Microsoft.PowerShell.Commands.JsonObject]::ConvertFromJson($str, $ReturnHashTable, [ref]$errRecord) } | Should -Throw -ErrorId "ArgumentException"60    }61 62    It 'Can Convert json with depth less than -Depth:<Depth> with -ReturnHashTable:<ReturnHashTable>' -TestCases $testCasesJsonDepthWithAndWithoutReturnHashTable {63        param ($Depth, $ReturnHashTable)64        $errRecord = $null65        $json = New-NestedJson -Depth ($Depth - 1)66        [Microsoft.PowerShell.Commands.JsonObject]::ConvertFromJson($json, $ReturnHashTable, $Depth, [ref]$errRecord)67        $errRecord | Should -BeNullOrEmpty68    }69 70    It 'Can Convert json with depth equal to -Depth:<Depth> with -ReturnHashTable:<ReturnHashTable>' -TestCases $testCasesJsonDepthWithAndWithoutReturnHashTable {71        param ($Depth, $ReturnHashTable)72        $errRecord = $null73        $json = New-NestedJson -Depth $Depth74        [Microsoft.PowerShell.Commands.JsonObject]::ConvertFromJson($json, $ReturnHashTable, $Depth, [ref]$errRecord)75        $errRecord | Should -BeNullOrEmpty76    }77 78    It 'Throws ArgumentException for json with greater depth than -Depth:<Depth> with -ReturnHashTable:<ReturnHashTable>' -TestCases $testCasesJsonDepthWithAndWithoutReturnHashTable {79        param ($Depth, $ReturnHashTable)80        $errRecord = $null81        $json = New-NestedJson -Depth ($Depth + 1)82        { [Microsoft.PowerShell.Commands.JsonObject]::ConvertFromJson($json, $ReturnHashTable, $Depth, [ref]$errRecord) } |83            Should -Throw -ErrorId "ArgumentException"84    }85 86    Context 'Empty key name' {87        It 'Throw InvalidOperationException when json contains empty key name' {88            $errorRecord = $null89            [Microsoft.PowerShell.Commands.JsonObject]::ConvertFromJson($jsonWithEmptyKey, [ref]$errorRecord)90            $errorRecord.FullyQualifiedErrorId | Should -BeExactly 'EmptyKeyInJsonString'91        }92 93        It 'Not throw when json contains empty key name when ReturnHashTable is true' {94            $errorRecord = $null95            $result = [Microsoft.PowerShell.Commands.JsonObject]::ConvertFromJson($jsonWithEmptyKey, $true, [ref]$errorRecord)96            $result | Should -Not -BeNullOrEmpty97            $result.Count | Should -Be 198            $result.'' | Should -Be 'Value'99        }100    }101 102    Context 'Keys with different casing ' {103 104        It 'Throw InvalidOperationException when json contains key with different casing' {105            $errorRecord = $null106            [Microsoft.PowerShell.Commands.JsonObject]::ConvertFromJson($jsonContainingKeysWithDifferentCasing, [ref]$errorRecord)107            $errorRecord.FullyQualifiedErrorId | Should -BeExactly 'KeysWithDifferentCasingInJsonString'108        }109 110        It 'Not throw when json contains key (same casing) when ReturnHashTable is true' {111            $errorRecord = $null112            $result = [Microsoft.PowerShell.Commands.JsonObject]::ConvertFromJson($jsonContainingKeysWithDifferentCasing, $true, [ref]$errorRecord)113            $result | Should -Not -BeNullOrEmpty114            $result.Count | Should -Be 2115            $result.key1  | Should -Be 'Value1'116            $result.Key1  | Should -Be 'Value2'117        }118    }119}120