diff --git a/release_notes.md b/release_notes.md index 9a7730feb..9aa8935b2 100644 --- a/release_notes.md +++ b/release_notes.md @@ -1,4 +1,5 @@ ###In Development + - [#294](https://github.com/MehdiK/Humanizer/pull/294): Added support for untyped comparison of ByteSize - [#277](https://github.com/MehdiK/Humanizer/pull/277): Added support for custom enum description attribute property names - [#276](https://github.com/Mehdik/Humanizer/pull/276): Added Farsi ToOrdinalWords - [#281](https://github.com/Mehdik/Humanizer/pull/281): Changed the logic for handling hyphenation and large numbers ending in twelve for English ordinal words; e.g. before "twenty first" now "twenty-first" diff --git a/src/Humanizer.Tests/ApiApprover/PublicApiApprovalTest.approve_public_api.approved.txt b/src/Humanizer.Tests/ApiApprover/PublicApiApprovalTest.approve_public_api.approved.txt index 3a2bd3dd3..79bae93e1 100644 --- a/src/Humanizer.Tests/ApiApprover/PublicApiApprovalTest.approve_public_api.approved.txt +++ b/src/Humanizer.Tests/ApiApprover/PublicApiApprovalTest.approve_public_api.approved.txt @@ -29,6 +29,7 @@ public Humanizer.Bytes.ByteSize AddKilobytes(double value) { } public Humanizer.Bytes.ByteSize AddMegabytes(double value) { } public Humanizer.Bytes.ByteSize AddTerabytes(double value) { } + public int CompareTo(object obj) { } public int CompareTo(Humanizer.Bytes.ByteSize other) { } public bool Equals(object value) { } public bool Equals(Humanizer.Bytes.ByteSize value) { } diff --git a/src/Humanizer.Tests/Bytes/ComparingTests.cs b/src/Humanizer.Tests/Bytes/ComparingTests.cs new file mode 100644 index 000000000..a1877a372 --- /dev/null +++ b/src/Humanizer.Tests/Bytes/ComparingTests.cs @@ -0,0 +1,48 @@ +using System.Collections.Generic; +using System.Linq; +using Humanizer.Bytes; +using Xunit; +using Xunit.Extensions; + +namespace Humanizer.Tests.Bytes +{ + public class ComparingTests + { + [Theory] + [InlineData(13, 23, -1)] + [InlineData(23, 23, 0)] + [InlineData(45, 23, 1)] + public void CompareStrongTyped(double value, double valueToCompareWith, int expectedResult) + { + var valueSize = new ByteSize(value); + var otherSize = new ByteSize(valueToCompareWith); + var result = valueSize.CompareTo(otherSize); + + Assert.Equal(expectedResult, result); + } + + [Theory] + [InlineData(13, 23, -1)] + [InlineData(23, 23, 0)] + [InlineData(45, 23, 1)] + public void CompareUntyped(double value, double valueToCompareWith, int expectedResult) + { + var valueSize = new ByteSize(value); + object otherSize = new ByteSize(valueToCompareWith); + var result = valueSize.CompareTo(otherSize); + + Assert.Equal(expectedResult, result); + } + + [Theory] + [InlineData(new[] { "1GB", "3KB", "5MB" }, new[] { "3KB", "5MB", "1GB"})] + [InlineData(new[] { "1MB", "3KB", "5MB" }, new[] { "3KB", "1MB", "5MB"})] + public void SortList(IEnumerable values, IEnumerable expected) + { + var list = values.Select(ByteSize.Parse).ToList(); + list.Sort(); + + Assert.Equal(expected.Select(ByteSize.Parse), list); + } + } +} \ No newline at end of file diff --git a/src/Humanizer.Tests/Humanizer.Tests.csproj b/src/Humanizer.Tests/Humanizer.Tests.csproj index 9d2a1d449..b50f55872 100644 --- a/src/Humanizer.Tests/Humanizer.Tests.csproj +++ b/src/Humanizer.Tests/Humanizer.Tests.csproj @@ -56,6 +56,7 @@ + diff --git a/src/Humanizer/Bytes/ByteSize.cs b/src/Humanizer/Bytes/ByteSize.cs index 253f749d5..89388bcaf 100644 --- a/src/Humanizer/Bytes/ByteSize.cs +++ b/src/Humanizer/Bytes/ByteSize.cs @@ -28,7 +28,7 @@ namespace Humanizer.Bytes /// Represents a byte size value. /// #pragma warning disable 1591 - public struct ByteSize : IComparable, IEquatable + public struct ByteSize : IComparable, IEquatable, IComparable { public static readonly ByteSize MinValue = FromBits(long.MinValue); public static readonly ByteSize MaxValue = FromBits(long.MaxValue); @@ -205,6 +205,17 @@ public override int GetHashCode() return Bits.GetHashCode(); } + public int CompareTo(object obj) + { + if (obj == null) + return 1; + + if (!(obj is ByteSize)) + throw new ArgumentException("Object is not a ByteSize"); + + return CompareTo((ByteSize) obj); + } + public int CompareTo(ByteSize other) { return Bits.CompareTo(other.Bits);