public void Compare_Same_Instance_Should_Succeed()
        {
            var container = new PropertyContainer();

            Assert.True(ObjectFiller.Fill(container, true));
            Assert.True(Identity(container));
        }
        public void ObjectFiller_FillComplexTypes()
        {
            ComplexTestDtoClass objectToFill = new ComplexTestDtoClass();

            objectToFill = ObjectFiller.FillThisObject(objectToFill, objectToFill.GetType());

            Assert.IsTrue(objectToFill.Array.Length > 0);
            Assert.IsTrue(objectToFill.EnumerableTestDtoClasses.Any());
            Assert.IsTrue(objectToFill.ListTestDtoClass.Any());
            Assert.IsTrue(objectToFill.StringList.Any());
            Assert.IsTrue(objectToFill.DictionaryTestDtoClasses.Any());
            Assert.IsTrue(objectToFill.ArrayListTestDtoClass.Count > 0);

            Assert.IsTrue(objectToFill.TestDtoClass.GetType() == typeof(TestDtoClass));


            Assert.AreNotEqual(objectToFill.TestDtoClass.Name, string.Empty);
            Assert.AreNotEqual(objectToFill.TestDtoClass.Title, string.Empty);
            Assert.AreNotEqual(objectToFill.TestDtoClass.Char, string.Empty);
            Assert.AreNotEqual(objectToFill.TestDtoClass.Object, null);

            Assert.AreEqual(objectToFill.TestDtoClass.Char, 'x');
            Assert.AreEqual(objectToFill.TestDtoClass.Sbyte, SByte.MaxValue);
            Assert.AreEqual(objectToFill.TestDtoClass.Single, Single.MaxValue);
            Assert.AreEqual(objectToFill.TestDtoClass.Long, long.MaxValue);

            Assert.IsTrue(objectToFill.TestDtoClass.Position > 0);
            Assert.IsTrue(objectToFill.TestDtoClass.Salary > 0.0);
            Assert.IsTrue(objectToFill.TestDtoClass.Long > 0.0);
            Assert.IsTrue(objectToFill.TestDtoClass.Boolean);
        }
Example #3
0
        public void ObjectFiller_TargetTypeIsStruct_StructIsFilledCorrectly()
        {
            var today      = Date.Today;
            var now        = DateTime.Now;
            var dataRecord = new MockDataReader(
                new List <MockRecordField>()
            {
                new MockRecordField(1, "productId", typeof(int)),
                new MockRecordField(1L, "nullableProductId", typeof(long)),
                new MockRecordField("woohoo!", "someString", typeof(string)),
                new MockRecordField(1.345356m, "price", typeof(decimal)),
                new MockRecordField(null, "maybePrice", typeof(decimal)),
                new MockRecordField(today, "date", "date", typeof(DateTime)),
                new MockRecordField(now, "datetime", typeof(DateTime)),
            });

            var objectFiller = new ObjectFiller();

            var result = (StructWithSingleConstructor)objectFiller.Build(typeof(StructWithSingleConstructor), dataRecord);

            Assert.AreEqual(result.productId, 1);
            Assert.AreEqual(new LongId <Product>(1) as LongId <Product>?, result.nullableProductId);
            Assert.AreEqual(result.someString, "woohoo!");
            Assert.AreEqual(result.price, 1.345356m);
            Assert.AreEqual(result.maybePrice, null);
            Assert.AreEqual(result.date, today);
            Assert.AreEqual(result.datetime, now);
        }
Example #4
0
        public void ObjectFiller_TargetTypeIsClassWithConstructorsAndAdditionalColumnsAreSelectedForSorting_ClassIsFilledCorrectly()
        {
            var today      = Date.Today;
            var now        = DateTime.Now;
            var dataRecord = new MockDataReader(
                new List <MockRecordField>()
            {
                new MockRecordField(1, "productId", typeof(int)),
                new MockRecordField("woohoo!", "someString", typeof(string)),
                new MockRecordField(1.345356m, "price", typeof(decimal)),
                new MockRecordField(today, "date", "date", typeof(DateTime)),
                new MockRecordField(now, "datetime", typeof(DateTime)),
                new MockRecordField(now, "insertDate", typeof(DateTime)),
                new MockRecordField(now, "updateDate", typeof(DateTime)),
            });

            var objectFiller = new ObjectFiller();

            var result = (ClassWithMultipleConstructors)objectFiller.Build(typeof(ClassWithMultipleConstructors), dataRecord);

            Assert.AreEqual(result.productId, 1);
            Assert.AreEqual(result.nullableProductId, null);
            Assert.AreEqual(result.someString, "woohoo!");
            Assert.AreEqual(result.price, 1.345356m);
            Assert.AreEqual(result.maybePrice, null);
            Assert.AreEqual(result.date, today);
            Assert.AreEqual(result.datetime, now);
        }
