I have a NUnit unit test, which I have two collections of different types that I want to claim are equivalent.
class A { public int x; } class B { public string y; } [Test] public void MyUnitTest() { var a = GetABunchOfAs();
where Assert.IsPrettySimilar is defined as such
public static void IsPrettySimilar<T1, T2>( IEnumerable<T1> left, IEnumerable<T2> right, Func<T1, T2, bool> predicate) { using (var leftEnumerator = left.GetEnumerator()) using (var rightEnumerator = right.GetEnumerator()) { while (true) { var leftMoved = leftEnumerator.MoveNext(); if (leftMoved != rightEnumerator.MoveNext()) { Assert.Fail("Enumerators not of equal size"); } if (!leftMoved) { break; } var isMatch = predicate(leftEnumerator.Current, rightEnumerator.Current); Assert.IsTrue(isMatch); } } }
My question is: is there a more idiomatic way to do this using existing methods in NUnit? I already looked at CollectionAssert and I donβt agree with what I want.
My description is "equivalent" in this case:
1) Collections should be the same size
2) Collections should be in the same logical order
3) To determine the equivalence of matching elements, you should use some predicate.
source share