Skip to content

Commit 4af0fe7

Browse files
Add aditional tests
Signed-off-by: Gabriel Adrian Samfira <[email protected]>
1 parent 4c0f8e9 commit 4af0fe7

File tree

1 file changed

+124
-0
lines changed

1 file changed

+124
-0
lines changed

Tests/powershell-yaml.Tests.ps1

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -493,7 +493,128 @@ bools:
493493
Assert-Equivalent -Options $compareStrictly -Actual $res -Expected $expected
494494
}
495495
}
496+
}
497+
498+
Describe "Test ConvertTo-Yaml can serialize more complex nesting" {
499+
BeforeAll {
500+
$global:sample = [PSCustomObject]@{
501+
a1 = "a"
502+
a2 = [PSCustomObject]@{
503+
"a1" = "a"
504+
a2 = [PSCustomObject]@{
505+
a1 = [PSCustomObject]@{
506+
"a1" = "a"
507+
a2 = [PSCustomObject]@{
508+
a1 = "a"
509+
}
510+
a3 = [ordered]@{
511+
a1 = @("a", "b")
512+
}
513+
a4 = @("a", "b")
514+
}
515+
}
516+
a3 = @(
517+
[PSCustomObject]@{
518+
a1 = "a"
519+
a2 = $False
520+
}
521+
)
522+
}
523+
}
524+
525+
$global:sample2 = [PSCustomObject]@{
526+
b1 = "b"
527+
b2 = [PSCustomObject]@{
528+
b1 = "b"
529+
b2 = [PSCustomObject]@{
530+
"b" = "b"
531+
}
532+
}
533+
b3 = [ordered]@{
534+
b1 = @("b1", "b2")
535+
}
536+
b4 = $True
537+
b5 = [PSCustomObject]@{
538+
b = "b"
539+
}
540+
}
541+
542+
$global:expected_json = '{"a1":"a","a2":{"a1":"a","a2":{"a1":{"a1":"a","a2":{"a1":"a"},"a3":{"a1":["a","b"]},"a4":["a","b"]}},"a3":[{"a1":"a","a2":false}]}}'
543+
$global:expected_json2 = '{"b1":"b","b2":{"b1":"b","b2":{"b":"b"}},"b3":{"b1":["b1","b2"]},"b4":true,"b5":{"b":"b"}}'
544+
$global:expected_block_yaml = @"
545+
a1: a
546+
a2:
547+
a1: a
548+
a2:
549+
a1:
550+
a1: a
551+
a2:
552+
a1: a
553+
a3:
554+
a1:
555+
- a
556+
- b
557+
a4:
558+
- a
559+
- b
560+
a3:
561+
- a1: a
562+
a2: false
496563
564+
"@
565+
566+
$global:expected_flow_yaml = '{a1: a, a2: {a1: a, a2: {a1: {a1: a, a2: {a1: a}, a3: {a1: [a, b]}, a4: [a, b]}}, a3: [{a1: a, a2: false}]}}'
567+
$global:expected_block_yaml2 = @"
568+
b1: b
569+
b2:
570+
b1: b
571+
b2:
572+
b: b
573+
b3:
574+
b1:
575+
- b1
576+
- b2
577+
b4: true
578+
b5:
579+
b: b
580+
581+
"@
582+
$global:expected_flow_yaml2 = '{b1: b, b2: {b1: b, b2: {b: b}}, b3: {b1: [b1, b2]}, b4: true, b5: {b: b}}'
583+
}
584+
585+
It "Should serialize nested PSCustomObjects to YAML" {
586+
$yaml = ConvertTo-Yaml $sample
587+
$yaml | Should -Be $expected_block_yaml
588+
589+
$yaml = ConvertTo-Yaml $sample2
590+
$yaml | Should -Be $expected_block_yaml2
591+
}
592+
593+
It "Should serialize nested PSCustomObjects to YAML flow format" {
594+
$yaml = ConvertTo-Yaml $sample -Options UseFlowStyle
595+
$yaml.Replace($([Environment]::NewLine), "") | Should -Be $expected_flow_yaml
596+
597+
$yaml = ConvertTo-Yaml $sample2 -Options UseFlowStyle
598+
$yaml.Replace($([Environment]::NewLine), "") | Should -Be $expected_flow_yaml2
599+
}
600+
601+
It "Should serialize nested PSCustomObjects to JSON" {
602+
# Converted with powershell-yaml
603+
$json = ConvertTo-Yaml $sample -Options JsonCompatible
604+
$json.Replace(" ", "").Replace($([Environment]::NewLine), "") | Should -Be $expected_json
605+
606+
# Converted with ConvertTo-Json
607+
$withJsonCommandlet = ConvertTo-Json -Compress -Depth 100 $sample
608+
$withJsonCommandlet | Should -Be $expected_json
609+
610+
# Converted with powershell-yaml
611+
$json = ConvertTo-Yaml $sample2 -Options JsonCompatible
612+
$json.Replace(" ", "").Replace($([Environment]::NewLine), "") | Should -Be $expected_json2
613+
614+
# Converted with ConvertTo-Json
615+
$withJsonCommandlet = ConvertTo-Json -Compress -Depth 100 $sample2
616+
$withJsonCommandlet | Should -Be $expected_json2
617+
}
497618
}
498619

499620
Describe "Test ConvertTo-Yaml -OutFile parameter behavior" {
@@ -704,6 +825,7 @@ bigInt: 99999999999999999999999999999999999
704825
int32: 2147483647
705826
int64: 9223372036854775807
706827
decimal: 3.10
828+
reallyLongDecimal: 3.9999999999999990
707829
'@
708830
}
709831

@@ -715,8 +837,10 @@ decimal: 3.10
715837
It "Should round-trip decimals with trailing 0" {
716838
$result = ConvertFrom-Yaml -Yaml $value
717839
$result.decimal | Should -Be ([decimal]3.10)
840+
$result.reallyLongDecimal | Should -Be ([decimal]::Parse("3.9999999999999990", [cultureinfo]::InvariantCulture))
718841

719842
ConvertTo-Yaml $result["decimal"] | Should -Be "3.10$([Environment]::NewLine)"
843+
ConvertTo-Yaml $result["reallyLongDecimal"] | Should -Be "3.9999999999999990$([Environment]::NewLine)"
720844
}
721845

722846
It 'Should be of proper type and value' {

0 commit comments

Comments
 (0)