Skip to content

Instantly share code, notes, and snippets.

@bymyslf
Last active May 30, 2016 17:00
Show Gist options
  • Save bymyslf/d32812a3c744e557178d880b61299fc4 to your computer and use it in GitHub Desktop.
Save bymyslf/d32812a3c744e557178d880b61299fc4 to your computer and use it in GitHub Desktop.
IEnumerable assert utilities
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
public static class EnumerableAssert
{
/// <summary>
/// Assert that an array, list or other collection is empty
/// </summary>
/// <param name="enumerable">An array, list or other collection implementing IEnumerable</param>
/// <param name="message">The message to be displayed on failure</param>
/// <param name="args">Arguments to be used in formatting the message</param>
public static void IsEmpty(IEnumerable enumerable, string message, params object[] args)
{
Assert.IsTrue(IsEmptyInternal(enumerable), message, args);
}
/// <summary>
/// Assert that an array, list or other collection is empty
/// </summary>
/// <param name="collection">An array, list or other collection implementing IEnumerable</param>
/// <param name="message">The message to be displayed on failure</param>
public static void IsEmpty(IEnumerable enumerable, string message)
{
IsEmpty(enumerable, message, null);
}
/// <summary>
/// Assert that an array,list or other collection is empty
/// </summary>
/// <param name="collection">An array, list or other collection implementing IEnumerable</param>
public static void IsEmpty(IEnumerable enumerable)
{
IsEmpty(enumerable, String.Empty, null);
}
/// <summary>
/// Assert that an array, list or other collection is not empty
/// </summary>
/// <param name="enumerable">An array, list or other collection implementing IEnumerable</param>
/// <param name="message">The message to be displayed on failure</param>
/// <param name="args">Arguments to be used in formatting the message</param>
public static void IsNotEmpty(IEnumerable enumerable, string message, params object[] args)
{
Assert.IsTrue(!IsEmptyInternal(enumerable), message, args);
}
/// <summary>
/// Assert that an array, list or other collection is not empty
/// </summary>
/// <param name="collection">An array, list or other collection implementing IEnumerable</param>
/// <param name="message">The message to be displayed on failure</param>
public static void IsNotEmpty(IEnumerable enumerable, string message)
{
IsNotEmpty(enumerable, message, null);
}
/// <summary>
/// Assert that an array,list or other collection is not empty
/// </summary>
/// <param name="collection">An array, list or other collection implementing IEnumerable</param>
public static void IsNotEmpty(IEnumerable enumerable)
{
IsNotEmpty(enumerable, String.Empty, null);
}
/// <summary>
/// Assert that two collections are equal
/// </summary>
/// <param name="left">An array, list or other collection implementing IEnumerable</param>
/// <param name="right">An array, list or other collection implementing IEnumerable</param>
public static bool AreEqual<T>(IEnumerable<T> left, IEnumerable<T> right)
{
return left.SequenceEqual(right);
}
/// <summary>
/// Assert that two collections are equal using the specified comparer
/// </summary>
/// <param name="left">An array, list or other collection implementing IEnumerable</param>
/// <param name="right">An array, list or other collection implementing IEnumerable</param>
/// <param name="right">Custom comaparer</param>
public static bool AreEqual<T>(IEnumerable<T> left, IEnumerable<T> right, IEqualityComparer<T> comparer)
{
return left.SequenceEqual(right, comparer);
}
private static bool IsEmptyInternal(IEnumerable enumerable)
{
ICollection collection = enumerable as ICollection;
if (collection != null)
{
return collection.Count == 0;
}
return enumerable == null || !enumerable.GetEnumerator().MoveNext();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment