-
-
Notifications
You must be signed in to change notification settings - Fork 4
Add polyfills for Enum.Parse<T>(string, [bool]), Enum.GetValue<T>() and Enum.GetNames<T>()
#56
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,34 @@ | ||
| using System; | ||
| using FluentAssertions; | ||
| using Xunit; | ||
|
|
||
| namespace PolyShim.Tests.Net50; | ||
|
|
||
| public class EnumTests | ||
| { | ||
| [Fact] | ||
| public void GetNames_Test() | ||
| { | ||
| // Act & assert | ||
| Enum.GetNames<DayOfWeek>() | ||
| .Should() | ||
| .Equal("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void GetValues_Test() | ||
| { | ||
| // Act & assert | ||
| Enum.GetValues<DayOfWeek>() | ||
| .Should() | ||
| .Equal( | ||
| DayOfWeek.Sunday, | ||
| DayOfWeek.Monday, | ||
| DayOfWeek.Tuesday, | ||
| DayOfWeek.Wednesday, | ||
| DayOfWeek.Thursday, | ||
| DayOfWeek.Friday, | ||
| DayOfWeek.Saturday | ||
| ); | ||
| } | ||
| } | ||
This file contains hidden or 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,18 @@ | ||
| using System; | ||
| using FluentAssertions; | ||
| using Xunit; | ||
|
|
||
| namespace PolyShim.Tests.NetCore20; | ||
|
|
||
| public class EnumTests | ||
| { | ||
| [Fact] | ||
| public void Parse_Test() | ||
| { | ||
| // Act & assert | ||
| Enum.Parse<DayOfWeek>("Friday").Should().Be(DayOfWeek.Friday); | ||
| Enum.Parse<DayOfWeek>("friday", true).Should().Be(DayOfWeek.Friday); | ||
| Assert.Throws<ArgumentException>(() => Enum.Parse<DayOfWeek>("Moonday")); | ||
| Assert.Throws<ArgumentException>(() => Enum.Parse<DayOfWeek>("friday", false)); | ||
| } | ||
| } |
This file contains hidden or 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,23 @@ | ||
| #if (NETCOREAPP && !NET5_0_OR_GREATER) || (NETFRAMEWORK) || (NETSTANDARD) | ||
| #nullable enable | ||
| // ReSharper disable RedundantUsingDirective | ||
| // ReSharper disable CheckNamespace | ||
| // ReSharper disable InconsistentNaming | ||
| // ReSharper disable PartialTypeWithSinglePart | ||
|
|
||
| using System; | ||
|
|
||
| internal static partial class PolyfillExtensions | ||
| { | ||
| extension(Enum) | ||
| { | ||
| // https://learn.microsoft.com/dotnet/api/system.enum.getnames#system-enum-getnames-1 | ||
| public static string[] GetNames<T>() | ||
| where T : struct, Enum => Enum.GetNames(typeof(T)); | ||
|
|
||
| // https://learn.microsoft.com/dotnet/api/system.enum.getvalues#system-enum-getvalues-1 | ||
| public static T[] GetValues<T>() | ||
| where T : struct, Enum => (T[])Enum.GetValues(typeof(T)); | ||
| } | ||
| } | ||
| #endif |
This file contains hidden or 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,47 @@ | ||
| #if (NETFRAMEWORK && !NET40_OR_GREATER) | ||
| #nullable enable | ||
| // ReSharper disable RedundantUsingDirective | ||
| // ReSharper disable CheckNamespace | ||
| // ReSharper disable InconsistentNaming | ||
| // ReSharper disable PartialTypeWithSinglePart | ||
|
|
||
| using System; | ||
|
|
||
| internal static partial class PolyfillExtensions | ||
| { | ||
| extension(Enum) | ||
| { | ||
| // https://learn.microsoft.com/dotnet/api/system.enum.tryparse#system-enum-tryparse-1(system-string-system-boolean-0@) | ||
| public static bool TryParse<T>(string value, bool ignoreCase, out T result) | ||
| where T : struct, Enum | ||
| { | ||
| try | ||
| { | ||
| result = (T)Enum.Parse(typeof(T), value, ignoreCase); | ||
| return true; | ||
| } | ||
| catch | ||
| { | ||
| result = default; | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| // https://learn.microsoft.com/dotnet/api/system.enum.tryparse#system-enum-tryparse-1(system-string-0@) | ||
| public static bool TryParse<T>(string value, out T result) | ||
| where T : struct, Enum | ||
| { | ||
| try | ||
| { | ||
| result = (T)Enum.Parse(typeof(T), value); | ||
| return true; | ||
| } | ||
| catch | ||
| { | ||
| result = default; | ||
| return false; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| #endif |
This file contains hidden or 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,23 @@ | ||
| #if (NETCOREAPP && !NETCOREAPP2_0_OR_GREATER) || (NETFRAMEWORK) || (NETSTANDARD && !NETSTANDARD2_1_OR_GREATER) | ||
| #nullable enable | ||
| // ReSharper disable RedundantUsingDirective | ||
| // ReSharper disable CheckNamespace | ||
| // ReSharper disable InconsistentNaming | ||
| // ReSharper disable PartialTypeWithSinglePart | ||
|
|
||
| using System; | ||
|
|
||
| internal static partial class PolyfillExtensions | ||
| { | ||
| extension(Enum) | ||
| { | ||
| // https://learn.microsoft.com/dotnet/api/system.enum.parse#system-enum-parse-1(system-string-system-boolean) | ||
| public static T Parse<T>(string value, bool ignoreCase) | ||
| where T : struct, Enum => (T)Enum.Parse(typeof(T), value, ignoreCase); | ||
|
|
||
| // https://learn.microsoft.com/dotnet/api/system.enum.parse#system-enum-parse-1(system-string) | ||
| public static T Parse<T>(string value) | ||
| where T : struct, Enum => (T)Enum.Parse(typeof(T), value); | ||
| } | ||
| } | ||
| #endif |
This file contains hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.