示例#1
0
        public void GenRandomDouble()
        {
            var min = 1.2;
            var max = 2.5;

            for (int i = 0; i < 20; i++)
            {
                var newDouble = ExtendedRandom.NextDouble(min, max);
                Assert.True(newDouble >= min && newDouble <= max);
            }
        }
示例#2
0
        public object Generate()
        {
            var obj = new TSource();

            foreach (var prop in typeof(TSource).GetProperties(BindingFlags.Public | BindingFlags.Instance))
            {
                if (Ignore.Contains(prop.Name))
                {
                    continue;
                }

                if (Values.ContainsKey(prop.Name))
                {
                    prop.SetValue(obj, Values[prop.Name], null);
                    continue;
                }

                if (Generators.ContainsKey(prop.Name))
                {
                    try {
                        prop.SetValue(obj, Generators[prop.Name].Generate(), null);
                    } catch (ArgumentException) {
                        throw new ArgumentException($"Mismatching generator type for property {prop.Name}");
                    }
                    continue;
                }

                if (OnlyCustom)
                {
                    continue;
                }

                if (TypeGenerators.ContainsKey(prop.PropertyType))
                {
                    try {
                        prop.SetValue(obj, TypeGenerators[prop.PropertyType].Generate(), null);
                    } catch (ArgumentException) {
                        throw new ArgumentException($"Mismatching generator type for type {prop.PropertyType}");
                    }
                    continue;
                }

                Type type = prop.PropertyType;

                if (type == typeof(string))
                {
                    prop.SetValue(obj, ExtendedRandom.NextString(10), null);
                }
                else if (type == typeof(int) || type == typeof(long) || type == typeof(uint) || type == typeof(ulong))
                {
                    prop.SetValue(obj, Random.Next(1, 500), null);
                }
                else if (type == typeof(byte))
                {
                    prop.SetValue(obj, Random.Next(0, 256), null);
                }
                else if (type == typeof(sbyte))
                {
                    prop.SetValue(obj, Random.Next(-128, 128), null);
                }
                else if (type == typeof(short) || type == typeof(ushort))
                {
                    prop.SetValue(obj, Random.Next(0, 32768), null);
                }
                else if (type == typeof(float) || type == typeof(double) || type == typeof(decimal))
                {
                    prop.SetValue(obj, Convert.ChangeType(ExtendedRandom.NextDouble(0, 500), type), null);
                }
                else if (type == typeof(bool))
                {
                    prop.SetValue(obj, ExtendedRandom.NextBoolean(), null);
                }
                else if (type == typeof(char))
                {
                    prop.SetValue(obj, ExtendedRandom.NextChar(), null);
                }
                else if (type == typeof(Guid))
                {
                    prop.SetValue(obj, Guid.NewGuid(), null);
                }
                else if (type == typeof(DateTime))
                {
                    prop.SetValue(obj, ExtendedRandom.NextDateTime(), null);
                }
            }
            return(obj);
        }