示例#1
0
        private static object GetMethodCallAssociatedWithType(Type propertyType, RandomValueSettings settings)
        {
            var supportType = GetSupportType(propertyType);

            switch (supportType)
            {
            case SupportType.Basic:
                var typefunc = settings.NumericZero ? Numerics.ContainsKey(propertyType) ? Numerics[propertyType] : SupportedTypes[propertyType] : SupportedTypes[propertyType];
                return(typefunc.Invoke(propertyType));

            case SupportType.Enum:
                return(EnumMethodCall(propertyType));

            case SupportType.Collection:
            {
                var collectionType = GetSupportedCollectionType(propertyType);
                return(GetListMethodOfCollections(propertyType, collectionType.First(), settings));
            }

            case SupportType.Nullable:
                return(NullableMethodCall(propertyType, settings));

            case SupportType.UserDefined:
                return(settings.NestedLevels == -1 ? null : ObjectMethodCall(propertyType, settings));

            default:
                return(null);
            }
        }
示例#2
0
        private static object GetMethodCallAssociatedWithType(Type propertyType, RandomValueSettings settings)
        {
            var supportType = GetSupportType(propertyType);

            switch (supportType)
            {
            case SupportType.Basic:
                return(SupportedTypes[propertyType].Invoke(propertyType));

            case SupportType.Enum:
                return(EnumMethodCall(propertyType));

            case SupportType.Collection:
            {
                var collectionType = GetSupportedCollectionType(propertyType);
                return(GetListMethodOfCollections(propertyType, collectionType.First(), settings));
            }

            case SupportType.Nullable:
                return(NullableMethodCall(propertyType, settings));

            case SupportType.UserDefined:
                return(ObjectMethodCall(propertyType, settings));

            default:
                return(null);
            }
        }
示例#3
0
 private static object ObjectMethodCall(Type type, RandomValueSettings settings)
 {
     return(typeof(RandomValue).GetRuntimeMethods()
            .First(x => x.Name == "Object" && x.GetParameters()?[0]?.ParameterType == typeof(RandomValueSettings))
            .MakeGenericMethod(type)
            .Invoke(null, new object[] { settings }));
 }
示例#4
0
        public static ICollection <T> ICollection <T>(RandomValueSettings settings)
        {
            var enumerable = LazyIEnumerable <T>(settings).Take(settings.LengthOfCollection);

            var randomList = new Collection <T>(enumerable.ToList());

            return(randomList);
        }
示例#5
0
        private static object IDictionaryMethodCall(Type[] genericTypeArguments, RandomValueSettings settings)
        {
            var method = GetMethod("IDictionary");

            return(method
                   .MakeGenericMethod(genericTypeArguments[0], genericTypeArguments[1])
                   .Invoke(null, new object[] { settings }));
        }
示例#6
0
        private static object InvokeCollectionMethod(string nameOfMethod, Type type, RandomValueSettings settings)
        {
            var method = GetMethod(nameOfMethod);

            return(method
                   .MakeGenericMethod(type)
                   .Invoke(null, new object[] { settings }));
        }
示例#7
0
        private static object NullableMethodCall(Type propertyType, RandomValueSettings settings)
        {
            if (settings.IncludeNullAsPossibleValueForNullables && Bool())
            {
                return(null);
            }

            var baseType = Nullable.GetUnderlyingType(propertyType);

            return(GetMethodCallAssociatedWithType(baseType, settings));
        }
示例#8
0
        public static IDictionary <TKey, TValue> IDictionary <TKey, TValue>(RandomValueSettings settings)
        {
            var length = settings.LengthOfCollection;

            var keys = LazyIEnumerable <TKey>().Distinct().Take(length);

            var values = ICollection <TValue>(settings);

            var keyValuePairs = keys.Zip(values, (key, value) => new KeyValuePair <TKey, TValue>(key, value));

            return(keyValuePairs.ToDictionary(key => key.Key, value => value.Value));
        }
示例#9
0
        public static IEnumerable <T> LazyIEnumerable <T>(RandomValueSettings settings)
        {
            var type = typeof(T);

            var supportType = GetSupportType(type);

            while (supportType != SupportType.NotSupported)
            {
                var method = GetMethodCallAssociatedWithType(type, settings);

                yield return((T)method);
            }
        }
