Search Results for

    Show / Hide Table of Contents

    GreaterThanOrEqual Constraint

    GreaterThanOrEqualConstraint tests that one value is greater than or equal to 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

    GreaterThanOrEqualConstraint(object expected)
    

    Syntax

    Is.GreaterThanOrEqualTo(object expected)
    Is.AtLeast(object expected)
    

    Modifiers

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

    Examples of Use

    [Test]
    public void GreaterThanOrEqual_Examples()
    {
        Assert.That(7, Is.GreaterThan(3));
        Assert.That(42, Is.Positive);
    
        var expectedDateTime = DateTime.Now;
        var myDateTime = DateTime.Now;
        Assert.That(myDateTime, Is.GreaterThan(expectedDateTime).Within(TimeSpan.FromSeconds(1)));
    
        Assert.That(7, Is.GreaterThanOrEqualTo(3));
        Assert.That(7, Is.AtLeast(3));
        Assert.That(7, Is.GreaterThanOrEqualTo(7));
        Assert.That(7, Is.AtLeast(7));
    
        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.GreaterThanOrEqualTo(42).Using(myComparer));
    
        // Using a lambda expression to compare MyOwnObject instances based on their Age property
        var myParentObject = new MyOwnObject(84);
        Assert.That(myParentObject, Is.GreaterThanOrEqualTo(myOwnObject).Using<MyOwnObject>((x, y) => x.Age.CompareTo(y.Age)));
    
        // Using a typed comparer
        var myTypedComparer = new MyComparerTyped();
        Assert.That(myParentObject, Is.GreaterThanOrEqualTo(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