Skip to content

Commit

Permalink
Fail parse for invalid suffix; unify tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dahlbyk committed May 3, 2021
1 parent 9159cbe commit d4972e0
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
23 changes: 11 additions & 12 deletions src/Humanizer.Tests.Shared/Bytes/ParsingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ public void Parse()
Assert.Equal(ByteSize.FromKilobytes(1020), ByteSize.Parse("1020KB"));
}

[Fact]
public void TryParseThrowsOnNull()
{
Assert.Throws<ArgumentNullException>(() => { ByteSize.TryParse(null, out var result); });
}

[Fact]
public void TryParse()
{
Expand Down Expand Up @@ -89,13 +95,18 @@ public void ParseDecimalMegabytes()
[Theory]
[InlineData("Unexpected Value")]
[InlineData("1000")]
[InlineData(" 1000 ")]
[InlineData("KB")]
[InlineData("1000.5b")] // Partial bits
[InlineData("1000KBB")] // Bad suffix
public void TryParseReturnsFalseOnBadValue(string input)
{
var resultBool = ByteSize.TryParse(input, out var resultByteSize);

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

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

[Fact]
Expand All @@ -104,18 +115,6 @@ public void TryParseWorksWithLotsOfSpaces()
Assert.Equal(ByteSize.FromKilobytes(100), ByteSize.Parse(" 100 KB "));
}

[Fact]
public void ParseThrowsOnPartialBits()
{
Assert.Throws<FormatException>(() => { ByteSize.Parse("10.5b"); });
}

[Fact]
public void ParseThrowsOnInvalid()
{
Assert.Throws<FormatException>(() => { ByteSize.Parse("Unexpected Value"); });
}

[Fact]
public void ParseThrowsOnNull()
{
Expand Down
3 changes: 3 additions & 0 deletions src/Humanizer/Bytes/ByteSize.cs
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,9 @@ public static bool TryParse(string s, IFormatProvider formatProvider, out ByteSi
case TerabyteSymbol:
result = FromTerabytes(number);
break;

default:
return false;
}

return true;
Expand Down

0 comments on commit d4972e0

Please sign in to comment.