Client manager components
- ReflectedManager now supports MemberInfo and event replacement - New chat manager interfaces for both client and server - New multiplayer manager interfaces for both client and server
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Reflection;
|
||||
using Torch.Utils;
|
||||
using Xunit;
|
||||
|
||||
@@ -11,7 +12,7 @@ namespace Torch.Tests
|
||||
{
|
||||
TestUtils.Init();
|
||||
}
|
||||
|
||||
|
||||
private static ReflectionTestManager _manager = new ReflectionTestManager().Init(typeof(ReflectionTestBinding));
|
||||
public static IEnumerable<object[]> Getters => _manager.Getters;
|
||||
|
||||
@@ -19,6 +20,10 @@ namespace Torch.Tests
|
||||
|
||||
public static IEnumerable<object[]> Invokers => _manager.Invokers;
|
||||
|
||||
public static IEnumerable<object[]> MemberInfo => _manager.MemberInfo;
|
||||
|
||||
public static IEnumerable<object[]> Events => _manager.Events;
|
||||
|
||||
#region Binding
|
||||
[Theory]
|
||||
[MemberData(nameof(Getters))]
|
||||
@@ -52,6 +57,28 @@ namespace Torch.Tests
|
||||
if (field.Field.IsStatic)
|
||||
Assert.NotNull(field.Field.GetValue(null));
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(MemberInfo))]
|
||||
public void TestBindingMemberInfo(ReflectionTestManager.FieldRef field)
|
||||
{
|
||||
if (field.Field == null)
|
||||
return;
|
||||
Assert.True(ReflectedManager.Process(field.Field));
|
||||
if (field.Field.IsStatic)
|
||||
Assert.NotNull(field.Field.GetValue(null));
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(Events))]
|
||||
public void TestBindingEvents(ReflectionTestManager.FieldRef field)
|
||||
{
|
||||
if (field.Field == null)
|
||||
return;
|
||||
Assert.True(ReflectedManager.Process(field.Field));
|
||||
if (field.Field.IsStatic)
|
||||
Assert.NotNull(field.Field.GetValue(null));
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Results
|
||||
@@ -79,10 +106,51 @@ namespace Torch.Tests
|
||||
{
|
||||
return k >= 0;
|
||||
}
|
||||
|
||||
public event Action Event1;
|
||||
|
||||
public ReflectionTestTarget()
|
||||
{
|
||||
Event1 += Callback1;
|
||||
}
|
||||
|
||||
public bool Callback1Flag = false;
|
||||
public void Callback1()
|
||||
{
|
||||
Callback1Flag = true;
|
||||
}
|
||||
public bool Callback2Flag = false;
|
||||
public void Callback2()
|
||||
{
|
||||
Callback2Flag = true;
|
||||
}
|
||||
|
||||
public void RaiseEvent()
|
||||
{
|
||||
Event1?.Invoke();
|
||||
}
|
||||
}
|
||||
|
||||
private class ReflectionTestBinding
|
||||
{
|
||||
#region Instance
|
||||
#region MemberInfo
|
||||
[ReflectedFieldInfo(typeof(ReflectionTestTarget), "TestField")]
|
||||
public static FieldInfo TestFieldInfo;
|
||||
|
||||
[ReflectedPropertyInfo(typeof(ReflectionTestTarget), "TestProperty")]
|
||||
public static PropertyInfo TestPropertyInfo;
|
||||
|
||||
[ReflectedMethodInfo(typeof(ReflectionTestTarget), "TestCall")]
|
||||
public static MethodInfo TestMethodInfoGeneral;
|
||||
|
||||
[ReflectedMethodInfo(typeof(ReflectionTestTarget), "TestCall", Parameters = new[] { typeof(int) })]
|
||||
public static MethodInfo TestMethodInfoExplicitArgs;
|
||||
|
||||
[ReflectedMethodInfo(typeof(ReflectionTestTarget), "TestCall", ReturnType = typeof(bool))]
|
||||
public static MethodInfo TestMethodInfoExplicitReturn;
|
||||
#endregion
|
||||
|
||||
[ReflectedGetter(Name = "TestField")]
|
||||
public static Func<ReflectionTestTarget, int> TestFieldGetter;
|
||||
[ReflectedSetter(Name = "TestField")]
|
||||
@@ -96,7 +164,27 @@ namespace Torch.Tests
|
||||
[ReflectedMethod]
|
||||
public static Func<ReflectionTestTarget, int, bool> TestCall;
|
||||
|
||||
[ReflectedEventReplace(typeof(ReflectionTestTarget), "Event1", typeof(ReflectionTestTarget), "Callback1")]
|
||||
public static Func<ReflectedEventReplacer> TestEventReplacer;
|
||||
#endregion
|
||||
|
||||
#region Static
|
||||
#region MemberInfo
|
||||
[ReflectedFieldInfo(typeof(ReflectionTestTarget), "TestFieldStatic")]
|
||||
public static FieldInfo TestStaticFieldInfo;
|
||||
|
||||
[ReflectedPropertyInfo(typeof(ReflectionTestTarget), "TestPropertyStatic")]
|
||||
public static PropertyInfo TestStaticPropertyInfo;
|
||||
|
||||
[ReflectedMethodInfo(typeof(ReflectionTestTarget), "TestCallStatic")]
|
||||
public static MethodInfo TestStaticMethodInfoGeneral;
|
||||
|
||||
[ReflectedMethodInfo(typeof(ReflectionTestTarget), "TestCallStatic", Parameters = new[] { typeof(int) })]
|
||||
public static MethodInfo TestStaticMethodInfoExplicitArgs;
|
||||
|
||||
[ReflectedMethodInfo(typeof(ReflectionTestTarget), "TestCallStatic", ReturnType = typeof(bool))]
|
||||
public static MethodInfo TestStaticMethodInfoExplicitReturn;
|
||||
#endregion
|
||||
[ReflectedGetter(Name = "TestFieldStatic", Type = typeof(ReflectionTestTarget))]
|
||||
public static Func<int> TestStaticFieldGetter;
|
||||
[ReflectedSetter(Name = "TestFieldStatic", Type = typeof(ReflectionTestTarget))]
|
||||
@@ -109,6 +197,7 @@ namespace Torch.Tests
|
||||
|
||||
[ReflectedStaticMethod(Type = typeof(ReflectionTestTarget))]
|
||||
public static Func<int, bool> TestCallStatic;
|
||||
#endregion
|
||||
}
|
||||
#endregion
|
||||
|
||||
@@ -215,7 +304,33 @@ namespace Torch.Tests
|
||||
Assert.True(ReflectionTestBinding.TestCallStatic.Invoke(1));
|
||||
Assert.False(ReflectionTestBinding.TestCallStatic.Invoke(-1));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void TestInstanceEventReplace()
|
||||
{
|
||||
var target = new ReflectionTestTarget();
|
||||
target.Callback1Flag = false;
|
||||
target.RaiseEvent();
|
||||
Assert.True(target.Callback1Flag, "Control test failed");
|
||||
|
||||
target.Callback1Flag = false;
|
||||
target.Callback2Flag = false;
|
||||
ReflectedEventReplacer binder = ReflectionTestBinding.TestEventReplacer.Invoke();
|
||||
Assert.True(binder.Test(target), "Binder was unable to find the requested method");
|
||||
|
||||
binder.Replace(new Action(() => target.Callback2()), target);
|
||||
target.RaiseEvent();
|
||||
Assert.True(target.Callback2Flag, "Substitute callback wasn't called");
|
||||
Assert.False(target.Callback1Flag, "Original callback wasn't removed");
|
||||
|
||||
target.Callback1Flag = false;
|
||||
target.Callback2Flag = false;
|
||||
binder.Restore(target);
|
||||
target.RaiseEvent();
|
||||
Assert.False(target.Callback2Flag, "Substitute callback wasn't removed");
|
||||
Assert.True(target.Callback1Flag, "Original callback wasn't restored");
|
||||
}
|
||||
#endregion
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -28,12 +28,16 @@ namespace Torch.Tests
|
||||
private readonly HashSet<object[]> _getters = new HashSet<object[]>();
|
||||
private readonly HashSet<object[]> _setters = new HashSet<object[]>();
|
||||
private readonly HashSet<object[]> _invokers = new HashSet<object[]>();
|
||||
private readonly HashSet<object[]> _memberInfo = new HashSet<object[]>();
|
||||
private readonly HashSet<object[]> _events = new HashSet<object[]>();
|
||||
|
||||
public ReflectionTestManager()
|
||||
{
|
||||
_getters.Add(new object[] { new FieldRef(null) });
|
||||
_setters.Add(new object[] { new FieldRef(null) });
|
||||
_invokers.Add(new object[] { new FieldRef(null) });
|
||||
_memberInfo.Add(new object[] {new FieldRef(null)});
|
||||
_events.Add(new object[] {new FieldRef(null)});
|
||||
}
|
||||
|
||||
public ReflectionTestManager Init(Assembly asm)
|
||||
@@ -50,12 +54,36 @@ namespace Torch.Tests
|
||||
BindingFlags.Public |
|
||||
BindingFlags.NonPublic))
|
||||
{
|
||||
if (field.GetCustomAttribute<ReflectedMethodAttribute>() != null)
|
||||
_invokers.Add(new object[] { new FieldRef(field) });
|
||||
if (field.GetCustomAttribute<ReflectedGetterAttribute>() != null)
|
||||
_getters.Add(new object[] { new FieldRef(field) });
|
||||
if (field.GetCustomAttribute<ReflectedSetterAttribute>() != null)
|
||||
_setters.Add(new object[] { new FieldRef(field) });
|
||||
var args = new object[] { new FieldRef(field) };
|
||||
foreach (ReflectedMemberAttribute attr in field.GetCustomAttributes<ReflectedMemberAttribute>())
|
||||
{
|
||||
if (!field.IsStatic)
|
||||
throw new ArgumentException("Field must be static to be reflected");
|
||||
switch (attr)
|
||||
{
|
||||
case ReflectedMethodAttribute rma:
|
||||
_invokers.Add(args);
|
||||
break;
|
||||
case ReflectedGetterAttribute rga:
|
||||
_getters.Add(args);
|
||||
break;
|
||||
case ReflectedSetterAttribute rsa:
|
||||
_setters.Add(args);
|
||||
break;
|
||||
case ReflectedFieldInfoAttribute rfia:
|
||||
case ReflectedPropertyInfoAttribute rpia:
|
||||
case ReflectedMethodInfoAttribute rmia:
|
||||
_memberInfo.Add(args);
|
||||
break;
|
||||
}
|
||||
}
|
||||
var reflectedEventReplacer = field.GetCustomAttribute<ReflectedEventReplaceAttribute>();
|
||||
if (reflectedEventReplacer != null)
|
||||
{
|
||||
if (!field.IsStatic)
|
||||
throw new ArgumentException("Field must be static to be reflected");
|
||||
_events.Add(args);
|
||||
}
|
||||
}
|
||||
return this;
|
||||
}
|
||||
@@ -66,6 +94,10 @@ namespace Torch.Tests
|
||||
|
||||
public IEnumerable<object[]> Invokers => _invokers;
|
||||
|
||||
public IEnumerable<object[]> MemberInfo => _memberInfo;
|
||||
|
||||
public IEnumerable<object[]> Events => _events;
|
||||
|
||||
#endregion
|
||||
|
||||
}
|
||||
|
@@ -27,6 +27,10 @@ namespace Torch.Tests
|
||||
|
||||
public static IEnumerable<object[]> Invokers => Manager().Invokers;
|
||||
|
||||
public static IEnumerable<object[]> MemberInfo => Manager().MemberInfo;
|
||||
|
||||
public static IEnumerable<object[]> Events => Manager().Events;
|
||||
|
||||
#region Binding
|
||||
[Theory]
|
||||
[MemberData(nameof(Getters))]
|
||||
@@ -60,6 +64,28 @@ namespace Torch.Tests
|
||||
if (field.Field.IsStatic)
|
||||
Assert.NotNull(field.Field.GetValue(null));
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(MemberInfo))]
|
||||
public void TestBindingMemberInfo(ReflectionTestManager.FieldRef field)
|
||||
{
|
||||
if (field.Field == null)
|
||||
return;
|
||||
Assert.True(ReflectedManager.Process(field.Field));
|
||||
if (field.Field.IsStatic)
|
||||
Assert.NotNull(field.Field.GetValue(null));
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(Events))]
|
||||
public void TestBindingEvents(ReflectionTestManager.FieldRef field)
|
||||
{
|
||||
if (field.Field == null)
|
||||
return;
|
||||
Assert.True(ReflectedManager.Process(field.Field));
|
||||
if (field.Field.IsStatic)
|
||||
Assert.NotNull(field.Field.GetValue(null));
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user