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

Namespace:  MbUnit.Framework.ContractVerifiers
Assembly:  MbUnit (in MbUnit.dll) Version: 3.2.0.0 (3.2.528.0)

Syntax

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

Type Parameters

TTarget
The type of the object to evaluate.

Remarks

Built-in verifications:

  • ComparableCompareTo : The type implements the method CompareTo(T). The method behaves as expected agains the provided equivalence classes.
  • OperatorGreaterThan : The type has a static "Greater Than" operator overload which behaves correctly against the provided equivalence classes. Disable that test by setting the ImplementsOperatorOverloads property to false.
  • OperatorGreaterThanOrEqual : The type has a static "Greater Than Or Equal" operator overload which behaves correctly against the provided equivalence classes. Disable that test by setting the ImplementsOperatorOverloads property to false.
  • OperatorLessThan : The type has a static "Less Than" operator overload which behaves correctly against the provided equivalence classes. Disable that test by setting the ImplementsOperatorOverloads property to false.
  • OperatorLessThanOrEqual : The type has a static "Less Than Or Equal" 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 IComparable<(Of <(T>)>) interface, and a test fixture which uses the comparison contract to test it.
CopyC#
public class SampleComparable : IComparable<SampleComparable>
{
    private int value;

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

    public int CompareTo(SampleComparable other)
    {
        return Object.ReferenceEquals(other, null) 
            ? Int32.MaxValue 
            : value.CompareTo(other.value);
    }

    public static bool operator >=(SampleComparable left, SampleComparable right)
    {
        return (Object.ReferenceEquals(left, null) 
            && Object.ReferenceEquals(right, null)) 
            || (!Object.ReferenceEquals(left, null) 
            && (left.CompareTo(right) >= 0));
    }

    public static bool operator <=(SampleComparable left, SampleComparable right)
    {
        return Object.ReferenceEquals(left, null) 
            || (left.CompareTo(right) <= 0);
    }

    public static bool operator >(SampleComparable left, SampleComparable right)
    {
        return !Object.ReferenceEquals(left, null) 
            && (left.CompareTo(right) > 0);
    }

    public static bool operator <(SampleComparable left, SampleComparable right)
    {
        return (!Object.ReferenceEquals(left, null) 
            || !Object.ReferenceEquals(right, null)) 
            && (Object.ReferenceEquals(left, null) 
            || (left.CompareTo(right) < 0));
    }

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

Inheritance Hierarchy

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

See Also