forked from EliteLoser/ConvertTo-Json
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConvertTo-STJson.Tests.ps1
202 lines (181 loc) · 8.14 KB
/
ConvertTo-STJson.Tests.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#requires -version 2
<#
Pester 4.x tests for Svendsen Tech's ConvertTo-STJson. Joakim Borger Svendsen.
Initially created on 2017-10-21.
#>
# Standardize the decimal separator to a period (not making it dynamic for now).
$Host.CurrentCulture.NumberFormat.NumberDecimalSeparator = "."
$MyScriptRoot = Split-Path -Path $MyInvocation.MyCommand.Path -Parent
. "$MyScriptRoot\ConvertTo-STJson.ps1"
Describe ConvertTo-STJson {
It "Test that null, true and false as value types are accounted for when passed in alone" {
ConvertTo-STJson -InputObject $Null | Should -Be ""
ConvertTo-STJson -InputObject $False | Should -Be "false"
ConvertTo-STJson -InputObject $True | Should -Be "true"
}
It "Given a number as a value, it should not be quoted" {
ConvertTo-STJson -InputObject 1 -Compress | Should -Be "1"
ConvertTo-STJson -InputObject 1.1 -Compress | Should -Be "1.1"
ConvertTo-STJson -InputObject 1.12e-2 -Compress | Should -Be "0.0112"
}
It "Given a number as a string, it should be quoted." {
ConvertTo-STJson -InputObject "1" -Compress | Should -Be '"1"'
ConvertTo-STJson -InputObject "1.1" -Compress | Should -Be """1.1"""
ConvertTo-STJson -InputObject "1.12e-2" -Compress | Should -Be """1.12e-2"""
}
It "Given a number as a string, it should not be quoted if -CoerceNumberStrings is passed" {
ConvertTo-STJson -InputObject "1" -Compress -CoerceNumberStrings | Should -Be "1"
ConvertTo-STJson -InputObject "1.1" -Compress -CoerceNumberStrings | Should -Be "1.1"
ConvertTo-STJson -InputObject "1.12e-2" -Compress -CoerceNumberStrings | Should -Be "1.12e-2"
}
It "Test hashtable structure with number, string, null, true and false as values" {
ConvertTo-STJson -InputObject @{ Key = 1.23 } -Compress | Should -Be "{`"Key`":1.23}"
ConvertTo-STJson -InputObject @{ Key = 'null' } -Compress | Should -Be "{`"Key`":`"null`"}"
ConvertTo-STJson -InputObject @{ Key = $Null } -Compress | Should -Be "{`"Key`":null}"
ConvertTo-STJson -InputObject @{ Key = $True } -Compress | Should -Be "{`"Key`":true}"
ConvertTo-STJson -InputObject @{ Key = $False } -Compress | Should -Be "{`"Key`":false}"
}
It "Test custom PowerShell object with number, string, null, true and false as values" {
ConvertTo-STJson -InputObject (New-Object -TypeName PSObject -Property @{ Key = 1.23 }) -Compress |
Should -Be "{`"Key`":1.23}"
ConvertTo-STJson -InputObject (New-Object -TypeName PSObject -Property @{ Key = 'null' }) -Compress |
Should -Be "{`"Key`":`"null`"}"
ConvertTo-STJson -InputObject (New-Object -TypeName PSObject -Property @{ Key = $Null }) -Compress |
Should -Be "{`"Key`":null}"
ConvertTo-STJson -InputObject (New-Object -TypeName PSObject -Property @{ Key = $True }) -Compress |
Should -Be "{`"Key`":true}"
ConvertTo-STJson -InputObject (New-Object -TypeName PSObject -Property @{ Key = $False }) -Compress |
Should -Be "{`"Key`":false}"
}
It "Test single array with numbers, strings, null, true and false as values" {
ConvertTo-STJson -InputObject @(1, 2, 3, "test", $Null, $True, $False, 'bar') -Compress |
Should -Be '[1,2,3,"test",null,true,false,"bar"]'
}
It "Test array as hashtable value, with numbers and strings" {
# Test a PSCustomObject at the same time. PSv2-compatible syntax/creation (not ordered).
$Number = New-Object -TypeName PSObject -Property @{
Key = @(1.12e-2, 2, "3", 'foo')
}
ConvertTo-STJson -InputObject $Number -Compress | Should -Be "{`"Key`":[0.0112,2,`"3`",`"foo`"]}"
}
It "Test complex/mixed data structure" {
ConvertTo-STJson -InputObject @(
@(1..3), 'a', 'b',
@{
NestedMore = @(1, @{
foo = @{ key = 'bar' }
})
}
) -Compress |
Should -Be '[[1,2,3],"a","b",{"NestedMore":[1,{"foo":{"key":"bar"}}]}]'
}
It "Test that compressed output from the built in ConvertTo-Json is identical if on PSv3+" {
if ($PSVersionTable.PSVersion.Major -lt 3) {
1 | Should -Be 1 # Can't test this on PSv2 or 1.
break
}
$Object = @{
a = @(@(1..3), 'a', 'b', @{ key = @(1, @(5,6,7), 'x') })
b = @{ a = @('y', 'z', @(1, @('innerinner', @('innerinnerinner', "innerinnerinner2", @{
innerkey = 'g' }, @{ inkey = 'f'} ), @(3,4) ) ) )} }
(ConvertTo-Json -InputObject $Object -Compress -Depth 99) -eq (ConvertTo-STJson -InputObject $Object -Compress) |
Should -Be $True
}
It "Test that double quotes, newlines and carriage returns are escaped within a string" {
ConvertTo-STJson -InputObject "string with a`n newline a `r carriage return and a `"quoted`" word" -Compress |
Should -Be '"string with a\n newline a \r carriage return and a \"quoted\" word"'
}
It "Test that double quotes, newlines and carriage returns are escaped within a string in a hashtable value" {
ConvertTo-STJson -InputObject @{ Key = "string with a`n newline a `r carriage return and a `"quoted`" word" } -Compress |
Should -Be '{"Key":"string with a\n newline a \r carriage return and a \"quoted\" word"}'
}
It "Test for PSScriptAnalyzer errors" {
if (Get-Command -Name "Invoke-ScriptAnalyzer" -ErrorAction SilentlyContinue) {
try {
@(Invoke-ScriptAnalyzer -Path "$MyScriptRoot\ConvertTo-STJson.ps1" -ErrorAction Stop |
Where-Object {
$_.Severity -notmatch 'Information|Warning'
}).Count | Should -Be 0
}
catch {
throw "Invoke-ScriptAnalyzer gave a critical error: $_"
}
}
else {
1 | Should -Be 1 # can't test without PSScriptAnalyzer
break
}
}
It "Formats datetime objects as ISO 8601 when you specify the switch parameter" {
ConvertTo-STJson -InputObject @{ nest =
@{ datetime = Get-Date -Year 2018 -Month 06 -Day 25 -Hour 00 -Minute 00 -Second 00 } } `
-Compress -DateTimeAsISO8601 |
Should -Be '{"nest":{"datetime":"2018-06-25T00:00:00"}}'
}
It "Test indentation/formatting of a complex data structure" {
ConvertTo-STJson -InputObject @(
@(@(1..3), 'a', 'b', @{ key = @(1, @(5,6,7), 'x') }),
@{ a = @('y', 'z', @(1, @('innerinner', @('innerinnerinner',
$Null, "foo", @{
innerkey = 'g' }, @{ inkey = @{ x = 'f' } } ),
@(3,4) ) ) ) } ) |
Should -Be (
@"
[
[
[
1,
2,
3
],
"a",
"b",
{
"key":
[
1,
[
5,
6,
7
],
"x"
]
}
],
{
"a":
[
"y",
"z",
[
1,
[
"innerinner",
[
"innerinnerinner",
null,
"foo",
{
"innerkey": "g"
},
{
"inkey":
{
"x": "f"
}
}
],
[
3,
4
]
]
]
]
}
]
"@ -replace '\r') # \n becomes \r\n in this string, but is only \n in the JSON,
# so it breaks the comparison. Workaround. Can't have \r in the test data.
}
}