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

Adding support for .NET6 DateOnly struct #1844

Merged
merged 4 commits into from Mar 13, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 24 additions & 0 deletions Src/FluentAssertions/AssertionExtensions.cs
Expand Up @@ -434,6 +434,30 @@ public static NullableDateTimeOffsetAssertions Should(this DateTimeOffset? actua
return new NullableDateTimeOffsetAssertions(actualValue);
}

#if NET6_0_OR_GREATER

/// <summary>
/// Returns an <see cref="DateOnlyAssertions"/> object that can be used to assert the
/// current <see cref="DateOnly"/>.
/// </summary>
[Pure]
public static DateOnlyAssertions Should(this DateOnly actualValue)
{
return new DateOnlyAssertions(actualValue);
}

/// <summary>
/// Returns an <see cref="NullableDateOnlyAssertions"/> object that can be used to assert the
/// current nullable <see cref="DateOnly"/>.
/// </summary>
[Pure]
public static NullableDateOnlyAssertions Should(this DateOnly? actualValue)
{
return new NullableDateOnlyAssertions(actualValue);
}

#endif

/// <summary>
/// Returns an <see cref="ComparableTypeAssertions{T}"/> object that can be used to assert the
/// current <see cref="IComparable{T}"/>.
Expand Down
34 changes: 34 additions & 0 deletions Src/FluentAssertions/Extensions/FluentDateOnlyExtensions.cs
@@ -0,0 +1,34 @@
using System;
using System.Diagnostics;

#if NET6_0_OR_GREATER

namespace FluentAssertions.Extensions
{
/// <summary>
/// Extension methods on <see cref="int"/> to allow for a more fluent way of specifying a <see cref="DateOnly"/>.
/// </summary>
[DebuggerNonUserCode]
public static class FluentDateOnlyExtensions
{
/// <summary>
/// Returns a new <see cref="DateOnly"/> value that is <paramref name="dayDifference"/> days before the
/// specified <paramref name="sourceDateOnly"/>.
/// </summary>
public static DateOnly Before(this int dayDifference, DateOnly sourceDateOnly)
{
return sourceDateOnly.AddDays(-1 * dayDifference);
}

/// <summary>
/// Returns a new <see cref="DateOnly"/> value that is <paramref name="dayDifference"/> days after the
/// specified <paramref name="sourceDateOnly"/>.
/// </summary>
public static DateOnly After(this int dayDifference, DateOnly sourceDateOnly)
{
return sourceDateOnly.AddDays(dayDifference);
}
}
}

#endif
33 changes: 33 additions & 0 deletions Src/FluentAssertions/Formatting/DateOnlyValueFormatter.cs
@@ -0,0 +1,33 @@
using System;
using System.Globalization;

#if NET6_0_OR_GREATER

namespace FluentAssertions.Formatting
{
public class DateOnlyValueFormatter : IValueFormatter
{
/// <summary>
/// Indicates whether the current <see cref="IValueFormatter"/> can handle the specified <paramref name="value"/>.
/// </summary>
/// <param name="value">The value for which to create a <see cref="string"/>.</param>
/// <returns>
/// <c>true</c> if the current <see cref="IValueFormatter"/> can handle the specified value; otherwise, <c>false</c>.
/// </returns>
public bool CanHandle(object value)
{
return value is DateOnly;
}

public void Format(object value, FormattedObjectGraph formattedGraph, FormattingContext context, FormatChild formatChild)
{
var dateOnly = (DateOnly)value;

formattedGraph.AddFragment("<");
formattedGraph.AddFragment(dateOnly.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture));
formattedGraph.AddFragment(">");
iliashkolyar marked this conversation as resolved.
Show resolved Hide resolved
}
}
}

#endif
3 changes: 3 additions & 0 deletions Src/FluentAssertions/Formatting/Formatter.cs
Expand Up @@ -31,6 +31,9 @@ public static class Formatter
new NullValueFormatter(),
new GuidValueFormatter(),
new DateTimeOffsetValueFormatter(),
#if NET6_0_OR_GREATER
new DateOnlyValueFormatter(),
#endif
new TimeSpanValueFormatter(),
new Int32ValueFormatter(),
new Int64ValueFormatter(),
Expand Down