Esempio n. 1
0
            /// <summary>
            /// Returns an enumeration of random strings from a pre-existing stock of values.
            /// </summary>
            /// <param name="count">The number of strings to generate.</param>
            /// <param name="stock">A stock of preset values.</param>
            /// <returns>An enumeration of random string values.</returns>
            /// <exception cref="GenerationException">Thrown if the specified parameters are inconsistent or invalid.</exception>
            public static IEnumerable <string> Strings(int count, RandomStringStock stock)
            {
                var generator = new RandomStockStringGenerator
                {
                    Count  = count,
                    Values = RandomStringStockInfo.FromStock(stock).GetItems(),
                };

                foreach (string value in generator.Run())
                {
                    yield return(value);
                }
            }
        private RandomStringGenerator GetGeneratorImpl(IPatternScope scope)
        {
            if (generator == null)
            {
                var invoker = MakeFilterInvoker(scope);

                if (Pattern == null && stock == null)
                {
                    throw new PatternUsageErrorException("You must specify how to generate random strings by setting either 'Pattern' or 'Stock' appropriately.");
                }
                if (!String.IsNullOrEmpty(Pattern) && stock.HasValue)
                {
                    throw new PatternUsageErrorException("You must specify how to generate random strings by setting either 'Pattern' or 'Stock' exclusively.");
                }

                try
                {
                    if (stock.HasValue)
                    {
                        return(generator = new RandomStockStringGenerator
                        {
                            Values = RandomStringStockInfo.FromStock(stock.Value).GetItems(),
                            Count = count,
                            Filter = invoker,
                            Seed = NullableSeed,
                        });
                    }

                    return(generator = new RandomRegexLiteStringGenerator
                    {
                        RegularExpressionPattern = Pattern,
                        Count = count,
                        Filter = invoker,
                        Seed = NullableSeed,
                    });
                }
                catch (GenerationException exception)
                {
                    throw new PatternUsageErrorException(String.Format("The random strings generator was incorrectly initialized ({0}).", exception.Message), exception);
                }
            }

            return(generator);
        }