diff --git a/release_notes.md b/release_notes.md index 213191fb1..e4a48d143 100644 --- a/release_notes.md +++ b/release_notes.md @@ -1,5 +1,6 @@ ###In Development - [#320](https://github.com/MehdiK/Humanizer/pull/320): Fixed Dehumanize actually humanizing an already dehumanized string + - [#322](https://github.com/MehdiK/Humanizer/pull/322): DefaultFormatter.TimeSpanHumanize throws a more meaningful exception when called with TimeUnits larger than TimeUnit.Week [Commits](https://github.com/MehdiK/Humanizer/compare/v1.28.0...master) diff --git a/src/Humanizer.Tests/Humanizer.Tests.csproj b/src/Humanizer.Tests/Humanizer.Tests.csproj index b39cce5b1..213ede989 100644 --- a/src/Humanizer.Tests/Humanizer.Tests.csproj +++ b/src/Humanizer.Tests/Humanizer.Tests.csproj @@ -70,6 +70,7 @@ + diff --git a/src/Humanizer.Tests/Localisation/DefaultFormatterTests.cs b/src/Humanizer.Tests/Localisation/DefaultFormatterTests.cs new file mode 100644 index 000000000..e00222ce5 --- /dev/null +++ b/src/Humanizer.Tests/Localisation/DefaultFormatterTests.cs @@ -0,0 +1,24 @@ +using System; +using System.Globalization; +using Humanizer.Localisation; +using Humanizer.Localisation.Formatters; +using Xunit; +using Xunit.Extensions; + +namespace Humanizer.Tests.Localisation +{ + public class DefaultFormatterTests + { + [Theory] + [InlineData(TimeUnit.Month, 1)] + [InlineData(TimeUnit.Month, 2)] + [InlineData(TimeUnit.Month, 10)] + [InlineData(TimeUnit.Year, 1)] + [InlineData(TimeUnit.Year, 2)] + [InlineData(TimeUnit.Year, 10)] + public void TimeSpanHumanizeThrowsExceptionForTimeUnitsLargerThanWeek(TimeUnit timeUnit, int unit) + { + Assert.Throws(() => new DefaultFormatter(CultureInfo.InvariantCulture.Name).TimeSpanHumanize(timeUnit, unit)); + } + } +} \ No newline at end of file diff --git a/src/Humanizer/Localisation/Formatters/DefaultFormatter.cs b/src/Humanizer/Localisation/Formatters/DefaultFormatter.cs index f0eaaa0d0..37cb2bd09 100644 --- a/src/Humanizer/Localisation/Formatters/DefaultFormatter.cs +++ b/src/Humanizer/Localisation/Formatters/DefaultFormatter.cs @@ -1,4 +1,5 @@ -using System.Globalization; +using System; +using System.Globalization; namespace Humanizer.Localisation.Formatters { @@ -51,11 +52,15 @@ public virtual string TimeSpanHumanize_Zero() /// /// Returns the string representation of the provided TimeSpan /// - /// + /// Must be less than or equal to TimeUnit.Week /// /// + /// Is thrown when timeUnit is larger than TimeUnit.Week public virtual string TimeSpanHumanize(TimeUnit timeUnit, int unit) { + if (timeUnit > TimeUnit.Week) + throw new ArgumentOutOfRangeException("timeUnit", "There's no meaningful way to humanize passed timeUnit."); + return GetResourceForTimeSpan(timeUnit, unit); }