-
Notifications
You must be signed in to change notification settings - Fork 967
Commit
Needs: - [] Rest of tests - [] Dehumanize byte quantities - [] Refactor namespaces all around - [] Add to readme - [] Licensing
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
using Humanizer.ByteSizez; | ||
This comment has been minimized.
Sorry, something went wrong. |
||
using Xunit; | ||
|
||
namespace Humanizer.Tests.Bytes | ||
{ | ||
public class CreatingMethods | ||
{ | ||
[Fact] | ||
public void Constructor() | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
GeorgeHahn
Author
Contributor
|
||
{ | ||
// 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); | ||
} | ||
} | ||
} |
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.
Sorry, something went wrong. |
||
{ | ||
var humanized = (2.0).Terabytes().Humanize(); | ||
Assert.Equal("2 TB", humanized); | ||
} | ||
|
||
[Fact] | ||
public void Dehumanizes() | ||
{ | ||
// Todo | ||
} | ||
} | ||
} |
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); | ||
} | ||
} | ||
} |
1 comment
on commit 7e1bdc6
There was a problem hiding this comment.
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.
I think you mean ByteSize and not ByteSizez!?