Skip to content

Commit

Permalink
Added more meaningful exception to DefaultFormatter.TimespanHumanize
Browse files Browse the repository at this point in the history
  • Loading branch information
Borzoo authored and mexx committed Aug 7, 2014
1 parent b53b431 commit 974fafc
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 2 deletions.
1 change: 1 addition & 0 deletions release_notes.md
Original file line number Diff line number Diff line change
@@ -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)

Expand Down
1 change: 1 addition & 0 deletions src/Humanizer.Tests/Humanizer.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
<Compile Include="Localisation\bn-BD\DateHumanizeTests.cs" />
<Compile Include="Localisation\bn-BD\NumberToWordsTests.cs" />
<Compile Include="Localisation\bn-BD\TimeSpanHumanizeTests.cs" />
<Compile Include="Localisation\DefaultFormatterTests.cs" />
<Compile Include="Localisation\nb\DateHumanizeTests.cs" />
<Compile Include="Localisation\nb\TimeSpanHumanizeTests.cs" />
<Compile Include="Localisation\cs\DateHumanizeTests.cs" />
Expand Down
24 changes: 24 additions & 0 deletions src/Humanizer.Tests/Localisation/DefaultFormatterTests.cs
Original file line number Diff line number Diff line change
@@ -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<ArgumentOutOfRangeException>(() => new DefaultFormatter(CultureInfo.InvariantCulture.Name).TimeSpanHumanize(timeUnit, unit));
}
}
}
9 changes: 7 additions & 2 deletions src/Humanizer/Localisation/Formatters/DefaultFormatter.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Globalization;
using System;
using System.Globalization;

namespace Humanizer.Localisation.Formatters
{
Expand Down Expand Up @@ -51,11 +52,15 @@ public virtual string TimeSpanHumanize_Zero()
/// <summary>
/// Returns the string representation of the provided TimeSpan
/// </summary>
/// <param name="timeUnit"></param>
/// <param name="timeUnit">Must be less than or equal to TimeUnit.Week</param>
/// <param name="unit"></param>
/// <returns></returns>
/// <exception cref="System.ArgumentOutOfRangeException">Is thrown when timeUnit is larger than TimeUnit.Week</exception>
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);
}

Expand Down

0 comments on commit 974fafc

Please sign in to comment.