Skip to content
This repository was archived by the owner on Aug 10, 2021. It is now read-only.

Commit 4341e6b

Browse files
authored
Tests (#13)
* Add listener tests, fix Vector2 GameEvent naming * Rename test class * Add game event listener test class, rename test methods * Update local var naming * Make API more flexible, rename raise method, update tests to match real use-cases * Fixup link in README * Add initial changelog * Put changes into proper place * Add mutable object test, update readme, update(?) quality settings, make mutable obj handler more flexible * Remove unnecessary Debug.Log statements * Copy over README to package docs
1 parent 65db3a8 commit 4341e6b

29 files changed

+650
-31
lines changed

CHANGELOG.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,20 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

7-
## [0.4.0] - 2020-10-07
7+
## [0.5.0] - 2020-10-07
8+
9+
### Added
10+
- Tests for game events and their listeners.
11+
- Tests for mutable objects.
12+
13+
### Changed
14+
- **Renamed** event and listener trigger functions (`OnGameEvent` -> `RaiseGameEvent`).
15+
- **Renamed** `GameEvents.Vector2.Vector3GameEvent` to `GameEvents.Vector2.Vector2GameEvent`, fixed typo.
16+
- Made `MutableObjectHandler` more extensible.
17+
- Exposed `GameEvent` and `OnGameEvent` on listeners via properties.
18+
- `MutableObject` instances are now reset using `SceneManager.activeSceneChanged`.
19+
20+
## [0.4.0] - 2020-10-06
821

922
### Added
1023
- `MutableObjectHandler` which would automate the process of resetting `MutableObjects`.

Documentation~/README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ Available game events:
3131
width="100%" alt="Example usage of Mutable Objects"
3232
/>
3333

34-
Mutable objects are used for storing and editing data on `ScriptableObject` assets at runtime. This data can be referenced, observed and used as a bridge by various scripts. Mutable objects are useful in situations where `ScriptableObject` data needs to be reset when a new level loads.
34+
Mutable objects are used for storing and editing data on `ScriptableObject` assets at runtime. This data can be referenced, observed and used as a bridge by various scripts. Mutable objects are useful in situations where `ScriptableObject` data needs to be reset when the [active scene changes](https://docs.unity3d.com/ScriptReference/SceneManagement.SceneManager-activeSceneChanged.html).
3535

3636
Available mutable objects:
3737
- `MutableBool` - encapsulates a `bool` value.
@@ -104,3 +104,5 @@ public class MutableCustom : MutableObject
104104
}
105105
}
106106
```
107+
108+
[Unity Package Manager]: https://docs.unity3d.com/Packages/com.unity.package-manager-ui@2.0/manual/index.html

Editor/GameEvents/Vector2/Vector2GameEventEditor.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44

55
namespace GameEvents.Vector2
66
{
7-
[CustomEditor(typeof(Vector3GameEvent))]
7+
[CustomEditor(typeof(Vector2GameEvent))]
88
public class Vector2GameEventEditor
9-
: ArgumentGameEventEditor<Vector3GameEvent, UnityEngine.Vector2>
9+
: ArgumentGameEventEditor<Vector2GameEvent, UnityEngine.Vector2>
1010
{
1111
protected override UnityEngine.Vector2 DrawArgumentField(UnityEngine.Vector2 value)
1212
{

Runtime/GameEvents/Game/GameEvent.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public void RaiseGameEvent()
2929
Debug.Log($"Raise event: {name}, listener: {listener}");
3030
}
3131

32-
listener.OnGameEvent();
32+
listener.RaiseGameEvent();
3333
}
3434
}
3535

Runtime/GameEvents/Generic/ArgumentGameEvent.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public void RaiseGameEvent(TArgument argument)
2828
Debug.Log($"Raise event: {name}, listener: {listener}, argument: {argument}");
2929
}
3030

31-
listener.OnGameEvent(argument);
31+
listener.RaiseGameEvent(argument);
3232
}
3333
}
3434

Runtime/GameEvents/Generic/ArgumentGameEventListener.cs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,28 @@ public abstract class ArgumentGameEventListener<TGameEvent, TUnityEvent, TArgume
1818
[Tooltip("Called when the listener is triggered with an argument")]
1919
private TUnityEvent onGameEvent = default;
2020

21+
/// <summary>
22+
/// Get or set the underlying GameEvent.
23+
/// </summary>
24+
public TGameEvent GameEvent
25+
{
26+
get => gameEvent;
27+
set => gameEvent = value;
28+
}
29+
30+
/// <summary>
31+
/// Get or set the underlying UnityEvent.
32+
/// </summary>
33+
public TUnityEvent OnGameEvent
34+
{
35+
get => onGameEvent;
36+
set => onGameEvent = value;
37+
}
38+
2139
/// <summary>
2240
/// Trigger this listener with an argument.
2341
/// </summary>
24-
public void OnGameEvent(TArgument argument)
42+
public void RaiseGameEvent(TArgument argument)
2543
{
2644
onGameEvent.Invoke(argument);
2745
}

Runtime/GameEvents/Generic/BaseGameEventListener.cs

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,29 @@ public abstract class BaseGameEventListener<TGameEvent>
1515
[Tooltip("Called when the listener is triggered with an argument")]
1616
private UnityEvent onGameEvent = default;
1717

18+
/// <summary>
19+
/// Get or set the underlying GameEvent.
20+
/// </summary>
21+
public TGameEvent GameEvent
22+
{
23+
get => gameEvent;
24+
set => gameEvent = value;
25+
}
26+
27+
/// <summary>
28+
/// Get or set the underlying UnityEvent.
29+
/// </summary>
30+
public UnityEvent OnGameEvent
31+
{
32+
get => onGameEvent;
33+
set => onGameEvent = value;
34+
}
35+
36+
public void RaiseGameEvent()
37+
{
38+
onGameEvent.Invoke();
39+
}
40+
1841
private void OnEnable()
1942
{
2043
if (gameEvent == null)
@@ -36,10 +59,5 @@ private void OnDisable()
3659

3760
gameEvent.UnregisterListener(this);
3861
}
39-
40-
public void OnGameEvent()
41-
{
42-
onGameEvent.Invoke();
43-
}
4462
}
4563
}

Runtime/GameEvents/Generic/IArgumentGameEventListener.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ public interface IArgumentGameEventListener<in TArgument>
55
/// <summary>
66
/// Trigger this listener with an argument.
77
/// </summary>
8-
void OnGameEvent(TArgument argument);
8+
void RaiseGameEvent(TArgument argument);
99
}
1010
}

Runtime/GameEvents/Generic/IGameEventListener.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ public interface IGameEventListener
55
/// <summary>
66
/// Trigger this listener.
77
/// </summary>
8-
void OnGameEvent();
8+
void RaiseGameEvent();
99
}
1010
}

Runtime/GameEvents/Vector2/Vector3GameEvent.cs renamed to Runtime/GameEvents/Vector2/Vector2GameEvent.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
namespace GameEvents.Vector2
55
{
66
[CreateAssetMenu(fileName = "Vector2GameEvent", menuName = "Game Events/Vector2 Game Event")]
7-
public class Vector3GameEvent : ArgumentGameEvent<UnityEngine.Vector2>
7+
public class Vector2GameEvent : ArgumentGameEvent<UnityEngine.Vector2>
88
{
99
}
1010
}

Runtime/GameEvents/Vector2/Vector2GameEventListener.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ namespace GameEvents.Vector2
55
{
66
[AddComponentMenu("Game Events/Vector2 Game Event Listener")]
77
public class Vector2GameEventListener
8-
: ArgumentGameEventListener<Vector3GameEvent, Vector2Event, UnityEngine.Vector2>
8+
: ArgumentGameEventListener<Vector2GameEvent, Vector2Event, UnityEngine.Vector2>
99
{
1010
}
1111
}

Runtime/MutableObjects/MutableObjectHandler.cs

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Collections.Generic;
1+
using System;
2+
using System.Collections.Generic;
23
using System.Linq;
34
using MutableObjects.Generic;
45
using UnityEngine;
@@ -8,31 +9,54 @@ namespace MutableObjects
89
{
910
public static class MutableObjectHandler
1011
{
12+
/// <summary>
13+
/// Default mutable object reset action.
14+
/// </summary>
15+
public static readonly Action<IEnumerable<IMutableObject>> DefaultOnReset =
16+
mutableObjects =>
17+
{
18+
foreach (var mutableObject in mutableObjects)
19+
{
20+
mutableObject.ResetValues();
21+
}
22+
};
23+
24+
/// <summary>
25+
/// Currently used mutable object reset action.
26+
/// </summary>
27+
public static Action<IEnumerable<IMutableObject>> OnReset { get; set; } = DefaultOnReset;
28+
29+
/// <summary>
30+
/// Reset mutable object asset values.
31+
/// </summary>
32+
public static void ResetMutableObjectValues(
33+
IEnumerable<IMutableObject> mutableObjects,
34+
bool excludePersisting = false
35+
)
36+
{
37+
if (excludePersisting)
38+
{
39+
mutableObjects = mutableObjects.Where(mutableObject => !mutableObject.Persisting);
40+
}
41+
42+
OnReset(mutableObjects);
43+
}
44+
1145
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
1246
private static void RuntimeInit()
1347
{
1448
SetInitialMutableObjectValues();
15-
SceneManager.sceneUnloaded += scene => ResetMutableObjectValues();
49+
SceneManager.activeSceneChanged += ResetMutableObjectValues;
1650
}
1751

1852
private static void SetInitialMutableObjectValues()
1953
{
20-
var mutableObjects = FindMutableObjects();
21-
foreach (var mutableObject in mutableObjects)
22-
{
23-
mutableObject.ResetValues();
24-
}
54+
ResetMutableObjectValues(FindMutableObjects());
2555
}
2656

27-
private static void ResetMutableObjectValues()
57+
private static void ResetMutableObjectValues(Scene curr, Scene next)
2858
{
29-
var mutableObjects = FindMutableObjects()
30-
.Where(mutableObject => !mutableObject.Persisting);
31-
32-
foreach (var mutableObject in mutableObjects)
33-
{
34-
mutableObject.ResetValues();
35-
}
59+
ResetMutableObjectValues(FindMutableObjects(), true);
3660
}
3761

3862
private static IEnumerable<IMutableObject> FindMutableObjects()

Tests.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Tests/Runtime.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"name": "Chark.UnityScriptableObjects.Tests",
3+
"references": [
4+
"UnityEngine.TestRunner",
5+
"Chark.UnityScriptableObjects"
6+
],
7+
"includePlatforms": [],
8+
"excludePlatforms": [],
9+
"allowUnsafeCode": false,
10+
"overrideReferences": true,
11+
"precompiledReferences": [
12+
"nunit.framework.dll"
13+
],
14+
"autoReferenced": false,
15+
"defineConstraints": [
16+
"UNITY_INCLUDE_TESTS"
17+
],
18+
"versionDefines": [],
19+
"noEngineReferences": false
20+
}

Tests/Runtime/Chark.UnityScriptableObjects.Tests.asmdef.meta

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Tests/Runtime/GameEvents.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)