/// <summary>
 /// Initializes a new instance of the <see cref="SomeDummiesList{T}"/> class.
 /// </summary>
 /// <param name="numberOfElementsSpecifiedInCallToSomeDummies">The number of elements in the list to generate as specified in the call to <see cref="Some.Dummies{T}(int,AutoFakeItEasy.CreateWith)"/>.</param>
 /// <param name="createWithSpecifiedInCallToSomeDummies">Determines if and how to populate the list with nulls as specified in the call to <see cref="Some.Dummies{T}(int,AutoFakeItEasy.CreateWith)"/>.</param>
 public SomeDummiesList(
     int numberOfElementsSpecifiedInCallToSomeDummies,
     CreateWith createWithSpecifiedInCallToSomeDummies)
 {
     this.NumberOfElementsSpecifiedInCallToSomeDummies = numberOfElementsSpecifiedInCallToSomeDummies;
     this.CreateWithSpecifiedInCallToSomeDummies       = createWithSpecifiedInCallToSomeDummies;
 }
Example #2
0
        /// <summary>
        /// Gets a list of read-only dummies of the specified type.
        /// </summary>
        /// <param name="numberOfElements">The number of elements in the read-only list to generate.  If negative then a random number of elements between 1 and 10 will be generated.</param>
        /// <param name="createWith">Determines if and how to populate the read-only list with nulls.  The default is to create a list with no nulls.</param>
        /// <typeparam name="T">The type of dummies to return.</typeparam>
        /// <returns>A list of read-only dummy objects of the specified type.</returns>
        public static IReadOnlyList <T> ReadOnlyDummies <T>(
            int numberOfElements  = -1,
            CreateWith createWith = CreateWith.NoNulls)
        {
            var dummies = Dummies <T>(numberOfElements, createWith);
            var result  = new SomeReadOnlyDummiesList <T>(dummies, numberOfElements, createWith);

            return(result);
        }
Example #3
0
 /// <summary>
 /// Initializes a new instance of the <see cref="SomeReadOnlyDummiesList{T}"/> class.
 /// </summary>
 /// <param name="list">The list to wrap.</param>
 /// <param name="numberOfElementsSpecifiedInCallToSomeDummies">The number of elements in the list to generate as specified in the call to <see cref="Some.ReadOnlyDummies{T}(int,AutoFakeItEasy.CreateWith)"/>.</param>
 /// <param name="createWithSpecifiedInCallToSomeDummies">Determines if and how to populate the list with nulls as specified in the call to <see cref="Some.ReadOnlyDummies{T}(int,AutoFakeItEasy.CreateWith)"/>.</param>
 public SomeReadOnlyDummiesList(
     IList <T> list,
     int numberOfElementsSpecifiedInCallToSomeDummies,
     CreateWith createWithSpecifiedInCallToSomeDummies)
     : base(list)
 {
     this.NumberOfElementsSpecifiedInCallToSomeDummies = numberOfElementsSpecifiedInCallToSomeDummies;
     this.CreateWithSpecifiedInCallToSomeDummies       = createWithSpecifiedInCallToSomeDummies;
 }
Example #4
0
        /// <summary>
        /// Gets a list of dummies of the specified type.
        /// </summary>
        /// <param name="numberOfElements">The number of elements in the list to generate.  If negative then a random number of elements between 1 and 10 will be generated.</param>
        /// <param name="createWith">Determines if and how to populate the list with nulls.  The default is to create a list with no nulls.</param>
        /// <typeparam name="T">The type of dummies to return.</typeparam>
        /// <returns>A list of dummy objects of the specified type.</returns>
        public static IList <T> Dummies <T>(
            int numberOfElements  = -1,
            CreateWith createWith = CreateWith.NoNulls)
        {
            var result = new SomeDummiesList <T>(numberOfElements, createWith);

            if (numberOfElements < 0)
            {
                numberOfElements = ThreadSafeRandom.Next(MinRandomNumberOfElements, MaxRandomNumberOfElements + 1);
            }

            var  type       = typeof(T);
            bool isNullable = !type.IsValueType || (Nullable.GetUnderlyingType(type) != null);

            if (createWith == CreateWith.OneOrMoreNulls)
            {
                if (!isNullable)
                {
                    throw new ArgumentException("Cannot create a list of value type with one or more nulls.  Value types cannot be null.");
                }

                if (numberOfElements == 0)
                {
                    throw new ArgumentException("Cannot create a list with one or more nulls when number of elements is zero.");
                }
            }

            for (int x = 0; x < numberOfElements; x++)
            {
                if (createWith == CreateWith.NoNulls)
                {
                    result.Add(A.Dummy <T>());
                }
                else if ((createWith == CreateWith.OneOrMoreNulls) || (createWith == CreateWith.ZeroOrMoreNulls))
                {
                    var useNull = isNullable && (ThreadSafeRandom.NextDouble() <= ProbabilityOfNull);
                    result.Add(useNull ? default(T) : A.Dummy <T>());
                }
                else
                {
                    throw new NotSupportedException("This create with option is not supported: " + createWith);
                }
            }

            if (createWith == CreateWith.OneOrMoreNulls)
            {
                if (result.All(_ => _ != null))
                {
                    int randomIndex = ThreadSafeRandom.Next(0, numberOfElements);
                    result[randomIndex] = default(T);
                }
            }

            return(result);
        }