Skip to content

Commit

Permalink
Merge pull request #42 from bazyleu/feature/tests-refactoring
Browse files Browse the repository at this point in the history
Major test refactoring and updates
  • Loading branch information
bazyleu authored Sep 11, 2024
2 parents 55886b9 + e28f58f commit a9af1a0
Show file tree
Hide file tree
Showing 66 changed files with 594 additions and 299 deletions.
3 changes: 3 additions & 0 deletions Assets/UniStateTests/Common/Logger.meta

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

3 changes: 3 additions & 0 deletions Assets/UniStateTests/Common/StateMachine.meta

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using UniState;

namespace UniStateTests.Common
{
public interface IVerifiableStateMachine : IStateMachine
{
public void Verify();
}
}

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

22 changes: 22 additions & 0 deletions Assets/UniStateTests/Common/StateMachine/StateMachineTestHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System.Threading;
using Cysharp.Threading.Tasks;
using UniState;

namespace UniStateTests.Common
{
public static class StateMachineTestHelper
{
public static async UniTask RunAndVerify<TStateMachine, TState>(ITypeResolver typeResolver,
CancellationToken cancellationToken)
where TStateMachine : class, IStateMachine, IVerifiableStateMachine
where TState : class, IState<EmptyPayload>
{
var stateMachine =
StateMachineHelper.CreateStateMachine<TStateMachine, IVerifiableStateMachine>(
typeResolver);
await stateMachine.Execute<TState>(cancellationToken);

stateMachine.Verify();
}
}
}

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

28 changes: 28 additions & 0 deletions Assets/UniStateTests/Common/StateMachine/VerifiableStateMachine.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System;
using NUnit.Framework;
using UniState;

namespace UniStateTests.Common
{
public abstract class VerifiableStateMachine : StateMachine, IVerifiableStateMachine
{
private readonly ExecutionLogger _logger;

protected abstract string ExpectedLog { get; }

protected VerifiableStateMachine(ExecutionLogger logger)
{
_logger = logger;
}

protected override void OnError(Exception exception, StateMachineErrorType phase)
{
throw new Exception($"StateMachine OnError. Current log: {_logger.FinishLogging()}", exception);
}

public void Verify()
{
Assert.AreEqual(ExpectedLog, _logger.FinishLogging());
}
}
}

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

3 changes: 3 additions & 0 deletions Assets/UniStateTests/Common/TestFixture.meta

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

Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,30 @@
using NUnit.Framework;
using Zenject;

namespace UniStateTests.PlayMode.Common
namespace UniStateTests.Common
{
public abstract class ZenjectTestsBase
public abstract class TestsBase
{
private const int TimeoutSec = 5;

private CancellationTokenSource _ctx;
private IDisposable _timeoutSlim;
private DiContainer _container;

protected DiContainer Container => _container;


[SetUp]
public virtual void Setup()
{
_container = new DiContainer(StaticContext.Container);

SetupBindings();
}

[TearDown]
public virtual void Teardown()
public virtual void TearDown()
{
_ctx?.Cancel();
_ctx?.Dispose();
_ctx = null;

_timeoutSlim?.Dispose();
_timeoutSlim = null;

StaticContext.Clear();
}

protected CancellationToken GetTimeoutToken()
Expand All @@ -49,9 +41,5 @@ protected CancellationToken GetTimeoutToken()

return _ctx.Token;
}

protected virtual void SetupBindings()
{
}
}
}
3 changes: 3 additions & 0 deletions Assets/UniStateTests/Common/TestFixture/TestsBase.cs.meta

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

50 changes: 50 additions & 0 deletions Assets/UniStateTests/Common/TestFixture/VContainerTestsBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using Cysharp.Threading.Tasks;
using UniState;
using UnityEngine;
using VContainer;
using VContainer.Unity;

