Contract for verifying the implementation of the generic IEquatable<(Of <(T>)>) interface.

Namespace:  MbUnit.Framework.ContractVerifiers
Assembly:  MbUnit (in MbUnit.dll) Version: 3.3.0.0 (3.3.610.0)

Syntax

C#
public class EqualityContract<TTarget> : AbstractContract
where TTarget : Object, IEquatable<TTarget>
Visual Basic (Declaration)
Public Class EqualityContract(Of TTarget As {Object, IEquatable(Of TTarget)}) _
	Inherits AbstractContract

Type Parameters

TTarget
The target tested type which implements the generic IEquatable<(Of <(T>)>) interface.

Remarks

Built-in verifications:

  • ObjectEquals : The Equals(Object) method was overriden and behaves correctly against the provided equivalence classes.
  • ObjectGetHashCode : The GetHashCode()()() method was overriden and behaves correctly against the provided equivalence classes.
  • EquatableEquals : The Equals(T) method is implemented and behaves as expected agains the provided equivalence classes.
  • OperatorEquals : The type has a static equality operator (==) overload which behaves correctly against the provided equivalence classes. Disable that test by setting the ImplementsOperatorOverloads property to false.
  • OperatorNotEquals : The type has a static inequality operator (!=) overload which behaves correctly against the provided equivalence classes. Disable that test by setting the ImplementsOperatorOverloads property to false.

Examples

The following example shows a simple class implementing the IEquatable<(Of <(T>)>) interface, and a test fixture which uses the equality contract to test it.
CopyC#
public class SampleEquatable : IEquatable<SampleEquatable>
{
    private int value;

    public SampleEquatable(int value)
    {
        this.value = value;
    }

    public override int GetHashCode()
    {
        return value.GetHashCode();
    }

    public override bool Equals(object obj)
    {
        return Equals(obj as SampleEquatable);
    }

    public bool Equals(SampleEquatable other)
    {
        return !Object.ReferenceEquals(other, null) 
            && (value == other.value);
    }

    public static bool operator ==(SampleEquatable left, SampleEquatable right)
    {
        return (Object.ReferenceEquals(left, null)
            && Object.ReferenceEquals(right, null))
            || (!Object.ReferenceEquals(left, null) 
            && left.Equals(right));
    }

    public static bool operator !=(SampleEquatable left, SampleEquatable right)
    {
        return !(left == right);
    }
}

public class SampleEquatableTest
{
    [VerifyContract]
    public readonly IContract EqualityTests = new EqualityContract<SampleEquatable>
    {
        ImplementsOperatorOverloads = true, // Optional (default is true)
        EquivalenceClasses =
        {
            { new SampleEquatable(1) },
            { new SampleEquatable(2) },
            { new SampleEquatable(3) },
            { new SampleEquatable(4) },
            { new SampleEquatable(5) }
        }
    };
}

Inheritance Hierarchy

System..::.Object
  MbUnit.Framework.ContractVerifiers..::.AbstractContract
    MbUnit.Framework.ContractVerifiers..::.EqualityContract<(Of <(TTarget>)>)

See Also