-
-
Notifications
You must be signed in to change notification settings - Fork 540
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add [Nullable]EnumAssertions to handle enums
- Loading branch information
Showing
22 changed files
with
1,169 additions
and
291 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,36 @@ | ||
using System; | ||
using System.Diagnostics; | ||
using System.Diagnostics.Contracts; | ||
using FluentAssertions.Primitives; | ||
|
||
namespace FluentAssertions | ||
{ | ||
/// <summary> | ||
/// Contains an extension method for custom assertions in unit tests related to Enum objects. | ||
/// </summary> | ||
[DebuggerNonUserCode] | ||
public static class EnumAssertionsExtensions | ||
{ | ||
/// <summary> | ||
/// Returns an <see cref="EnumAssertions{TEnum, TAssertions}"/> object that can be used to assert the | ||
/// current <typeparamref name="TEnum"/>. | ||
/// </summary> | ||
[Pure] | ||
public static EnumAssertions<TEnum> Should<TEnum>(this TEnum @enum) | ||
where TEnum : struct, Enum | ||
{ | ||
return new EnumAssertions<TEnum>(@enum); | ||
} | ||
|
||
/// <summary> | ||
/// Returns an <see cref="EnumAssertions{TEnum, TAssertions}"/> object that can be used to assert the | ||
/// current <typeparamref name="TEnum"/>. | ||
/// </summary> | ||
[Pure] | ||
public static NullableEnumAssertions<TEnum> Should<TEnum>(this TEnum? @enum) | ||
where TEnum : struct, Enum | ||
{ | ||
return new NullableEnumAssertions<TEnum>(@enum); | ||
} | ||
} | ||
} |
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,32 @@ | ||
using System; | ||
using System.Globalization; | ||
|
||
namespace FluentAssertions.Formatting | ||
{ | ||
public class EnumValueFormatter : 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 virtual bool CanHandle(object value) | ||
{ | ||
return value is Enum; | ||
} | ||
|
||
/// <inheritdoc /> | ||
public string Format(object value, FormattingContext context, FormatChild formatChild) | ||
{ | ||
Type type = value.GetType(); | ||
string typePart = type.Name; | ||
string namePart = Enum.GetName(type, value); | ||
string valuePart = Convert.ToDecimal(value, CultureInfo.InvariantCulture) | ||
.ToString(CultureInfo.InvariantCulture); | ||
|
||
return $"{typePart}.{namePart}({valuePart})"; | ||
} | ||
} | ||
} |
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.