Skip to content

Commit

Permalink
Merge pull request #590 from ExtendRealityLtd/feat/bool-event-proxy
Browse files Browse the repository at this point in the history
feat(Proxy): add boolean event proxy emitter
  • Loading branch information
thestonefox authored Jul 8, 2023
2 parents 6c0d740 + 0e1f0d9 commit 61fb69f
Show file tree
Hide file tree
Showing 4 changed files with 192 additions and 0 deletions.
23 changes: 23 additions & 0 deletions Runtime/Event/Proxy/BooleanEventProxyEmitter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
namespace Zinnia.Event.Proxy
{
using System;
using UnityEngine.Events;

/// <summary>
/// Emits a <see cref="UnityEvent"/> with a <see cref="bool"/> payload whenever the <see cref="SingleEventProxyEmitter{TValue,TEvent}.Receive"/> method is called.
/// </summary>
public class BooleanEventProxyEmitter : RestrictableSingleEventProxyEmitter<bool, BooleanEventProxyEmitter.UnityEvent>
{
/// <summary>
/// Defines the event with the specified state.
/// </summary>
[Serializable]
public class UnityEvent : UnityEvent<bool> { }

/// <inheritdoc />
protected override object GetTargetToCheck()
{
return Payload;
}
}
}
11 changes: 11 additions & 0 deletions Runtime/Event/Proxy/BooleanEventProxyEmitter.cs.meta

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

147 changes: 147 additions & 0 deletions Tests/Editor/Event/Proxy/BooleanEventProxyEmitterTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
using Zinnia.Data.Type;
using Zinnia.Event.Proxy;
using Zinnia.Rule;

namespace Test.Zinnia.Event.Proxy
{
using NUnit.Framework;
using Test.Zinnia.Utility.Mock;
using UnityEngine;

public class BooleanEventProxyEmitterTest
{
private GameObject containingObject;
private BooleanEventProxyEmitter subject;

[SetUp]
public void SetUp()
{
containingObject = new GameObject("BooleanEventProxyEmitterTest");
subject = containingObject.AddComponent<BooleanEventProxyEmitter>();
}

[TearDown]
public void TearDown()
{
Object.DestroyImmediate(containingObject);
}

[Test]
public void Receive()
{
UnityEventListenerMock emittedMock = new UnityEventListenerMock();
subject.Emitted.AddListener(emittedMock.Listen);
bool payload = true;

Assert.IsFalse(subject.Payload);
Assert.IsFalse(emittedMock.Received);

subject.Receive(payload);

Assert.IsTrue(subject.Payload);
Assert.IsTrue(emittedMock.Received);
}

[Test]
public void EmitPayload()
{
UnityEventListenerMock emittedMock = new UnityEventListenerMock();
subject.Emitted.AddListener(emittedMock.Listen);
bool payload = true;
subject.Payload = payload;

Assert.IsTrue(subject.Payload);
Assert.IsFalse(emittedMock.Received);

subject.EmitPayload();

Assert.IsTrue(subject.Payload);
Assert.IsTrue(emittedMock.Received);
}

[Test]
public void ClearPayload()
{
UnityEventListenerMock emittedMock = new UnityEventListenerMock();
subject.Emitted.AddListener(emittedMock.Listen);
bool payload = true;
subject.Payload = payload;

Assert.IsTrue(subject.Payload);
subject.ClearPayload();
Assert.IsFalse(subject.Payload);
}

[Test]
public void ReceiveInactiveGameObject()
{
UnityEventListenerMock emittedMock = new UnityEventListenerMock();
subject.Emitted.AddListener(emittedMock.Listen);
subject.gameObject.SetActive(false);

bool payload = true;

Assert.IsFalse(subject.Payload);
Assert.IsFalse(emittedMock.Received);

subject.Receive(payload);

Assert.IsFalse(subject.Payload);
Assert.IsFalse(emittedMock.Received);
}

[Test]
public void ReceiveInactiveComponent()
{
UnityEventListenerMock emittedMock = new UnityEventListenerMock();
subject.Emitted.AddListener(emittedMock.Listen);
subject.enabled = false;

bool payload = true;

Assert.IsFalse(subject.Payload);
Assert.IsFalse(emittedMock.Received);

subject.Receive(payload);

Assert.IsFalse(subject.Payload);
Assert.IsFalse(emittedMock.Received);
}

[Test]
public void EmitPayloadInactiveGameObject()
{
UnityEventListenerMock emittedMock = new UnityEventListenerMock();
subject.Emitted.AddListener(emittedMock.Listen);
subject.gameObject.SetActive(false);
bool payload = false;
subject.Payload = payload;

Assert.IsFalse(subject.Payload);
Assert.IsFalse(emittedMock.Received);

subject.EmitPayload();

Assert.IsFalse(subject.Payload);
Assert.IsFalse(emittedMock.Received);
}

[Test]
public void EmitPayloadInactiveComponent()
{
UnityEventListenerMock emittedMock = new UnityEventListenerMock();
subject.Emitted.AddListener(emittedMock.Listen);
subject.enabled = false;
bool payload = false;
subject.Payload = payload;

Assert.IsFalse(subject.Payload);
Assert.IsFalse(emittedMock.Received);

subject.EmitPayload();

Assert.IsFalse(subject.Payload);
Assert.IsFalse(emittedMock.Received);
}
}
}
11 changes: 11 additions & 0 deletions Tests/Editor/Event/Proxy/BooleanEventProxyEmitterTest.cs.meta

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

0 comments on commit 61fb69f

Please sign in to comment.