public void CreateConstructor()
        {
            var attributes = MethodAttributes.Public;
            var parameters =
                new[]
            {
                ParameterDeclarationObjectMother.Create(typeof(string), "param1"),
                ParameterDeclarationObjectMother.Create(typeof(int), "param2")
            };
            var fakeBody = ExpressionTreeObjectMother.GetSomeExpression(typeof(object));
            Func <ConstructorBodyCreationContext, Expression> bodyProvider = ctx =>
            {
                Assert.That(ctx.This.Type, Is.SameAs(_mutableType));
                Assert.That(ctx.IsStatic, Is.False);
                Assert.That(ctx.Parameters.Select(p => p.Type), Is.EqualTo(new[] { typeof(string), typeof(int) }));
                Assert.That(ctx.Parameters.Select(p => p.Name), Is.EqualTo(new[] { "param1", "param2" }));

                return(fakeBody);
            };

            var ctor = _factory.CreateConstructor(_mutableType, attributes, parameters.AsOneTime(), bodyProvider);

            Assert.That(ctor.DeclaringType, Is.SameAs(_mutableType));
            Assert.That(ctor.Name, Is.EqualTo(".ctor"));
            Assert.That(ctor.Attributes, Is.EqualTo(attributes | MethodAttributes.SpecialName | MethodAttributes.RTSpecialName));
            var expectedParameterInfos =
                new[]
            {
                new { ParameterType = parameters[0].Type },
                new { ParameterType = parameters[1].Type }
            };
            var actualParameterInfos = ctor.GetParameters().Select(pi => new { pi.ParameterType });

            Assert.That(actualParameterInfos, Is.EqualTo(expectedParameterInfos));
            var expectedBody = Expression.Block(typeof(void), fakeBody);

            ExpressionTreeComparer.CheckAreEqualTrees(expectedBody, ctor.Body);
        }
Exemple #2
0
        public void NullablePrimitiveCollection()
        {
            //DateTime is not managed
            var nullableTypes = new Type[]
            {
                // typeof( bool? ),//Bool flattens value to 0 or 1 so hashset differ too much. change the verifier to take care of conversions
                typeof(char?),
                typeof(sbyte?),
                typeof(byte?),
                typeof(int?),
                typeof(uint?),
                typeof(int?),
                typeof(uint?),
                typeof(int?),
                typeof(uint?),
                typeof(float?),
                typeof(double?),
                typeof(decimal? ),
                //typeof( string )
            };

            foreach (var sourceElementType in nullableTypes)
            {
                foreach (var targetElementType in nullableTypes)
                {
                    if (sourceElementType == typeof(char?) &&
                        targetElementType == typeof(bool?))
                    {
                        continue;
                    }

                    if (sourceElementType == typeof(bool?) &&
                        targetElementType == typeof(char?))
                    {
                        continue;
                    }

                    //for the following pairs a conversion is known
                    //to be harder (not possible or convention-based),
                    //so here we just skip that few cases
                    if (sourceElementType == typeof(bool?) &&
                        targetElementType == typeof(string))
                    {
                        continue;
                    }

                    if (sourceElementType == typeof(string) &&
                        targetElementType == typeof(bool?))
                    {
                        continue;
                    }

                    var sourceType = typeof(GenericCollections <>)
                                     .MakeGenericType(sourceElementType);

                    var targetType = typeof(GenericCollections <>)
                                     .MakeGenericType(targetElementType);

                    var sourceTypeCtor = ConstructorFactory.CreateConstructor <bool, uint, uint>(sourceType);
                    var targetTypeCtor = ConstructorFactory.CreateConstructor <bool, uint, uint>(targetType);

                    var source = sourceTypeCtor(true, 0, 10);
                    var target = targetTypeCtor(true, 0, 10);

                    var ultraMapper = new Mapper();
                    ultraMapper.Map(source, target);

                    bool isResultOk = ultraMapper.VerifyMapperResult(source, target);
                    Assert.IsTrue(isResultOk);
                }
            }
        }
Exemple #3
0
        public void PrimitiveCollection()
        {
            var excludeTypes = new TypeCode[]
            {
                TypeCode.Empty,
                TypeCode.DBNull,
                TypeCode.DateTime, //DateTime is not managed
                TypeCode.Object,
                TypeCode.Boolean,  //Bool flattens value to 0 or 1 so hashset differ too much. change the verifier to take care of conversions
            };

            var types = Enum.GetValues(typeof(TypeCode)).Cast <TypeCode>()
                        .Except(excludeTypes)
                        .Select(typeCode => TypeExtensions.GetType(typeCode)).ToList();

            foreach (var sourceElementType in types)
            {
                foreach (var targetElementType in types)
                {
                    //for the following pairs a conversion is known
                    //to be harder (not possible or convention-based),
                    //so here we just skip that few cases

                    if (sourceElementType == typeof(string) &&
                        targetElementType == typeof(bool))
                    {
                        continue;
                    }

                    if (sourceElementType == typeof(char) &&
                        targetElementType == typeof(bool))
                    {
                        continue;
                    }

                    if (sourceElementType == typeof(bool) &&
                        targetElementType == typeof(char))
                    {
                        continue;
                    }


                    var sourceType = typeof(GenericCollections <>)
                                     .MakeGenericType(sourceElementType);

                    var targetType = typeof(GenericCollections <>)
                                     .MakeGenericType(targetElementType);

                    var sourceTypeCtor = ConstructorFactory.CreateConstructor <bool, uint, uint>(sourceType);
                    var targetTypeCtor = ConstructorFactory.CreateConstructor <bool, uint, uint>(targetType);

                    var source = sourceTypeCtor(true, 0, 10);
                    var target = targetTypeCtor(false, 0, 10);

                    var ultraMapper = new Mapper();
                    ultraMapper.Map(source, target);

                    bool isResultOk = ultraMapper.VerifyMapperResult(source, target);
                    Assert.IsTrue(isResultOk);
                }
            }
        }