namespace UniStateTests.Common
{
public abstract class VContainerTestsBase : TestsBase
{
private GameObject _containerHolder;
private IObjectResolver _objectResolver;

protected IObjectResolver Container => _objectResolver;

public override void Setup()
{
base.Setup();

_containerHolder = new GameObject("container");
var component = _containerHolder.AddComponent<TestsLifetimeScope>();
var testScope = component.CreateChild(SetupBindings);
_objectResolver = testScope.Container;
}

public override void TearDown()
{
base.TearDown();

Object.Destroy(_containerHolder);
}

protected async UniTask RunAndVerify<TStateMachine, TState>()
where TStateMachine : class, IStateMachine, IVerifiableStateMachine
where TState : class, IState<EmptyPayload>
{
await StateMachineTestHelper.RunAndVerify<TStateMachine, TState>(Container.ToTypeResolver(),
GetTimeoutToken());
}

private class TestsLifetimeScope : LifetimeScope
{
}

protected virtual void SetupBindings(IContainerBuilder builder)
{
builder.Register<ExecutionLogger>(Lifetime.Singleton);
}
}
}

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

42 changes: 42 additions & 0 deletions Assets/UniStateTests/Common/TestFixture/ZenjectTestsBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using Cysharp.Threading.Tasks;
using UniState;
using Zenject;

namespace UniStateTests.Common
{
public abstract class ZenjectTestsBase : TestsBase
{
private DiContainer _container;

protected DiContainer Container => _container;

public override void Setup()
{
base.Setup();

_container = new DiContainer(StaticContext.Container);

SetupBindings(Container);
}

public override void TearDown()
{
base.TearDown();

StaticContext.Clear();
}

protected async UniTask RunAndVerify<TStateMachine, TState>()
where TStateMachine : class, IStateMachine, IVerifiableStateMachine
where TState : class, IState<EmptyPayload>
{
await StateMachineTestHelper.RunAndVerify<TStateMachine, TState>(Container.ToTypeResolver(),
GetTimeoutToken());
}

protected virtual void SetupBindings(DiContainer container)
{
container.BindInterfacesAndSelfTo<ExecutionLogger>().AsSingle();
}
}
}
5 changes: 4 additions & 1 deletion Assets/UniStateTests/Common/UniStateTests.Common.asmdef
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
{
"name": "UniStateTests.Common",
"references": [
"UniTask"
"UniState",
"UniTask",
"VContainer",
"Zenject"
],
"excludePlatforms": [],
"allowUnsafeCode": false,
Expand Down
21 changes: 12 additions & 9 deletions Assets/UniStateTests/EditMode/States/StateDisposablesTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
using Cysharp.Threading.Tasks;
using NUnit.Framework;
using UniState;
using UniStateTests.Common;
using Zenject;

namespace UniStateTests.EditMode.Common
{
[TestFixture]
internal class StateDisposablesTests
internal class StateDisposablesTests: ZenjectTestsBase
{
private class DisposablesState : StateBase<IList<IDisposable>>
{
Expand Down Expand Up @@ -37,7 +38,7 @@ public override UniTask<StateTransitionInfo> Execute(CancellationToken token)
[Test]
public void Execute_WithException_DisposesInternalList() => CheckDisposeIsCalled<ExceptionDisposableState>();

private static void CheckDisposeIsCalled<TState>()
private void CheckDisposeIsCalled<TState>()
where TState : DisposablesState
{
var disposedObjects = 0;
Expand All @@ -52,17 +53,19 @@ private static void CheckDisposeIsCalled<TState>()
Assert.AreEqual(disposedObjects, 2);
}

private static void ExecuteState<TState>(IList<IDisposable> disposables)
private void ExecuteState<TState>(IList<IDisposable> disposables)
where TState: DisposablesState
{
var container = new DiContainer(StaticContext.Container);

container.Bind<StateMachine>().ToSelf().AsTransient();
container.Bind<TState>().ToSelf().AsTransient();

var stateMachine = StateMachineHelper.CreateStateMachine<StateMachine>(container.ToTypeResolver());
var stateMachine = StateMachineHelper.CreateStateMachine<StateMachine>(Container.ToTypeResolver());

stateMachine.Execute<TState, IList<IDisposable>>(disposables, default).GetAwaiter().GetResult();
}

protected override void SetupBindings(DiContainer container)
{
container.BindStateMachine<StateMachine>();
container.BindState<DisposablesState>();
container.BindState<ExceptionDisposableState>();
}
}
}
3 changes: 0 additions & 3 deletions Assets/UniStateTests/PlayMode/Common.meta

This file was deleted.

51 changes: 0 additions & 51 deletions Assets/UniStateTests/PlayMode/GoBackTests.cs

This file was deleted.

File renamed without changes.
Loading

0 comments on commit a9af1a0

Please sign in to comment.