Search Results for

    Show / Hide Table of Contents

    GreaterThan Constraint

    GreaterThanConstraint tests that one value is greater than another.

    It works with numeric types, DateTime, TimeSpan, and any type implementing IComparable. For custom types, a user-specified comparer can be provided using the Using modifier.

    Constructor

    GreaterThanConstraint(object expected)
    

    Syntax

    Is.GreaterThan(object expected)
    Is.Positive // Equivalent to Is.GreaterThan(0)
    

    Modifiers

    ...Using(IComparer comparer)
    ...Using<T>(IComparer<T> comparer)
    ...Using<T>(Comparison<T> comparer)
    ...Within(object tolerance)
    

    Examples of Use

    [Test]
    public void GreaterThan_Examples()
    {
        Assert.That(7, Is.GreaterThan(3));
        Assert.That(42, Is.Positive);
    
        var expectedDateTime = DateTime.Now.AddMinutes(-1);
        var myDateTime = DateTime.Now;
        Assert.That(myDateTime, Is.GreaterThan(expectedDateTime).Within(TimeSpan.FromSeconds(1)));
    
        var myOwnObject = new MyOwnObject(42);
    
        // Using an untyped comparer that can handle both MyOwnObject and int types, comparing based on the Age property of MyOwnObject instances.
        var myComparer = new MyComparerUntyped();
        Assert.That(myOwnObject, Is.GreaterThan(41).Using(myComparer));
    
        // Using a lambda expression to compare MyOwnObject instances based on their Age property
        var myParentObject = new MyOwnObject(84);
        Assert.That(myParentObject, Is.GreaterThan(myOwnObject).Using<MyOwnObject>((x, y) => x.Age.CompareTo(y.Age)));
    
        // Using a typed comparer
        var myTypedComparer = new MyComparerTyped();
        Assert.That(myParentObject, Is.GreaterThan(myOwnObject).Using(myTypedComparer));
    }
    
    public class MyOwnObject(int age)
    {
        public int Age => age;
    }
    
    /// <summary>
    /// Comparer handling different types of objects (MyOwnObject and int) and comparing them based on the Age property of MyOwnObject instances.
    /// </summary>
    public class MyComparerUntyped : IComparer
    {
        public int Compare(object? x, object? y)
        {
            // Handle nulls
            if (ReferenceEquals(x, y)) return 0;
            if (x is null) return -1;
            if (y is null) return 1;
            // Identify which one is the object and which is the int
            int actualAge = x is MyOwnObject obj ? obj.Age : (int)x;
            int expectedAge = y is MyOwnObject objY ? objY.Age : (int)y;
    
            return actualAge.CompareTo(expectedAge);
        }
    }
    
    /// <summary>
    /// High performance comparer for equal typed objects that only compares the Age property of MyOwnObject instances.
    /// </summary>
    public class MyComparerTyped : IComparer<MyOwnObject>
    {
        public int Compare(MyOwnObject? x, MyOwnObject? y)
        {
            if (ReferenceEquals(x, y)) return 0;
            if (x is null) return -1;
            if (y is null) return 1;
    
            return x.Age.CompareTo(y.Age);
        }
    }
    
    
    • Edit this page
    In this article
    Back to top Generated by DocFX | Copyright (c) 2018- The NUnit Project - Licensed under CC BY-NC-SA 4.0