Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

provide untyped comparison for ByteSize #294

Merged
merged 3 commits into from
Jun 12, 2014
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions release_notes.md
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) { }
Expand Down
35 changes: 35 additions & 0 deletions src/Humanizer.Tests/Bytes/ComparingTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the unit tests. I'd also like to see tests for sorting here; e.g. 1GB, 3KB and 5MB should sort to 3KB, 5MB and 1GB.

{
var valueSize = new ByteSize(value);
object otherSize = new ByteSize(valueToCompareWith);
var result = valueSize.CompareTo(otherSize);

Assert.Equal(expectedResult, result);
}
}
}
1 change: 1 addition & 0 deletions src/Humanizer.Tests/Humanizer.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="Bytes\ComparingTests.cs" />
<Compile Include="Bytes\CreatingTests.cs" />
<Compile Include="Bytes\ByteSizeExtensionsTests.cs" />
<Compile Include="Bytes\ParsingTests.cs" />
Expand Down
13 changes: 12 additions & 1 deletion src/Humanizer/Bytes/ByteSize.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ namespace Humanizer.Bytes
/// Represents a byte size value.
/// </summary>
#pragma warning disable 1591
public struct ByteSize : IComparable<ByteSize>, IEquatable<ByteSize>
public struct ByteSize : IComparable<ByteSize>, IEquatable<ByteSize>, IComparable
{
public static readonly ByteSize MinValue = FromBits(long.MinValue);
public static readonly ByteSize MaxValue = FromBits(long.MaxValue);
Expand Down Expand Up @@ -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);
Expand Down