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 TimeOnly struct
- Loading branch information
1 parent
b66f77d
commit 9d66732
Showing
8 changed files
with
1,659 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
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 TimeOnlyValueFormatter : 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 TimeOnly; | ||
} | ||
|
||
public void Format(object value, FormattedObjectGraph formattedGraph, FormattingContext context, FormatChild formatChild) | ||
{ | ||
var timeOnly = (TimeOnly)value; | ||
formattedGraph.AddFragment(timeOnly.ToString("<HH:mm:ss.fff>", CultureInfo.InvariantCulture)); | ||
} | ||
} | ||
} | ||
|
||
#endif |
105 changes: 105 additions & 0 deletions
105
Src/FluentAssertions/Primitives/NullableTimeOnlyAssertions.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="TimeOnly"/> or | ||
/// </summary> | ||
[DebuggerNonUserCode] | ||
public class NullableTimeOnlyAssertions : NullableTimeOnlyAssertions<NullableTimeOnlyAssertions> | ||
{ | ||
public NullableTimeOnlyAssertions(TimeOnly? value) | ||
: base(value) | ||
{ | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Contains a number of methods to assert that a nullable <see cref="TimeOnly"/> or | ||
/// </summary> | ||
[DebuggerNonUserCode] | ||
public class NullableTimeOnlyAssertions<TAssertions> : TimeOnlyAssertions<TAssertions> | ||
where TAssertions : NullableTimeOnlyAssertions<TAssertions> | ||
{ | ||
public NullableTimeOnlyAssertions(TimeOnly? value) | ||
: base(value) | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// Asserts that a nullable <see cref="TimeOnly"/> 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 time} to have a value{reason}, but found {0}.", Subject); | ||
|
||
return new AndConstraint<TAssertions>((TAssertions)this); | ||
} | ||
|
||
/// <summary> | ||
/// Asserts that a nullable <see cref="TimeOnly"/> 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="TimeOnly"/> 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 time} to have a value{reason}, but found {0}.", Subject); | ||
|
||
return new AndConstraint<TAssertions>((TAssertions)this); | ||
} | ||
|
||
/// <summary> | ||
/// Asserts that a nullable <see cref="TimeOnly"/> 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 |
Oops, something went wrong.