Ejemplo n.º 1
0
        public void GetFunction_ShouldReturnInvocable(TestCaseData <MethodInfo> testCase)
        {
            var factory       = new DynamicMethodFactory();
            var methodRequest = DynamicMethodRequest.MakeRequest(testCase.Method);
            var method        = factory.GetFunction <int>(methodRequest);

            Assert.That(method, Is.Not.Null);

            var methodArgs = TestCaseHelper.GetArgs(testCase.Args, testCase.Instance);
            var result     = (int)method.Invoker.DynamicInvoke(methodArgs);

            Assert.That(result, Is.EqualTo(testCase.ExpectedResult));
        }
Ejemplo n.º 2
0
        public void GenerateProperty_ShouldGetValue()
        {
            var factory  = new DynamicMethodFactory();
            var instance = new TestClass()
            {
                PropertyTest = 3
            };
            var member = typeof(TestClass).GetProperty(nameof(TestClass.PropertyTest));

            var getRequest = DynamicMethodRequest.MakeRequest(member.GetMethod);
            var getMethod  = factory.GetFunction <int>(getRequest);
            var methodFunc = (Func <TestClass, int>)getMethod.Invoker;

            var result = methodFunc(instance);

            Assert.That(result, Is.EqualTo(instance.PropertyTest));
        }
Ejemplo n.º 3
0
        public void GenerateField_ShouldBeBidirectional(TestCaseData <FieldInfo> testCase)
        {
            var factory        = new DynamicMethodFactory();
            var expectedResult = testCase.Args.Last();

            var setRequest = DynamicMethodRequest.MakeSetterRequest(testCase.Method);
            var setMethod  = factory.GetAction(setRequest);
            var setArgs    = TestCaseHelper.GetArgs(testCase.Args, testCase.Instance);

            setMethod.Invoke(setArgs);

            var getRequest = DynamicMethodRequest.MakeGetterRequest(testCase.Method);
            var getMethod  = factory.GetFunction <int>(getRequest);
            var getArgs    = TestCaseHelper.GetArgs(null, testCase.Instance);
            var result     = getMethod.Invoke(getArgs);

            Assert.That(result, Is.EqualTo(expectedResult));
        }
Ejemplo n.º 4
0
        public void GenerateProperty_ShouldSetAndGetIndexer()
        {
            const string expectedKey   = "Hello";
            const string expectedValue = "World";

            var factory  = new DynamicMethodFactory();
            var instance = new TestClass();
            var member   = typeof(TestClass)
                           .GetProperties()
                           .First(o => o.GetIndexParameters().Any());

            var setRequest = DynamicMethodRequest.MakeRequest(member.SetMethod);
            var setMethod  = factory.GetAction(setRequest);

            setMethod.Invoke(instance, expectedKey, expectedValue);

            var getRequest = DynamicMethodRequest.MakeRequest(member.GetMethod);
            var getMethod  = factory.GetFunction <string>(getRequest);
            var value      = getMethod.Invoke(instance, expectedKey);

            Assert.That(value, Is.EqualTo(expectedValue));
        }