예제 #1
0
        /// <summary>Creates a concrete arg type from the given generic arg.</summary>
        /// <param name="type">Generic arg to create.</param>
        /// <returns>Created arg type.</returns>
        private Type CreateArg(Type type)
        {
            bool newNeeded = type.GenericParameterAttributes.HasFlag(
                GenericParameterAttributes.DefaultConstructorConstraint);

            Type arg;

            if (type.GenericParameterAttributes.HasFlag(
                    GenericParameterAttributes.NotNullableValueTypeConstraint))
            {
                arg = _gen.NextItem(ValueRandom.ValueTypes);
            }
            else if (newNeeded)
            {
                arg = typeof(object);
            }
            else
            {
                arg = typeof(string);
            }

            Type[] constraints = type.GetGenericParameterConstraints();

            Limiter.Dozen.Repeat(() =>
            {
                while (!constraints.All(c => arg.Inherits(c)) &&
                       (!newNeeded || arg.GetConstructor(Type.EmptyTypes) != null))
                {
                    arg = _randomizer.Create(_gen.NextItem(constraints)).GetType();
                }
            }).Wait();

            return(arg);
        }
예제 #2
0
        /// <inheritdoc/>
        public object Variant(Type type, object instance, params object[] extraInstances)
        {
            IEnumerable <object> values = (extraInstances ?? Enumerable.Empty <object>()).Prepend(instance);

            object result = default;

            try
            {
                _limiter.StallUntil(
                    () => result = _randomizer.Create(type),
                    () => values.All(o => !_valuer.Equals(result, o))).Wait();
            }
            catch (AggregateException e)
            {
                throw new TimeoutException($"Could not create different instance of type '{type}'.", e);
            }
            return(result);
        }