Example #5
0
        public object GenerateValue(Type objectType, ObjectFiller objectFiller)
        {
            long result = _rand.Next(1, Int32.MaxValue) << 32;

            result = result | (long)_rand.Next();
            return(result);
        }
Example #6
0
        public object GenerateValue(Type objectType, ObjectFiller objectFiller)
        {
            double mantissa = (_rand.NextDouble() * 2.0) - 1.0;
            double exponent = Math.Pow(2.0, _rand.Next(-126, 128));

            return((float)(mantissa * exponent));
        }
 public ObjectFiller GetObjectFiller()
 {
     if (instance == null)
     {
         instance = new ObjectFiller();
     }
     return(instance);
 }
Example #8
0
        public object GenerateValue(Type objectType, ObjectFiller objectFiller)
        {
            DateTime start        = new DateTime(1995, 1, 1);
            TimeSpan range        = DateTime.Today - start;
            TimeSpan randTimeSpan = new TimeSpan((long)(_rand.NextDouble() * range.Ticks));

            return(start + randTimeSpan);
        }
        public void InitializeTests()
        {
            _filledPerson = ObjectFiller.FillThisObject(_filledPerson, _filledPerson.GetType());

            for (int i = 0; i < 5; i++)
            {
                var filledPerson = ObjectFiller.FillThisObject(_filledPerson, _filledPerson.GetType());
                _filledPeople.Add(filledPerson);
            }
        }
Example #10
0
        public void NominalCase()
        {
            var container = new MyAssertionClass();

            Check.That(ObjectFiller.Fill(container, true)).IsTrue();
            Check.ThatObject(container).HavePropertiesThat().AreInitialized();
            var clone = ObjectBuilder.MemberwiseClone(container);

            Check.ThatObject(container).HavePropertiesThat().AreEqualToPropertiesOf(clone);
        }
Example #11
0
        public object GenerateValue(Type objectType, ObjectFiller objectFiller)
        {
            string word       = "";
            int    wordLength = _rand.Next() % 24 + 1;

            for (int i = 0; i < wordLength; i++)
            {
                word += _letters[_rand.Next() % _letters.Length];
            }
            return(word);
        }
Example #12
0
        public void ObjectFiller_TargetTypeIsNullableTypedId_ClassIsFilledCorrectly()
        {
            var dataRecord = new MockDataReader(
                new List <MockRecordField>()
            {
                new MockRecordField(null, "productId", typeof(int))
            });

            var objectFiller = new ObjectFiller();

            var result = (Id <Product>?)objectFiller.Build(typeof(Id <Product>?), dataRecord);

            Assert.AreEqual(result, null);
        }
Example #13
0
        public object GenerateValue(Type objectType, ObjectFiller objectFiller)
        {
            Type  createdElement = objectType.GetGenericArguments()[0];
            IList list           = (IList)Activator.CreateInstance(objectType);

            int countObject = _rand.Next() % 7 + 1;

            for (int i = 0; i < countObject; i++)
            {
                object value = objectFiller.FillObject(new FakerType(createdElement));
                list.Add(value);
            }
            return(list);
        }
Example #14
0
        /// <summary>
        ///     Creates the instance and populates it.
        /// </summary>
        /// <param name="type">The type.</param>
        /// <param name="seed">The seed.</param>
        /// <param name="recurse">if set to <c>true</c> [recurse].</param>
        /// <returns></returns>
        /// <exception cref="System.InvalidOperationException"></exception>
        public static object CreateInstance(Type type, int seed, bool recurse)
        {
            object resultValue;
            var    flag = CreateValue(type, seed, out resultValue);

            if (flag)
            {
                return(resultValue);
            }
            if (type == typeof(string))
            {
                return(seed.ToString());
            }
            if (typeof(IList).IsAssignableFrom(type))
            {
                return(CreateIList(type, seed, recurse));
            }
            var elementInfo = type.GetConstructor(Type.EmptyTypes);

            if (elementInfo != null)
            {
                resultValue = elementInfo.Invoke(null);
                if (recurse)
                {
                    ObjectFiller.Fill(resultValue, true);
                }
            }
            else
            {
                var constructorInfos = type.GetConstructors();
                if (constructorInfos.Length == 0)
                {
                    throw new InvalidOperationException($"Cannot find constructors for {type}");
                }
                elementInfo = constructorInfos[0];
                var parameters = new List <object>();
                foreach (var info in elementInfo.GetParameters())
                {
                    parameters.Add(CreateInstance(info.ParameterType, Environment.TickCount, false));
                }
                resultValue = elementInfo.Invoke(parameters.ToArray());
                if (recurse)
                {
                    ObjectFiller.Fill(resultValue, true);
                }
            }
            return(resultValue);
        }
