Skip to content

Commit

Permalink
Tests: add Test_SerializeLinkRelations
Browse files Browse the repository at this point in the history
  • Loading branch information
friflo committed Nov 29, 2024
1 parent bf7fd0b commit 219a2b6
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 2 deletions.
89 changes: 89 additions & 0 deletions src/Tests-internal/ECS/Test_SerializeLinkRelations.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
using System.IO;
using Friflo.Engine.ECS;
using Friflo.Engine.ECS.Serialize;
using NUnit.Framework;
using Tests.ECS.Relations;
using Tests.ECS.Serialize;
using static NUnit.Framework.Assert;


// ReSharper disable InconsistentNaming
namespace Internal.ECS {

public static class Test_SerializeLinkRelations
{

// referenced entity 1002 is loaded after loading its LinkRelation
// The fields of store.nodes[1002] .isLinked & .isOwner are assigned
private const string JsonLinkRelation =
@"[{
""id"": 1,
""components"": {
""multi-attack"": [{""speed"":0,""target"":1002}]
}
},{
""id"": 1002
}]";


#region read relations

[Test]
public static void Test_SerializeLinkRelations_read_LinkRelation()
{
var store = new EntityStore();
var serializer = new EntitySerializer();
var stream = Test_Serializer.StringAsStream(JsonLinkRelation);

var result = serializer.ReadIntoStore(store, stream);
IsNull(result.error);

var entity1 = store.GetEntityById(1);
var entity2 = store.GetEntityById(1002);
var relations1 = entity1.GetRelations<AttackRelation>();
AreEqual(1, relations1.Length);
AreEqual(1002 , relations1[0].target.Id);

var entity1Relations = entity1.GetRelations<AttackRelation>();
AreEqual(1, entity1Relations.Length);
AreEqual(1002, entity1Relations[0].target.Id);

var incomingLinks = entity2.GetIncomingLinks<AttackRelation>();
var attackBit = 1 << StructInfo<AttackRelation>.Index;
AreEqual(attackBit, store.nodes[1].isOwner);
AreEqual(attackBit, store.nodes[1002].isLinked);

AreEqual(1, incomingLinks.Count);
AreEqual(1, incomingLinks[0].Entity.Id);

entity1.DeleteEntity();
AreEqual(0, store.nodes[1].isOwner);
// AreEqual(0, store.nodes[1002].isLinked);

incomingLinks = entity2.GetIncomingLinks<AttackRelation>();
AreEqual(0, incomingLinks.Count);
}
#endregion


#region write relations
[Test]
public static void Test_SerializeLinkRelations_write_LinkRelation()
{
var store = new EntityStore();
Entity entity1 = store.CreateEntity(1);
Entity entity2 = store.CreateEntity(1002);

entity1.AddRelation(new AttackRelation { target = entity2 });

var serializer = new EntitySerializer();
using MemoryStream writeStream = new MemoryStream();
serializer.WriteEntities(new []{ entity1, entity2 }, writeStream);

var str = Test_Serializer.MemoryStreamAsString(writeStream);
AreEqual(JsonLinkRelation, str);
}
#endregion
}

}
4 changes: 2 additions & 2 deletions src/Tests/ECS/Serialize/Test_EntitySerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -265,12 +265,12 @@ public static void Test_Serializer_read_into_store_Perf()
stream.Close();
}

internal static string MemoryStreamAsString(MemoryStream stream) {
public static string MemoryStreamAsString(MemoryStream stream) {
stream.Flush();
return Encoding.UTF8.GetString(stream.GetBuffer(), 0, (int)stream.Length);
}

internal static Stream StringAsStream(string json) {
public static Stream StringAsStream(string json) {
var bytes = Encoding.UTF8.GetBytes(json);
var stream = new MemoryStream(bytes.Length);
stream.Write(bytes);
Expand Down

0 comments on commit 219a2b6

Please sign in to comment.