Skip to content

Commit

Permalink
Fix tests and normalize line endings
Browse files Browse the repository at this point in the history
  • Loading branch information
xoofx committed Feb 17, 2024
1 parent 33462c4 commit d1d596b
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 25 deletions.
53 changes: 28 additions & 25 deletions src/SharpYaml.Tests/Serialization/SerializationTests2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ public void TestSimpleObjectAndPrimitive()
UInt16: 4
UInt32: 6
UInt64: 8
".Trim();
".Trim().NormnalizeLineEndings();
var settings = new SerializerSettings() { LimitPrimitiveFlowSequence = 20 };
settings.RegisterTagMapping("MyObject", typeof(MyObject));
SerialRoundTrip(settings, text);
Expand Down Expand Up @@ -378,7 +378,7 @@ public void TestFloatDoublePrecision()
var text = @"!ObjectFloatDoublePrecision
Double: 1.0000000000000001E-05
Float: 9.99999975E-06
".Trim();
".Trim().NormnalizeLineEndings();

SerialRoundTrip(settings, text);
}
Expand Down Expand Up @@ -411,7 +411,7 @@ public void TestFloatDoubleNaNInfinity()
FloatNaN: NaN
FloatNegativeInfinity: -Infinity
FloatPositiveInfinity: Infinity
".Trim();
".Trim().NormnalizeLineEndings();

SerialRoundTrip(settings, text);
}
Expand All @@ -438,7 +438,7 @@ public void TestObjectWithCollection()
var text = @"!MyObjectAndCollection
Name: Yes
Values: [a, b, c]
".Trim();
".Trim().NormnalizeLineEndings();

var settings = new SerializerSettings() { LimitPrimitiveFlowSequence = 20 };
settings.RegisterTagMapping("MyObjectAndCollection", typeof(MyObjectAndCollection));
Expand Down Expand Up @@ -469,7 +469,7 @@ public void TestCustomCollectionWithProperties()
- a
- b
- c
".Trim();
".Trim().NormnalizeLineEndings();

var settings = new SerializerSettings() { LimitPrimitiveFlowSequence = 0 };
settings.RegisterTagMapping("MyCustomCollectionWithProperties", typeof(MyCustomCollectionWithProperties));
Expand Down Expand Up @@ -502,7 +502,7 @@ public void TestCustomDictionaryWithProperties()
a: true
b: false
c: true
".Trim();
".Trim().NormnalizeLineEndings();

var settings = new SerializerSettings() { LimitPrimitiveFlowSequence = 0 };
settings.RegisterTagMapping("MyCustomDictionaryWithProperties", typeof(MyCustomDictionaryWithProperties));
Expand All @@ -523,7 +523,7 @@ public void TestCustomDictionaryWithItemsAsMembers()
a: true
b: false
c: true
".Trim();
".Trim().NormnalizeLineEndings();

var settings = new SerializerSettings() { LimitPrimitiveFlowSequence = 0, SerializeDictionaryItemsAsMembers = true };
settings.RegisterTagMapping("MyCustomDictionaryWithProperties", typeof(MyCustomDictionaryWithProperties));
Expand Down Expand Up @@ -625,7 +625,7 @@ public void TestMyCustomClassWithSpecialMembers()
- ""3""
- ""4""
Value: 0
".Trim();
".Trim().NormnalizeLineEndings();

var settings = new SerializerSettings() { LimitPrimitiveFlowSequence = 0 };
settings.RegisterTagMapping("MyCustomClassWithSpecialMembers", typeof(MyCustomClassWithSpecialMembers));
Expand Down Expand Up @@ -786,7 +786,7 @@ public void TestImplicitDictionaryAndList()
e: 4
f: no
Value: 0
".Trim();
".Trim().NormnalizeLineEndings();

SerialRoundTrip(settings, text, typeof(Dictionary<object, object>));
}
Expand Down Expand Up @@ -1063,7 +1063,7 @@ public void TestStyles()
classNoStyle.G_ListCustom.Add(7);

var serializer = new Serializer(settings);
var text = serializer.Serialize(classNoStyle).Trim();
var text = serializer.Serialize(classNoStyle).Trim().NormnalizeLineEndings();

var textReference = @"!ClassNoStyle
A_ListWithCustomStyle: [a, b, c]
Expand All @@ -1081,7 +1081,7 @@ public void TestStyles()
- 5
F_ListClassWithStyleDefaultFormat:
- {Name: name3, Value: 3}
G_ListCustom: {Name: name4, ~Items: [1, 2, 3, 4, 5, 6, 7]}";
G_ListCustom: {Name: name4, ~Items: [1, 2, 3, 4, 5, 6, 7]}".NormnalizeLineEndings();

Assert.AreEqual(textReference, text);
}
Expand All @@ -1101,13 +1101,13 @@ public void TestDefaultStyle()

var settings1 = new SerializerSettings { DefaultStyle = YamlStyle.Flow };
var serializer1 = new Serializer(settings1);
var yaml1 = serializer1.Serialize(testObject);
Assert.AreEqual("{Items: [1, 2, 3], Name: John Doe, Value: 42}\r\n", yaml1);
var yaml1 = serializer1.Serialize(testObject).Trim().NormnalizeLineEndings();
Assert.AreEqual("{Items: [1, 2, 3], Name: John Doe, Value: 42}", yaml1);

