public void TestGetObjectInstanceFromExpressionValueType() { DateTime dateTime = new DateTime(); Expression <Func <DateTime> > expression = () => dateTime.AddDays(2); Assert.ThrowsException <NotSupportedException>( () => ShimHelper.GetObjectInstanceOrType((expression.Body as MethodCallExpression).Object)); }
private static Shim ReplaceImpl <T>(Expression <T> expression, bool setter) { MethodBase methodBase = ShimHelper.GetMethodFromExpression(expression.Body, setter, out object instance); return(new Shim(methodBase, instance) { _setter = setter }); }
public static Shim Replace(Expression <Action> expression) { MethodCallExpression methodCall = expression.Body as MethodCallExpression; return(new Shim(methodCall.Method, null) { _instance = ShimHelper.GetObjectInstanceFromExpression(methodCall.Object) }); }
public void TestValidateReplacementMethodSignatureInValid() { MethodBase original = typeof(Console).GetMethod("WriteLine", new Type[] { typeof(string) }); MethodInfo replacement = new Action(() => { }).Method; Assert.AreEqual <bool>(false, ShimHelper.ValidateReplacementMethodSignature(original, replacement)); original = typeof(DateTime).GetMethod("Add"); Assert.AreEqual <bool>(false, ShimHelper.ValidateReplacementMethodSignature(original, replacement)); }
public void TestGetObjectInstanceFromExpression() { ShimHelperTests shimHelperTests = new ShimHelperTests(); Expression <Action> expression = () => shimHelperTests.TestGetObjectInstanceFromExpression(); var instance = ShimHelper.GetObjectInstanceOrType((expression.Body as MethodCallExpression).Object); Assert.IsNotNull(instance); Assert.AreEqual <Type>(typeof(ShimHelperTests), instance.GetType()); Assert.AreSame(shimHelperTests, instance); Assert.AreNotSame(new ShimHelperTests(), instance); }
public void TestGetMethodFromExpressionValid() { Expression <Func <DateTime> > expr = () => DateTime.Now; Expression <Func <string> > expr1 = () => ReadLine(); Assert.AreEqual <MethodBase>(typeof(DateTime).GetMethod("get_Now"), ShimHelper.GetMethodFromExpression(expr.Body, false, out Object instance)); Assert.AreEqual <MethodBase>(typeof(Console).GetMethod("ReadLine"), ShimHelper.GetMethodFromExpression(expr1.Body, false, out instance)); }
public void TestGetMethodFromExpressionThrowNotImplemented() { Expression <Func <bool> > expr = () => true; Expression <Func <DateTime> > expr1 = () => DateTime.MaxValue; Assert.ThrowsException <NotImplementedException>( () => ShimHelper.GetMethodFromExpression(expr.Body, false, out Object instance)); Assert.ThrowsException <NotImplementedException>( () => ShimHelper.GetMethodFromExpression(expr1.Body, false, out Object instance)); }
public void TestValidateReplacementMethodSignatureValid() { MethodBase original = typeof(Console).GetMethod("WriteLine", new Type[] { typeof(string) }); MethodInfo replacement = new Action <string>((s) => { }).Method; Assert.AreEqual <bool>(true, ShimHelper.ValidateReplacementMethodSignature(original, replacement)); original = typeof(string).GetMethod("Contains"); replacement = new Func <string, string, bool>((d, t) => true).Method; Assert.AreEqual <bool>(true, ShimHelper.ValidateReplacementMethodSignature(original, replacement)); }
private Shim WithImpl(Delegate replacement, bool isAuto = false) { _replacement = replacement; ShimHelper.ValidateReplacementMethodSignature(this._original, this._replacement.Method, _instance?.GetType() ?? _type, _setter, isAuto); return(this); }
public static Shim Replace <T>(Expression <Func <T> > expression) { return(new Shim(ShimHelper.GetMethodFromExpression(expression.Body, out object instance), null) { _instance = instance });
private Shim WithImpl(Delegate replacement) { ShimHelper.ValidateReplacementMethodSignature(this._original, replacement.Method, _instance?.GetType() ?? _type, _setter); _replacement = new ShimDelegate(replacement); return(this); }