mirror of
https://github.com/PowerShell/PowerShell
synced 2026-06-08 12:12:50 +00:00
* This change fixes the Invoke-RestMethod cmdlet behavior if the input is neither xml nor json. * test description * fixed the failed tests * removed some duplication * refactoring: eliminated some double castings * added JsonObject unit tests; enhanced JSON deserialization exception message * refactored JsonObject.Tests.ps1 * entitled test cases * formatting
47 lines
1.3 KiB
PowerShell
47 lines
1.3 KiB
PowerShell
Describe 'Unit tests for JsonObject' -tags "CI" {
|
|
|
|
function ShouldThrow
|
|
{
|
|
param (
|
|
[Parameter(ValueFromPipeline = $true)]
|
|
$InputObject,
|
|
[Parameter(Position = 0)]
|
|
$ExpectedException
|
|
)
|
|
|
|
try
|
|
{
|
|
& $InputObject
|
|
throw "Should throw exception"
|
|
}
|
|
catch
|
|
{
|
|
$_.FullyQualifiedErrorId | should be $ExpectedException
|
|
}
|
|
}
|
|
|
|
$validStrings = @(
|
|
@{ name = "empty"; str = "" }
|
|
@{ name = "spaces"; str = " " }
|
|
@{ name = "object"; str = "{a:1}" }
|
|
)
|
|
|
|
It 'no error for valid string - <name>' -TestCase $validStrings {
|
|
param ($str)
|
|
$errRecord = $null
|
|
[Microsoft.PowerShell.Commands.JsonObject]::ConvertFromJson($str, [ref]$errRecord)
|
|
$errRecord | Should BeNullOrEmpty
|
|
}
|
|
|
|
$invalidStrings = @(
|
|
@{ name = "plain text"; str = "plaintext" }
|
|
@{ name = "part"; str = '{"a" :' }
|
|
)
|
|
|
|
It 'throw ArgumentException for invalid string - <name>' -TestCase $invalidStrings {
|
|
param ($str)
|
|
$errRecord = $null
|
|
{ [Microsoft.PowerShell.Commands.JsonObject]::ConvertFromJson($str, [ref]$errRecord) } | ShouldThrow 'ArgumentException'
|
|
}
|
|
}
|