-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Tests: add Test_SerializeLinkRelations
- Loading branch information
Showing
2 changed files
with
91 additions
and
2 deletions.
There are no files selected for viewing
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,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 | ||
} | ||
|
||
} |
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