-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
145 additions
and
17 deletions.
There are no files selected for viewing
40 changes: 40 additions & 0 deletions
40
ByteSerialization.Tests/Integration/Attributes/FinalElement/FinalElementAttributeTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// SPDX-License-Identifier: MIT | ||
|
||
using ByteSerialization.IO; | ||
using ByteSerialization.Tests.Integration.Attributes.FinalElement.TestObjects; | ||
using Xunit; | ||
using Xunit.Abstractions; | ||
|
||
namespace ByteSerialization.Tests.Integration.Attributes.FinalElement | ||
{ | ||
public class FinalElementAttributeTest(ITestOutputHelper testOutputHelper) : | ||
IntegrationTestBase(testOutputHelper) | ||
{ | ||
#region Fields | ||
|
||
private static readonly byte[] TestBytes = | ||
HexStringConverter.ToByteArray("01 02 03 FF 05"); | ||
|
||
private static readonly ListContainer TestObject = new() | ||
{ | ||
Bytes = [1, 2, 3, 0xFF], | ||
EndByte = 5, | ||
}; | ||
|
||
#endregion | ||
|
||
#region Methods | ||
|
||
[Fact] | ||
public void Test_Deserialization() => | ||
AssertDeserializedObject( | ||
TestObject, TestBytes, Endianness.BigEndian); | ||
|
||
[Fact] | ||
public void Test_Serialization() => | ||
AssertSerializedObject( | ||
TestBytes, TestObject, Endianness.BigEndian); | ||
|
||
#endregion | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
ByteSerialization.Tests/Integration/Attributes/FinalElement/TestObjects/ListContainer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// SPDX-License-Identifier: MIT | ||
|
||
using ByteSerialization.Attributes; | ||
using ByteSerialization.IO; | ||
|
||
namespace ByteSerialization.Tests.Integration.Attributes.FinalElement.TestObjects | ||
{ | ||
internal class ListContainer | ||
{ | ||
#region Properties | ||
|
||
[Order(0), FinalElement(byte.MaxValue)] | ||
public List<byte> Bytes { get; set; } | ||
[Order(1)] | ||
public byte EndByte { get; set; } | ||
|
||
#endregion | ||
|
||
#region Methods (: object) | ||
|
||
public override bool Equals(object obj) | ||
{ | ||
if (obj is ListContainer other) | ||
{ | ||
if (!Enumerable.SequenceEqual(Bytes, other.Bytes)) | ||
return false; | ||
if (!Equals(EndByte, other.EndByte)) | ||
return false; | ||
return true; | ||
} | ||
else | ||
return false; | ||
} | ||
|
||
public override int GetHashCode() => | ||
HashCode.Combine( | ||
HashCodeHelper.CombineHashCodes(Bytes?.ToArray()), | ||
EndByte); | ||
|
||
#endregion | ||
} | ||
} |
16 changes: 0 additions & 16 deletions
16
ByteSerialization.Tests/Integration/Values/Composites/TestObjects/HashCodeHelper.cs
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// SPDX-License-Identifier: MIT | ||
|
||
using ByteSerialization.Components.Attributes.Limiting; | ||
using System; | ||
|
||
namespace ByteSerialization.Attributes | ||
{ | ||
[AttributeComponent(typeof(FinalElementComponent))] | ||
[AttributeUsage(AttributeTargets.Property)] | ||
public class FinalElementAttribute : ByteSerializationAttribute | ||
{ | ||
#region Properties | ||
|
||
public object Value { get; } | ||
public Type ValueType { get; } | ||
|
||
#endregion | ||
|
||
#region Constructor | ||
|
||
public FinalElementAttribute(object value) => | ||
Value = value; | ||
|
||
public FinalElementAttribute(Type valueType) => | ||
ValueType = valueType; | ||
|
||
#endregion | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
ByteSerialization/Components/Attributes/Limiting/FinalElementComponent.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// SPDX-License-Identifier: MIT | ||
|
||
using ByteSerialization.Attributes; | ||
using ByteSerialization.Attributes.Limiting; | ||
using ByteSerialization.Components.Values.Composites.Collections; | ||
using System; | ||
using System.Linq; | ||
|
||
namespace ByteSerialization.Components.Attributes.Limiting | ||
{ | ||
public class FinalElementComponent : | ||
AttributeComponent<FinalElementAttribute>, ILimitingComponent | ||
{ | ||
#region Properties | ||
|
||
private object _expectedFinalElementValue; | ||
|
||
#endregion | ||
|
||
#region Methods | ||
|
||
protected override void OnInitialized() | ||
{ | ||
base.OnInitialized(); | ||
_expectedFinalElementValue = Attribute.Value ?? Activator.CreateInstance(Attribute.ValueType); | ||
} | ||
|
||
public bool IsDeserializedUntilEnd(CollectionComponent collection) => | ||
collection.Elements.LastOrDefault()?.Value.Equals(_expectedFinalElementValue) ?? false; | ||
|
||
#endregion | ||
} | ||
} |