From ef976e098a9cf5a85c81a33081a22bf48d73f1aa Mon Sep 17 00:00:00 2001 From: Alexander Kopetsch Date: Mon, 17 Jun 2024 17:44:26 +0200 Subject: [PATCH] fix/test IndicatorComponent --- .../Indicator/IndicatorAttributeTest.cs | 31 +++++++++++++++---- .../Conditional/IndicatorComponent.cs | 9 ++++-- ByteSerialization/IO/EndianBinaryReader.cs | 1 - 3 files changed, 31 insertions(+), 10 deletions(-) diff --git a/ByteSerialization.Tests/Integration/Attributes/Indicator/IndicatorAttributeTest.cs b/ByteSerialization.Tests/Integration/Attributes/Indicator/IndicatorAttributeTest.cs index b2abafd..82e512c 100644 --- a/ByteSerialization.Tests/Integration/Attributes/Indicator/IndicatorAttributeTest.cs +++ b/ByteSerialization.Tests/Integration/Attributes/Indicator/IndicatorAttributeTest.cs @@ -12,10 +12,10 @@ public class IndicatorAttributeTest(ITestOutputHelper testOutputHelper) : { #region Fields - private static readonly byte[] TestBytes = + private static readonly byte[] Indicated_TestBytes = HexStringConverter.ToByteArray("41 58747261 42 43"); // 'A', 'Xtra', 'B', C' - private static readonly IndicatorTestClass TestObject = new() + private static readonly IndicatorTestClass Indicated_TestObject = new() { Byte0 = (byte)'A', Bar = new IndicatorTestClass2() @@ -25,19 +25,38 @@ public class IndicatorAttributeTest(ITestOutputHelper testOutputHelper) : }, }; + private static readonly byte[] NotIndicated_TestBytes = + HexStringConverter.ToByteArray("41"); // 'A' + + private static readonly IndicatorTestClass NotIndicated_TestObject = new() + { + Byte0 = (byte)'A', + Bar = null, + }; + #endregion #region Methods [Fact] - public void Test_Deserialization() => + public void Test_Indicated_Deserialization() => + AssertDeserializedObject( + Indicated_TestObject, Indicated_TestBytes, Endianness.BigEndian); + + [Fact] + public void Test_Indicated_Serialization() => + AssertSerializedObject( + Indicated_TestBytes, Indicated_TestObject, Endianness.BigEndian); + + [Fact] + public void Test_NotIndicated_Deserialization() => AssertDeserializedObject( - TestObject, TestBytes, Endianness.BigEndian); + NotIndicated_TestObject, [ 0x41, 0, 0, 0, 0], Endianness.BigEndian); // FIXME: testBytes [Fact] - public void Test_Serialization() => + public void Test_NotIndicated_Serialization() => AssertSerializedObject( - TestBytes, TestObject, Endianness.BigEndian); + NotIndicated_TestBytes, NotIndicated_TestObject, Endianness.BigEndian); #endregion } diff --git a/ByteSerialization/Components/Attributes/Conditional/IndicatorComponent.cs b/ByteSerialization/Components/Attributes/Conditional/IndicatorComponent.cs index b9433f6..cbc8c54 100644 --- a/ByteSerialization/Components/Attributes/Conditional/IndicatorComponent.cs +++ b/ByteSerialization/Components/Attributes/Conditional/IndicatorComponent.cs @@ -12,11 +12,14 @@ public class IndicatorComponent : AttributeComponent, ICondi protected override void OnInitialized() { base.OnInitialized(); - Node.OnSerializing += WriteIndicator; + Node.OnSerializing += WriteIndicatorIfNotNull; } - private void WriteIndicator() => - Writer.Write(Attribute.Value); + private void WriteIndicatorIfNotNull() + { + if (Node.Value != null) + Writer.Write(Attribute.Value); + } public bool IsSerialized(Node node) => Reader.TryRead(Attribute.Value); diff --git a/ByteSerialization/IO/EndianBinaryReader.cs b/ByteSerialization/IO/EndianBinaryReader.cs index 028b9df..96f2146 100644 --- a/ByteSerialization/IO/EndianBinaryReader.cs +++ b/ByteSerialization/IO/EndianBinaryReader.cs @@ -5,7 +5,6 @@ using System; using System.Collections.Generic; using System.IO; -using System.Linq; namespace ByteSerialization.IO {