Files
RobustToolbox/Robust.UnitTesting/Shared/Serialization/YamlConstantSerializer_Test.cs
T
ComicIronicandGitHub c9157c8b57 Adds custom YAML serializer for ints in terms of named constants (#1088)
Works similar to the flag serializer, except it allows for magic numbers
to be given names in the YAML.
2020-05-30 12:07:19 +02:00

75 lines
2.3 KiB
C#

using System;
using System.Collections.Generic;
using System.IO;
using NUnit.Framework;
using Robust.Shared.Maths;
using Robust.Shared.Serialization;
using Robust.Shared.Utility;
using YamlDotNet.RepresentationModel;
namespace Robust.UnitTesting.Shared.Serialization
{
[TestFixture]
[TestOf(typeof(YamlConstantSerializer))]
class YamlConstantSerializer_Test : RobustUnitTest
{
public sealed class GenericConstantTag {}
[Test]
public void SerializeOneConstantTest()
{
// Arrange
var data = (int)GenericConstants.One;
var mapping = new YamlMappingNode();
var serializer = YamlObjectSerializer.NewWriter(mapping);
// Act
serializer.DataField(ref data, "generic_constants", 0, WithFormat.Constants<GenericConstantTag>());
// Assert
var result = YamlObjectSerializer_Test.NodeToYamlText(mapping);
Assert.That(result, Is.EqualTo(SerializedOneConstant));
}
[Test]
public void DeserializeOneConstantTest()
{
// Arrange
var data = 0;
var rootNode = YamlObjectSerializer_Test.YamlTextToNode(SerializedOneConstant);
var serializer = YamlObjectSerializer.NewReader(rootNode);
// Act
serializer.DataField(ref data, "generic_constants", 0, WithFormat.Constants<GenericConstantTag>());
// Assert
Assert.That(data, Is.EqualTo((int)GenericConstants.One));
}
[Test]
public void DeserializeLegacyFormatTest()
{
// Arrange
var data = 0;
var rootNode = YamlObjectSerializer_Test.YamlTextToNode(SerializedThree);
var serializer = YamlObjectSerializer.NewReader(rootNode);
// Act
serializer.DataField(ref data, "generic_constants", 0, WithFormat.Constants<GenericConstantTag>());
// Assert
Assert.That(data, Is.EqualTo((int)GenericConstants.Three));
}
private const string SerializedOneConstant = "generic_constants: One\n...\n";
private const string SerializedThree = "generic_constants: 3\n...\n";
[ConstantsFor(typeof(GenericConstantTag))]
private enum GenericConstants
{
One = 1,
Three = 3,
}
}
}