Initial import
This commit is contained in:
1 parent
ea3f8549e4
commit
fa6feafba7
133 files changed
+17503
-4
No files matched your search
@@ -0,0 +1,58 @@
|
||||
using System;
|
||||
using System.Reflection;
|
||||
|
||||
namespace DelphiProjectHandler.Tests.Utilities
|
||||
{
|
||||
/// <summary>
|
||||
/// Runs static and non-static private methods.
|
||||
/// </summary>
|
||||
public class MethodRunner
|
||||
{
|
||||
/// <summary>
|
||||
/// Runs a private method of an object instance.
|
||||
/// </summary>
|
||||
/// <param name="iObject">Existing object instance.</param>
|
||||
/// <param name="iMethodName">Method to run.</param>
|
||||
/// <param name="iParams">Parameters for the method.</param>
|
||||
/// <returns>Result of running the method.</returns>
|
||||
public static object RunInstance(object iObject, string iMethodName, object[] iParams)
|
||||
{
|
||||
BindingFlags vFlags = BindingFlags.Instance;
|
||||
return RunMethod(iObject.GetType(), iObject, vFlags, iMethodName, iParams);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Runs a private static method of a class.
|
||||
/// </summary>
|
||||
/// <param name="iType">Class type.</param>
|
||||
/// <param name="iMethodName">Method to run.</param>
|
||||
/// <param name="iParams">Parameters for the method.</param>
|
||||
/// <returns>Result of running the method.</returns>
|
||||
public static object RunStatic(Type iType, string iMethodName, object[] iParams)
|
||||
{
|
||||
BindingFlags vFlags = BindingFlags.Static;
|
||||
return RunMethod(iType, null, vFlags, iMethodName, iParams);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Runs a private static or non-static method of a class or existing object instance.
|
||||
/// </summary>
|
||||
/// <param name="iType">Class type.</param>
|
||||
/// <param name="iObject">Existing object instance.</param>
|
||||
/// <param name="iBindingFlags">Flags used when searching for the method.
|
||||
/// By default BindingFlags.NonPublic and BindingFlags.Public will be
|
||||
/// added.</param>
|
||||
/// <param name="iMethodName">Method to run.</param>
|
||||
/// <param name="iParams">Parameters for the method.</param>
|
||||
/// <returns>Result of running the method.</returns>
|
||||
protected static object RunMethod(Type iType, object iObject, BindingFlags iBindingFlags, string iMethodName, params object[] iParams)
|
||||
{
|
||||
iBindingFlags = iBindingFlags | BindingFlags.NonPublic | BindingFlags.Public;
|
||||
MethodInfo vMethodInfo = iType.GetMethod(iMethodName, iBindingFlags);
|
||||
if (vMethodInfo == null)
|
||||
throw new ArgumentException("Private method " + iMethodName + " was not found in type " + iType.ToString());
|
||||
|
||||
return vMethodInfo.Invoke(iObject, iParams);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,93 @@
|
||||
using System;
|
||||
using NUnit.Framework;
|
||||
|
||||
namespace DelphiProjectHandler.Tests.Utilities
|
||||
{
|
||||
[TestFixture]
|
||||
public class MethodRunnerClassTests
|
||||
{
|
||||
protected class MethodRunnerTester
|
||||
{
|
||||
protected static int Calculate(int iLeft, int iRight, int iMultiplier)
|
||||
{
|
||||
return (iLeft + iRight) * iMultiplier;
|
||||
}
|
||||
|
||||
public MethodRunnerTester(int iOffset)
|
||||
{
|
||||
Offset = iOffset;
|
||||
}
|
||||
|
||||
public int Offset;
|
||||
|
||||
public void VoidFunction(int iLeft, int iRight, ref int ivTotal)
|
||||
{
|
||||
ivTotal = Calculate(iLeft, iRight, 2) + Offset;
|
||||
}
|
||||
|
||||
public int Function(int iLeft, int iRight)
|
||||
{
|
||||
return Calculate(iLeft, iRight, 3) + Offset;
|
||||
}
|
||||
|
||||
public static void StaticVoidFunction(int iLeft, int iRight, ref int ivTotal)
|
||||
{
|
||||
ivTotal = Calculate(iLeft, iRight, 4);
|
||||
}
|
||||
|
||||
public static int StaticFunction(int iLeft, int iRight)
|
||||
{
|
||||
return Calculate(iLeft, iRight, 5);
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void RunInstanceFunction()
|
||||
{
|
||||
MethodRunnerTester vTester = new MethodRunnerTester(1);
|
||||
int vActual = (int)MethodRunner.RunInstance(vTester, "Function", new object[] { 2, 3 });
|
||||
Assert.AreEqual(16, vActual, "Wrong result for instance function");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void RunInstanceVoidFunction()
|
||||
{
|
||||
MethodRunnerTester vTester = new MethodRunnerTester(2);
|
||||
int vActual = 0;
|
||||
object[] vParams = new object[] { 2, 3, vActual };
|
||||
MethodRunner.RunInstance(vTester, "VoidFunction", vParams);
|
||||
Assert.AreEqual(12, vParams[2], "Wrong result for void instance function");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void RunStaticFunction()
|
||||
{
|
||||
int vActual = (int)MethodRunner.RunStatic(typeof(MethodRunnerTester), "StaticFunction", new object[] { 2, 3 });
|
||||
Assert.AreEqual(25, vActual, "Wrong result for instance function");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void RunStaticVoidFunction()
|
||||
{
|
||||
int vActual = 0;
|
||||
object[] vParams = new object[] { 2, 3, vActual };
|
||||
MethodRunner.RunStatic(typeof(MethodRunnerTester), "StaticVoidFunction", vParams);
|
||||
Assert.AreEqual(20, vParams[2], "Wrong result for instance function");
|
||||
}
|
||||
|
||||
[Test]
|
||||
[ExpectedException(typeof(ArgumentException))]
|
||||
public void RunNonExistingMethod()
|
||||
{
|
||||
MethodRunnerTester vTester = new MethodRunnerTester(1);
|
||||
MethodRunner.RunInstance(vTester, "NonExistingMethod", null);
|
||||
}
|
||||
|
||||
[Test]
|
||||
[ExpectedException(typeof(ArgumentException))]
|
||||
public void RunNonExistingStaticMethod()
|
||||
{
|
||||
MethodRunner.RunStatic(typeof(MethodRunnerTester), "NonExistingMethod", null);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user