Example #15
0
        /// <summary>
        ///     Creates a <see cref="IList" />.
        /// </summary>
        /// <param name="type">The type.</param>
        /// <param name="seed">The seed.</param>
        /// <param name="recurse">if set to <c>true</c> [recurse].</param>
        /// <returns></returns>
        // ReSharper disable once ExcessiveIndentation
        internal static object CreateIList(Type type, int seed, bool recurse)
        {
            //ConstructorInfo constructorInfo = type.GetConstructor(BindingFlags.Static|BindingFlags.Public |BindingFlags.CreateInstance, null, new Type[]{typeof(int)}, new ParameterModifier[]{});
            var args     = new object[] { 3 };
            var newValue = Activator.CreateInstance(type, args);

            for (var i = 0; i < 3; ++i)
            {
                object resultValue;
                Type   elementType;
                if (type.HasElementType)
                {
                    elementType = type.GetElementType();
                }
                else
                {
                    elementType = type.GetGenericArguments()[0];
                }
                var flag = CreateValue(elementType, seed, out resultValue);
                if (!flag)
                {
                    if (elementType == typeof(string))
                    {
                        resultValue = seed.ToString();
                    }
                    else
                    {
                        var elementInfo = elementType.GetConstructor(Type.EmptyTypes);
                        if (elementInfo != null)
                        {
                            resultValue = elementInfo.Invoke(null);
                            if (recurse)
                            {
                                ObjectFiller.Fill(resultValue, true);
                            }
                        }
                    }
                }
                AddInList(type, i, newValue, resultValue);
            }
            return(newValue);
        }
        public void ObjectFiller_FillListBasicTypes()
        {
            List <TestDtoClass> objectsToFill = new List <TestDtoClass> {
                new TestDtoClass(), new TestDtoClass(), new TestDtoClass()
            };

            foreach (var blankObject in objectsToFill)
            {
                ObjectFiller.FillThisObject(blankObject, blankObject.GetType());
            }

            foreach (var filledobject in objectsToFill)
            {
                Assert.AreNotEqual(filledobject.Name, string.Empty);
                Assert.AreNotEqual(filledobject.Title, string.Empty);

                Assert.IsTrue(filledobject.Position > 0);
                Assert.IsTrue(filledobject.Salary > 0.0);
            }
        }
        /// <summary>
        ///     Checks assertions depending on the concrete type of checker.
        /// </summary>
        public void CheckInterface()
        {
            var type   = typeof(TInstance);
            var result = (TInstance)ObjectBuilder.CreateInstance(type, Environment.TickCount, false);

            ObjectFiller.Fill(result, false);
            Check.That(result.Equals(result)).IsTrue();
            Check.That(result).IsOperatorEqualTo(result);
            Check.That(result).Is(e => e.IsOperatorEqual(result)).IsTrue();
            Check.That(result).Is(e => e.IsOperatorNotEqual(result)).IsFalse();

            Check.That(result.Equals((object)result)).IsTrue();

            Check.That(result.Equals(null)).IsFalse();

            Check.That(result.Equals("")).IsFalse();

            Check.That(result.Equals((object)default(TInstance))).IsFalse();

            Check.That(result.Equals(default(TInstance))).IsFalse();
            Check.That(result).Is(e => e.IsOperatorEqual(default(TInstance))).IsFalse();
            Check.That(result).Is(e => e.IsOperatorNotEqual(default(TInstance))).IsTrue();

            Check.That(result.GetHashCode()).IsEqualTo(result.GetHashCode());

            var clone = (TInstance)ObjectBuilder.MemberwiseClone(result);

            Check.That(result.Equals(clone)).IsTrue();
            Check.That(result).Is(e => e.IsOperatorEqual(clone)).IsTrue();
            Check.That(result).Is(e => e.IsOperatorNotEqual(clone)).IsFalse();

            Check.That(result.Equals((object)clone)).IsTrue();

            if (ObjectModifier.ChangeFirstProperty(clone))
            {
                Check.That(result.Equals(clone)).IsFalse();
                Check.That(result).Is(e => e.IsOperatorEqual(clone)).IsFalse();
                Check.That(result).Is(e => e.IsOperatorNotEqual(clone)).IsTrue();
                Check.That(result.GetHashCode()).IsNotEqualTo(clone.GetHashCode());
            }
        }
        public void ObjectFiller_FillBasicTypes()
        {
            TestDtoClass objectToFill = new TestDtoClass();

            objectToFill = ObjectFiller.FillThisObject(objectToFill, objectToFill.GetType());

            Assert.AreNotEqual(objectToFill.Name, string.Empty);
            Assert.AreNotEqual(objectToFill.Title, string.Empty);
            Assert.AreNotEqual(objectToFill.Char, string.Empty);
            Assert.AreNotEqual(objectToFill.Object, null);

            Assert.AreEqual(objectToFill.Char, 'x');
            Assert.AreEqual(objectToFill.Sbyte, SByte.MaxValue);
            Assert.AreEqual(objectToFill.Single, Single.MaxValue);
            Assert.AreEqual(objectToFill.Long, long.MaxValue);

            Assert.IsTrue(objectToFill.Position > 0);
            Assert.IsTrue(objectToFill.Salary > 0.0);
            Assert.IsTrue(objectToFill.Long > 0.0);
            Assert.IsTrue(objectToFill.Boolean);
        }
