Skip to content
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
37 changes: 37 additions & 0 deletions PowerKit.Tests/Extensions/EnumExtensionsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System;
using FluentAssertions;
using PowerKit.Extensions;
using Xunit;

namespace PowerKit.Tests.Extensions;

file enum TestEnum
{
Foo,
Bar,
}

public class EnumExtensionsTests
{
[Fact]
public void ParseOrNull_Test()
{
// Act & assert
Enum.ParseOrNull<TestEnum>("Foo").Should().Be(TestEnum.Foo);
Enum.ParseOrNull<TestEnum>("foo").Should().BeNull();
Enum.ParseOrNull<TestEnum>("foo", true).Should().Be(TestEnum.Foo);
Enum.ParseOrNull<TestEnum>("invalid").Should().BeNull();
Enum.ParseOrNull<TestEnum>(null).Should().BeNull();
}

[Fact]
public void ParseOrDefault_Test()
{
// Act & assert
Enum.ParseOrDefault<TestEnum>("Foo").Should().Be(TestEnum.Foo);
Enum.ParseOrDefault<TestEnum>("foo").Should().Be(default);
Enum.ParseOrDefault<TestEnum>("foo", true).Should().Be(TestEnum.Foo);
Enum.ParseOrDefault("invalid", TestEnum.Bar).Should().Be(TestEnum.Bar);
Enum.ParseOrDefault(null, TestEnum.Bar).Should().Be(TestEnum.Bar);
}
}
47 changes: 47 additions & 0 deletions PowerKit/Extensions/EnumExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#nullable enable
using System;
using System.Diagnostics.CodeAnalysis;

namespace PowerKit.Extensions;

#if !POWERKIT_INCLUDE_COVERAGE
[ExcludeFromCodeCoverage]
#endif
internal static class EnumExtensions
{
extension(Enum)
{
/// <summary>
/// Parses the string as an enum value of type <typeparamref name="TEnum" />,
/// returning <see langword="null" /> if parsing fails.
/// </summary>
public static TEnum? ParseOrNull<TEnum>(string? str, bool ignoreCase)
where TEnum : struct, Enum =>
Enum.TryParse<TEnum>(str, ignoreCase, out var result) ? result : null;

/// <summary>
/// Parses the string as an enum value of type <typeparamref name="TEnum" />,
/// returning <see langword="null" /> if parsing fails.
/// </summary>
public static TEnum? ParseOrNull<TEnum>(string? str)
where TEnum : struct, Enum => Enum.ParseOrNull<TEnum>(str, false);

/// <summary>
/// Parses the string as an enum value of type <typeparamref name="TEnum" />,
/// returning <paramref name="defaultValue" /> if parsing fails.
/// </summary>
public static TEnum ParseOrDefault<TEnum>(
string? str,
bool ignoreCase,
TEnum defaultValue = default
)
where TEnum : struct, Enum => Enum.ParseOrNull<TEnum>(str, ignoreCase) ?? defaultValue;

/// <summary>
/// Parses the string as an enum value of type <typeparamref name="TEnum" />,
/// returning <paramref name="defaultValue" /> if parsing fails.
/// </summary>
public static TEnum ParseOrDefault<TEnum>(string? str, TEnum defaultValue = default)
where TEnum : struct, Enum => Enum.ParseOrDefault(str, false, defaultValue);
}
}