A general-purpose structural equality comparer that defines a fully customizable equality operation without the
need to implement IEquatable<(Of <(T>)>).
Namespace:
MbUnit.FrameworkAssembly: MbUnit (in MbUnit.dll) Version: 3.2.0.0 (3.2.300.0)
Syntax
| C# |
|---|
public class StructuralEqualityComparer<T> : IEqualityComparer<T>, IEnumerable<EqualityComparison<T>>, IEnumerable |
| Visual Basic (Declaration) |
|---|
Public Class StructuralEqualityComparer(Of T) _ Implements IEqualityComparer(Of T), IEnumerable(Of EqualityComparison(Of T)), _ IEnumerable |
Type Parameters
- T
- The type of the objects. to compare.
Remarks
That equality comparer can be used in any MbUnit assertion that takes an IEqualityComparer<(Of <(T>)>) object as argument, such as AreEqual<(Of <(T>)>)(T, T, IEqualityComparer<(Of <(T>)>)).
The comparer must be initialized with a list of one or several matching criteria. Two instances are considered equal by the comparer if and only if all the criteria are true for that pair of instances.
Examples
The following example shows a test fixture that checks for the equality between two Foo
objects by using the well known AreEqual<(Of <(T>)>)(T, T, IEqualityComparer<(Of <(T>)>)) assertion. The custom equality comparer which
is provided to the assertion method, declares two Foo objects equal when the Value
fields have the same parity, and when the Text fields are equal (case insensitive):
CopyC#
public class Foo { public int Value; public string Text; } [TestFixture] public class FooTest { [Test] public void MyTest() { var foo1 = new Foo() { Value = 123, Text = "Hello" }; var foo2 = new Foo() { Value = 789, Text = "hElLo" }; Assert.AreEqual(foo1, foo2, new StructuralEqualityComparer<Foo> { { x => x.Value, (x, y) => x % 2 == y % 2 }, { x => x.Text, (x, y) => String.Compare(x, y, true) == 0 } }); } }
