Skip to content

Commit

Permalink
Added some tests
Browse files Browse the repository at this point in the history
Signed-off-by: Gabriel Adrian Samfira <gsamfira@cloudbasesolutions.com>
  • Loading branch information
gabriel-samfira committed Aug 26, 2024
1 parent 063c033 commit 2eeb24b
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 8 deletions.
78 changes: 78 additions & 0 deletions Tests/powershell-yaml.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,84 @@ bools:
}
}

Describe 'Numbers are parsed as the smallest type possible' {
BeforeAll {
$global:value = @'
bigInt: 99999999999999999999999999999999999
int32: 2147483647
int64: 9223372036854775807
'@
}

It 'Should be a BigInt' {
$result = ConvertFrom-Yaml -Yaml $value
$result.bigInt | Should -BeOfType System.Numerics.BigInteger
}

It 'Should be of proper type and value' {
$result = ConvertFrom-Yaml -Yaml $value
$result.bigInt | Should -Be ([System.Numerics.BigInteger]::Parse("99999999999999999999999999999999999"))
$result.int32 | Should -Be ([int32]2147483647)
$result.int64 | Should -Be ([int64]9223372036854775807)
}
}

Describe 'PSCustomObjects' {
Context 'Classes with PSCustomObjects' {
It 'Should serialise as a hash' {
$nestedPsO = [PSCustomObject]@{
Nested = 'NestedValue'
}
$PsO = [PSCustomObject]@{
Name = 'Value'
Nested = $nestedPsO
}

class TestClass {
[PSCustomObject]$PsO
[string]$Ok
}
$Class = [TestClass]@{
PsO = $PsO
Ok = 'aye'
}
$asYaml = ConvertTo-Yaml $Class
$result = ConvertFrom-Yaml -Yaml $asYaml -Ordered
[System.Collections.Specialized.OrderedDictionary]$ret = [System.Collections.Specialized.OrderedDictionary]::new()
$ret["PsO"] = [System.Collections.Specialized.OrderedDictionary]::new()
$ret["PsO"]["Name"] = "Value"
$ret["PsO"]["Nested"] = [System.Collections.Specialized.OrderedDictionary]::new()
$ret["PsO"]["Nested"]["Nested"] = "NestedValue"
$ret["Ok"] = "aye"
Assert-Equivalent -Options $compareStrictly -Expected $ret -Actual $result
}
}

Context 'PSCustomObject with a single property' {
BeforeAll {
$global:value = [PSCustomObject]@{key="value"}
}
It 'Should serialise as a hash' {
$result = ConvertTo-Yaml $value
$result | Should -Be "key: value$([Environment]::NewLine)"
}
}
Context 'PSCustomObject with multiple properties' {
BeforeAll {
$global:value = [PSCustomObject]@{key1="value1"; key2="value2"}
}
It 'Should serialise as a hash' {
$result = ConvertTo-Yaml $value
$result | Should -Be "key1: value1$([Environment]::NewLine)key2: value2$([Environment]::NewLine)"
}
It 'Should deserialise as a hash' {
$asYaml = ConvertTo-Yaml $value
$result = ConvertFrom-Yaml -Yaml $asYaml -Ordered
Assert-Equivalent -Options $compareStrictly -Expected @{key1="value1"; key2="value2"} -Actual ([hashtable]$result)
}
}
}

Describe 'StringQuotingEmitter' {
BeforeAll {
$oldYamlPkgUrl = 'https://www.nuget.org/api/v2/package/YamlDotNet/11.2.1'
Expand Down
Binary file modified lib/net47/StringQuotingEmitter.dll
Binary file not shown.
Binary file modified lib/netstandard2.1/StringQuotingEmitter.dll
Binary file not shown.
11 changes: 3 additions & 8 deletions src/serializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,11 @@ public object ReadYaml(IParser parser, Type type, ObjectDeserializer rootDeseria

public void WriteYaml(IEmitter emitter, object value, Type type, ObjectSerializer serializer) {
var psObj = (PSObject)value;

emitter.Emit(new MappingStart());

foreach (var prop in psObj.Properties) {
emitter.Emit(new Scalar(prop.Name));
var propType = prop.Value.GetType();
if (propType == typeof(string) || propType.IsPrimitive || propType.IsValueType) {
emitter.Emit(new Scalar(prop.Value.ToString()));
continue;
}
serializer(prop.Value, propType);
serializer(prop.Name, prop.Name.GetType());
serializer(prop.Value, prop.Value.GetType());
}
emitter.Emit(new MappingEnd());
}
Expand Down

0 comments on commit 2eeb24b

Please sign in to comment.