Skip to content

Commit

Permalink
Tickable method can not have arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
mewlist committed Feb 22, 2024
1 parent 969870e commit c081ca0
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 8 deletions.
2 changes: 0 additions & 2 deletions Runtime/Attribute/TickableAttribute.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System;
using Mew.Core;
using UnityEngine.PlayerLoop;

namespace Doinject
{
Expand Down
2 changes: 2 additions & 0 deletions Runtime/TargetMethodsInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ public TargetMethodsInfo(Type targetType)
var tickableAttr = methodInfo.GetCustomAttribute(typeof(TickableAttribute), true) as TickableAttribute;
if (!methodInfo.IsPublic)
throw new Exception($"Tickable method must be public. {targetType.Name}.{methodInfo.Name}()");
if (methodInfo.GetParameters().Length > 0)
throw new Exception("Tickable method should not have any parameters");
if (!TickableMethods.TryGetValue(tickableAttr.Timing, out var tickableMethods))
TickableMethods[tickableAttr.Timing] = new List<MethodInfo>();
TickableMethods[tickableAttr.Timing].Add(methodInfo);
Expand Down
8 changes: 8 additions & 0 deletions Tests/TestObjects.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,14 @@ public class TickableObject
}
}

public class InvalidTickableObject
{
[Tickable]
public void PostLateUpdate(int arg)
{
}
}

public class FieldInjectionWithNonPublicObject
{
[Inject] private InjectedObject injectedObject;
Expand Down
24 changes: 18 additions & 6 deletions Tests/TickableTest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Threading.Tasks;
using System;
using System.Threading.Tasks;
using Mew.Core.TaskHelpers;
using NUnit.Framework;
using UnityEngine;
Expand Down Expand Up @@ -48,12 +49,23 @@ public async Task UpdateTimingTest()
Assert.That(instance.UpdateCount, Is.GreaterThan(8));
Assert.That(instance.PreLateUpdateCount, Is.GreaterThan(8));
Assert.That(instance.PostLateUpdateCount, Is.GreaterThan(8));

Assert.That(instance.UpdateCount, Is.EqualTo(instance.EarlyUpdateCount + 1));
Assert.That(instance.UpdateCount, Is.EqualTo(instance.PreUpdateCount + 1));
Assert.That(instance.UpdateCount, Is.EqualTo(instance.PreLateUpdateCount + 1));
Assert.That(instance.UpdateCount, Is.EqualTo(instance.PostLateUpdateCount + 1));
Assert.That(instance.FixedUpdateCount, Is.GreaterThan(instance.UpdateCount));
}

[Test]
public async Task InvalidTickableTest()
{
try
{
container.Bind<InvalidTickableObject>();
var _ = await container.ResolveAsync<InvalidTickableObject>();
}
catch (Exception e)
{
if (e.Message.Contains("Tickable method should not have any parameters"))
Assert.Pass();
}
Assert.Fail();
}
}
}

0 comments on commit c081ca0

Please sign in to comment.