Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Original file line number Diff line number Diff line change
@@ -1,64 +1,80 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
Describe 'ConvertTo-Json' -tags "CI" {
BeforeAll {
$newline = [System.Environment]::NewLine
}

using namespace System.Environment
using namespace System.Management.Automation
using namespace Newtonsoft.Json.Linq

Describe 'ConvertTo-Json' -tags 'CI' {
It 'Newtonsoft.Json.Linq.Jproperty should be converted to Json properly' {
$EgJObject = New-Object -TypeName Newtonsoft.Json.Linq.JObject
$EgJObject.Add("TestValue1", "123456")
$EgJObject.Add("TestValue2", "78910")
$EgJObject.Add("TestValue3", "99999")
$dict = @{}
$dict.Add('JObject', $EgJObject)
$dict.Add('StrObject', 'This is a string Object')
$properties = @{'DictObject' = $dict; 'RandomString' = 'A quick brown fox jumped over the lazy dog'}
$object = New-Object -TypeName psobject -Property $properties
$object = [pscustomobject]@{
HashTable = [hashtable]@{
JObject = [JObject]::FromObject( @{
TestValue1 = 123456;
TestValue2 = 78910;
TestValue3 = 99999
} );
StrObject = 'This is a string Object'
};
RandomString = 'A quick brown fox jumped over the lazy dog'
}

$jsonFormat = ConvertTo-Json -InputObject $object
$jsonFormat | Should -Match '"TestValue1": 123456'
$jsonFormat | Should -Match '"TestValue2": 78910'
$jsonFormat | Should -Match '"TestValue3": 99999'
}

It "StopProcessing should succeed" {
$ps = [PowerShell]::Create()
$null = $ps.AddScript({
$obj = [PSCustomObject]@{P1 = ''; P2 = ''; P3 = ''; P4 = ''; P5 = ''; P6 = ''}
$obj.P1 = $obj.P2 = $obj.P3 = $obj.P4 = $obj.P5 = $obj.P6 = $obj
1..100 | Foreach-Object { $obj } | ConvertTo-Json -Depth 10 -Verbose
# the conversion is expected to take some time, this throw is in case it doesn't
throw "Should not have thrown exception"
})
$null = $ps.BeginInvoke()
# wait for verbose message from ConvertTo-Json to ensure cmdlet is processing
Wait-UntilTrue { $ps.Streams.Verbose.Count -gt 0 }
$null = $ps.BeginStop($null, $null)
# wait a bit to ensure state has changed, not using synchronous Stop() to avoid blocking Pester
Start-Sleep -Milliseconds 100
$ps.InvocationStateInfo.State | Should -BeExactly "Stopped"
It 'StopProcessing should succeed' {
$obj = (1..100).ForEach{
$_ = [pscustomobject]@{P1 = ''; P2 = ''; P3 = ''; P4 = ''; P5 = ''; P6 = ''}
$_.P1 = $_.P2 = $_.P3 = $_.P4 = $_.P5 = $_.P6 = $_
Write-Output $_
}

$ps = [powershell]::Create()
[void]$ps.AddScript( {
param ($obj)
Write-Verbose -Message 'Ready' -Verbose
ConvertTo-Json -InputObject $obj -Depth 10
throw 'ConvertTo-Json finished processing before it could be stopped.'
} ).AddArgument($obj)
[void]$ps.BeginInvoke()

# Wait until there is output in the verbose stream.
Wait-UntilTrue { $ps.Streams.Verbose.Count -gt 0 } -IntervalInMilliseconds 10

# Wait to ensure ConvertTo-Json has started processing.
Start-Sleep -Milliseconds 1000

# Not using synchronous Stop() to avoid blocking Pester.
[void]$ps.BeginStop($null, $null)

# Instance should stop.
Wait-UntilTrue { $ps.InvocationStateInfo.State -eq [PSInvocationState]::Stopped } -IntervalInMilliseconds 10 | Should -BeTrue
$ps.Dispose()
}

It "The result string is packed in an array symbols when AsArray parameter is used." {
$output = 1 | ConvertTo-Json -AsArray
$output | Should -BeLike "``[*1*]"
It 'The result string is packed in an array symbols when AsArray parameter is used.' {
$output = ConvertTo-Json -InputObject 1 -AsArray

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any particular reason to change all of the usage of pipeline to -InputObject. Seems like the correct thing is to have some use pipeline and some use -InputObject

$output | Should -BeLike '`[*1*]'

$output = 1,2 | ConvertTo-Json -AsArray
$output | Should -BeLike "``[*1*2*]"
$output = ConvertTo-Json -InputObject (1, 2) -AsArray
$output | Should -BeLike '`[*1*2*]'
}

It "The result string is not packed in the array symbols when there is only one input object and AsArray parameter is not used." {
$output = 1 | ConvertTo-Json
It 'The result string is not packed in the array symbols when there is only one input object and AsArray parameter is not used.' {
$output = ConvertTo-Json -InputObject 1
$output | Should -BeExactly '1'
}

It "The result string should <Name>." -TestCases @(
@{name = "be not escaped by default."; params = @{}; expected = "{$newline ""abc"": ""'def'""$newline}" }
@{name = "be not escaped with '-EscapeHandling Default'."; params = @{EscapeHandling = 'Default'}; expected = "{$newline ""abc"": ""'def'""$newline}" }
@{name = "be escaped with '-EscapeHandling EscapeHtml'."; params = @{EscapeHandling = 'EscapeHtml'}; expected = "{$newline ""abc"": ""\u0027def\u0027""$newline}" }
It 'The result string should <Name>.' -TestCases @(
$nl = [Environment]::NewLine
@{name = 'be not escaped by default.'; params = @{}; expected = "{$nl ""abc"": ""'def'""$nl}" }
@{name = 'be not escaped with "-EscapeHandling Default".'; params = @{EscapeHandling = 'Default'}; expected = "{$nl ""abc"": ""'def'""$nl}" }
@{name = 'be escaped with "-EscapeHandling EscapeHtml".'; params = @{EscapeHandling = 'EscapeHtml'}; expected = "{$nl ""abc"": ""\u0027def\u0027""$nl}" }
) {
param ($name, $params ,$expected)
param ($name, $params, $expected)

@{ 'abc' = "'def'" } | ConvertTo-Json @params | Should -BeExactly $expected
}
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.