Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding support for .NET6 DateOnly struct (#1844)
- Loading branch information
1 parent
f7d85f4
commit 1349525
Showing
8 changed files
with
1,529 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
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(dateOnly.ToString("<yyyy-MM-dd>", CultureInfo.InvariantCulture)); | ||
} | ||
} | ||
} | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
105 changes: 105 additions & 0 deletions
105
Src/FluentAssertions/Primitives/NullableDateOnlyAssertions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
using System; | ||
using System.Diagnostics; | ||
using FluentAssertions.Execution; | ||
|
||
#if NET6_0_OR_GREATER | ||
|
||
namespace FluentAssertions.Primitives | ||
{ | ||
/// <summary> | ||
/// Contains a number of methods to assert that a nullable <see cref="DateOnly"/> or | ||
/// </summary> | ||
[DebuggerNonUserCode] | ||
public class NullableDateOnlyAssertions : NullableDateOnlyAssertions<NullableDateOnlyAssertions> | ||
{ | ||
public NullableDateOnlyAssertions(DateOnly? expected) | ||
: base(expected) | ||
{ | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Contains a number of methods to assert that a nullable <see cref="DateOnly"/> or | ||
/// </summary> | ||
[DebuggerNonUserCode] | ||
public class NullableDateOnlyAssertions<TAssertions> : DateOnlyAssertions<TAssertions> | ||
where TAssertions : NullableDateOnlyAssertions<TAssertions> | ||
{ | ||
public NullableDateOnlyAssertions(DateOnly? expected) | ||
: base(expected) | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// Asserts that a nullable <see cref="DateOnly"/> value is not <c>null</c>. | ||
/// </summary> | ||
/// <param name="because"> | ||
/// A formatted phrase as is supported by <see cref="string.Format(string,object[])"/> explaining why the assertion | ||
/// is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically. | ||
/// </param> | ||
/// <param name="becauseArgs"> | ||
/// Zero or more objects to format using the placeholders in <paramref name="because" />. | ||
/// </param> | ||
public AndConstraint<TAssertions> HaveValue(string because = "", params object[] becauseArgs) | ||
{ | ||
Execute.Assertion | ||
.ForCondition(Subject.HasValue) | ||
.BecauseOf(because, becauseArgs) | ||
.FailWith("Expected {context:nullable date} to have a value{reason}, but found {0}.", Subject); | ||
|
||
return new AndConstraint<TAssertions>((TAssertions)this); | ||
} | ||
|
||
/// <summary> | ||
/// Asserts that a nullable <see cref="DateOnly"/> value is not <c>null</c>. | ||
/// </summary> | ||
/// <param name="because"> | ||
/// A formatted phrase as is supported by <see cref="string.Format(string,object[])"/> explaining why the assertion | ||
/// is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically. | ||
/// </param> | ||
/// <param name="becauseArgs"> | ||
/// Zero or more objects to format using the placeholders in <paramref name="because" />. | ||
/// </param> | ||
public AndConstraint<TAssertions> NotBeNull(string because = "", params object[] becauseArgs) | ||
{ | ||
return HaveValue(because, becauseArgs); | ||
} | ||
|
||
/// <summary> | ||
/// Asserts that a nullable <see cref="DateOnly"/> value is <c>null</c>. | ||
/// </summary> | ||
/// <param name="because"> | ||
/// A formatted phrase as is supported by <see cref="string.Format(string,object[])"/> explaining why the assertion | ||
/// is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically. | ||
/// </param> | ||
/// <param name="becauseArgs"> | ||
/// Zero or more objects to format using the placeholders in <paramref name="because" />. | ||
/// </param> | ||
public AndConstraint<TAssertions> NotHaveValue(string because = "", params object[] becauseArgs) | ||
{ | ||
Execute.Assertion | ||
.ForCondition(!Subject.HasValue) | ||
.BecauseOf(because, becauseArgs) | ||
.FailWith("Did not expect {context:nullable date} to have a value{reason}, but found {0}.", Subject); | ||
|
||
return new AndConstraint<TAssertions>((TAssertions)this); | ||
} | ||
|
||
/// <summary> | ||
/// Asserts that a nullable <see cref="DateOnly"/> value is <c>null</c>. | ||
/// </summary> | ||
/// <param name="because"> | ||
/// A formatted phrase as is supported by <see cref="string.Format(string,object[])"/> explaining why the assertion | ||
/// is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically. | ||
/// </param> | ||
/// <param name="becauseArgs"> | ||
/// Zero or more objects to format using the placeholders in <paramref name="because" />. | ||
/// </param> | ||
public AndConstraint<TAssertions> BeNull(string because = "", params object[] becauseArgs) | ||
{ | ||
return NotHaveValue(because, becauseArgs); | ||
} | ||
} | ||
} | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.