Skip to content

Commit

Permalink
Merge pull request #131 from MehdiK/clean-DateHumanizeTests
Browse files Browse the repository at this point in the history
Clean date humanize tests
  • Loading branch information
MehdiK committed Apr 6, 2014
2 parents af56c1a + 17c55ae commit 6335996
Show file tree
Hide file tree
Showing 20 changed files with 332 additions and 385 deletions.
2 changes: 2 additions & 0 deletions release_notes.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
###In Development
- [#124](https://github.com/MehdiK/Humanizer/pull/124): Added Slovak localization (strings, formatter, tests)
- [#130](https://github.com/MehdiK/Humanizer/pull/130): Added Czech localization (strings, formatter, tests)

[Commits](https://github.com/MehdiK/Humanizer/compare/v1.16.4...master)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,15 +149,15 @@ public enum LetterCasing
public class DefaultFormatter
{
public DefaultFormatter() { }
public string DateHumanize(Humanizer.Localisation.TimeUnit timeUnit, Humanizer.Localisation.TimeUnitTense timeUnitTense, int unit) { }
public string DateHumanize(Humanizer.Localisation.TimeUnit timeUnit, Humanizer.Localisation.Tense timeUnitTense, int unit) { }
public string DateHumanize_Now() { }
public string TimeSpanHumanize(Humanizer.Localisation.TimeUnit timeUnit, int unit) { }
public string TimeSpanHumanize_Zero() { }
}

public interface IFormatter
{
string DateHumanize(Humanizer.Localisation.TimeUnit timeUnit, Humanizer.Localisation.TimeUnitTense timeUnitTense, int unit);
string DateHumanize(Humanizer.Localisation.TimeUnit timeUnit, Humanizer.Localisation.Tense timeUnitTense, int unit);
string DateHumanize_Now();
string TimeSpanHumanize(Humanizer.Localisation.TimeUnit timeUnit, int unit);
string TimeSpanHumanize_Zero();
Expand All @@ -173,6 +173,13 @@ public class Resources
public string GetResource(string resourceKey) { }
}

public enum Tense
{
Future,
Past,
value__,
}

public enum TimeUnit
{
Day,
Expand All @@ -186,13 +193,6 @@ public enum TimeUnit
Year,
}

public enum TimeUnitTense
{
Future,
Past,
value__,
}

public class NoMatchFoundException
{
public NoMatchFoundException() { }
Expand Down
62 changes: 62 additions & 0 deletions src/Humanizer.Tests/DateHumanize.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
using System;
using Humanizer.Localisation;
using Xunit;

namespace Humanizer.Tests
{
public class DateHumanize
{
static void VerifyWithCurrentDate(string expectedString, TimeSpan deltaFromNow)
{
var utcNow = DateTime.UtcNow;
var localNow = DateTime.Now;

// feels like the only way to avoid breaking tests because CPU ticks over is to inject the base date
Assert.Equal(expectedString, utcNow.Add(deltaFromNow).Humanize(utcDate: true, dateToCompareAgainst: utcNow));
Assert.Equal(expectedString, localNow.Add(deltaFromNow).Humanize(utcDate: false, dateToCompareAgainst: localNow));
}

static void VerifyWithDateInjection(string expectedString, TimeSpan deltaFromNow)
{
var utcNow = new DateTime(2013, 6, 20, 9, 58, 22, DateTimeKind.Utc);
var now = new DateTime(2013, 6, 20, 11, 58, 22, DateTimeKind.Local);

Assert.Equal(expectedString, utcNow.Add(deltaFromNow).Humanize(utcDate: true, dateToCompareAgainst: utcNow));
Assert.Equal(expectedString, now.Add(deltaFromNow).Humanize(false, now));
}

public static void Verify(string expectedString, int unit, TimeUnit timeUnit, Tense tense)
{
var deltaFromNow = new TimeSpan();
unit = Math.Abs(unit);

if (tense == Tense.Past)
unit = -unit;

switch (timeUnit)
{
case TimeUnit.Second:
deltaFromNow = TimeSpan.FromSeconds(unit);
break;
case TimeUnit.Minute:
deltaFromNow = TimeSpan.FromMinutes(unit);
break;
case TimeUnit.Hour:
deltaFromNow = TimeSpan.FromHours(unit);
break;
case TimeUnit.Day:
deltaFromNow = TimeSpan.FromDays(unit);
break;
case TimeUnit.Month:
deltaFromNow = TimeSpan.FromDays(unit*31);
break;
case TimeUnit.Year:
deltaFromNow = TimeSpan.FromDays(unit*366);
break;
}

VerifyWithCurrentDate(expectedString, deltaFromNow);
VerifyWithDateInjection(expectedString, deltaFromNow);
}
}
}
Loading

0 comments on commit 6335996

Please sign in to comment.