Skip to content

Commit

Permalink
Engine - Tests: add Test_Serializer_ignored_field_types() & Test_Seri…
Browse files Browse the repository at this point in the history
…alizer_unsupported_field_types()
  • Loading branch information
friflo committed Jun 10, 2024
1 parent c322501 commit 8b8a5a2
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 24 deletions.
2 changes: 2 additions & 0 deletions Engine/src/ECS/Base/TrimmingAttributes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
// File is copy of https://github.com/eerhardt/blog-resources/blob/main/creating-aot-compatible-libraries/TrimmingAttributes.cs
// As recommended in [How to make libraries compatible with native AOT](https://devblogs.microsoft.com/dotnet/creating-aot-compatible-libraries/#approach-2-define-the-attributes-internally)

// ReSharper disable once EmptyNamespace
// ReSharper disable once CheckNamespace
namespace System.Diagnostics.CodeAnalysis
{
#if !NET7_0_OR_GREATER
Expand Down
9 changes: 5 additions & 4 deletions Engine/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: 43 scripts: 10 entity tags: 11", schema.ToString());
AreEqual(44, components.Length);
AreEqual("components: 44 scripts: 10 entity tags: 11", schema.ToString());
AreEqual(45, components.Length);
AreEqual(11, scripts.Length);

AreEqual(49, schema.SchemaTypeByKey.Count);
AreEqual(43, schema.ComponentTypeByType.Count);
AreEqual(50, schema.SchemaTypeByKey.Count);
AreEqual(44, schema.ComponentTypeByType.Count);
AreEqual(10, schema.ScriptTypeByType.Count);

IsNull(components[0]);
Expand Down Expand Up @@ -93,6 +93,7 @@ public static void Test_ComponentTypes()
AssertBlittableComponent<BlittableDatetime> (schema, true);
AssertBlittableComponent<BlittableGuid> (schema, true);
AssertBlittableComponent<BlittableBigInteger> (schema, true);
AssertBlittableComponent<BlittableUri> (schema, true);


// --- Test blittable types
Expand Down
18 changes: 17 additions & 1 deletion Engine/src/Tests/ECS/Components.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public struct NonBlittableCycle2 : IComponent { internal CycleClass1
public struct BlittableDatetime : IComponent { public DateTime dateTime; }
public struct BlittableGuid : IComponent { public Guid guid; }
public struct BlittableBigInteger : IComponent { public BigInteger bigInteger; }
// public struct BlittableUri : IComponent { public Uri uri; } todo requires fix in Fliox.Mapper
public struct BlittableUri : IComponent { public Uri uri; } // throws exception when serialized

[ComponentKey(null)]
public struct NonSerializedComponent : IComponent { public int value; }
Expand Down Expand Up @@ -126,6 +126,22 @@ public static class MyEntityExtensions
// test missing [StructComponent()] attribute
struct MyInvalidComponent : IComponent { public int b; }

// --------------------- component field types ignored by serializer
public struct GenericStruct<T> { public T value; }
public class GenericClass<T> { public T value; }
public interface IGenericInterface<T> { public T Value { get; set; } }

// [Generic structs in components is not supported] https://github.com/friflo/Friflo.Json.Fliox/issues/45
// Fixed by commit: [Mapper - Ignore unsupported fields/properties in custom classes, structs and interfaces.]
// https://github.com/friflo/Friflo.Json.Fliox/commit/12c4f88f26d86cffd014f00f823d152eede29d36
// Remarks: Unsupported fields/properties in custom classes, structs and interfaces are now ignored by mapper/serialization.
// Fix published in: https://www.nuget.org/packages/Friflo.Json.Fliox/1.0.2
public struct ComponentWithGenerics : IComponent
{
public GenericStruct<int> genericStruct;
public GenericClass<int> genericClass;
public IGenericInterface<int> genericInterface;
}

// ------------------------------------------------ tags
[TagName("test-tag")]
Expand Down
19 changes: 0 additions & 19 deletions Engine/src/Tests/ECS/Entity/Test_StructComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -513,25 +513,6 @@ public static void Test_StructComponent_Archetype_CreateEntity_default_component
AreEqual(0, entity.MyComponent1().a);
}

[Test]
// [Generic structs in components is not supported] https://github.com/friflo/Friflo.Json.Fliox/issues/45
// Fixed by commit: [Mapper - Ignore unsupported fields/properties in custom classes, structs and interfaces.]
// https://github.com/friflo/Friflo.Json.Fliox/commit/12c4f88f26d86cffd014f00f823d152eede29d36
// Remarks: Unsupported fields/properties in custom classes, structs and interfaces are now ignored by mapper/serialization.
// Fix published in: https://www.nuget.org/packages/Friflo.Json.Fliox/1.0.2
public static void Test_StructComponent_generic_struct() {
_ = new EntityStore();
}

public struct GenericStruct<T>
{
public T value;
}

public struct ComponentWithGenericStruct : IComponent
{
public GenericStruct<int> gs;
}
}

}
37 changes: 37 additions & 0 deletions Engine/src/Tests/ECS/Serialize/Test_EntitySerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using Friflo.Engine.ECS;
Expand Down Expand Up @@ -456,6 +457,42 @@ public static void Test_Serializer_Non_CLS()
var nonClsRead = entityRead.GetComponent<NonClsTypes>();
AreEqual(nonClsWrite, nonClsRead);
}

[Test]
public static void Test_Serializer_ignored_field_types()
{
var store = new EntityStore();
var serializer = new EntitySerializer();

var entity = store.CreateEntity();
entity.AddComponent(new ComponentWithGenerics());
var json = serializer.WriteEntity(entity);
var expect =
@"{
""id"": 1,
""components"": {
""ComponentWithGenerics"": {}
}
}";
AreEqual(expect, json);
}

[Test]
public static void Test_Serializer_unsupported_field_types()
{
var store = new EntityStore();
var serializer = new EntitySerializer();

var entity = store.CreateEntity();
entity.AddComponent(new BlittableUri());

var e = Throws<TargetInvocationException>(() => {
serializer.WriteEntity(entity);
});
var inner = e!.InnerException as ArgumentException;
NotNull(inner);
AreEqual("Type 'System.Uri' does not have a default constructor (Parameter 'type')", inner.Message);
}
}

}

0 comments on commit 8b8a5a2

Please sign in to comment.