using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; using System.Reflection; using System.Text; using System.Threading.Tasks; using UnitTestSharp; namespace TestsForUnitTestSharp { public class LambdaHelperTests : TestFixture { private bool called = false; [IgnoreTest] void ExtractMe() { called = true; } [IgnoreTest] int ReturnValue() => 4; [IgnoreTest] void Overload(int x) { } [IgnoreTest] void Overload(string x) { } [IgnoreTest] void Generic(T x) { } public override void TestSetup() { called = false; } public void DoesNotActuallyCallMethod() { LambdaHelper.GetMethodInfoForWrappedCall(() => ExtractMe()); CheckFalse(called); } public void CanExtractFromAction() { var method = LambdaHelper.GetMethodInfoForWrappedCall(() => ExtractMe()); var expected = typeof(LambdaHelperTests).GetMethod( nameof(ExtractMe), BindingFlags.NonPublic | BindingFlags.Instance); CheckEqual(expected, method); } public void CanExtractFromActionWithArgument() { var method = LambdaHelper.GetMethodInfoForWrappedCall(fixture => fixture.ExtractMe()); var expected = typeof(LambdaHelperTests).GetMethod( nameof(ExtractMe), BindingFlags.NonPublic | BindingFlags.Instance); CheckEqual(expected, method); } public void CanExtractFromFunc() { var method = LambdaHelper.GetMethodInfoForWrappedCall(() => ReturnValue()); var expected = typeof(LambdaHelperTests).GetMethod( nameof(ReturnValue), BindingFlags.NonPublic | BindingFlags.Instance); CheckEqual(expected, method); } public void CanSpecifyOverload_Int() { var method = LambdaHelper.GetMethodInfoForWrappedCall(() => Overload(5)); var expected = typeof(LambdaHelperTests).GetMethod(nameof(Overload), BindingFlags.NonPublic | BindingFlags.Instance, binder: null, new Type[] { typeof(int) }, modifiers: null); CheckEqual(expected, method); } public void CanSpecifyOverload_String() { var method = LambdaHelper.GetMethodInfoForWrappedCall(() => Overload("")); var expected = typeof(LambdaHelperTests).GetMethod(nameof(Overload), BindingFlags.NonPublic | BindingFlags.Instance, binder: null, new Type[] { typeof(string) }, modifiers: null); CheckEqual(expected, method); } public void CanFindGenericMethod() { var method = LambdaHelper.GetMethodInfoForWrappedCall(() => Generic("")); var expected = typeof(LambdaHelperTests) .GetMethod(nameof(Generic), BindingFlags.NonPublic | BindingFlags.Instance) .MakeGenericMethod(new Type[] { typeof(string) }); CheckEqual(expected, method); } } }