Given some AssertResemblance.Like
Collection helpers[1], you
- Put them in the same order (which is most cleanly done in the test body as the input and output collections will be of different types and unless
SemanticComparison
grows a feature I don't think it can be usefully generalised)
- Let
Ploeh.SemanticComparison
's Likeness
do the matching on Name
- Let xUnit.net V2's Assert.Collection do the correlation (it doesn't yet give a greate message)
Final result is:
var results = from result in outputs orderby result.Name select result;
var expectations = from input in inputs orderby input.Name select input;
AssertResemblance.Like( expectations, results,
configure=>configure
.Without(x=>x.IgnoreThisField) );
[1]
static class AssertResemblance
{
public static void Like<T, T2>( IEnumerable<T> expected, IEnumerable<T2> actual )
{
Like<T, T2>( expected, actual, x=>x );
}
public static void Like<T, T2>( IEnumerable<T> expected, IEnumerable<T2> actual, Func<Likeness<T, T2>, Likeness<T, T2>> configureLikeness )
{
Assert2.Collection( actual, SelectShouldEqualAsAction( expected.Select( x => configureLikeness( x.AsSource().OfLikeness<T2>() ) ) ) );
}
public static void Like<T>( IEnumerable<T> expected, IEnumerable<T> actual, Func<IEnumerable<T>, IEnumerable<T>> unify )
{
Like<T>( expected, actual, unify, x=>x );
}
public static void Like<T>( IEnumerable<T> expected, IEnumerable<T> actual, Func<IEnumerable<T>,IEnumerable<T>> unify, Func<Likeness<T, T>, Likeness<T, T>> configureLikeness )
{
Assert2.Collection( unify( actual ), SelectShouldEqualAsAction( unify(expected).Select( x => configureLikeness( x.AsSource().OfLikeness<T>() ) ) ) );
}
static Action<TDestination>[] SelectShouldEqualAsAction<TSource, TDestination>( IEnumerable<Likeness<TSource, TDestination>> that )
{
return (from it in that select (Action<TDestination>)it.ShouldEqual).ToArray();
}
}