Пример #1
0
        public void TestGetObjectInstanceFromExpressionValueType()
        {
            DateTime dateTime = new DateTime();
            Expression <Func <DateTime> > expression = () => dateTime.AddDays(2);

            Assert.ThrowsException <NotSupportedException>(
                () => ShimHelper.GetObjectInstanceOrType((expression.Body as MethodCallExpression).Object));
        }
Пример #2
0
        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
            });
        }
Пример #3
0
        public static Shim Replace(Expression <Action> expression)
        {
            MethodCallExpression methodCall = expression.Body as MethodCallExpression;

            return(new Shim(methodCall.Method, null)
            {
                _instance = ShimHelper.GetObjectInstanceFromExpression(methodCall.Object)
            });
        }
Пример #4
0
        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));
        }
Пример #5
0
        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);
        }
Пример #6
0
        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));
        }
Пример #7
0
        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));
        }
Пример #8
0
        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));
        }
Пример #9
0
 private Shim WithImpl(Delegate replacement, bool isAuto = false)
 {
     _replacement = replacement;
     ShimHelper.ValidateReplacementMethodSignature(this._original, this._replacement.Method, _instance?.GetType() ?? _type, _setter, isAuto);
     return(this);
 }
Пример #10
0
 public static Shim Replace <T>(Expression <Func <T> > expression)
 {
     return(new Shim(ShimHelper.GetMethodFromExpression(expression.Body, out object instance), null)
     {
         _instance = instance
     });
Пример #11
0
 private Shim WithImpl(Delegate replacement)
 {
     ShimHelper.ValidateReplacementMethodSignature(this._original, replacement.Method, _instance?.GetType() ?? _type, _setter);
     _replacement = new ShimDelegate(replacement);
     return(this);
 }