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

Issue #70 - Replacing current static resource keys with new dynamic keys #106

Merged
merged 4 commits into from
Mar 23, 2014
Merged
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
84 changes: 84 additions & 0 deletions src/Humanizer.Tests/DateHumanizeOneTimeUnitTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
using System;
using System.Collections.Generic;
using Humanizer.Localisation;
using Xunit;
using Xunit.Extensions;

namespace Humanizer.Tests
{
public class DateHumanizeOneTimeUnitTests
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need these tests? These are already verified in DateHumanizeTests.

{
static void VerifyWithCurrentDate(string expectedString, TimeSpan deltaFromNow)
{
if (expectedString == null)
throw new ArgumentNullException("expectedString");

Assert.Equal(expectedString, DateTime.UtcNow.Add(deltaFromNow).Humanize());
Assert.Equal(expectedString, DateTime.Now.Add(deltaFromNow).Humanize(false));
}

static void VerifyWithDateInjection(string expectedString, TimeSpan deltaFromNow)
{
if (expectedString == null)
throw new ArgumentNullException("expectedString");

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(true, utcNow));
Assert.Equal(expectedString, now.Add(deltaFromNow).Humanize(false, now));
}

static void Verify(string expectedString, TimeSpan deltaFromNow)
{
VerifyWithCurrentDate(expectedString, deltaFromNow);
VerifyWithDateInjection(expectedString, deltaFromNow);
}

public static IEnumerable<object[]> OneTimeUnitAgoTestsSource
{
get
{
return new[] {
new object[]{ TimeUnit.Second, TimeSpan.FromSeconds(-1) },
new object[]{ TimeUnit.Minute, TimeSpan.FromMinutes(-1) },
new object[]{ TimeUnit.Hour, TimeSpan.FromHours(-1) },
new object[]{ TimeUnit.Day, TimeSpan.FromDays(-1) },
new object[]{ TimeUnit.Month, TimeSpan.FromDays(-30) },
new object[]{ TimeUnit.Year, TimeSpan.FromDays(-365) },
};
}
}

public static IEnumerable<object[]> OneTimeUnitFromNowTestsSource
{
get
{
return new[] {
new object[]{ TimeUnit.Second, TimeSpan.FromSeconds(1) },
new object[]{ TimeUnit.Minute, TimeSpan.FromMinutes(1) },
new object[]{ TimeUnit.Hour, TimeSpan.FromHours(1) },
new object[]{ TimeUnit.Day, TimeSpan.FromDays(1) },
new object[]{ TimeUnit.Month, TimeSpan.FromDays(30) },
new object[]{ TimeUnit.Year, TimeSpan.FromDays(365) },
};
}
}

[Theory]
[PropertyData("OneTimeUnitAgoTestsSource")]
public void OneTimeUnitAgo(TimeUnit timeUnit, TimeSpan timeSpan)
{
string resourceKey = ResourceKeys.DateHumanize.GetResourceKey(timeUnit, 1);
Verify(Resources.GetResource(resourceKey), timeSpan);
}

[Theory]
[PropertyData("OneTimeUnitFromNowTestsSource")]
public void OneTimeUnitFromNow(TimeUnit timeUnit, TimeSpan timeSpan)
{
string resourceKey = ResourceKeys.DateHumanize.GetResourceKey(timeUnit, 1, true);
Verify(Resources.GetResource(resourceKey), timeSpan);
}
}
}
60 changes: 33 additions & 27 deletions src/Humanizer.Tests/DateHumanizeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ public class DateHumanizeTests
{
static void VerifyWithCurrentDate(string expectedString, TimeSpan deltaFromNow)
{
if (expectedString == null)
throw new ArgumentNullException("expectedString");

var utcNow = DateTime.UtcNow;
var localNow = DateTime.Now;

Expand All @@ -18,6 +21,9 @@ static void VerifyWithCurrentDate(string expectedString, TimeSpan deltaFromNow)

static void VerifyWithDateInjection(string expectedString, TimeSpan deltaFromNow)
{
if (expectedString == null)
throw new ArgumentNullException("expectedString");

var utcNow = new DateTime(2013, 6, 20, 9, 58, 22, DateTimeKind.Utc);
var now = new DateTime(2013, 6, 20, 11, 58, 22, DateTimeKind.Local);

Expand All @@ -34,79 +40,79 @@ static void Verify(string expectedString, TimeSpan deltaFromNow)
[Fact]
public void OneSecondFromNow()
{
Verify(Resources.GetResource(ResourceKeys.DateHumanize.SingleSecondFromNow), new TimeSpan(0, 0, 0, 1));
Verify(Resources.GetResource(ResourceKeys.DateHumanize.GetResourceKey(TimeUnit.Second, 1, true)), new TimeSpan(0, 0, 0, 1));
}

[Fact]
public void SecondsFromNow()
{
Verify(string.Format(Resources.GetResource(ResourceKeys.DateHumanize.MultipleSecondsFromNow), 10), new TimeSpan(0, 0, 0, 10));
Verify(string.Format(Resources.GetResource(ResourceKeys.DateHumanize.GetResourceKey(TimeUnit.Second, 10, true)), 10), new TimeSpan(0, 0, 0, 10));
}

[Fact]
public void OneMinuteFromNow()
{
Verify(Resources.GetResource(ResourceKeys.DateHumanize.SingleMinuteFromNow), new TimeSpan(0, 0, 1, 1));
Verify(Resources.GetResource(ResourceKeys.DateHumanize.GetResourceKey(TimeUnit.Minute, 1, true)), new TimeSpan(0, 0, 1, 1));
}

[Fact]
public void AFewMinutesFromNow()
{
Verify(string.Format(Resources.GetResource(ResourceKeys.DateHumanize.MultipleMinutesFromNow), 10), new TimeSpan(0, 0, 10, 0));
Verify(string.Format(Resources.GetResource(ResourceKeys.DateHumanize.GetResourceKey(TimeUnit.Minute, 10, true)), 10), new TimeSpan(0, 0, 10, 0));
}

[Fact]
public void AnHourFromNow()
{
Verify(Resources.GetResource(ResourceKeys.DateHumanize.SingleHourFromNow), new TimeSpan(0, 1, 10, 0));
Verify(Resources.GetResource(ResourceKeys.DateHumanize.GetResourceKey(TimeUnit.Hour, 1, true)), new TimeSpan(0, 1, 10, 0));
}

[Fact]
public void HoursFromNow()
{
Verify(string.Format(Resources.GetResource(ResourceKeys.DateHumanize.MultipleHoursFromNow), 10), new TimeSpan(0, 10, 0, 0));
Verify(string.Format(Resources.GetResource(ResourceKeys.DateHumanize.GetResourceKey(TimeUnit.Hour, 10, true)), 10), new TimeSpan(0, 10, 0, 0));
}

[Fact]
public void Tomorrow()
{
Verify(Resources.GetResource(ResourceKeys.DateHumanize.SingleDayFromNow), new TimeSpan(1, 10, 0, 0));
Verify(Resources.GetResource(ResourceKeys.DateHumanize.GetResourceKey(TimeUnit.Day, 1, true)), new TimeSpan(1, 10, 0, 0));
}

[Fact]
public void AFewDaysFromNow()
{
Verify(string.Format(Resources.GetResource(ResourceKeys.DateHumanize.MultipleDaysFromNow), 10), new TimeSpan(10, 1, 0, 0));
Verify(string.Format(Resources.GetResource(ResourceKeys.DateHumanize.GetResourceKey(TimeUnit.Day, 10, true)), 10), new TimeSpan(10, 1, 0, 0));
}

[Fact]
public void OneMonthFromNow()
{
Verify(Resources.GetResource(ResourceKeys.DateHumanize.SingleMonthFromNow), new TimeSpan(31, 1, 0, 0));
Verify(Resources.GetResource(ResourceKeys.DateHumanize.GetResourceKey(TimeUnit.Month, 1, true)), new TimeSpan(31, 1, 0, 0));
}

[Fact]
public void AFewMonthsFromNow()
{
Verify(string.Format(Resources.GetResource(ResourceKeys.DateHumanize.MultipleMonthsFromNow), 2), new TimeSpan(62, 1, 0, 0));
Verify(string.Format(Resources.GetResource(ResourceKeys.DateHumanize.GetResourceKey(TimeUnit.Month, 2, true)), 2), new TimeSpan(62, 1, 0, 0));
}

[Fact]
public void OneYearFromNowIsNotAccureate()
{
Verify(Resources.GetResource(ResourceKeys.DateHumanize.SingleYearFromNow), new TimeSpan(360, 0, 0, 0));
Verify(Resources.GetResource(ResourceKeys.DateHumanize.GetResourceKey(TimeUnit.Year, 1, true)), new TimeSpan(360, 0, 0, 0));
}

[Fact]
public void OneYearFromNow()
{
Verify(Resources.GetResource(ResourceKeys.DateHumanize.SingleYearFromNow), new TimeSpan(400, 0, 0, 0));
Verify(Resources.GetResource(ResourceKeys.DateHumanize.GetResourceKey(TimeUnit.Year, 1, true)), new TimeSpan(400, 0, 0, 0));
}

[Fact]
public void FewYearsFromNow()
{
Verify(string.Format(Resources.GetResource(ResourceKeys.DateHumanize.MultipleYearsFromNow), 2), new TimeSpan(900, 0, 0, 0));
Verify(string.Format(Resources.GetResource(ResourceKeys.DateHumanize.GetResourceKey(TimeUnit.Year, 2, true)), 2), new TimeSpan(900, 0, 0, 0));
}

[Fact]
Expand All @@ -118,79 +124,79 @@ public void JustNow()
[Fact]
public void OneSecondAgo()
{
Verify(Resources.GetResource(ResourceKeys.DateHumanize.SingleSecondAgo), new TimeSpan(0, 0, 0, -1));
Verify(Resources.GetResource(ResourceKeys.DateHumanize.GetResourceKey(TimeUnit.Second)), new TimeSpan(0, 0, 0, -1));
}

[Fact]
public void SecondsAgo()
{
Verify(string.Format(Resources.GetResource(ResourceKeys.DateHumanize.MultipleSecondsAgo), 10), new TimeSpan(0, 0, 0, -10));
Verify(string.Format(Resources.GetResource(ResourceKeys.DateHumanize.GetResourceKey(TimeUnit.Second, 10)), 10), new TimeSpan(0, 0, 0, -10));
}

[Fact]
public void OneMinuteAgo()
{
Verify(Resources.GetResource(ResourceKeys.DateHumanize.SingleMinuteAgo), new TimeSpan(0, 0, -1, -10));
Verify(Resources.GetResource(ResourceKeys.DateHumanize.GetResourceKey(TimeUnit.Minute)), new TimeSpan(0, 0, -1, -10));
}

[Fact]
public void AFewMinutesAgo()
{
Verify(string.Format(Resources.GetResource(ResourceKeys.DateHumanize.MultipleMinutesAgo), 10), new TimeSpan(0, 0, -10, 0));
Verify(string.Format(Resources.GetResource(ResourceKeys.DateHumanize.GetResourceKey(TimeUnit.Minute, 10)), 10), new TimeSpan(0, 0, -10, 0));
}

[Fact]
public void AnHourAgo()
{
Verify(Resources.GetResource(ResourceKeys.DateHumanize.SingleHourAgo), new TimeSpan(0, -1, -10, 0));
Verify(Resources.GetResource(ResourceKeys.DateHumanize.GetResourceKey(TimeUnit.Hour)), new TimeSpan(0, -1, -10, 0));
}

[Fact]
public void HoursAgo()
{
Verify(string.Format(Resources.GetResource(ResourceKeys.DateHumanize.MultipleHoursAgo), 10), new TimeSpan(0, -10, 0, 0));
Verify(string.Format(Resources.GetResource(ResourceKeys.DateHumanize.GetResourceKey(TimeUnit.Hour, 10)), 10), new TimeSpan(0, -10, 0, 0));
}

[Fact]
public void Yesterday()
{
Verify(Resources.GetResource(ResourceKeys.DateHumanize.SingleDayAgo), new TimeSpan(-1, -10, 0, 0));
Verify(Resources.GetResource(ResourceKeys.DateHumanize.GetResourceKey(TimeUnit.Day)), new TimeSpan(-1, -10, 0, 0));
}

[Fact]
public void AFewDaysAgo()
{
Verify(string.Format(Resources.GetResource(ResourceKeys.DateHumanize.MultipleDaysAgo), 10), new TimeSpan(-10, -1, 0, 0));
Verify(string.Format(Resources.GetResource(ResourceKeys.DateHumanize.GetResourceKey(TimeUnit.Day, 10)), 10), new TimeSpan(-10, -1, 0, 0));
}

[Fact]
public void OneMonthAgo()
{
Verify(Resources.GetResource(ResourceKeys.DateHumanize.SingleMonthAgo), new TimeSpan(-31, -1, 0, 0));
Verify(Resources.GetResource(ResourceKeys.DateHumanize.GetResourceKey(TimeUnit.Month)), new TimeSpan(-31, -1, 0, 0));
}

[Fact]
public void AFewMonthsAgo()
{
Verify(string.Format(Resources.GetResource(ResourceKeys.DateHumanize.MultipleMonthsAgo), 2), new TimeSpan(-62, -1, 0, 0));
Verify(string.Format(Resources.GetResource(ResourceKeys.DateHumanize.GetResourceKey(TimeUnit.Month, 2)), 2), new TimeSpan(-62, -1, 0, 0));
}

[Fact]
public void OneYearAgoIsNotAccureate()
{
Verify(Resources.GetResource(ResourceKeys.DateHumanize.SingleYearAgo), new TimeSpan(-360, 0, 0, 0));
Verify(Resources.GetResource(ResourceKeys.DateHumanize.GetResourceKey(TimeUnit.Year)), new TimeSpan(-360, 0, 0, 0));
}

[Fact]
public void OneYearAgo()
{
Verify(Resources.GetResource(ResourceKeys.DateHumanize.SingleYearAgo), new TimeSpan(-400, 0, 0, 0));
Verify(Resources.GetResource(ResourceKeys.DateHumanize.GetResourceKey(TimeUnit.Year)), new TimeSpan(-400, 0, 0, 0));
}

[Fact]
public void FewYearsAgo()
{
Verify(string.Format(Resources.GetResource(ResourceKeys.DateHumanize.MultipleYearsAgo), 2), new TimeSpan(-900, 0, 0, 0));
Verify(string.Format(Resources.GetResource(ResourceKeys.DateHumanize.GetResourceKey(TimeUnit.Year, 2)), 2), new TimeSpan(-900, 0, 0, 0));
}
}
}
}
2 changes: 2 additions & 0 deletions src/Humanizer.Tests/Humanizer.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
<Compile Include="Bytes\ParsingTests.cs" />
<Compile Include="Bytes\ToStringTests.cs" />
<Compile Include="CasingTests.cs" />
<Compile Include="DateHumanizeOneTimeUnitTests.cs" />
<Compile Include="Localisation\ar\DateHumanizeTests.cs" />
<Compile Include="Localisation\ar\NumberToWordsTests.cs" />
<Compile Include="Localisation\ar\TimeSpanHumanizeTests.cs" />
Expand All @@ -83,6 +84,7 @@
<Compile Include="Localisation\es\TimeSpanHumanizeTests.cs" />
<Compile Include="Localisation\pt-BR\DateHumanizeTests.cs" />
<Compile Include="Localisation\pt-BR\TimeSpanHumanizeTests.cs" />
<Compile Include="ResourceKeyTests.cs" />
<Compile Include="RomanNumeralTests.cs" />
<Compile Include="RunnableInDebugModeOnlyAttribute.cs" />
<Compile Include="ToQuantityTests.cs" />
Expand Down
Loading