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

Byte size #99

Merged
merged 11 commits into from
Mar 17, 2014
89 changes: 89 additions & 0 deletions src/Humanizer.Tests/ByteSizeTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
using Humanizer.Bytes;
using Xunit;

namespace Humanizer.Tests
{
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 Gigabytes()
{
var size = (2.0).Gigabytes();
Assert.Equal(ByteSize.FromGigaBytes(2), size);
}

[Fact]
public void HumanizesGB()
{
var humanized = (2.0).Gigabytes().Humanize();
Assert.Equal("2 GB", humanized);
}

[Fact]
public void Kilobytes()
{
var size = (2.0).Kilobytes();
Assert.Equal(ByteSize.FromKiloBytes(2), size);
}

[Fact]
public void HumanizesKB()
{
var humanized = (2.0).Kilobytes().Humanize();
Assert.Equal("2 KB", humanized);
}

[Fact]
public void Bytes()
{
var size = (2.0).Bytes();
Assert.Equal(ByteSize.FromBytes(2), size);
}

[Fact]
public void HumanizesBytes()
{
var humanized = (2.0).Bytes().Humanize();
Assert.Equal("2 B", humanized);
}

[Fact]
public void Bits()
{
var size = (2).Bits();
Assert.Equal(ByteSize.FromBits(2), size);
}

[Fact]
public void HumanizesBits()
{
var humanized = (2).Bits().Humanize();
Assert.Equal("2 b", humanized);
}
[Fact]
public void HumanizesWithFormat()
{
// TODO
}

[Fact]
public void Dehumanizes()
{
// TODO
}
}
}
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.Bytes;
using Xunit;

namespace Humanizer.Tests.Bytes
{
public class CreatingMethods
{
[Fact]
public void Constructor()
{
// 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);
}
}
}
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.Bytes;
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