2020-03-24 11:08:37 -07:00
# Copyright (c) Microsoft Corporation.
2018-02-13 09:23:53 -08:00
# Licensed under the MIT License.
Describe 'Unit tests for JsonObject' -tags "CI" {
2016-12-15 11:06:26 +03:00
2017-10-25 16:57:05 +01:00
BeforeAll {
$jsonWithEmptyKey = '{"": "Value"}'
$jsonContainingKeysWithDifferentCasing = '{"key1": "Value1", "Key1": "Value2"}'
2019-02-20 00:37:37 -05:00
$testCasesJsonDepthWithAndWithoutReturnHashTable = @ (
@ { Depth = 2 ; ReturnHashTable = $true }
@ { Depth = 2 ; ReturnHashTable = $false }
@ { Depth = 200 ; ReturnHashTable = $true }
@ { Depth = 200 ; ReturnHashTable = $false }
@ { Depth = 2000 ; ReturnHashTable = $true }
@ { Depth = 2000 ; ReturnHashTable = $false }
)
function New-NestedJson {
Param (
[ ValidateRange ( 1 , 2048 )]
[ int ]
$Depth
)
$nestedJson = "true"
$Depth . .1 | ForEach-Object {
$nestedJson = '{"' + $_ + '":' + $nestedJson + '}'
}
return $nestedJson
}
2016-12-15 11:06:26 +03:00
}
2019-02-20 00:37:37 -05:00
It 'No error for valid string ''<name>'' with -ReturnHashTable:<ReturnHashTable>' -TestCases @ (
2017-10-25 16:57:05 +01:00
@ { name = "null" ; str = $null ; ReturnHashTable = $true }
@ { name = "empty" ; str = "" ; ReturnHashTable = $true }
@ { name = "spaces" ; str = " " ; ReturnHashTable = $true }
@ { name = "object" ; str = "{a:1}" ; ReturnHashTable = $true }
@ { name = "null" ; str = $null ; ReturnHashTable = $false }
@ { name = "empty" ; str = "" ; ReturnHashTable = $false }
@ { name = "spaces" ; str = " " ; ReturnHashTable = $false }
@ { name = "object" ; str = "{a:1}" ; ReturnHashTable = $false }
) {
param ( $str , $ReturnHashTable )
2016-12-15 11:06:26 +03:00
$errRecord = $null
2017-10-25 16:57:05 +01:00
[ Microsoft.PowerShell.Commands.JsonObject ]:: ConvertFromJson ( $str , $ReturnHashTable , [ ref ] $errRecord )
2018-03-23 10:26:23 -07:00
$errRecord | Should -BeNullOrEmpty
2016-12-15 11:06:26 +03:00
}
2019-02-20 00:37:37 -05:00
It 'Throw ArgumentException for invalid string ''<name>'' with -ReturnHashTable:<ReturnHashTable>' -TestCases @ (
2017-10-25 16:57:05 +01:00
@ { name = "plain text" ; str = "plaintext" ; ReturnHashTable = $true }
@ { name = "part" ; str = '{"a" :' ; ReturnHashTable = $true }
@ { name = "plain text" ; str = "plaintext" ; ReturnHashTable = $false }
@ { name = "part" ; str = '{"a" :' ; ReturnHashTable = $false }
) {
param ( $str , $ReturnHashTable )
2016-12-15 11:06:26 +03:00
$errRecord = $null
2018-05-17 14:42:04 -07:00
{ [ Microsoft.PowerShell.Commands.JsonObject ]:: ConvertFromJson ( $str , $ReturnHashTable , [ ref ] $errRecord ) } | Should -Throw -ErrorId "ArgumentException"
2017-10-25 16:57:05 +01:00
}
2019-02-20 00:37:37 -05:00
It 'Can Convert json with depth less than -Depth:<Depth> with -ReturnHashTable:<ReturnHashTable>' -TestCases $testCasesJsonDepthWithAndWithoutReturnHashTable {
param ( $Depth , $ReturnHashTable )
$errRecord = $null
$json = New-NestedJson -Depth ( $Depth - 1 )
[ Microsoft.PowerShell.Commands.JsonObject ]:: ConvertFromJson ( $json , $ReturnHashTable , $Depth , [ ref ] $errRecord )
$errRecord | Should -BeNullOrEmpty
}
It 'Can Convert json with depth equal to -Depth:<Depth> with -ReturnHashTable:<ReturnHashTable>' -TestCases $testCasesJsonDepthWithAndWithoutReturnHashTable {
param ( $Depth , $ReturnHashTable )
$errRecord = $null
$json = New-NestedJson -Depth $Depth
[ Microsoft.PowerShell.Commands.JsonObject ]:: ConvertFromJson ( $json , $ReturnHashTable , $Depth , [ ref ] $errRecord )
$errRecord | Should -BeNullOrEmpty
}
It 'Throws ArgumentException for json with greater depth than -Depth:<Depth> with -ReturnHashTable:<ReturnHashTable>' -TestCases $testCasesJsonDepthWithAndWithoutReturnHashTable {
param ( $Depth , $ReturnHashTable )
$errRecord = $null
$json = New-NestedJson -Depth ( $Depth + 1 )
{ [ Microsoft.PowerShell.Commands.JsonObject ]:: ConvertFromJson ( $json , $ReturnHashTable , $Depth , [ ref ] $errRecord ) } |
Should -Throw -ErrorId "ArgumentException"
}
Context 'Empty key name' {
2017-10-25 16:57:05 +01:00
It 'Throw InvalidOperationException when json contains empty key name' {
$errorRecord = $null
[ Microsoft.PowerShell.Commands.JsonObject ]:: ConvertFromJson ( $jsonWithEmptyKey , [ ref ] $errorRecord )
2018-03-23 10:26:23 -07:00
$errorRecord . FullyQualifiedErrorId | Should -BeExactly 'EmptyKeyInJsonString'
2017-10-25 16:57:05 +01:00
}
It 'Not throw when json contains empty key name when ReturnHashTable is true' {
$errorRecord = $null
$result = [ Microsoft.PowerShell.Commands.JsonObject ]:: ConvertFromJson ( $jsonWithEmptyKey , $true , [ ref ] $errorRecord )
2018-03-23 10:26:23 -07:00
$result | Should -Not -BeNullOrEmpty
$result . Count | Should -Be 1
$result . '' | Should -Be 'Value'
2017-10-25 16:57:05 +01:00
}
}
Context 'Keys with different casing ' {
2019-02-20 00:37:37 -05:00
2017-10-25 16:57:05 +01:00
It 'Throw InvalidOperationException when json contains key with different casing' {
$errorRecord = $null
[ Microsoft.PowerShell.Commands.JsonObject ]:: ConvertFromJson ( $jsonContainingKeysWithDifferentCasing , [ ref ] $errorRecord )
2018-03-23 10:26:23 -07:00
$errorRecord . FullyQualifiedErrorId | Should -BeExactly 'KeysWithDifferentCasingInJsonString'
2017-10-25 16:57:05 +01:00
}
It 'Not throw when json contains key (same casing) when ReturnHashTable is true' {
$errorRecord = $null
$result = [ Microsoft.PowerShell.Commands.JsonObject ]:: ConvertFromJson ( $jsonContainingKeysWithDifferentCasing , $true , [ ref ] $errorRecord )
2018-03-23 10:26:23 -07:00
$result | Should -Not -BeNullOrEmpty
$result . Count | Should -Be 2
$result . key1 | Should -Be 'Value1'
$result . Key1 | Should -Be 'Value2'
2017-10-25 16:57:05 +01:00
}
2016-12-15 11:06:26 +03:00
}
}