From 2b920959fc653a7feb265c4d43378929007dd0e6 Mon Sep 17 00:00:00 2001 From: Pim Brouwers Date: Sat, 7 Dec 2024 14:31:30 -0500 Subject: [PATCH] option icomparable --- .github/workflows/build.yml | 2 +- src/Danom/Option/Option.cs | 55 +++++++++++++++++++++++++- test/Danom.Tests/Option/OptionTests.cs | 24 +++++++++++ 3 files changed, 79 insertions(+), 2 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index b147a6a..4911a12 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -20,7 +20,7 @@ jobs: dotnet-version: ${{ matrix.dotnet-version }} - name: Install solution dependencies - run: dotnet restore --force-evaluate --no-http-cache --force + run: dotnet restore src/Danom.Mvc --force-evaluate --no-http-cache --force - name: Build Core run: dotnet build src/Danom -c Release diff --git a/src/Danom/Option/Option.cs b/src/Danom/Option/Option.cs index c0172b2..f50124f 100644 --- a/src/Danom/Option/Option.cs +++ b/src/Danom/Option/Option.cs @@ -6,7 +6,7 @@ namespace Danom; /// /// public readonly struct Option - : IEquatable> + : IEquatable>, IComparable> { private readonly T? _some = default; @@ -208,6 +208,42 @@ public static Task> NoneAsync() => public static bool operator !=(Option left, Option right) => !(left == right); + /// + /// Returns true if the specified is less than the other. + /// + /// + /// + /// + public static bool operator <(Option left, Option right) => + left.CompareTo(right) < 0; + + /// + /// Returns true if the specified is less than or equal to the other. + /// + /// + /// + /// + public static bool operator <=(Option left, Option right) => + left.CompareTo(right) <= 0; + + /// + /// Returns true if the specified is greater than the other. + /// + /// + /// + /// + public static bool operator >(Option left, Option right) => + left.CompareTo(right) > 0; + + /// + /// Returns true if the specified is greater than or equal to the other. + /// + /// + /// + /// + public static bool operator >=(Option left, Option right) => + left.CompareTo(right) >= 0; + /// /// Returns true if the specified s are equal. /// @@ -242,6 +278,23 @@ public override int GetHashCode() => some: x => x is null ? 0 : x.GetHashCode(), none: () => 0); + /// + /// Compares the to another . + /// + /// + /// + public int CompareTo(Option other) => + Match( + some: x1 => + other.Match( + some: x2 => Comparer.Default.Compare(x1, x2), + none: () => 1), + none: () => + other.Match( + some: _ => -1, + none: () => 0) + ); + /// /// Returns the string representation of the . /// diff --git a/test/Danom.Tests/Option/OptionTests.cs b/test/Danom.Tests/Option/OptionTests.cs index ac7580c..54e5eb8 100644 --- a/test/Danom.Tests/Option/OptionTests.cs +++ b/test/Danom.Tests/Option/OptionTests.cs @@ -205,4 +205,28 @@ public void ToStringDefaultOrFormat() Assert.NotEqual("0", Option.Some(1.9878565765675M).ToString("0", "C2")); Assert.Equal("£1.99", Option.Some(1.9878565765675M).ToString("0", "C2", CultureInfo.CreateSpecificCulture("en-GB"))); } + +#pragma warning disable CS1718 // Comparison made to same variable + [Fact] + public void Comparability() + { + var some1 = Option.Some(1); + var some2 = Option.Some(2); + var none = Option.None(); + + Assert.True(some1 < some2); + Assert.True(some1 <= some2); + Assert.True(some2 > some1); + Assert.True(some2 >= some1); + Assert.True(some1 <= some1); + Assert.True(some1 >= some1); + + Assert.True(none < some1); + Assert.True(none <= some1); + Assert.True(some1 > none); + Assert.True(some1 >= none); + Assert.True(none <= none); + Assert.True(none >= none); + } } +#pragma warning restore CS1718 // Comparison made to same variable