Beispiel #1
0
        /// <inheritdoc/>
        public object Create(Type type, Func <object, bool> condition = null)
        {
            object result = default;

            try
            {
                _limiter.StallUntil(
                    () => result = Create(type, new RandomizerChainer(_faker, _gen, Create)),
                    () => condition?.Invoke(result) ?? true).Wait();
            }
            catch (AggregateException e)
            {
                if (e.InnerException is InsufficientExecutionStackException)
                {
                    throw new InsufficientExecutionStackException(
                              $"Ran into infinite generation trying to randomize type '{type}'.");
                }
                else if (e.InnerException is TimeoutException)
                {
                    throw new TimeoutException(
                              $"Could not create instance of type '{type}' matching condition.", e);
                }
                else if (e.InnerException is NotSupportedException)
                {
                    throw e.InnerException;
                }
                else
                {
                    throw new InvalidOperationException(
                              $"Encountered issue creating instance of type '{type}'.", e);
                }
            }
            return(result);
        }
Beispiel #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);
        }