Esempio n. 1
0
        /// <summary>
        /// Fill the target property with a random value from the specified array
        /// </summary>
        /// <param name="values">A array of values to choose from</param>
        /// <returns>A configurator for the target object type</returns>
        public AngieConfigurator <T> WithRandom(int[] values)
        {
            CustomFiller <int> customFiller = new CustomFiller <int>(PropertyInfo.Name, typeof(T), () => BaseValueGenerator.GetRandomValue(values));

            _maggie.RegisterFiller(customFiller);
            return(this);
        }
Esempio n. 2
0
        /// <summary>
        /// Fill the specified property with the result of the specified function
        /// </summary>
        /// <typeparam name="T1">The target object type</typeparam>
        /// <typeparam name="T2">The target property type</typeparam>
        /// <param name="expression">The target property</param>
        /// <param name="filler">A function that will return a property value</param>
        /// <returns>A configurator for the target object type</returns>
        public AngieConfigurator Fill <T1, T2>(Expression <Func <T1, T2> > expression, Func <T2> filler)
        {
            PropertyInfo      propertyInfo = (expression.Body as MemberExpression).Member as PropertyInfo;
            CustomFiller <T2> customFiller = new CustomFiller <T2>(propertyInfo.Name, typeof(T1), filler);

            _fillerManager.RegisterFiller(customFiller);
            return(this);
        }
Esempio n. 3
0
        /// <summary>
        /// Populate the specified property with a date in the future
        /// </summary>
        /// <typeparam name="T">The target object type</typeparam>
        /// <param name="configurator"></param>
        /// <returns>A configurator for the specified object type</returns>
        public static AngieConfigurator <T> AsFutureDate <T>(this AngieDateTimeConfigurator <T> configurator)
            where T : new ()
        {
            CustomFiller <DateTime> filler = new CustomFiller <DateTime>(configurator.PropertyInfo.Name, typeof(T),
                                                                         () => CalendarDate.Date(DateRules.FutureDates));

            configurator.Maggie.RegisterFiller(filler);
            return(configurator);
        }
Esempio n. 4
0
        public IPropertyFiller GetFiller(PropertyInfo propertyInfo)
        {
            IPropertyFiller result     = null;
            Type            objectType = propertyInfo.DeclaringType;

            while (objectType != null && result == null)
            {
                //First try to get a specific filler based on a full type name (including namespace)
                string fullTypeName = objectType.FullName.ToLowerInvariant();
                if (_specificPropertyFillersByObjectType.ContainsKey(fullTypeName))
                {
                    IList <IPropertyFiller> propertyFillers = _specificPropertyFillersByObjectType[fullTypeName];
                    result = GetMatchingPropertyFiller(propertyInfo, propertyFillers);
                }

                //Second try to get a more generic filler based on only the class name (no namespace)
                if (result == null)
                {
                    string classTypeName = objectType.Name.ToLowerInvariant();
                    if (_specificPropertyFillersByObjectType.ContainsKey(classTypeName))
                    {
                        IList <IPropertyFiller> propertyFillers = _specificPropertyFillersByObjectType[classTypeName];
                        result = GetMatchingPropertyFiller(propertyInfo, propertyFillers);
                    }
                }

                objectType = objectType.BaseType;
            }

            if (result == null)
            {
                //Finally, grab a generic property filler for that property type
                if (_genericPropertyFillersByPropertyType.ContainsKey(propertyInfo.PropertyType))
                {
                    result = _genericPropertyFillersByPropertyType[propertyInfo.PropertyType];
                }
                else
                {
                    //TODO: Can we build a custom filler here for other value types that we have not explicitly implemented (eg. long, decimal, etc.)
                    result = new CustomFiller <object>("*", typeof(object), true, () => null);

                    _genericPropertyFillersByPropertyType.Add(propertyInfo.PropertyType, result);
                }
            }

            return(result);
        }