Example #19
0
        /// <summary>
        ///     Checks assertions depending on the concrete type of checker.
        /// </summary>
        public void CheckInterface()
        {
            var result = (TCloneable)ObjectBuilder.CreateInstance(typeof(TCloneable), Environment.TickCount, false);

            ObjectFiller.Fill(result, false);
            var clone = (TCloneable)result.Clone();

            // Clone should be equal
            Check.ThatObject(result).IsEqualTo(clone);
            // Check if we change a property on a side it is not changed on other side
            foreach (var info in typeof(TCloneable).GetProperties())
            {
                clone = (TCloneable)result.Clone();
                if (ObjectModifier.ChangeValue(result, info.Name, true))
                {
                    var type = result.GetType();
                    Check.ThatObject(info.GetValue(result)).IsNotEqualTo(
                        info.GetValue(clone),
                        $"After clone of {type}, property {info.Name} should not remain equal to the same property of original object");
                }
            }
        }
Example #20
0
 public object GenerateValue(Type objectType, ObjectFiller objectFiller)
 {
     return(_rand.NextDouble());
 }
Example #21
0
 public object GenerateValue(Type objectType, ObjectFiller objectFiller)
 {
     return(_letters[_rand.Next(0, _letters.Length)]);
 }
Example #22
0
        public void Fill(ISqlProvider sqlProvider, IMappingProvider mappingProvider, IDataReader reader, object entity, ObjectFiller objectFiller)
        {
            foreach (PropertyFiller propertyFiller in objectFiller.PropertyFillers)
            {
                //Console.WriteLine(propertyFiller.Column.ColumnName + ":" + propertyFiller.OutputColumnIndex + ":" + propertyFiller.Column.PropertyAdapter.PropertyName + ":" + propertyFiller.Column.PropertyAdapter.PropertyType);
                //propertyFiller.Column.FillPropertyValue(entityObject, dataReader, propertyFiller.OutputColumnIndex);
                //propertyFiller.getPropertyAdapter().setPropertyValue(object, resultSet.getObject(propertyFiller.getOutputColumnIndex()));

                //为值类型赋值null会出问题
                object value = sqlProvider.ConvertDbValue(reader[propertyFiller.ColumnIndex], propertyFiller.PropertyType);
                if (!(propertyFiller.PropertyType.IsValueType && value == null))
                {
                    Set(entity, propertyFiller.PropertyName, value);
                }
            }
            foreach (ObjectFiller f in objectFiller.ObjectFillers)
            {
                //如果已经有实例,则不需要实例化
                object existsInstance = Get(entity, f.PropertyName);
                if (existsInstance == null)
                {
                    existsInstance = CreateInstance(f.EntityType);
                    Set(entity, f.PropertyName, existsInstance);
                }
                Fill(sqlProvider, mappingProvider, reader, existsInstance, f);
            }
        }
Example #23
0
 public object GenerateValue(Type objectType, ObjectFiller objectFiller)
 {
     return(-1);
 }
Example #24
0
        public T Create <T>()
        {
            ObjectFiller objectFiller = new ObjectFiller(_generator);

            return((T)objectFiller.FillObject(new FakerType(typeof(T))));
        }
Example #25
0
 public object GenerateValue(Type objectType, ObjectFiller objectFiller)
 {
     return(new DateTime(1995, 1, 1));
 }
Example #26
0
 public object GenerateValue(Type objectType, ObjectFiller objectFiller)
 {
     return(_rand.Next(1, Int32.MaxValue));
 }