public void Counts_pure_generic_collection() // Implemented ICollection<T> only
 {
     var counter = new EnumerableCounter(GetHashSet());
     AssertStrategies(counter.Count(), CountingStrategyName.ByCountGetter, CountingStrategyName.ByEnumeratingElements);
 }
 public void Counts_mixed_generic_collection() // Implemented both ICollection and ICollection<T>
 {
     var counter = new EnumerableCounter(GetGenericList());
     AssertStrategies(counter.Count(), CountingStrategyName.ByCountGetter, CountingStrategyName.ByEnumeratingElements);
 }
 public void Counts_array()
 {
     var counter = new EnumerableCounter(GetArray());
     AssertStrategies(counter.Count(), CountingStrategyName.ByLengthGetter);
 }
 public void Counts_custom_enumerable_with_count()
 {
     var counter = new EnumerableCounter(new CustomEnumerable());
     AssertStrategies(counter.Count(), CountingStrategyName.ByEnumeratingElements, CountingStrategyName.ByReflectedCountGetter);
 }
 public void Counts_simple_enumerable()
 {
     var counter = new EnumerableCounter(GetSimpleEnumerable());
     AssertStrategies(counter.Count(), CountingStrategyName.ByEnumeratingElements);
 }
Exemplo n.º 6
0
        /// <summary>
        /// Verifies that the specified sequence, collection, or array contains the expected number of elements.
        /// </summary>
        /// <remarks>
        /// <para>
        /// The assertion counts the elements according to the underlying type of the sequence.
        /// <list type="bullet">
        /// <item>Uses <see cref="Array.Length"/> if the sequence is an array.</item>
        /// <item>Uses <see cref="ICollection.Count"/> or <see cref="ICollection{T}.Count"/> if the sequence is a collection such as <see cref="List{T}"/> or <see cref="Dictionary{K,V}"/>. It enumerates and counts the elements as well.</item>
        /// <item>Enumerates and counts the elements if the sequence is a simple <see cref="IEnumerable"/>.</item>
        /// </list>
        /// </para>
        /// </remarks>
        /// <param name="expectedCount">The expected number of elements.</param>
        /// <param name="values">The enumeration of elements to count.</param>
        /// <param name="messageFormat">The custom assertion message format, or null if none.</param>
        /// <param name="messageArgs">The custom assertion message arguments, or null if none.</param>
        /// <exception cref="AssertionException">Thrown if the verification failed unless the current <see cref="AssertionContext.AssertionFailureBehavior" /> indicates otherwise.</exception>
        /// <exception cref="ArgumentOutOfRangeException">Thrown if <paramref name="expectedCount"/> is negative.</exception>
        /// <exception cref="ArgumentNullException">Thrown if <paramref name="values"/> is null.</exception>
        public static void Count(int expectedCount, IEnumerable values, string messageFormat, params object[] messageArgs)
        {
            if (expectedCount < 0)
                throw new ArgumentOutOfRangeException("expectedCount", "The expected count value must be greater than or equal to 0.");

            AssertionHelper.Verify(() =>
            {
                var counter = new EnumerableCounter(values);
                var failures = new List<ICountingStrategy>();

                foreach (ICountingStrategy strategy in counter.Count())
                {
                    if (strategy.Count != expectedCount)
                    {
                        failures.Add(strategy);
                    }
                }

                if (failures.Count == 0)
                    return null;

                var builder = new AssertionFailureBuilder(String.Format(
                    "Expected the sequence to contain a certain number of elements but {0} counting strateg{1} failed.", failures.Count, failures.Count > 1 ? "ies have" : "y has"))
                    .AddRawExpectedValue(expectedCount);

                foreach (var failure in failures)
                {
                    builder.AddRawLabeledValue(String.Format("Actual Value ({0})", failure.Description), failure.Count);
                }

                return builder
                    .SetMessage(messageFormat, messageArgs)
                    .ToAssertionFailure();
            });
        }