Skip to content

Commit

Permalink
Tests: add Test_Serialize_enum - see https://github.com/friflo/Friflo…
Browse files Browse the repository at this point in the history
  • Loading branch information
friflo committed Nov 27, 2024
1 parent d845f4f commit 95ce0bf
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/Tests/ECS/Base/Test_ComponentSchema.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@ public static void Test_ComponentTypes()
var components = schema.Components;
var scripts = schema.Scripts;

AreEqual("components: 67 scripts: 10 entity tags: 16", schema.ToString());
AreEqual(68, components.Length);
AreEqual("components: 68 scripts: 10 entity tags: 16", schema.ToString());
AreEqual(69, components.Length);
AreEqual(11, scripts.Length);

AreEqual(73, schema.SchemaTypeByKey.Count);
AreEqual(67, schema.ComponentTypeByType.Count);
AreEqual(74, schema.SchemaTypeByKey.Count);
AreEqual(68, schema.ComponentTypeByType.Count);
AreEqual(10, schema.ScriptTypeByType.Count);

IsNull(components[0]);
Expand Down
56 changes: 56 additions & 0 deletions src/Tests/ECS/Serialize/Test_Serialize_enum.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using System.Collections.Generic;
using System.IO;
using System.Text;
using Friflo.Engine.ECS;
using Friflo.Engine.ECS.Serialize;
using NUnit.Framework;
using static NUnit.Framework.Assert;


// ReSharper disable InconsistentNaming
namespace Tests.ECS.Serialize {

enum MyEnum { two = 2 }

struct EnumComp : IComponent
{
public MyEnum myEnum;
}

public static class Test_Serialize_enum
{
// https://github.com/friflo/Friflo.Engine.ECS/issues/35
[Test]
public static void Test_Serialize_enum_component()
{
var store = new EntityStore();
Entity entity = store.CreateEntity(1);
entity.AddComponent(new EnumComp { myEnum = MyEnum.two });

var serializer = new EntitySerializer();

using MemoryStream writeStream = new MemoryStream();
var entities = new List<Entity> { entity };
serializer.WriteEntities(entities, writeStream);

writeStream.Position = 0;
using StreamReader reader = new(writeStream);
string json = reader.ReadToEnd();
var expect =
@"[{
""id"": 1,
""components"": {
""EnumComp"": {""myEnum"":""two""}
}
}]";
AreEqual(expect, json);

var newStore = new EntityStore();
var readStream = new MemoryStream(Encoding.UTF8.GetBytes(json));
serializer.ReadIntoStore(newStore, readStream);
var newEntity = newStore.GetEntityById(1);
AreEqual(MyEnum.two, newEntity.GetComponent<EnumComp>().myEnum);
}
}

}

0 comments on commit 95ce0bf

Please sign in to comment.