Exemplo n.º 1
0
        public void CreateConvertExpression_MultipleCases_ConvertsCorrectly(
            Type sourceType,
            Type targetType,
            string sourcePropertyName,
            string targetPropertyName,
            object sourcePropertyValue)
        {
            var target       = new PropertyConverter();
            var fixture      = new Fixture();
            var createMethod = fixture.GetType().GetMethod("CreateAnonymous");
            var sourceObject = Activator.CreateInstance(sourceType);
            var targetObject = Activator.CreateInstance(targetType);
            var sourceParam  = Expression.Parameter(sourceType);
            var targetParam  = Expression.Parameter(targetType);
            var sourceProp   = sourceType.GetProperty(sourcePropertyName);
            var targetProp   = targetType.GetProperty(targetPropertyName);

            if (sourcePropertyValue != null)
            {
                sourceProp.SetValue(sourceObject, sourcePropertyValue, null);
            }

            var expr = target.CreateConvertExpression(
                sourceProp,
                targetProp,
                sourceParam,
                targetParam);

            var actionType = typeof(MappingAction <,>).MakeGenericType(sourceType, targetType);
            var lambda     = Expression.Lambda(actionType, expr, sourceParam, targetParam);
            var action     = lambda.Compile();

            action.DynamicInvoke(sourceObject, targetObject);
            var resultProp = targetProp.GetValue(targetObject, null);

            resultProp.ShouldBe(sourcePropertyValue);
        }