示例#10
0
        /// <summary>
        /// Use for getting a random object. You can configure the generator by passing in a new RandomValueSettings object.
        /// </summary>
        /// <returns>A random Object T</returns>
        public static T Object <T>(RandomValueSettings settings) where T : new()
        {
            var genericObject = (T)Activator.CreateInstance(typeof(T));

            var properties = typeof(T).GetRuntimeProperties().ToArray();

            foreach (var prop in properties)
            {
                if (PropertyHasNoSetter(prop))
                {
                    // Property doesn't have a public setter so let's ignore it
                    continue;
                }

                if (settings.RecursiveDepth <= 0 && PropertyTypeIsRecursiveOrCircular <T>(prop))
                {
                    // Prevent infinite loop when called recursively
                    continue;
                }

                var newSettings = new RandomValueSettings
                {
                    RecursiveDepth = settings.RecursiveDepth - 1,
                    IncludeNullAsPossibleValueForNullables = settings.IncludeNullAsPossibleValueForNullables,
                    LengthOfCollection = settings.LengthOfCollection,
                    NestedLevels       = settings.NestedLevels == -1 ? -1 : settings.NestedLevels - 1,
                    NumericZero        = settings.NumericZero
                };

                try
                {
                    var method = GetMethodCallAssociatedWithType(prop.PropertyType, newSettings);

                    prop.SetValue(genericObject, method, null);
                }
                catch (Exception)
                {
                    continue;
                }
            }

            return(genericObject);
        }
示例#11
0
        private static object GetListMethodOfCollections(Type propertyType, Type genericType, RandomValueSettings settings)
        {
            var typeOfList = genericType;

            object listMethod = null;

            Type type = propertyType;

            if (propertyType.IsArray)
            {
                listMethod = ArrayMethodCall(propertyType.GetElementType(), settings);
            }
            else if (type.GetTypeInfo().IsGenericType&& (type.GetGenericTypeDefinition() == typeof(List <>)))
            {
                listMethod = ListMethodCall(typeOfList, settings);
            }
            else if (type.GetGenericTypeDefinition() == typeof(IList <>))
            {
                listMethod = IListMethodCall(typeOfList, settings);
            }
            else if (type.GetTypeInfo().IsGenericType&& (type.GetGenericTypeDefinition() == typeof(Collection <>)))
            {
                listMethod = CollectionMethodCall(typeOfList, settings);
            }
            else if (type.GetGenericTypeDefinition() == typeof(ICollection <>))
            {
                listMethod = ICollectionMethodCall(typeOfList, settings);
            }
            else if (type.GetTypeInfo().IsGenericType&& (type.GetGenericTypeDefinition() == typeof(Dictionary <,>)))
            {
                listMethod = DictionaryMethodCall(type.GenericTypeArguments, settings);
            }
            else if (type.GetGenericTypeDefinition() == typeof(IDictionary <,>))
            {
                listMethod = IDictionaryMethodCall(type.GenericTypeArguments, settings);
            }
            else if (type.GetGenericTypeDefinition() == typeof(IEnumerable <>))
            {
                listMethod = IEnumerableMethodCall(typeOfList, settings);
            }

            return(listMethod);
        }
示例#12
0
 public static IList <T> IList <T>(RandomValueSettings settings)
 {
     return(ICollection <T>(settings).ToList());
 }
示例#13
0
 public static T[] Array <T>(RandomValueSettings settings)
 {
     return(Collection <T>(settings).ToArray());;
 }
示例#14
0
 public static Collection <T> Collection <T>(RandomValueSettings settings)
 {
     return((Collection <T>)ICollection <T>(settings));
 }
示例#15
0
 private static object ICollectionMethodCall(Type typeOfList, RandomValueSettings settings)
 {
     return(InvokeCollectionMethod("ICollection", typeOfList, settings));
 }
示例#16
0
 private static object IEnumerableMethodCall(Type type, RandomValueSettings settings)
 {
     return(GetMethod("IEnumerable")
            .MakeGenericMethod(type)
            .Invoke(null, new object[] { settings }));
 }
示例#17
0
 public static IEnumerable <T> IEnumerable <T>(RandomValueSettings settings)
 {
     return(LazyIEnumerable <T>(settings).Take(settings.LengthOfCollection));
 }
示例#18
0
 public static Dictionary <TKey, TValue> Dictionary <TKey, TValue>(RandomValueSettings settings)
 {
     return((Dictionary <TKey, TValue>)IDictionary <TKey, TValue>(settings));
 }