Skip to content

Commit

Permalink
Adjust ByteSize Parse/TryParse exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
dahlbyk committed May 3, 2021
1 parent d4972e0 commit 94694af
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
8 changes: 6 additions & 2 deletions src/Humanizer.Tests.Shared/Bytes/ParsingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,10 @@ public void Parse()
}

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

[Fact]
Expand Down Expand Up @@ -93,6 +94,9 @@ public void ParseDecimalMegabytes()
}

[Theory]
[InlineData("")]
[InlineData(" ")]
[InlineData("\t")]
[InlineData("Unexpected Value")]
[InlineData("1000")]
[InlineData(" 1000 ")]
Expand Down
8 changes: 7 additions & 1 deletion src/Humanizer/Bytes/ByteSize.cs
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,8 @@ public static bool TryParse(string s, IFormatProvider formatProvider, out ByteSi
// Arg checking
if (string.IsNullOrWhiteSpace(s))
{
throw new ArgumentNullException(nameof(s), "String is null or whitespace");
result = default;
return false;
}

// Acquiring culture-specific parsing info
Expand Down Expand Up @@ -586,6 +587,11 @@ public static ByteSize Parse(string s)

public static ByteSize Parse(string s, IFormatProvider formatProvider)
{
if (s == null)
{
throw new ArgumentNullException(nameof(s));
}

if (TryParse(s, formatProvider, out var result))
{
return result;
Expand Down

0 comments on commit 94694af

Please sign in to comment.