Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Precision-based DateTimeHumanize (2nd shot) #165

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ public class CasingExtensions

public class Configurator
{
public Humanizer.DateTimeStrategy.IDateTimeHumanizeStrategy DateTimeHumanizeStrategy { get; set; }
public Humanizer.Localisation.IFormatter Formatter { get; }
}

Expand All @@ -73,6 +74,23 @@ public class DateHumanizeExtensions
public string Humanize(System.DateTime input, bool utcDate, System.Nullable<System.DateTime> dateToCompareAgainst) { }
}

public class DefaultDateTimeStrategy
{
public DefaultDateTimeStrategy() { }
public string Humanize(System.DateTime input, System.DateTime comparisonBase) { }
}

public interface IDateTimeHumanizeStrategy
{
string Humanize(System.DateTime input, System.DateTime comparisonBase);
}

public class PrecisionDateTimeStrategy
{
public PrecisionDateTimeStrategy(double precision) { }
public string Humanize(System.DateTime input, System.DateTime comparisonBase) { }
}

public class EnumDehumanizeExtensions
{
public TTargetEnum DehumanizeTo(string input) { }
Expand Down
12 changes: 11 additions & 1 deletion src/Humanizer.Tests/DateHumanize.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
using Humanizer.Configuration;
using Humanizer.DateTimeStrategy;
using Humanizer.Localisation;
using Xunit;

Expand All @@ -25,8 +27,13 @@ static void VerifyWithDateInjection(string expectedString, TimeSpan deltaFromNow
Assert.Equal(expectedString, now.Add(deltaFromNow).Humanize(false, now));
}

public static void Verify(string expectedString, int unit, TimeUnit timeUnit, Tense tense)
public static void Verify(string expectedString, int unit, TimeUnit timeUnit, Tense tense, double? precision = null)
{
if (precision.HasValue)
Configurator.DateTimeHumanizeStrategy = new PrecisionDateTimeStrategy(precision.Value);
else
Configurator.DateTimeHumanizeStrategy = new DefaultDateTimeStrategy();

var deltaFromNow = new TimeSpan();
unit = Math.Abs(unit);

Expand All @@ -35,6 +42,9 @@ public static void Verify(string expectedString, int unit, TimeUnit timeUnit, Te

switch (timeUnit)
{
case TimeUnit.Millisecond:
deltaFromNow = TimeSpan.FromMilliseconds(unit);
break;
case TimeUnit.Second:
deltaFromNow = TimeSpan.FromSeconds(unit);
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace Humanizer.Tests
{
public class DateHumanizeTests
public class DateHumanizeDefaultStrategyTests
{
[Theory]
[InlineData(1, "one second ago")]
Expand Down Expand Up @@ -125,4 +125,4 @@ public void YearsFromNow(int years, string expected)
DateHumanize.Verify(expected, years, TimeUnit.Year, Tense.Future);
}
}
}
}
189 changes: 189 additions & 0 deletions src/Humanizer.Tests/DateTimeHumanizePrecisionStrategyTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
using Humanizer.Localisation;
using Xunit.Extensions;

namespace Humanizer.Tests
{
public class DateTimeHumanizePrecisionStrategyTests
{
private const double defaultPrecision = .75;

[Theory]
[InlineData(1, "now")]
[InlineData(749, "now")]
[InlineData(750, "one second ago")]
[InlineData(1000, "one second ago")]
[InlineData(1749, "one second ago")]
[InlineData(1750, "2 seconds ago")]
public void MillisecondsAgo(int milliseconds, string expected)
{
DateHumanize.Verify(expected, milliseconds, TimeUnit.Millisecond, Tense.Past, defaultPrecision);
}

[Theory]
[InlineData(1, "now")]
[InlineData(749, "now")]
[InlineData(750, "one second from now")]
[InlineData(1000, "one second from now")]
[InlineData(1749, "one second from now")]
[InlineData(1750, "2 seconds from now")]
public void MillisecondsFromNow(int milliseconds, string expected)
{
DateHumanize.Verify(expected, milliseconds, TimeUnit.Millisecond, Tense.Future, defaultPrecision);
}

[Theory]
[InlineData(1, "one second ago")]
[InlineData(10, "10 seconds ago")]
[InlineData(44, "44 seconds ago")]
[InlineData(45, "a minute ago")]
[InlineData(60, "a minute ago")]
[InlineData(104, "a minute ago")]
[InlineData(105, "2 minutes ago")]
[InlineData(120, "2 minutes ago")]
public void SecondsAgo(int seconds, string expected)
{
DateHumanize.Verify(expected, seconds, TimeUnit.Second, Tense.Past, defaultPrecision);
}

[Theory]
[InlineData(1, "one second from now")]
[InlineData(10, "10 seconds from now")]
[InlineData(44, "44 seconds from now")]
[InlineData(45, "a minute from now")]
[InlineData(60, "a minute from now")]
[InlineData(104, "a minute from now")]
[InlineData(105, "2 minutes from now")]
[InlineData(120, "2 minutes from now")]
public void SecondsFromNow(int seconds, string expected)
{
DateHumanize.Verify(expected, seconds, TimeUnit.Second, Tense.Future, defaultPrecision);
}

[Theory]
[InlineData(1, "a minute ago")]
[InlineData(10, "10 minutes ago")]
[InlineData(44, "44 minutes ago")]
[InlineData(45, "an hour ago")]
[InlineData(60, "an hour ago")]
[InlineData(104, "an hour ago")]
[InlineData(105, "2 hours ago")]
[InlineData(120, "2 hours ago")]
public void MinutesAgo(int minutes, string expected)
{
DateHumanize.Verify(expected, minutes, TimeUnit.Minute, Tense.Past, defaultPrecision);
}

[Theory]
[InlineData(1, "a minute from now")]
[InlineData(10, "10 minutes from now")]
[InlineData(44, "44 minutes from now")]
[InlineData(45, "an hour from now")]
[InlineData(60, "an hour from now")]
[InlineData(104, "an hour from now")]
[InlineData(105, "2 hours from now")]
[InlineData(120, "2 hours from now")]
public void MinutesFromNow(int minutes, string expected)
{
DateHumanize.Verify(expected, minutes, TimeUnit.Minute, Tense.Future, defaultPrecision);
}

[Theory]
[InlineData(1, "an hour ago")]
[InlineData(10, "10 hours ago")]
[InlineData(17, "17 hours ago")]
[InlineData(18, "yesterday")]
[InlineData(24, "yesterday")]
[InlineData(41, "yesterday")]
[InlineData(42, "2 days ago")]
[InlineData(48, "2 days ago")]
[InlineData(60, "2 days ago")]
public void HoursAgo(int hours, string expected)
{
DateHumanize.Verify(expected, hours, TimeUnit.Hour, Tense.Past, defaultPrecision);
}

[Theory]
[InlineData(1, "an hour from now")]
[InlineData(10, "10 hours from now")]
[InlineData(18, "tomorrow")]
[InlineData(24, "tomorrow")]
[InlineData(41, "tomorrow")]
[InlineData(42, "2 days from now")]
[InlineData(48, "2 days from now")]
[InlineData(60, "2 days from now")]
public void HoursFromNow(int hours, string expected)
{
DateHumanize.Verify(expected, hours, TimeUnit.Hour, Tense.Future, defaultPrecision);
}

[Theory]
[InlineData(1, "yesterday")]
[InlineData(10, "10 days ago")]
[InlineData(20, "20 days ago")]
[InlineData(22, "22 days ago")]
[InlineData(23, "one month ago")]
[InlineData(31, "one month ago")]
[InlineData(43, "one month ago")]
[InlineData(53, "2 months ago")]
public void DaysAgo(int days, string expected)
{
DateHumanize.Verify(expected, days, TimeUnit.Day, Tense.Past, defaultPrecision);
}

[Theory]
[InlineData(1, "tomorrow")]
[InlineData(10, "10 days from now")]
[InlineData(20, "20 days from now")]
[InlineData(22, "22 days from now")]
[InlineData(23, "one month from now")]
[InlineData(31, "one month from now")]
[InlineData(43, "one month from now")]
[InlineData(53, "2 months from now")]
public void DaysFromNow(int days, string expected)
{
DateHumanize.Verify(expected, days, TimeUnit.Day, Tense.Future, defaultPrecision);
}

[Theory]
[InlineData(1, "one month ago")]
[InlineData(8, "8 months ago")]
[InlineData(9, "one year ago")]
[InlineData(12, "one year ago")]
[InlineData(19, "one year ago")]
[InlineData(21, "2 years ago")]
[InlineData(24, "2 years ago")]
public void MonthsAgo(int months, string expected)
{
DateHumanize.Verify(expected, months, TimeUnit.Month, Tense.Past, defaultPrecision);
}

[Theory]
[InlineData(1, "one month from now")]
[InlineData(8, "8 months from now")]
[InlineData(9, "one year from now")]
[InlineData(12, "one year from now")]
[InlineData(19, "one year from now")]
[InlineData(21, "2 years from now")]
[InlineData(24, "2 years from now")]
public void MonthsFromNow(int months, string expected)
{
DateHumanize.Verify(expected, months, TimeUnit.Month, Tense.Future, defaultPrecision);
}

[Theory]
[InlineData(1, "one year ago")]
[InlineData(2, "2 years ago")]
public void YearsAgo(int years, string expected)
{
DateHumanize.Verify(expected, years, TimeUnit.Year, Tense.Past, defaultPrecision);
}

[Theory]
[InlineData(1, "one year from now")]
[InlineData(2, "2 years from now")]
public void YearsFromNow(int years, string expected)
{
DateHumanize.Verify(expected, years, TimeUnit.Year, Tense.Future, defaultPrecision);
}
}
}
3 changes: 2 additions & 1 deletion src/Humanizer.Tests/Humanizer.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,15 @@
<Compile Include="Localisation\sk\TimeSpanHumanizeTests.cs" />
<Compile Include="Localisation\sv\DateHumanizeTests.cs" />
<Compile Include="Localisation\sv\TimeSpanHumanizeTests.cs" />
<Compile Include="DateTimeHumanizePrecisionStrategyTests.cs" />
<Compile Include="ResourceKeyTests.cs" />
<Compile Include="RomanNumeralTests.cs" />
<Compile Include="StringExtensionsTests.cs" />
<Compile Include="ToQuantityTests.cs" />
<Compile Include="TransformersTests.cs" />
<Compile Include="NumberToOrdinalWordsTests.cs" />
<Compile Include="Localisation\ro-Ro\DateHumanizeTests.cs" />
<Compile Include="DateHumanizeTests.cs" />
<Compile Include="DateHumanizeDefaultStrategyTests.cs" />
<Compile Include="DehumanizeToEnumTests.cs" />
<Compile Include="EnumHumanizeTests.cs" />
<Compile Include="EnumUnderTest.cs" />
Expand Down
Loading