Skip to content

Commit

Permalink
rework and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vanifatovvlad committed Sep 25, 2023
1 parent 2715a0f commit f90967b
Show file tree
Hide file tree
Showing 11 changed files with 250 additions and 22 deletions.
3 changes: 3 additions & 0 deletions Scellecs.Morpeh/Assembly.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
using System.Runtime.CompilerServices;

[assembly:InternalsVisibleTo("Morpeh.Events.Tests")]
3 changes: 3 additions & 0 deletions Scellecs.Morpeh/Assembly.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 2 additions & 20 deletions Scellecs.Morpeh/EventWorldExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,12 @@ namespace Scellecs.Morpeh
{
public static class EventWorldExtensions
{
private static readonly IntHashMap<EventRegistry> Registries = new IntHashMap<EventRegistry>();

internal static void SetupEventRegistry(World world)
{
var registry = new EventRegistry();

var eventSystemGroup = world.CreateSystemsGroup();
eventSystemGroup.AddSystem(new ProcessEventsSystem(registry));
world.AddPluginSystemsGroup(eventSystemGroup);

Registries.Add(world.identifier, registry, out _);
}

internal static void CleanupEventRegistry(World world)
{
Registries.Remove(world.identifier, out _);
}

[PublicAPI]
public static Event<TData> GetEvent<TData>(this World world)
where TData : struct, IEventData
{
var type = typeof(TData);
var registry = Registries.GetValueByKey(world.identifier);
var registry = world.CodeWriterEventsRegistry;

if (registry.RegisteredEvents.TryGetValue(type, out var registeredEvent))
{
Expand All @@ -47,7 +29,7 @@ public static Event<TData> GetEvent<TData>(this World world)
[PublicAPI]
public static EventBase GetReflectionEvent(this World world, Type type)
{
var registry = Registries.GetValueByKey(world.identifier);
var registry = world.CodeWriterEventsRegistry;

if (registry.RegisteredEvents.TryGetValue(type, out var registeredEvent))
{
Expand Down
7 changes: 5 additions & 2 deletions Scellecs.Morpeh/EventWorldPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,15 @@ public static void RuntimeInitialize()
[Preserve]
public void Initialize(World world)
{
EventWorldExtensions.SetupEventRegistry(world);
world.CodeWriterEventsRegistry = new EventRegistry();

var eventSystemGroup = world.CreateSystemsGroup();
eventSystemGroup.AddSystem(new ProcessEventsSystem(world.CodeWriterEventsRegistry));
world.AddPluginSystemsGroup(eventSystemGroup);
}

public void Deinitialize(World world)
{
EventWorldExtensions.CleanupEventRegistry(world);
}
}
}
7 changes: 7 additions & 0 deletions Scellecs.Morpeh/World.Events.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Scellecs.Morpeh
{
public partial class World
{
internal EventRegistry CodeWriterEventsRegistry;
}
}
3 changes: 3 additions & 0 deletions Scellecs.Morpeh/World.Events.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions Tests.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

185 changes: 185 additions & 0 deletions Tests/EventTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
using NUnit.Framework;
using Scellecs.Morpeh;

namespace Tests
{
public class EventTests
{
private const float DT = 0.01f;

private World _world;
private EventWorldPlugin _eventWorldPlugin;

[SetUp]
public void SetUp()
{
_world = World.Create();
_eventWorldPlugin = new EventWorldPlugin();

_eventWorldPlugin.Initialize(_world);
}

[TearDown]
public void TearDown()
{
_eventWorldPlugin.Deinitialize(_world);

_world.Dispose();
}

[Test]
public void SendEvent_CurrentFrameScheduled()
{
var evt = _world.GetEvent<TestEvent>();

evt.NextFrame(new TestEvent {value = 1f});
evt.NextFrame(new TestEvent {value = 2f});
evt.NextFrame(new TestEvent {value = 3f});

Assert.IsTrue(evt.IsScheduled);
Assert.IsFalse(evt.IsPublished);
Assert.AreEqual(3, evt.ScheduledChanges.length);
Assert.AreEqual(0, evt.BatchedChanges.length);
Assert.AreEqual(1f, evt.ScheduledChanges.data[0].value);
Assert.AreEqual(2f, evt.ScheduledChanges.data[1].value);
Assert.AreEqual(3f, evt.ScheduledChanges.data[2].value);
}

[Test]
public void SendEvent_NextFramePublished()
{
var evt = _world.GetEvent<TestEvent>();

evt.NextFrame(new TestEvent {value = 1f});
evt.NextFrame(new TestEvent {value = 2f});
evt.NextFrame(new TestEvent {value = 3f});

_world.Update(DT);
_world.CleanupUpdate(DT);

Assert.IsFalse(evt.IsScheduled);
Assert.IsTrue(evt.IsPublished);
Assert.AreEqual(0, evt.ScheduledChanges.length);
Assert.AreEqual(3, evt.BatchedChanges.length);
Assert.AreEqual(1f, evt.BatchedChanges.data[0].value);
Assert.AreEqual(2f, evt.BatchedChanges.data[1].value);
Assert.AreEqual(3f, evt.BatchedChanges.data[2].value);
}

[Test]
public void SendEvent_ToFramesCleared()
{
var evt = _world.GetEvent<TestEvent>();

evt.NextFrame(new TestEvent {value = 1f});
evt.NextFrame(new TestEvent {value = 2f});
evt.NextFrame(new TestEvent {value = 3f});

_world.Update(DT);
_world.CleanupUpdate(DT);

_world.Update(DT);
_world.CleanupUpdate(DT);

Assert.IsFalse(evt.IsScheduled);
Assert.IsFalse(evt.IsPublished);
Assert.AreEqual(0, evt.ScheduledChanges.length);
Assert.AreEqual(0, evt.BatchedChanges.length);
}

[Test]
public void SendEvent_Callback_FrameOne_NoCall()
{
var calls = 0;

var evt = _world.GetEvent<TestEvent>();

evt.Subscribe(changes =>
{
++calls;

Assert.AreEqual(3, changes.length);
Assert.AreEqual(1f, evt.BatchedChanges.data[0].value);
Assert.AreEqual(2f, evt.BatchedChanges.data[1].value);
Assert.AreEqual(3f, evt.BatchedChanges.data[2].value);
});

evt.NextFrame(new TestEvent {value = 1f});
evt.NextFrame(new TestEvent {value = 2f});
evt.NextFrame(new TestEvent {value = 3f});

_world.Update(DT);
_world.CleanupUpdate(DT);

Assert.AreEqual(0, calls);
}

[Test]
public void SendEvent_Callback_FrameTwo_OneCall()
{
var calls = 0;

var evt = _world.GetEvent<TestEvent>();

evt.Subscribe(changes =>
{
++calls;

Assert.AreEqual(3, changes.length);
Assert.AreEqual(1f, evt.BatchedChanges.data[0].value);
Assert.AreEqual(2f, evt.BatchedChanges.data[1].value);
Assert.AreEqual(3f, evt.BatchedChanges.data[2].value);
});

evt.NextFrame(new TestEvent {value = 1f});
evt.NextFrame(new TestEvent {value = 2f});
evt.NextFrame(new TestEvent {value = 3f});

_world.Update(DT);
_world.CleanupUpdate(DT);

_world.Update(DT);
_world.CleanupUpdate(DT);

Assert.AreEqual(1, calls);
}

[Test]
public void SendEvent_Callback_FrameThree_OneCall()
{
var calls = 0;

var evt = _world.GetEvent<TestEvent>();

evt.Subscribe(changes =>
{
++calls;

Assert.AreEqual(3, changes.length);
Assert.AreEqual(1f, evt.BatchedChanges.data[0].value);
Assert.AreEqual(2f, evt.BatchedChanges.data[1].value);
Assert.AreEqual(3f, evt.BatchedChanges.data[2].value);
});

evt.NextFrame(new TestEvent {value = 1f});
evt.NextFrame(new TestEvent {value = 2f});
evt.NextFrame(new TestEvent {value = 3f});

_world.Update(DT);
_world.CleanupUpdate(DT);

_world.Update(DT);
_world.CleanupUpdate(DT);

_world.Update(DT);
_world.CleanupUpdate(DT);

Assert.AreEqual(1, calls);
}

private struct TestEvent : IEventData
{
public float value;
}
}
}
3 changes: 3 additions & 0 deletions Tests/EventTests.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 24 additions & 0 deletions Tests/Morpeh.Events.Tests.asmdef
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"name": "Morpeh.Events.Tests",
"rootNamespace": "",
"references": [
"Scellecs.Morpeh",
"UnityEngine.TestRunner",
"UnityEditor.TestRunner"
],
"includePlatforms": [
"Editor"
],
"excludePlatforms": [],
"allowUnsafeCode": false,
"overrideReferences": true,
"precompiledReferences": [
"nunit.framework.dll"
],
"autoReferenced": false,
"defineConstraints": [
"UNITY_INCLUDE_TESTS"
],
"versionDefines": [],
"noEngineReferences": false
}
7 changes: 7 additions & 0 deletions Tests/Morpeh.Events.Tests.asmdef.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit f90967b

Please sign in to comment.