Skip to content

Commit

Permalink
Integrate ByteSize library to address issue #94. Rough sketch.
Browse files Browse the repository at this point in the history
Needs:
- [] Rest of tests
- [] Dehumanize byte quantities
- [] Refactor namespaces all around
- [] Add to readme
- [] Licensing
  • Loading branch information
GeorgeHahn committed Mar 6, 2014
1 parent fc4ec70 commit 7e1bdc6
Show file tree
Hide file tree
Showing 8 changed files with 936 additions and 0 deletions.
110 changes: 110 additions & 0 deletions src/Humanizer.Tests/Bytes/CreatingMethods.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
using Humanizer.ByteSizez;

This comment has been minimized.

Copy link
@MehdiK

MehdiK Mar 7, 2014

Member

I think you mean ByteSize and not ByteSizez!?

using Xunit;

namespace Humanizer.Tests.Bytes
{
public class CreatingMethods
{
[Fact]
public void Constructor()

This comment has been minimized.

Copy link
@MehdiK

MehdiK Mar 7, 2014

Member

Please change the tests to use Theory and InlineData

This comment has been minimized.

Copy link
@GeorgeHahn

GeorgeHahn Mar 7, 2014

Author Contributor

Having a bit of trouble on this one.

    [Theory]
    [InlineData(1099511627776, 8.796093022208e12, x => (x.Bits)] // Doesn't know what Bits is
    [InlineData(1099511627776, 8.796093022208e12, (x) => (((ByteSize)x).Bits)] // Can't cast lambda to object
    // ...
    public void Constructorx(double size, double expected, Func<ByteSize, double> getProp)
    {
        // Arrange
        double byteSize = size;

        // Act
        var result = new ByteSize(byteSize);

        // Assert
        Assert.Equal(expected, getProp(result));
    }

The only way to break this up may be to split it into multiple functions.

This comment has been minimized.

Copy link
@MehdiK

MehdiK Mar 8, 2014

Member

Yeah, that won't work. Don't worry about this one then.

{
// Arrange
double byteSize = 1099511627776;

// Act
var result = new ByteSize(byteSize);

// Assert
Assert.Equal(8.796093022208e12, result.Bits);
Assert.Equal(1099511627776, result.Bytes);
Assert.Equal(1073741824, result.KiloBytes);
Assert.Equal(1048576, result.MegaBytes);
Assert.Equal(1024, result.GigaBytes);
Assert.Equal(1, result.TeraBytes);
}

[Fact]
public void FromBitsMethod()
{
// Arrange
long value = 8;

// Act
var result = ByteSize.FromBits(value);

// Assert
Assert.Equal(8, result.Bits);
Assert.Equal(1, result.Bytes);
}

[Fact]
public void FromBytesMethod()
{
// Arrange
double value = 1.5;

// Act
var result = ByteSize.FromBytes(value);

// Assert
Assert.Equal(12, result.Bits);
Assert.Equal(1.5, result.Bytes);
}

[Fact]
public void FromKiloBytesMethod()
{
// Arrange
double value = 1.5;

// Act
var result = ByteSize.FromKiloBytes(value);

// Assert
Assert.Equal(1536, result.Bytes);
Assert.Equal(1.5, result.KiloBytes);
}

[Fact]
public void FromMegaBytesMethod()
{
// Arrange
double value = 1.5;

// Act
var result = ByteSize.FromMegaBytes(value);

// Assert
Assert.Equal(1572864, result.Bytes);
Assert.Equal(1.5, result.MegaBytes);
}

[Fact]
public void FromGigaBytesMethod()
{
// Arrange
double value = 1.5;

// Act
var result = ByteSize.FromGigaBytes(value);

// Assert
Assert.Equal(1610612736, result.Bytes);
Assert.Equal(1.5, result.GigaBytes);
}

[Fact]
public void FromTeraBytesMethod()
{
// Arrange
double value = 1.5;

// Act
var result = ByteSize.FromTeraBytes(value);

// Assert
Assert.Equal(1649267441664, result.Bytes);
Assert.Equal(1.5, result.TeraBytes);
}
}
}
35 changes: 35 additions & 0 deletions src/Humanizer.Tests/Bytes/FluentMethods.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using Humanizer.ByteSizez;
using Xunit;

namespace Humanizer.Tests.Bytes
{
public class FluentMethods
{
[Fact]
public void Terabytes()
{
var size = (2.0).Terabytes();
Assert.Equal(ByteSize.FromTeraBytes(2), size);
}

[Fact]
public void HumanizesTB()
{
var humanized = (2.0).Terabytes().Humanize();
Assert.Equal("2 TB", humanized);
}

[Fact]
public void Humanizes()

This comment has been minimized.

Copy link
@MehdiK

MehdiK Mar 7, 2014

Member

Cool :)

{
var humanized = (2.0).Terabytes().Humanize();
Assert.Equal("2 TB", humanized);
}

[Fact]
public void Dehumanizes()
{
// Todo
}
}
}
185 changes: 185 additions & 0 deletions src/Humanizer.Tests/Bytes/ParsingMethods.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
using System;
using Humanizer.ByteSizez;
using Xunit;

namespace Humanizer.Tests.Bytes
{
public class ParsingMethods
{
// Base parsing functionality
[Fact]
public void Parse()
{
string val = "1020KB";
var expected = ByteSize.FromKiloBytes(1020);

var result = ByteSize.Parse(val);

Assert.Equal(expected, result);
}

[Fact]
public void TryParse()
{
string val = "1020KB";
var expected = ByteSize.FromKiloBytes(1020);

ByteSize resultByteSize;
var resultBool = ByteSize.TryParse(val, out resultByteSize);

Assert.True(resultBool);
Assert.Equal(expected, resultByteSize);
}

[Fact]
public void ParseDecimalMB()
{
string val = "100.5MB";
var expected = ByteSize.FromMegaBytes(100.5);

var result = ByteSize.Parse(val);

Assert.Equal(expected, result);
}

// Failure modes
[Fact]
public void TryParseReturnsFalseOnBadValue()
{
string val = "Unexpected Value";

ByteSize resultByteSize;
var resultBool = ByteSize.TryParse(val, out resultByteSize);

Assert.False(resultBool);
Assert.Equal(new ByteSize(), resultByteSize);
}

[Fact]
public void TryParseReturnsFalseOnMissingMagnitude()
{
string val = "1000";

ByteSize resultByteSize;
var resultBool = ByteSize.TryParse(val, out resultByteSize);

Assert.False(resultBool);
Assert.Equal(new ByteSize(), resultByteSize);
}

[Fact]
public void TryParseReturnsFalseOnMissingValue()
{
string val = "KB";

ByteSize resultByteSize;
var resultBool = ByteSize.TryParse(val, out resultByteSize);

Assert.False(resultBool);
Assert.Equal(new ByteSize(), resultByteSize);
}

[Fact]
public void TryParseWorksWithLotsOfSpaces()
{
string val = " 100 KB ";
var expected = ByteSize.FromKiloBytes(100);

var result = ByteSize.Parse(val);

Assert.Equal(expected, result);
}

[Fact]
public void ParsePartialBits()
{
string val = "10.5b";

Assert.Throws<FormatException>(() => { ByteSize.Parse(val); });
}


// Parse method throws exceptions
[Fact]
public void ParseThrowsOnInvalid()
{
string badValue = "Unexpected Value";

Assert.Throws<FormatException>(() => { ByteSize.Parse(badValue); });
}

[Fact]
public void ParseThrowsOnNull()
{
Assert.Throws<ArgumentNullException>(() => { ByteSize.Parse(null); });
}


// Various magnitudes
[Fact]
public void ParseBits()
{
string val = "1b";
var expected = ByteSize.FromBits(1);

var result = ByteSize.Parse(val);

Assert.Equal(expected, result);
}

[Fact]
public void ParseBytes()
{
string val = "1B";
var expected = ByteSize.FromBytes(1);

var result = ByteSize.Parse(val);

Assert.Equal(expected, result);
}

[Fact]
public void ParseKB()
{
string val = "1020KB";
var expected = ByteSize.FromKiloBytes(1020);

var result = ByteSize.Parse(val);

Assert.Equal(expected, result);
}

[Fact]
public void ParseMB()
{
string val = "1000MB";
var expected = ByteSize.FromMegaBytes(1000);

var result = ByteSize.Parse(val);

Assert.Equal(expected, result);
}

[Fact]
public void ParseGB()
{
string val = "805GB";
var expected = ByteSize.FromGigaBytes(805);

var result = ByteSize.Parse(val);

Assert.Equal(expected, result);
}

[Fact]
public void ParseTB()
{
string val = "100TB";
var expected = ByteSize.FromTeraBytes(100);

var result = ByteSize.Parse(val);

Assert.Equal(expected, result);
}
}
}
Loading

1 comment on commit 7e1bdc6

@MehdiK
Copy link
Member

@MehdiK MehdiK commented on 7e1bdc6 Mar 7, 2014

Choose a reason for hiding this comment

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

Thanks a lot for getting the ball rolling. If you could send this as a PR I will be able to work with you on it and hopefully we can get something out quickly.

Please sign in to comment.