var settings2 = new SerializerSettings { DefaultStyle = YamlStyle.Block };
var serializer2 = new Serializer(settings2);
var yaml2 = serializer2.Serialize(testObject);
Assert.AreEqual("Items:\r\n - 1\r\n - 2\r\n - 3\r\nName: John Doe\r\nValue: 42\r\n", yaml2);
var yaml2 = serializer2.Serialize(testObject).Trim().NormnalizeLineEndings();
Assert.AreEqual("Items:\n - 1\n - 2\n - 3\nName: John Doe\nValue: 42", yaml2);
}

public class ClassWithKeyTransform
Expand Down Expand Up @@ -1519,7 +1519,7 @@ public void TestImplicitMemberType()
var text = @"!ClassWithImplicitMemberType
Test:
String: test
".Trim();
".Trim().NormnalizeLineEndings();

settings.RegisterTagMapping("ClassWithImplicitMemberType", typeof(ClassWithImplicitMemberType));
settings.RegisterTagMapping("ClassWithImplicitMemberTypeInner", typeof(ClassWithImplicitMemberTypeInner));
Expand All @@ -1534,7 +1534,7 @@ public void TestNonImplicitMemberType()
var text = @"!ClassWithNonImplicitMemberType
Test: !ClassWithImplicitMemberTypeInner
String: test
".Trim();
".Trim().NormnalizeLineEndings();

settings.RegisterTagMapping("ClassWithNonImplicitMemberType", typeof(ClassWithNonImplicitMemberType));
settings.RegisterTagMapping("ClassWithImplicitMemberTypeInner", typeof(ClassWithImplicitMemberTypeInner));
Expand Down Expand Up @@ -1670,9 +1670,9 @@ public void TestNumberInString()
};

var serializer = new Serializer();
var str = serializer.Serialize(test).Trim();
var str = serializer.Serialize(test).Trim().NormnalizeLineEndings();
System.Console.WriteLine(str);
Assert.AreEqual("f1: \"1.2\"\r\nf2: \"1e3\"\r\nf3: \"-0.0\"\r\nf4: \".inf\"\r\ni1: \"1234\"\r\ni2: \"-2\"", str);
Assert.AreEqual("f1: \"1.2\"\nf2: \"1e3\"\nf3: \"-0.0\"\nf4: \".inf\"\ni1: \"1234\"\ni2: \"-2\"", str);
}

[Test]
Expand All @@ -1687,9 +1687,9 @@ public void TestBooleanInString()
};

var serializer = new Serializer();
var str = serializer.Serialize(test).Trim();
var str = serializer.Serialize(test).Trim().NormnalizeLineEndings();
System.Console.WriteLine(str);
Assert.AreEqual("b1: true\r\nb2: false\r\ns1: \"true\"\r\ns2: \"false\"", str);
Assert.AreEqual("b1: true\nb2: false\ns1: \"true\"\ns2: \"false\"", str);
}

[Test]
Expand All @@ -1702,13 +1702,14 @@ public void TestNullInString()
};

var serializer = new Serializer();
var str = serializer.Serialize(test).Trim();
var str = serializer.Serialize(test).Trim().NormnalizeLineEndings();
System.Console.WriteLine(str);
Assert.AreEqual("n: null\r\ns: \"null\"", str);
Assert.AreEqual("n: null\ns: \"null\"", str);
}

private void SerialRoundTrip(SerializerSettings settings, string text, Type serializedType = null)
{
text = text.Trim().NormnalizeLineEndings();
var serializer = new Serializer(settings);
// not working yet, scalar read/write are not yet implemented
Console.WriteLine("Text to serialize:");
Expand All @@ -1718,7 +1719,7 @@ private void SerialRoundTrip(SerializerSettings settings, string text, Type seri

Console.WriteLine();

var text2 = serializer.Serialize(value, serializedType).Trim();
var text2 = serializer.Serialize(value, serializedType).Trim().NormnalizeLineEndings();
Console.WriteLine("Text deserialized:");
Console.WriteLine("------------------");
Console.WriteLine(text2);
Expand All @@ -1735,11 +1736,13 @@ private object SerialRoundTrip(SerializerSettings settings, object value, Type e
Console.WriteLine("------------------");
Console.WriteLine(text);

text = text.Trim().NormnalizeLineEndings();

// not working yet, scalar read/write are not yet implemented
Console.WriteLine("Text to deserialize/serialize:");
Console.WriteLine("------------------");
var valueDeserialized = serializer.Deserialize(text);
var text2 = serializer.Serialize(valueDeserialized);
var text2 = serializer.Serialize(valueDeserialized).Trim().NormnalizeLineEndings();
Console.WriteLine(text2);

Assert.AreEqual(text, text2);
Expand Down
34 changes: 34 additions & 0 deletions src/SharpYaml.Tests/StringHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright (c) SharpYaml - Alexandre Mutel
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

namespace SharpYaml.Tests;

internal static class StringHelper
{
public static string NormnalizeLineEndings(this string text)
{
#if NET8_0_OR_GREATER
return text.ReplaceLineEndings("\n");
#else
return text.Replace("\r\n", "\n").Replace("\r", "\n");
#endif
}

}

0 comments on commit d1d596b

Please sign in to comment.