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

Add DateTimeOffsetValueComparer #180

Merged
merged 1 commit into from
Jun 12, 2024
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
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@

* Reqnroll.Verify: Support for Verify v24 (Verify.Xunit v24.2.0) for .NET 4.7.2+ and .NET 6.0+. For earlier versions of Verify or for .NET 4.6.2, use the latest 2.0.3 version of the plugin that is compatible with Reqnroll v2.*. (#151)
* Optimize creation of test-thread context using test framework independent resource pooling (#144)
* Support DateTimeOffset in value comparer (#180)

## Bug fixes:

*Contributors of this release (in alphabetical order):* @ajeckmans, @obligaron
*Contributors of this release (in alphabetical order):* @ajeckmans, @cimnine, @obligaron

# v2.0.3 - 2024-06-10

Expand Down
3 changes: 2 additions & 1 deletion Reqnroll/Assist/ReqnrollDefaultValueComparerList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ internal sealed class ReqnrollDefaultValueComparerList : ServiceComponentList<IV
public ReqnrollDefaultValueComparerList()
: base(new List<IValueComparer> {
new DateTimeValueComparer(),
new DateTimeOffsetValueComparer(),
new BoolValueComparer(),
new GuidValueComparer(),
new DecimalValueComparer(),
Expand All @@ -19,4 +20,4 @@ public ReqnrollDefaultValueComparerList()
{
}
}
}
}
18 changes: 18 additions & 0 deletions Reqnroll/Assist/ValueComparers/DateTimeOffsetValueComparer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;

namespace Reqnroll.Assist.ValueComparers
{
public class DateTimeOffsetValueComparer : IValueComparer
{
public bool CanCompare(object actualValue)
{
return actualValue is DateTimeOffset;
}

public bool Compare(string expectedValue, object actualValue)
{
return DateTimeOffset.TryParse(expectedValue, out var expected) &&
expected == (DateTimeOffset)actualValue;
}
}
}
12 changes: 6 additions & 6 deletions Tests/Reqnroll.RuntimeTests/AssistTests/ServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@ public void Should_load_the_value_comparers_by_default()
{
var expectedDefaultComparers = new object[]
{
typeof(DateTimeValueComparer),
typeof(BoolValueComparer),
typeof(GuidValueComparer),
typeof(DateTimeValueComparer),
typeof(DateTimeOffsetValueComparer),
typeof(DefaultValueComparer),
typeof(DecimalValueComparer),
typeof(DoubleValueComparer),
typeof(GuidValueComparer),
typeof(FloatValueComparer),
typeof(DefaultValueComparer),
};

var service = new Service();
Expand Down Expand Up @@ -53,12 +54,11 @@ public void Should_load_the_value_retrievers_by_default()
typeof(CharValueRetriever),
typeof(BoolValueRetriever),
typeof(DateTimeValueRetriever),
typeof(DateTimeOffsetValueRetriever),
typeof(GuidValueRetriever),
typeof(EnumValueRetriever),
typeof(TimeSpanValueRetriever),
typeof(DateTimeOffsetValueRetriever),
typeof(UriValueRetriever),

typeof(ArrayValueRetriever),
typeof(ListValueRetriever),
};
Expand Down Expand Up @@ -153,4 +153,4 @@ public bool CanRetrieve(KeyValuePair<string, string> keyValuePair, Type targetTy
throw new NotImplementedException();
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
using System;
using System.Globalization;
using System.Threading;
using Xunit;
using FluentAssertions;
using Reqnroll.Assist.ValueComparers;

namespace Reqnroll.RuntimeTests.AssistTests.ValueComparerTests
{
public class DateTimeOffsetValueComparerTests
{
public DateTimeOffsetValueComparerTests()
{
Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US", false);
}

[Fact]
public void Can_compare_if_the_value_is_a_datetimeoffset()
{
new DateTimeOffsetValueComparer()
.CanCompare(new DateTimeOffset())
.Should()
.BeTrue();
}

[Fact]
public void Cannot_compare_if_the_value_is_not_a_datetimeoffset()
{
var comparer = new DateTimeOffsetValueComparer();
comparer.CanCompare(3).Should().BeFalse();
comparer.CanCompare("x").Should().BeFalse();
comparer.CanCompare(4.3).Should().BeFalse();
}

[Fact]
public void Cannot_compare_if_the_value_is_null()
{
new DateTimeOffsetValueComparer()
.CanCompare(null)
.Should()
.BeFalse();
}

[Fact]
public void Returns_true_when_the_string_and_values_match_exactly()
{
var comparer = new DateTimeOffsetValueComparer();
comparer.Compare(new DateTimeOffset(new DateTime(2011, 1, 2)).ToString(), new DateTimeOffset(new DateTime(2011, 1, 2)))
.Should()
.BeTrue();
comparer.Compare(new DateTimeOffset(new DateTime(2020, 12, 31)).ToString(), new DateTimeOffset(new DateTime(2020, 12, 31)))
.Should()
.BeTrue();
}

[Fact]
public void Returns_false_when_the_string_and_values_match_for_different_dates()
{
var comparer = new DateTimeOffsetValueComparer();
comparer.Compare(new DateTimeOffset(new DateTime(2011, 1, 3)).ToString(), new DateTimeOffset(new DateTime(2011, 1, 2)))
.Should()
.BeFalse();
comparer.Compare(new DateTimeOffset(new DateTime(2011, 1, 2)).ToString(), new DateTimeOffset(new DateTime(2011, 1, 3)))
.Should()
.BeFalse();
comparer.Compare(new DateTimeOffset(new DateTime(2011, 1, 1)).ToString(), new DateTimeOffset(new DateTime(2012, 1, 1)))
.Should()
.BeFalse();
comparer.Compare(new DateTimeOffset(new DateTime(2011, 1, 1)).ToString(), new DateTimeOffset(new DateTime(2011, 2, 1)))
.Should()
.BeFalse();
}

[Fact]
public void Returns_false_when_the_expected_value_is_not_a_valid_datetimeoffset()
{
var comparer = new DateTimeOffsetValueComparer();
comparer.Compare("x", new DateTimeOffset(new DateTime(1990, 1, 1)))
.Should()
.BeFalse();
comparer.Compare("January1", new DateTimeOffset(new DateTime(1990, 1, 1)))
.Should()
.BeFalse();
}

[Fact]
public void Returns_false_the_value_is_correct_format_but_not_a_valid_date()
{
var comparer = new DateTimeOffsetValueComparer();
comparer.Compare(new DateTimeOffset(new DateTime(2011, 2, 28)).ToString().Replace("28", "29"), new DateTimeOffset(new DateTime(2011, 2, 28)))
.Should()
.BeFalse();
comparer.Compare(new DateTimeOffset(new DateTime(2011, 12, 1)).ToString().Replace("12", "13"), new DateTimeOffset(new DateTime(2011, 12, 1)))
.Should()
.BeFalse();
}
}
}