From d4972e020bb759328a0969ee67da162303123bc3 Mon Sep 17 00:00:00 2001 From: Keith Dahlby Date: Sun, 2 May 2021 01:43:34 -0500 Subject: [PATCH] Fail parse for invalid suffix; unify tests --- .../Bytes/ParsingTests.cs | 23 +++++++++---------- src/Humanizer/Bytes/ByteSize.cs | 3 +++ 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/src/Humanizer.Tests.Shared/Bytes/ParsingTests.cs b/src/Humanizer.Tests.Shared/Bytes/ParsingTests.cs index b4478ba05..eec480745 100644 --- a/src/Humanizer.Tests.Shared/Bytes/ParsingTests.cs +++ b/src/Humanizer.Tests.Shared/Bytes/ParsingTests.cs @@ -37,6 +37,12 @@ public void Parse() Assert.Equal(ByteSize.FromKilobytes(1020), ByteSize.Parse("1020KB")); } + [Fact] + public void TryParseThrowsOnNull() + { + Assert.Throws(() => { ByteSize.TryParse(null, out var result); }); + } + [Fact] public void TryParse() { @@ -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(() => { ByteSize.Parse(input); }); } [Fact] @@ -104,18 +115,6 @@ public void TryParseWorksWithLotsOfSpaces() Assert.Equal(ByteSize.FromKilobytes(100), ByteSize.Parse(" 100 KB ")); } - [Fact] - public void ParseThrowsOnPartialBits() - { - Assert.Throws(() => { ByteSize.Parse("10.5b"); }); - } - - [Fact] - public void ParseThrowsOnInvalid() - { - Assert.Throws(() => { ByteSize.Parse("Unexpected Value"); }); - } - [Fact] public void ParseThrowsOnNull() { diff --git a/src/Humanizer/Bytes/ByteSize.cs b/src/Humanizer/Bytes/ByteSize.cs index dc54d5ace..77d14d0da 100644 --- a/src/Humanizer/Bytes/ByteSize.cs +++ b/src/Humanizer/Bytes/ByteSize.cs @@ -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;