From 94694afd58056ae924c1ae5a8064c292b4f39185 Mon Sep 17 00:00:00 2001 From: Keith Dahlby Date: Sun, 2 May 2021 09:45:30 -0500 Subject: [PATCH] Adjust ByteSize Parse/TryParse exceptions --- src/Humanizer.Tests.Shared/Bytes/ParsingTests.cs | 8 ++++++-- src/Humanizer/Bytes/ByteSize.cs | 8 +++++++- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/src/Humanizer.Tests.Shared/Bytes/ParsingTests.cs b/src/Humanizer.Tests.Shared/Bytes/ParsingTests.cs index eec480745..9c10f7acb 100644 --- a/src/Humanizer.Tests.Shared/Bytes/ParsingTests.cs +++ b/src/Humanizer.Tests.Shared/Bytes/ParsingTests.cs @@ -38,9 +38,10 @@ public void Parse() } [Fact] - public void TryParseThrowsOnNull() + public void TryParseReturnsFalseOnNull() { - Assert.Throws(() => { ByteSize.TryParse(null, out var result); }); + Assert.False(ByteSize.TryParse(null, out var result)); + Assert.Equal(default, result); } [Fact] @@ -93,6 +94,9 @@ public void ParseDecimalMegabytes() } [Theory] + [InlineData("")] + [InlineData(" ")] + [InlineData("\t")] [InlineData("Unexpected Value")] [InlineData("1000")] [InlineData(" 1000 ")] diff --git a/src/Humanizer/Bytes/ByteSize.cs b/src/Humanizer/Bytes/ByteSize.cs index 77d14d0da..0cab8a7d7 100644 --- a/src/Humanizer/Bytes/ByteSize.cs +++ b/src/Humanizer/Bytes/ByteSize.cs @@ -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 @@ -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;