mirror of
https://github.com/space-wizards/RobustToolbox.git
synced 2026-02-15 11:40:52 +01:00
76 lines
2.2 KiB
C#
76 lines
2.2 KiB
C#
using System.Collections.Generic;
|
|
using System.IO;
|
|
using NUnit.Framework;
|
|
using Robust.Shared.GameObjects;
|
|
using Robust.Shared.IoC;
|
|
using Robust.Shared.Serialization;
|
|
using Robust.Shared.Serialization.Manager;
|
|
using Robust.Shared.Serialization.Manager.Attributes;
|
|
using Robust.Shared.Serialization.Markdown;
|
|
using Robust.Shared.Serialization.Markdown.Mapping;
|
|
using Robust.Shared.Serialization.Markdown.Sequence;
|
|
using YamlDotNet.RepresentationModel;
|
|
|
|
namespace Robust.UnitTesting.Shared.Serialization
|
|
{
|
|
public sealed class SerializationPriorityTest : RobustUnitTest
|
|
{
|
|
[Test]
|
|
public void Test()
|
|
{
|
|
var serializationManager = IoCManager.Resolve<ISerializationManager>();
|
|
serializationManager.Initialize();
|
|
|
|
var prototype = @"
|
|
- type: PriorityTest
|
|
first: A
|
|
second: B
|
|
third: C";
|
|
|
|
var yamlStream = new YamlStream();
|
|
yamlStream.Load(new StringReader(prototype));
|
|
|
|
var mapping = yamlStream.Documents[0].RootNode.ToDataNodeCast<SequenceDataNode>().Cast<MappingDataNode>(0);
|
|
|
|
var component = serializationManager.Read<PriorityTestComponent>(mapping, notNullableOverride: true);
|
|
|
|
Assert.That(component.Strings.Count, Is.EqualTo(3));
|
|
Assert.That(component.First, Is.EqualTo("A"));
|
|
Assert.That(component.Second, Is.EqualTo("C"));
|
|
Assert.That(component.Third, Is.EqualTo("B"));
|
|
}
|
|
}
|
|
|
|
public sealed partial class PriorityTestComponent : Component, ISerializationHooks
|
|
{
|
|
|
|
public readonly List<string> Strings = new() {string.Empty, string.Empty, string.Empty};
|
|
|
|
[DataField("first", priority: 3)]
|
|
public string First
|
|
{
|
|
get => Strings[0];
|
|
set => Strings.Add(value);
|
|
}
|
|
|
|
[DataField("second", priority: 1)]
|
|
public string Second
|
|
{
|
|
get => Strings[1];
|
|
set => Strings.Add(value);
|
|
}
|
|
|
|
[DataField("third", priority: 2)]
|
|
public string Third
|
|
{
|
|
get => Strings[2];
|
|
set => Strings.Add(value);
|
|
}
|
|
|
|
void ISerializationHooks.AfterDeserialization()
|
|
{
|
|
Strings.RemoveRange(0, 3);
|
|
}
|
|
}
|
|
}
|