Skip to content

Commit

Permalink
Optional: Remove nongeneric CompareTo methods
Browse files Browse the repository at this point in the history
  • Loading branch information
andreise committed Jan 6, 2025
1 parent 8e297dc commit df2d95b
Show file tree
Hide file tree
Showing 4 changed files with 1 addition and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,46 +17,4 @@ public void Comparison_CompareTo_ValueAndDefault_ExpectZero()
var actual = Absent<T>.Value.CompareTo(default);
Assert.That(actual, Is.Zero);
}

[Test]
public void Comparison_CompareToObj_NewAndNull_ExpectOne()
{
var actual = new Absent<T>().CompareTo(null);
Assert.That(actual, Is.Positive);
Assert.That(actual, Is.EqualTo(1));
}

[Test]
public void Comparison_CompareToObj_ValueAndNull_ExpectOne()
{
var actual = Absent<T>.Value.CompareTo(null);
Assert.That(actual, Is.Positive);
Assert.That(actual, Is.EqualTo(1));
}

[Test]
public void Comparison_CompareToObj_NewAndDefault_ExpectZero()
{
var actual = new Absent<T>().CompareTo((object?)default(Absent<T>));
Assert.That(actual, Is.Zero);
}

[Test]
public void Comparison_CompareToObj_ValueAndDefault_ExpectZero()
{
var actual = Absent<T>.Value.CompareTo((object?)new Absent<T>());
Assert.That(actual, Is.Zero);
}

[Test]
public void Comparison_CompareToObj_NewAndAnotherType_ExpectZero()
{
Assert.Throws<ArgumentException>(() => _ = new Absent<T>().CompareTo(new object()));
}

[Test]
public void Comparison_CompareToObj_ValueAndAnotherType_ExpectZero()
{
Assert.Throws<ArgumentException>(() => _ = Absent<T>.Value.CompareTo(new object()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,4 @@
partial struct Absent<T>
{
public int CompareTo(Absent<T> other) => default;

public int CompareTo(object? obj) => obj switch
{
null => ComparisonResult.GreaterThan,

Absent<T> => ComparisonResult.EqualTo,

_ => throw new ArgumentException($"The object is not Absent<{typeof(T).Name}>.", nameof(obj))
};
}
2 changes: 1 addition & 1 deletion src/core-taggeds-optional/Optional/Absent.T/Absent.T.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
namespace System;

public readonly partial struct Absent<T> : IEquatable<Absent<T>>, IComparable<Absent<T>>, IComparable
public readonly partial struct Absent<T> : IEquatable<Absent<T>>, IComparable<Absent<T>>
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,4 @@ partial class OptionalExtensions
internal static int CompareTo<T>(this Optional<T> optional, Optional<T> other) where T : IComparable<T>
=>
optional.InternalCompareTo(other, Comparer<T>.Default);

// TODO: Add the tests and open the method
internal static int CompareTo<T>(this Optional<T> optional, object? obj) where T : IComparable<T>
=>
obj switch
{
null => ComparisonResult.GreaterThan,

Optional<T> other => optional.CompareTo(other),

_ => throw new ArgumentException($"The object is not Optional<{typeof(T).Name}>.", nameof(obj))
};
}

0 comments on commit df2d95b

Please sign in to comment.