From 0eac27bf90f3feaa747c5f7fba42a7abb32b60ef Mon Sep 17 00:00:00 2001 From: Andrew Lock Date: Tue, 12 Aug 2025 21:34:25 +0100 Subject: [PATCH 1/5] Add support for [EnumMember] and change defaults --- .../EnumExtensionsAttribute.cs | 30 +- .../MetadataSource.cs | 39 + src/NetEscapades.EnumGenerators/Attributes.cs | 2 + src/NetEscapades.EnumGenerators/Constants.cs | 1 + .../DuplicateExtensionClassAnalyzer.cs | 3 +- .../EnumGenerator.cs | 104 ++- .../EnumToGenerate.cs | 7 +- .../EnumValueOption.cs | 29 +- .../NetEscapades.EnumGenerators.csproj | 3 +- .../NetEscapades.EnumGenerators.props | 8 + .../SourceGenerationHelper.cs | 820 +++++++++++------- .../DuplicateEnumValueAnalyzerTests.cs | 1 + .../DuplicateExtensionClassAnalyzerTests.cs | 1 + .../EnumGeneratorTests.cs | 209 +++-- .../EnumInGenericTypeAnalyzerTests.cs | 1 + .../NetEscapades.EnumGenerators.Tests.csproj | 3 + ...ExtensionsForFlagsEnum_Params.verified.txt | 94 +- ...numExtensionsInChildNamespace.verified.txt | 90 +- ...umExtensionsInGlobalNamespace.verified.txt | 88 +- ...onsInGlobalNamespace_CSharp14.verified.txt | 88 +- ...teEnumExtensionsInNestedClass.verified.txt | 90 +- ...eEnumExtensionsWithCustomName.verified.txt | 88 +- ...mExtensionsWithCustomNames__.verified.txt} | 131 ++- ...ExtensionsWithCustomNamespace.verified.txt | 88 +- ...onsWithCustomNamespaceAndName.verified.txt | 90 +- ...MetadataName_IgnoringOthers__.verified.txt | 651 ++++++++++++++ ...ensionsWithNoneMetadataName__.verified.txt | 457 ++++++++++ ...ensionsWithSameDisplayName__.verified.txt} | 129 ++- ...nsionsWithWrongMetadataName__.verified.txt | 569 ++++++++++++ ...lEnumExtensionsWithCustomName.verified.txt | 92 +- ...ExtensionsWithCustomNamespace.verified.txt | 94 +- ...onsWithCustomNamespaceAndName.verified.txt | 92 +- ...ts.CanGenerateForExternalEnum.verified.txt | 106 ++- ...nGenerateForExternalFlagsEnum.verified.txt | 106 ++- ...erateForMultipleExternalEnums.verified.txt | 200 +++-- ...mespaceAndClassNameAreTheSame.verified.txt | 84 +- ...rObsoleteEnums_CS0612_Issue97.verified.txt | 90 +- ...rObsoleteEnums_CS0618_Issue97.verified.txt | 90 +- ...bsoleteMembers_CS0612_Issue97.verified.txt | 90 +- ...bsoleteMembers_CS0618_Issue97.verified.txt | 90 +- ...tesAndSlashesInDescription__.verified.txt} | 147 ++-- ...SameNameInDifferentNamespaces.verified.txt | 186 ++-- ...rceptEnumInDifferentNamespace.verified.txt | 94 +- ...nterceptEnumInGlobalNamespace.verified.txt | 94 +- ...InterceptExternalEnumToString.verified.txt | 106 ++- ...ptorTests.CanInterceptHasFlag.verified.txt | 94 +- ...nterceptMultipleEnumsToString.verified.txt | 186 ++-- ...torTests.CanInterceptToString.verified.txt | 90 +- ...InterceptToStringWhenCsharp11.verified.txt | 90 +- ...nterceptableAttributeToString.verified.txt | 106 ++- ...EnumWithoutInterceptorPackage.verified.txt | 90 +- ...eptUsageInStringInterpolation.verified.txt | 90 +- ...nterceptUsageWithEnumDirectly.verified.txt | 90 +- ...fObsoleteEnums_CS0612_Issue97.verified.txt | 88 +- ...fObsoleteEnums_CS0618_Issue97.verified.txt | 90 +- ...tEnumMarkedAsNotInterceptable.verified.txt | 186 ++-- ...lEnumMarkedAsNotInterceptable.verified.txt | 198 +++-- ...InterceptToStringWhenDisabled.verified.txt | 90 +- ...nterceptToStringWhenOldCsharp.verified.txt | 90 +- ...ltSource=EnumMemberAttribute.verified.txt} | 88 +- ...rted=False_defaultSource=None.verified.txt | 419 +++++++++ ...ltSource=EnumMemberAttribute.verified.txt} | 88 +- ...orted=True_defaultSource=None.verified.txt | 424 +++++++++ ...tly_csharp14IsSupported=False.verified.txt | 92 +- ...ctly_csharp14IsSupported=True.verified.txt | 92 +- ...tly_csharp14IsSupported=False.verified.txt | 88 +- ...ctly_csharp14IsSupported=True.verified.txt | 88 +- .../SourceGenerationHelperSnapshotTests.cs | 38 +- .../TestHelpers.cs | 5 + 69 files changed, 5731 insertions(+), 2944 deletions(-) create mode 100644 src/NetEscapades.EnumGenerators.Attributes/MetadataSource.cs create mode 100644 src/NetEscapades.EnumGenerators/NetEscapades.EnumGenerators.props rename tests/NetEscapades.EnumGenerators.Tests/Snapshots/{EnumGeneratorTests.CanGenerateEnumExtensionsWithCustomNames.verified.txt => EnumGeneratorTests.CanGenerateEnumExtensionsWithCustomNames__.verified.txt} (92%) create mode 100644 tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateEnumExtensionsWithMetadataName_IgnoringOthers__.verified.txt create mode 100644 tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateEnumExtensionsWithNoneMetadataName__.verified.txt rename tests/NetEscapades.EnumGenerators.Tests/Snapshots/{EnumGeneratorTests.CanGenerateEnumExtensionsWithSameDisplayName.verified.txt => EnumGeneratorTests.CanGenerateEnumExtensionsWithSameDisplayName__.verified.txt} (92%) create mode 100644 tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateEnumExtensionsWithWrongMetadataName__.verified.txt rename tests/NetEscapades.EnumGenerators.Tests/Snapshots/{EnumGeneratorTests.HandlesStringsWithQuotesAndSlashesInDescription.verified.txt => EnumGeneratorTests.HandlesStringsWithQuotesAndSlashesInDescription__.verified.txt} (91%) rename tests/NetEscapades.EnumGenerators.Tests/Snapshots/{SourceGenerationHelperSnapshotTests.GeneratesEnumCorrectly_csharp14IsSupported=False.verified.txt => SourceGenerationHelperSnapshotTests.GeneratesEnumCorrectly_csharp14IsSupported=False_defaultSource=EnumMemberAttribute.verified.txt} (94%) create mode 100644 tests/NetEscapades.EnumGenerators.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesEnumCorrectly_csharp14IsSupported=False_defaultSource=None.verified.txt rename tests/NetEscapades.EnumGenerators.Tests/Snapshots/{SourceGenerationHelperSnapshotTests.GeneratesEnumCorrectly_csharp14IsSupported=True.verified.txt => SourceGenerationHelperSnapshotTests.GeneratesEnumCorrectly_csharp14IsSupported=True_defaultSource=EnumMemberAttribute.verified.txt} (94%) create mode 100644 tests/NetEscapades.EnumGenerators.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesEnumCorrectly_csharp14IsSupported=True_defaultSource=None.verified.txt diff --git a/src/NetEscapades.EnumGenerators.Attributes/EnumExtensionsAttribute.cs b/src/NetEscapades.EnumGenerators.Attributes/EnumExtensionsAttribute.cs index a7a45f1..f32f33d 100644 --- a/src/NetEscapades.EnumGenerators.Attributes/EnumExtensionsAttribute.cs +++ b/src/NetEscapades.EnumGenerators.Attributes/EnumExtensionsAttribute.cs @@ -9,18 +9,27 @@ public class EnumExtensionsAttribute : System.Attribute { /// /// The namespace to generate the extension class. - /// If not provided, the namespace of the enum will be used + /// If not provided, the namespace of the enum will be used. /// public string? ExtensionClassNamespace { get; set; } /// /// The name to use for the extension class. - /// If not provided, the enum name with ""Extensions"" will be used. - /// For example for an Enum called StatusCodes, the default name - /// will be StatusCodesExtensions + /// If not provided, the enum name with an Extensions suffix will be used. + /// For example for an Enum called StatusCodes, the default name + /// will be StatusCodesExtensions. /// public string? ExtensionClassName { get; set; } + /// + /// The metadata source to use when serializing and deserializing using + /// ToStringFast() and TryParse(). If not provided, the + /// will be + /// used to provide the values. Alternatively, you can disable this feature + /// entirely by using . + /// + public MetadataSource MetadataSource { get; set; } = MetadataSource.EnumMemberAttribute; + /// /// By default, when used with NetEscapades.EnumGenerators.Interceptors /// any interceptable usages of the enum will be replaced by usages of @@ -48,11 +57,20 @@ public class EnumExtensionsAttribute : System.Attribute /// /// The name to use for the extension class. /// If not provided, the enum name with an Extensions suffix will be used. - /// For example for an Enum called StatusCodes, the default name - /// will be StatusCodesExtensions. + /// For example for an Enum called StatusCodes, the default name + /// will be StatusCodesExtensions. /// public string? ExtensionClassName { get; set; } + /// + /// The metadata source to use when serializing and deserializing using + /// ToStringFast() and TryParse(). If not provided, the + /// will be + /// used to provide the values. Alternatively, you can disable this feature + /// entirely by using . + /// + public MetadataSource MetadataSource { get; set; } = MetadataSource.EnumMemberAttribute; + /// /// By default, when used with NetEscapades.EnumGenerators.Interceptors /// any interceptable usages of the enum will be replaced by usages of diff --git a/src/NetEscapades.EnumGenerators.Attributes/MetadataSource.cs b/src/NetEscapades.EnumGenerators.Attributes/MetadataSource.cs new file mode 100644 index 0000000..92ccfe8 --- /dev/null +++ b/src/NetEscapades.EnumGenerators.Attributes/MetadataSource.cs @@ -0,0 +1,39 @@ +namespace NetEscapades.EnumGenerators +{ + /// + /// Defines where to obtain metadata for serializing and deserializing the enum + /// + public enum MetadataSource + { + /// + /// Don't use attributes applied to enum members as a source of metadata for + /// ToStringFast() and TryParse(). The name of the enum member + /// will always be used for serialization. + /// + None, + + /// + /// Use values provided in System.ComponentModel.DataAnnotations.DisplayAttribute for + /// determining the value to use for ToStringFast() and TryParse(). + /// The value of the attribute will be used if available, otherwise the + /// name of the enum member will be used for serialization. + /// + DisplayAttribute, + + /// + /// Use values provided in for + /// determining the value to use for ToStringFast() and TryParse(). + /// The value of the attribute will be used if available, otherwise the + /// name of the enum member will be used for serialization. + /// + DescriptionAttribute, + + /// + /// Use values provided in for + /// determining the value to use for ToStringFast() and TryParse(). + /// The value of the attribute will be used if available, otherwise the + /// name of the enum member will be used for serialization. + /// + EnumMemberAttribute, + } +} \ No newline at end of file diff --git a/src/NetEscapades.EnumGenerators/Attributes.cs b/src/NetEscapades.EnumGenerators/Attributes.cs index c542908..40c056b 100644 --- a/src/NetEscapades.EnumGenerators/Attributes.cs +++ b/src/NetEscapades.EnumGenerators/Attributes.cs @@ -4,6 +4,8 @@ internal static class Attributes { public const string DisplayAttribute = "System.ComponentModel.DataAnnotations.DisplayAttribute"; public const string DescriptionAttribute = "System.ComponentModel.DescriptionAttribute"; + public const string EnumMemberAttribute = "System.Runtime.Serialization.EnumMemberAttribute"; + public const string EnumExtensionsAttribute = "NetEscapades.EnumGenerators.EnumExtensionsAttribute"; public const string ExternalEnumExtensionsAttribute = "NetEscapades.EnumGenerators.EnumExtensionsAttribute`1"; public const string FlagsAttribute = "System.FlagsAttribute"; diff --git a/src/NetEscapades.EnumGenerators/Constants.cs b/src/NetEscapades.EnumGenerators/Constants.cs index 7726681..024b570 100644 --- a/src/NetEscapades.EnumGenerators/Constants.cs +++ b/src/NetEscapades.EnumGenerators/Constants.cs @@ -3,4 +3,5 @@ namespace NetEscapades.EnumGenerators; public static class Constants { public const string Version = "1.0.0-beta14"; + public const string MetadataSourcePropertyName = "EnumGenerator_EnumMetadataSource"; } \ No newline at end of file diff --git a/src/NetEscapades.EnumGenerators/Diagnostics/DuplicateExtensionClassAnalyzer.cs b/src/NetEscapades.EnumGenerators/Diagnostics/DuplicateExtensionClassAnalyzer.cs index e5b6474..fcc5eb2 100644 --- a/src/NetEscapades.EnumGenerators/Diagnostics/DuplicateExtensionClassAnalyzer.cs +++ b/src/NetEscapades.EnumGenerators/Diagnostics/DuplicateExtensionClassAnalyzer.cs @@ -48,6 +48,7 @@ public override void Initialize(AnalysisContext context) Location? location = null; string? ns = null; string? name = null; + MetadataSource? source = null; foreach (var attributeData in enumSymbol.GetAttributes()) { if (ct.IsCancellationRequested) @@ -55,7 +56,7 @@ public override void Initialize(AnalysisContext context) return; } - if (EnumGenerator.TryGetExtensionAttributeDetails(attributeData, ref ns, ref name)) + if (EnumGenerator.TryGetExtensionAttributeDetails(attributeData, ref ns, ref name, ref source)) { location = attributeData.ApplicationSyntaxReference?.GetSyntax(ct).GetLocation() ?? enumSymbol.Locations[0]; diff --git a/src/NetEscapades.EnumGenerators/EnumGenerator.cs b/src/NetEscapades.EnumGenerators/EnumGenerator.cs index 21515e3..8b2b771 100644 --- a/src/NetEscapades.EnumGenerators/EnumGenerator.cs +++ b/src/NetEscapades.EnumGenerators/EnumGenerator.cs @@ -3,6 +3,7 @@ using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.CSharp.Syntax; +using Microsoft.CodeAnalysis.Diagnostics; using Microsoft.CodeAnalysis.Operations; using Microsoft.CodeAnalysis.Text; @@ -13,11 +14,16 @@ public class EnumGenerator : IIncrementalGenerator { public void Initialize(IncrementalGeneratorInitializationContext context) { + var defaultMetadataSource = context.AnalyzerConfigOptionsProvider + .Select(GetDefaultMetadataSource); + var csharp14IsSupported = context.CompilationProvider .Select((x,_) => x is CSharpCompilation { LanguageVersion: LanguageVersion.Preview or >= (LanguageVersion)1400 // C#14 }); + + var defaults = csharp14IsSupported.Combine(defaultMetadataSource); IncrementalValuesProvider enumsToGenerate = context.SyntaxProvider .ForAttributeWithMetadataName(Attributes.EnumExtensionsAttribute, @@ -37,16 +43,35 @@ public void Initialize(IncrementalGeneratorInitializationContext context) .SelectMany(static (m, _) => m!.Value) .WithTrackingName(TrackingNames.InitialExternalExtraction); - context.RegisterSourceOutput(enumsToGenerate.Combine(csharp14IsSupported), - static (spc, enumToGenerate) => Execute(in enumToGenerate.Left, enumToGenerate.Right, spc)); + context.RegisterSourceOutput(enumsToGenerate.Combine(defaults), + static (spc, enumToGenerate) => Execute(in enumToGenerate.Left, enumToGenerate.Right.Left, enumToGenerate.Right.Right, spc)); - context.RegisterSourceOutput(externalEnums.Combine(csharp14IsSupported), - static (spc, enumToGenerate) => Execute(in enumToGenerate.Left, enumToGenerate.Right, spc)); + context.RegisterSourceOutput(externalEnums.Combine(defaults), + static (spc, enumToGenerate) => Execute(in enumToGenerate.Left, enumToGenerate.Right.Left, enumToGenerate.Right.Right, spc)); } - static void Execute(in EnumToGenerate enumToGenerate, bool csharp14IsSupported, SourceProductionContext context) + private static MetadataSource GetDefaultMetadataSource(AnalyzerConfigOptionsProvider configOptions, CancellationToken ct) { - var (result, filename) = SourceGenerationHelper.GenerateExtensionClass(in enumToGenerate, csharp14IsSupported); + const MetadataSource defaultValue = MetadataSource.EnumMemberAttribute; + if (configOptions.GlobalOptions.TryGetValue($"build_property.{Constants.MetadataSourcePropertyName}", + out var source)) + { + return source switch + { + nameof(MetadataSource.None) => MetadataSource.None, + nameof(MetadataSource.DisplayAttribute) => MetadataSource.DisplayAttribute, + nameof(MetadataSource.DescriptionAttribute) => MetadataSource.DescriptionAttribute, + nameof(MetadataSource.EnumMemberAttribute) => MetadataSource.EnumMemberAttribute, + _ => defaultValue, + }; + } + + return defaultValue; + } + + static void Execute(in EnumToGenerate enumToGenerate, bool csharp14IsSupported, MetadataSource source, SourceProductionContext context) + { + var (result, filename) = SourceGenerationHelper.GenerateExtensionClass(in enumToGenerate, csharp14IsSupported, source); context.AddSource(filename, SourceText.From(result, Encoding.UTF8)); } @@ -74,6 +99,7 @@ static void Execute(in EnumToGenerate enumToGenerate, bool csharp14IsSupported, bool hasFlags = false; string? name = null; string? nameSpace = null; + MetadataSource? source = null; foreach (KeyValuePair namedArgument in attribute.NamedArguments) { @@ -89,6 +115,12 @@ static void Execute(in EnumToGenerate enumToGenerate, bool csharp14IsSupported, { name = n; } + + if (namedArgument.Key == "MetadataSource" + && namedArgument.Value is { Kind: TypedConstantKind.Enum, Value: { } ms }) + { + source = (MetadataSource)(int)ms; + } } foreach (var attrData in enumSymbol.GetAttributes()) @@ -102,7 +134,7 @@ static void Execute(in EnumToGenerate enumToGenerate, bool csharp14IsSupported, } } - var enumToGenerate = TryExtractEnumSymbol(enumSymbol, name, nameSpace, hasFlags); + var enumToGenerate = TryExtractEnumSymbol(enumSymbol, name, nameSpace, source, hasFlags); if (enumToGenerate is not null) { enums ??= new(); @@ -135,6 +167,7 @@ static void Execute(in EnumToGenerate enumToGenerate, bool csharp14IsSupported, var hasFlags = false; string? nameSpace = null; string? name = null; + MetadataSource? metadataSource = null; foreach (AttributeData attributeData in enumSymbol.GetAttributes()) { @@ -146,13 +179,17 @@ static void Execute(in EnumToGenerate enumToGenerate, bool csharp14IsSupported, continue; } - TryGetExtensionAttributeDetails(attributeData, ref nameSpace, ref name); + TryGetExtensionAttributeDetails(attributeData, ref nameSpace, ref name, ref metadataSource); } - return TryExtractEnumSymbol(enumSymbol, name, nameSpace, hasFlags); + return TryExtractEnumSymbol(enumSymbol, name, nameSpace, metadataSource, hasFlags); } - internal static bool TryGetExtensionAttributeDetails(AttributeData attributeData, ref string? nameSpace, ref string? name) + internal static bool TryGetExtensionAttributeDetails( + AttributeData attributeData, + ref string? nameSpace, + ref string? name, + ref MetadataSource? source) { if (attributeData.AttributeClass?.Name != "EnumExtensionsAttribute" || attributeData.AttributeClass.ToDisplayString() != Attributes.EnumExtensionsAttribute) @@ -174,6 +211,12 @@ internal static bool TryGetExtensionAttributeDetails(AttributeData attributeData { name = n; } + + if (namedArgument.Key == "MetadataSource" + && namedArgument.Value is { Kind: TypedConstantKind.Enum, Value: { } ms }) + { + source = (MetadataSource)(int)ms; + } } return true; @@ -185,7 +228,12 @@ internal static string GetEnumExtensionNamespace(INamedTypeSymbol enumSymbol) internal static string GetEnumExtensionName(INamedTypeSymbol enumSymbol) => enumSymbol.Name + "Extensions"; - static EnumToGenerate? TryExtractEnumSymbol(INamedTypeSymbol enumSymbol, string? name, string? nameSpace, bool hasFlags) + static EnumToGenerate? TryExtractEnumSymbol( + INamedTypeSymbol enumSymbol, + string? name, + string? nameSpace, + MetadataSource? metadataSource, + bool hasFlags) { name ??= GetEnumExtensionName(enumSymbol); nameSpace ??= GetEnumExtensionNamespace(enumSymbol); @@ -195,8 +243,6 @@ internal static string GetEnumExtensionName(INamedTypeSymbol enumSymbol) var enumMembers = enumSymbol.GetMembers(); var members = new List<(string, EnumValueOption)>(enumMembers.Length); - HashSet? displayNames = null; - var isDisplayNameTheFirstPresence = false; foreach (var member in enumMembers) { @@ -206,6 +252,8 @@ internal static string GetEnumExtensionName(INamedTypeSymbol enumSymbol) } string? displayName = null; + string? description = null; + string? enumMemberValue = null; foreach (var attribute in member.GetAttributes()) { if (attribute.AttributeClass?.Name == "DisplayAttribute" && @@ -215,9 +263,7 @@ internal static string GetEnumExtensionName(INamedTypeSymbol enumSymbol) { if (namedArgument.Key == "Name" && namedArgument.Value.Value?.ToString() is { } dn) { - // found display attribute, all done displayName = dn; - goto addDisplayName; } } } @@ -228,22 +274,24 @@ internal static string GetEnumExtensionName(INamedTypeSymbol enumSymbol) { if (attribute.ConstructorArguments[0].Value?.ToString() is { } dn) { - // found display attribute, all done - // Handle cases where contains a quote or a backslash - displayName = dn; - goto addDisplayName; + description = dn; } } - } - addDisplayName: - if (displayName is not null) - { - displayNames ??= new(); - isDisplayNameTheFirstPresence = displayNames.Add(displayName); + if (attribute.AttributeClass?.Name == "EnumMemberAttribute" && + attribute.AttributeClass.ToDisplayString() == Attributes.EnumMemberAttribute) + { + foreach (var namedArgument in attribute.NamedArguments) + { + if (namedArgument.Key == "Value" && namedArgument.Value.Value?.ToString() is { } dn) + { + enumMemberValue = dn; + } + } + } } - - members.Add((member.Name, new EnumValueOption(displayName, isDisplayNameTheFirstPresence, constantValue))); + + members.Add((member.Name, new EnumValueOption(displayName, description, enumMemberValue, constantValue))); } return new EnumToGenerate( @@ -254,7 +302,7 @@ internal static string GetEnumExtensionName(INamedTypeSymbol enumSymbol) isPublic: enumSymbol.DeclaredAccessibility == Accessibility.Public, hasFlags: hasFlags, names: members, - isDisplayAttributeUsed: displayNames?.Count > 0); + metadataSource: metadataSource); } } diff --git a/src/NetEscapades.EnumGenerators/EnumToGenerate.cs b/src/NetEscapades.EnumGenerators/EnumToGenerate.cs index 92a58e2..f36ff21 100644 --- a/src/NetEscapades.EnumGenerators/EnumToGenerate.cs +++ b/src/NetEscapades.EnumGenerators/EnumToGenerate.cs @@ -11,14 +11,13 @@ public readonly record struct EnumToGenerate public readonly bool IsPublic; public readonly bool HasFlags; public readonly string UnderlyingType; + public readonly MetadataSource? MetadataSource; /// /// Key is the enum name. /// public readonly EquatableArray<(string Key, EnumValueOption Value)> Names; - public readonly bool IsDisplayAttributeUsed; - public EnumToGenerate( string name, string ns, @@ -27,7 +26,7 @@ public EnumToGenerate( bool isPublic, List<(string Key, EnumValueOption Value)> names, bool hasFlags, - bool isDisplayAttributeUsed) + MetadataSource? metadataSource) { Name = name; Namespace = ns; @@ -36,6 +35,6 @@ public EnumToGenerate( HasFlags = hasFlags; IsPublic = isPublic; FullyQualifiedName = fullyQualifiedName; - IsDisplayAttributeUsed = isDisplayAttributeUsed; + MetadataSource = metadataSource; } } \ No newline at end of file diff --git a/src/NetEscapades.EnumGenerators/EnumValueOption.cs b/src/NetEscapades.EnumGenerators/EnumValueOption.cs index 5d37cb3..a57fd17 100644 --- a/src/NetEscapades.EnumGenerators/EnumValueOption.cs +++ b/src/NetEscapades.EnumGenerators/EnumValueOption.cs @@ -5,21 +5,36 @@ /// /// Custom name set by the [Display(Name)] attribute. /// - public string? DisplayName { get; } - public bool IsDisplayNameTheFirstPresence { get; } + private readonly string? _displayName; + private readonly string? _description; + private readonly string? _enumMemberValue; public object ConstantValue { get; } - public EnumValueOption(string? displayName, bool isDisplayNameTheFirstPresence, object constantValue) + public EnumValueOption(string? displayName, string? description, string? enumMemberValue, object constantValue) { - DisplayName = displayName; - IsDisplayNameTheFirstPresence = isDisplayNameTheFirstPresence; + _displayName = displayName; ConstantValue = constantValue; + _description = description; + _enumMemberValue = enumMemberValue; } public bool Equals(EnumValueOption other) { - return DisplayName == other.DisplayName && - IsDisplayNameTheFirstPresence == other.IsDisplayNameTheFirstPresence && + return _displayName == other._displayName && + _description == other._description && + _enumMemberValue == other._enumMemberValue && Equals(ConstantValue, other.ConstantValue); } + + public string? GetMetadataName(MetadataSource metadataSource) + => metadataSource switch + { + MetadataSource.DisplayAttribute => _displayName, + MetadataSource.DescriptionAttribute => _description, + MetadataSource.EnumMemberAttribute => _enumMemberValue, + _ => null, + }; + + public static EnumValueOption CreateWithoutAttributes(object constantValue) + => new(null, null, null, constantValue); } diff --git a/src/NetEscapades.EnumGenerators/NetEscapades.EnumGenerators.csproj b/src/NetEscapades.EnumGenerators/NetEscapades.EnumGenerators.csproj index 147426e..14c08a7 100644 --- a/src/NetEscapades.EnumGenerators/NetEscapades.EnumGenerators.csproj +++ b/src/NetEscapades.EnumGenerators/NetEscapades.EnumGenerators.csproj @@ -22,7 +22,7 @@ all runtime; build; native; contentfiles; analyzers; buildtransitive - + @@ -33,5 +33,6 @@ + \ No newline at end of file diff --git a/src/NetEscapades.EnumGenerators/NetEscapades.EnumGenerators.props b/src/NetEscapades.EnumGenerators/NetEscapades.EnumGenerators.props new file mode 100644 index 0000000..cb4d3b0 --- /dev/null +++ b/src/NetEscapades.EnumGenerators/NetEscapades.EnumGenerators.props @@ -0,0 +1,8 @@ + + + EnumMemberAttribute + + + + + \ No newline at end of file diff --git a/src/NetEscapades.EnumGenerators/SourceGenerationHelper.cs b/src/NetEscapades.EnumGenerators/SourceGenerationHelper.cs index 83394e0..d22c141 100644 --- a/src/NetEscapades.EnumGenerators/SourceGenerationHelper.cs +++ b/src/NetEscapades.EnumGenerators/SourceGenerationHelper.cs @@ -20,11 +20,24 @@ public static class SourceGenerationHelper """; - public static (string Content, string HintName) GenerateExtensionClass(in EnumToGenerate enumToGenerate, bool csharp14IsSupported) + public static (string Content, string HintName) GenerateExtensionClass(in EnumToGenerate enumToGenerate, bool csharp14IsSupported, MetadataSource defaultMetadataSource) { + var metadataSource = enumToGenerate.MetadataSource ?? defaultMetadataSource; + var isMetadataSourcesEnabled = metadataSource != MetadataSource.None; + var attributeName = metadataSource switch + { + MetadataSource.DisplayAttribute => Attributes.DisplayAttribute, + MetadataSource.DescriptionAttribute => Attributes.DescriptionAttribute, + MetadataSource.EnumMemberAttribute => Attributes.EnumMemberAttribute, + _ => null, + }; + + HashSet? metadataNames = null; + var constantValues = new HashSet(); - var sb = new StringBuilder(); + // The smallest size we will generate is 27,841 characters, so this is pretty much a lower bound + var sb = new StringBuilder(32_768); //512 * 8 * 8 sb.AppendLine(Header); if (!string.IsNullOrEmpty(enumToGenerate.Namespace)) { @@ -70,26 +83,6 @@ namespace """ - /// - /// Returns the string representation of the value. - /// If the attribute is decorated with a [Display] or [Description]attribute, then - /// uses the provided value. Otherwise uses the name of the member, equivalent to - /// calling ToString() on . - /// - /// The value to retrieve the string value for - /// If uses the value provided in the - /// [Display] or [Description]attribute as the string representation of the member. - /// If , always uses the name of the member, the same as if ToString() was called. - /// The string representation of the value - public static string ToStringFast(this - """).Append(fullyQualifiedName).Append( - """ - value, bool useMetadataAttributes) - => useMetadataAttributes ? value.ToStringFastWithMetadata() : value.ToStringFast(); - /// /// Returns the string representation of the nameof(").Append(fullyQualifiedName).Append('.').Append(member.Key).Append("),"); + .Append(" => nameof(").Append(fullyQualifiedName).Append('.').Append(member.Key).Append("),"); } } @@ -144,71 +137,100 @@ public static string ToStringFast(this """); } - sb.Append( - """ - - - private static string ToStringFastWithMetadata(this - """).Append(fullyQualifiedName).Append( - """ - value) - => - """); - if (hasDisplayNames) + if (isMetadataSourcesEnabled) { sb.Append( """ - value switch - { + + + /// + /// Returns the string representation of the value. + /// If the member is decorated with the attribute + /// then that value is returned. Otherwise returns uses the name of the member, + /// equivalent to calling ToString() on . + /// + /// The value to retrieve the string value for + /// If uses the value provided in the + /// attribute as the string representation of the member. + /// If , always uses the name of the member, the same as if ToString() was called. + /// The string representation of the value + public static string ToStringFast(this + """).Append(fullyQualifiedName).Append( + """ + value, bool useMetadataAttributes) + => useMetadataAttributes ? value.ToStringFastWithMetadata() : value.ToStringFast(); + + private static string ToStringFastWithMetadata(this + """).Append(fullyQualifiedName).Append( + """ + value) + => """); - constantValues.Clear(); - foreach (var member in enumToGenerate.Names) + if (hasMetadataNames) { - if (constantValues.Add(member.Value.ConstantValue)) + sb.Append( + """ + value switch + { + """); + constantValues.Clear(); + foreach (var member in enumToGenerate.Names) { - sb.Append( - """ + if (constantValues.Add(member.Value.ConstantValue)) + { + sb.Append( + """ - - """).Append(fullyQualifiedName).Append('.').Append(member.Key) - .Append(" => "); + + """).Append(fullyQualifiedName).Append('.').Append(member.Key) + .Append(" => "); - if (member.Value.DisplayName is { } dn) - { - sb.Append(SymbolDisplay.FormatLiteral(dn, quote: true)).Append(','); - } - else - { - sb.Append("nameof(").Append(fullyQualifiedName).Append('.').Append(member.Key).Append("),"); + if (member.Value.GetMetadataName(metadataSource) is { } dn) + { + sb.Append(SymbolDisplay.FormatLiteral(dn, quote: true)).Append(','); + } + else + { + sb.Append("nameof(").Append(fullyQualifiedName).Append('.').Append(member.Key).Append("),"); + } } } - } - if (enumToGenerate.HasFlags) - { - // We currently don't handle ToString of custom flag-combinations, so lets fall back to the default - sb.Append( - """ + if (enumToGenerate.HasFlags) + { + // We currently don't handle ToString of custom flag-combinations, so lets fall back to the default + sb.Append( + """ - _ => value.ToString(), - }; - """); + _ => value.ToString(), + }; + """); + } + else + { + // This should mean, that the value is not named -> generate a numeric string + sb.Append( + """ + + _ => value.AsUnderlyingType().ToString(), + }; + """); + } } else { - // This should mean, that the value is not named -> generate a numeric string - sb.Append( - """ - - _ => value.AsUnderlyingType().ToString(), - }; - """); + sb.Append("value.ToStringFast();"); } } - else - { - sb.Append("value.ToStringFast();"); - } if (enumToGenerate.HasFlags) { @@ -324,71 +346,118 @@ public static bool IsDefined( /// /// The name to check if it's defined /// if a member with the name exists in the enumeration, otherwise - public static bool IsDefined(string name) => IsDefined(name, allowMatchingMetadataAttribute: false); + public static bool IsDefined(string name) + => name switch + { + """); + foreach (var member in enumToGenerate.Names) + { + sb.Append( + """ - /// - /// Returns a boolean telling whether an enum with the given name exists in the enumeration, - /// or if a member decorated with a [Display] attribute - /// with the required name exists. - /// - /// The name to check if it's defined - /// If , considers the value of metadata attributes,otherwise ignores them - /// if a member with the name exists in the enumeration, or a member is decorated - /// with a [Display] attribute with the name, otherwise - public static bool IsDefined(string name, bool allowMatchingMetadataAttribute) - { + nameof( + """).Append(fullyQualifiedName).Append('.').Append(member.Key).Append(") => true,"); + } + + sb.Append( + """ + + _ => false, + }; """); - if (enumToGenerate.IsDisplayAttributeUsed) + + if (isMetadataSourcesEnabled) { sb.Append( """ - var isDefinedInDisplayAttribute = false; - if (allowMatchingMetadataAttribute) - { - isDefinedInDisplayAttribute = name switch - { + + /// + /// Returns a boolean telling whether an enum with the given name exists in the enumeration, + /// or if a member decorated with + /// with the required name exists. + /// + /// The name to check if it's defined + /// If , + /// considers the value of + /// instead of the member name, otherwise ignores them + /// if a member with the name exists in the enumeration, or a member is decorated + /// with a [Display] attribute with the name, otherwise + public static bool IsDefined(string name, bool allowMatchingMetadataAttribute) """); - foreach (var member in enumToGenerate.Names) + if (!hasMetadataNames) { - if (member.Value is { DisplayName: { } dn, IsDisplayNameTheFirstPresence: true }) - { - sb.Append( - """ + sb.Append( + """ - - """).Append(SymbolDisplay.FormatLiteral(dn, quote: true)).Append(" => true,"); - } + => IsDefined(name); + """); } - - sb.Append( + else + { + sb.Append( """ - _ => false, - }; - } + => allowMatchingMetadataAttribute ? IsMetadataNameDefined(name) : IsDefined(name); - if (isDefinedInDisplayAttribute) + private static bool IsMetadataNameDefined(string name) + => name switch { - return true; - } + """); + + metadataNames ??= []; + metadataNames.Clear(); + foreach (var member in enumToGenerate.Names) + { + if(member.Value.GetMetadataName(metadataSource) is { } memberName + && metadataNames.Add(memberName)) + { + sb.Append( + """ + + + """).Append(SymbolDisplay.FormatLiteral(memberName, quote: true)).Append(" => true,"); + } + } + + sb.Append( + """ + _ => IsDefined(name), + }; """); + } } sb.Append( """ - return name switch + + #if NETCOREAPP && !NETCOREAPP2_0 && !NETCOREAPP1_1 && !NETCOREAPP1_0 + /// + /// Returns a boolean telling whether an enum with the given name exists in the enumeration + /// + /// The name to check if it's defined + /// if a member with the name exists in the enumeration, otherwise + public static bool IsDefined(in global::System.ReadOnlySpan name) + => name switch { """); foreach (var member in enumToGenerate.Names) { sb.Append( - """ + """ - nameof( - """).Append(fullyQualifiedName).Append('.').Append(member.Key).Append(") => true,"); + global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof( + """).Append(fullyQualifiedName).Append('.') + .Append(member.Key) + .Append("), global::System.StringComparison.Ordinal) => true,"); } sb.Append( @@ -396,104 +465,81 @@ public static bool IsDefined(string name, bool allowMatchingMetadataAttribute) _ => false, }; - } """); - sb.Append( - """ - - - #if NETCOREAPP && !NETCOREAPP2_0 && !NETCOREAPP1_1 && !NETCOREAPP1_0 - /// - /// Returns a boolean telling whether an enum with the given name exists in the enumeration - /// - /// The name to check if it's defined - /// if a member with the name exists in the enumeration, otherwise - public static bool IsDefined(in global::System.ReadOnlySpan name) => IsDefined(name, allowMatchingMetadataAttribute: false); - - /// - /// Returns a boolean telling whether an enum with the given name exists in the enumeration, - /// or optionally if a member decorated with a [Display] attribute - /// with the required name exists. - /// Slower then the overload, but doesn't allocate memory./> - /// - /// The name to check if it's defined - /// If , considers the value of metadata attributes,otherwise ignores them - /// if a member with the name exists in the enumeration, or a member is decorated - /// with a [Display] attribute with the name, otherwise - public static bool IsDefined(in global::System.ReadOnlySpan name, bool allowMatchingMetadataAttribute) - { - """); - if (enumToGenerate.IsDisplayAttributeUsed) + if (isMetadataSourcesEnabled) { + sb.Append( """ - var isDefinedInDisplayAttribute = false; - if (allowMatchingMetadataAttribute) - { - isDefinedInDisplayAttribute = name switch - { + + /// + /// Returns a boolean telling whether an enum with the given name exists in the enumeration, + /// or optionally if a member decorated with + /// exists. Slower then the overload, but doesn't allocate memory./> + /// + /// The name to check if it's defined + /// If , considers the value of metadata attributes,otherwise ignores them + /// if a member with the name exists in the enumeration, or a member is decorated + /// with a [Display] attribute with the name, otherwise + public static bool IsDefined(in global::System.ReadOnlySpan name, bool allowMatchingMetadataAttribute) """); - foreach (var member in enumToGenerate.Names) + + if (!hasMetadataNames) { - if (member.Value is { DisplayName: { } dn, IsDisplayNameTheFirstPresence: true }) - { - sb.Append( - """ + sb.Append( + """ - global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, - """) - .Append(SymbolDisplay.FormatLiteral(dn, quote: true)) - .Append(", global::System.StringComparison.Ordinal) => true,"); - } + => IsDefined(name); + """); } + else + { + sb.Append( + """ + => allowMatchingMetadataAttribute ? IsMetadataNameDefined(in name) : IsDefined(in name); - sb.Append( - """ - - _ => false, - }; - } + private static bool IsMetadataNameDefined(in global::System.ReadOnlySpan name) + => name switch + { + """); - if (isDefinedInDisplayAttribute) - { - return true; - } + metadataNames ??= []; + metadataNames.Clear(); - """); - } + foreach (var member in enumToGenerate.Names) + { + if (member.Value.GetMetadataName(metadataSource) is { } memberName + && metadataNames.Add(memberName)) + { + sb.Append( + """ - sb.Append( - """ + global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, + """) + .Append(SymbolDisplay.FormatLiteral(memberName, quote: true)) + .Append(", global::System.StringComparison.Ordinal) => true,"); + } + } - return name switch - { - """); - foreach (var member in enumToGenerate.Names) - { - sb.Append( - """ + sb.Append( + """ - global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof( - """).Append(fullyQualifiedName).Append('.') - .Append(member.Key) - .Append("), global::System.StringComparison.Ordinal) => true,"); + _ => IsDefined(name), + }; + """); + } } sb.Append( """ - _ => false, - }; - } #endif - """); - - sb.Append( - """ - /// /// Converts the string representation of the name or numeric value of @@ -542,34 +588,47 @@ public static string? name, bool ignoreCase) => TryParse(name, out var value, ignoreCase, false) ? value : ThrowValueNotFound(name); + """); - /// - /// Converts the string representation of the name or numeric value of - /// an to the equivalent instance. - /// - /// The case-sensitive string representation of the enumeration name or underlying value to convert - /// to read value in case insensitive mode; to read value in case sensitive mode. - /// If , considers the value included in metadata attributes such as - /// [Display] attribute when parsing, otherwise only considers the member names. - /// An object of type whose - /// value is represented by - public static - """).Append(fullyQualifiedName).Append( + if (isMetadataSourcesEnabled) + { + sb.Append( + """ + + + /// + /// Converts the string representation of the name or numeric value of + /// an to the equivalent instance. + /// + /// The case-sensitive string representation of the enumeration name or underlying value to convert + /// to read value in case insensitive mode; to read value in case sensitive mode. + /// If , considers the value included in metadata attributes such as + /// [Display] attribute when parsing, otherwise only considers the member names. + /// An object of type whose + /// value is represented by + public static + """).Append(fullyQualifiedName).Append( + """ + Parse( + #if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] + #endif + string? name, + bool ignoreCase, + bool allowMatchingMetadataAttribute) + => TryParse(name, out var value, ignoreCase, allowMatchingMetadataAttribute) ? value : ThrowValueNotFound(name); + """); + } + + sb.Append( """ - Parse( - #if NETCOREAPP3_0_OR_GREATER - [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] - #endif - string? name, - bool ignoreCase, - bool allowMatchingMetadataAttribute) - => TryParse(name, out var value, ignoreCase, allowMatchingMetadataAttribute) ? value : ThrowValueNotFound(name); + #if NETCOREAPP3_0_OR_GREATER [global::System.Diagnostics.CodeAnalysis.DoesNotReturn] @@ -650,48 +709,83 @@ public static bool TryParse( bool ignoreCase) => TryParse(name, out value, ignoreCase, false); """); - sb.Append( - """ + if (isMetadataSourcesEnabled) + { + sb.Append( + """ - /// - /// Converts the string representation of the name or numeric value of - /// an to the equivalent instance. - /// The return value indicates whether the conversion succeeded. - /// - /// The string representation of the enumeration name or underlying value to convert - /// When this method returns, contains an object of type - /// whose - /// value is represented by if the parse operation succeeds. - /// If the parse operation fails, contains the default value of the underlying type - /// of . This parameter is passed uninitialized. - /// to read value in case insensitive mode; to read value in case sensitive mode. - /// If , considers the value included in metadata attributes such as - /// [Display] attribute when parsing, otherwise only considers the member names. - /// if the value parameter was converted successfully; otherwise, . - public static bool TryParse( - #if NETCOREAPP3_0_OR_GREATER - [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] - #endif - string? name, - out - """).Append(fullyQualifiedName).Append( + + /// + /// Converts the string representation of the name or numeric value of + /// an to the equivalent instance. + /// The return value indicates whether the conversion succeeded. + /// + /// The string representation of the enumeration name or underlying value to convert + /// When this method returns, contains an object of type + /// whose + /// value is represented by if the parse operation succeeds. + /// If the parse operation fails, contains the default value of the underlying type + /// of . This parameter is passed uninitialized. + /// to read value in case insensitive mode; to read value in case sensitive mode. + /// If , + /// considers the value included in attribute + /// when parsing, otherwise only considers the member names. + /// if the value parameter was converted successfully; otherwise, . + public static bool TryParse( + #if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] + #endif + string? name, + out + """).Append(fullyQualifiedName).Append( + """ + value, + bool ignoreCase, + bool allowMatchingMetadataAttribute) + => ignoreCase + ? TryParseIgnoreCase(name, out value, allowMatchingMetadataAttribute) + : TryParseWithCase(name, out value, allowMatchingMetadataAttribute); + """); + } + else + { + sb.Append( + """ + + + private static bool TryParse( + #if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] + #endif + string? name, + out + """).Append(fullyQualifiedName).Append( + """ + value, + bool ignoreCase, + bool allowMatchingMetadataAttribute) + => ignoreCase + ? TryParseIgnoreCase(name, out value, allowMatchingMetadataAttribute) + : TryParseWithCase(name, out value, allowMatchingMetadataAttribute); + """); + } + + sb.Append( """ - value, - bool ignoreCase, - bool allowMatchingMetadataAttribute) - => ignoreCase - ? TryParseIgnoreCase(name, out value, allowMatchingMetadataAttribute) - : TryParseWithCase(name, out value, allowMatchingMetadataAttribute); + private static bool TryParseIgnoreCase( #if NETCOREAPP3_0_OR_GREATER @@ -706,8 +800,10 @@ private static bool TryParseIgnoreCase( { """); - if (enumToGenerate.IsDisplayAttributeUsed) + if (isMetadataSourcesEnabled && hasMetadataNames) { + metadataNames ??= []; + metadataNames.Clear(); sb.Append( """ @@ -718,13 +814,14 @@ private static bool TryParseIgnoreCase( """); foreach (var member in enumToGenerate.Names) { - if (member.Value is { DisplayName: { } dn, IsDisplayNameTheFirstPresence: true }) + if (member.Value.GetMetadataName(metadataSource) is { } metadataName + && metadataNames.Add(metadataName)) { sb.Append( """ case string s when s.Equals( - """).Append(SymbolDisplay.FormatLiteral(dn, quote: true)).Append( + """).Append(SymbolDisplay.FormatLiteral(metadataName, quote: true)).Append( """ , global::System.StringComparison.OrdinalIgnoreCase): value = @@ -743,13 +840,13 @@ private static bool TryParseIgnoreCase( break; }; } + """); } sb.Append( """ - switch (name) { """); @@ -801,8 +898,10 @@ private static bool TryParseWithCase( { """); - if (enumToGenerate.IsDisplayAttributeUsed) + if (isMetadataSourcesEnabled && hasMetadataNames) { + metadataNames ??= []; + metadataNames.Clear(); sb.Append( """ @@ -814,13 +913,14 @@ private static bool TryParseWithCase( foreach (var member in enumToGenerate.Names) { - if (member.Value is { DisplayName: { } dn, IsDisplayNameTheFirstPresence: true }) + if (member.Value.GetMetadataName(metadataSource) is { } metadataName + && metadataNames.Add(metadataName)) { sb.Append( """ case - """).Append(SymbolDisplay.FormatLiteral(dn, quote: true)).Append( + """).Append(SymbolDisplay.FormatLiteral(metadataName, quote: true)).Append( """ : value = @@ -839,13 +939,13 @@ private static bool TryParseWithCase( break; }; } + """); } sb.Append( """ - switch (name) { """); @@ -932,6 +1032,13 @@ public static in global::System.ReadOnlySpan name, bool ignoreCase) => TryParse(name, out var value, ignoreCase, false) ? value : ThrowValueNotFound(name.ToString()); + """); + + if (isMetadataSourcesEnabled) + { + sb.Append( + """ + /// /// Converts the string representation of the name or numeric value of @@ -942,8 +1049,11 @@ public static /// /// The case-sensitive string representation of the enumeration name or underlying value to convert /// to read value in case insensitive mode; to read value in case sensitive mode. - /// If , considers the value included in metadata attributes such as - /// [Display] attribute when parsing, otherwise only considers the member names. + /// If , considers the value included in + /// attribute when parsing, otherwise only considers the member names. /// An object of type TryParse(name, out var value, ignoreCase, allowMatchingMetadataAttribute) ? value : ThrowValueNotFound(name.ToString()); + """); + } + + sb.Append( + """ + /// /// Converts the span representation of the name or numeric value of @@ -1032,49 +1148,82 @@ public static bool TryParse( => TryParse(name, out value, ignoreCase, false); """); - sb.Append( - """ + if (isMetadataSourcesEnabled) + { + sb.Append( + """ - /// - /// Converts the span representation of the name or numeric value of - /// an to the equivalent instance. - /// The return value indicates whether the conversion succeeded. - /// - /// The span representation of the enumeration name or underlying value to convert - /// When this method returns, contains an object of type - /// whose - /// value is represented by if the parse operation succeeds. - /// If the parse operation fails, contains the default value of the underlying type - /// of . This parameter is passed uninitialized. - /// to read value in case insensitive mode; to read value in case sensitive mode. - /// If , considers the value included in metadata attributes such as - /// [Display] attribute when parsing, otherwise only considers the member names. - /// if the value parameter was converted successfully; otherwise, . - public static bool TryParse( - #if NETCOREAPP3_0_OR_GREATER - [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] - #endif - in global::System.ReadOnlySpan name, - out - """).Append(fullyQualifiedName).Append( + /// + /// Converts the span representation of the name or numeric value of + /// an to the equivalent instance. + /// The return value indicates whether the conversion succeeded. + /// + /// The span representation of the enumeration name or underlying value to convert + /// When this method returns, contains an object of type + /// whose + /// value is represented by if the parse operation succeeds. + /// If the parse operation fails, contains the default value of the underlying type + /// of . This parameter is passed uninitialized. + /// to read value in case insensitive mode; to read value in case sensitive mode. + /// If , considers the value included in + /// attribute when parsing, otherwise only considers the member names. + /// if the value parameter was converted successfully; otherwise, . + public static bool TryParse( + #if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] + #endif + in global::System.ReadOnlySpan name, + out + """).Append(fullyQualifiedName).Append( + """ + result, + bool ignoreCase, + bool allowMatchingMetadataAttribute) + => ignoreCase + ? TryParseIgnoreCase(in name, out result, allowMatchingMetadataAttribute) + : TryParseWithCase(in name, out result, allowMatchingMetadataAttribute); + """); + } + else + { + sb.Append( + """ + + + private static bool TryParse( + #if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] + #endif + in global::System.ReadOnlySpan name, + out + """).Append(fullyQualifiedName).Append( + """ + value, + bool ignoreCase, + bool allowMatchingMetadataAttribute) + => ignoreCase + ? TryParseIgnoreCase(in name, out value, allowMatchingMetadataAttribute) + : TryParseWithCase(in name, out value, allowMatchingMetadataAttribute); + """); + } + + sb.Append( """ - result, - bool ignoreCase, - bool allowMatchingMetadataAttribute) - => ignoreCase - ? TryParseIgnoreCase(in name, out result, allowMatchingMetadataAttribute) - : TryParseWithCase(in name, out result, allowMatchingMetadataAttribute); + private static bool TryParseIgnoreCase( #if NETCOREAPP3_0_OR_GREATER [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] @@ -1088,8 +1237,10 @@ private static bool TryParseIgnoreCase( { """); - if (enumToGenerate.IsDisplayAttributeUsed) + if (isMetadataSourcesEnabled && hasMetadataNames) { + metadataNames ??= []; + metadataNames.Clear(); sb.Append( """ @@ -1100,22 +1251,23 @@ private static bool TryParseIgnoreCase( """); foreach (var member in enumToGenerate.Names) { - if (member.Value is { DisplayName: { } dn, IsDisplayNameTheFirstPresence: true }) + if (member.Value.GetMetadataName(metadataSource) is { } metadataName + && metadataNames.Add(metadataName)) { sb.Append( - """ - - case global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, - """) - .Append(SymbolDisplay.FormatLiteral(dn, quote: true)).Append( - """ - , global::System.StringComparison.OrdinalIgnoreCase): - result = - """).Append(fullyQualifiedName).Append('.').Append(member.Key).Append( - """ - ; - return true; - """); + """ + + case global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, + """) + .Append(SymbolDisplay.FormatLiteral(metadataName, quote: true)).Append( + """ + , global::System.StringComparison.OrdinalIgnoreCase): + result = + """).Append(fullyQualifiedName).Append('.').Append(member.Key).Append( + """ + ; + return true; + """); } } @@ -1185,8 +1337,12 @@ private static bool TryParseWithCase( { """); - if (enumToGenerate.IsDisplayAttributeUsed) + + + if (isMetadataSourcesEnabled && hasMetadataNames) { + metadataNames ??= []; + metadataNames.Clear(); sb.Append( """ @@ -1195,24 +1351,26 @@ private static bool TryParseWithCase( switch (name) { """); + foreach (var member in enumToGenerate.Names) { - if (member.Value is { DisplayName: { } dn, IsDisplayNameTheFirstPresence: true }) + if (member.Value.GetMetadataName(metadataSource) is { } metadataName + && metadataNames.Add(metadataName)) { sb.Append( - """ - - case global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, - """) - .Append(SymbolDisplay.FormatLiteral(dn, quote: true)).Append( - """ - , global::System.StringComparison.Ordinal): - result = - """).Append(fullyQualifiedName).Append('.').Append(member.Key).Append( - """ - ; - return true; - """); + """ + + case global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, + """) + .Append(SymbolDisplay.FormatLiteral(metadataName, quote: true)).Append( + """ + , global::System.StringComparison.Ordinal): + result = + """).Append(fullyQualifiedName).Append('.').Append(member.Key).Append( + """ + ; + return true; + """); } } diff --git a/tests/NetEscapades.EnumGenerators.Tests/DuplicateEnumValueAnalyzerTests.cs b/tests/NetEscapades.EnumGenerators.Tests/DuplicateEnumValueAnalyzerTests.cs index 4426be9..af57ab8 100644 --- a/tests/NetEscapades.EnumGenerators.Tests/DuplicateEnumValueAnalyzerTests.cs +++ b/tests/NetEscapades.EnumGenerators.Tests/DuplicateEnumValueAnalyzerTests.cs @@ -194,5 +194,6 @@ namespace ConsoleApplication1 } {{TestHelpers.LoadEmbeddedAttribute()}} + {{TestHelpers.LoadEmbeddedMetadataSource()}} """; } \ No newline at end of file diff --git a/tests/NetEscapades.EnumGenerators.Tests/DuplicateExtensionClassAnalyzerTests.cs b/tests/NetEscapades.EnumGenerators.Tests/DuplicateExtensionClassAnalyzerTests.cs index 96ce566..7fd4a6c 100644 --- a/tests/NetEscapades.EnumGenerators.Tests/DuplicateExtensionClassAnalyzerTests.cs +++ b/tests/NetEscapades.EnumGenerators.Tests/DuplicateExtensionClassAnalyzerTests.cs @@ -296,5 +296,6 @@ private static string GetTestCode(string testFragment) {{testFragment}} {{TestHelpers.LoadEmbeddedAttribute()}} + {{TestHelpers.LoadEmbeddedMetadataSource()}} """; } \ No newline at end of file diff --git a/tests/NetEscapades.EnumGenerators.Tests/EnumGeneratorTests.cs b/tests/NetEscapades.EnumGenerators.Tests/EnumGeneratorTests.cs index fff002d..b1088d5 100644 --- a/tests/NetEscapades.EnumGenerators.Tests/EnumGeneratorTests.cs +++ b/tests/NetEscapades.EnumGenerators.Tests/EnumGeneratorTests.cs @@ -1,3 +1,4 @@ +using System; using System.Linq; using System.Threading.Tasks; using FluentAssertions; @@ -195,26 +196,30 @@ internal enum MyEnum return Verifier.Verify(output, Settings()); } - [Fact] - public Task CanGenerateEnumExtensionsWithDisplayName() + [Theory] + [InlineData(MetadataSource.DisplayAttribute)] + [InlineData(MetadataSource.DescriptionAttribute)] + [InlineData(MetadataSource.EnumMemberAttribute)] + public Task CanGenerateEnumExtensionsWithMetadataName(MetadataSource source) { - const string input = - """ + var attr = GetAttribute(source); + + var input = + $$""" using NetEscapades.EnumGenerators; - using System.ComponentModel.DataAnnotations; namespace MyTestNameSpace { - [EnumExtensions] + [EnumExtensions(MetadataSource = MetadataSource.{{source}})] public enum MyEnum { First = 0, - [Display(Name = "2nd")] + [{{attr}}"2nd")] Second = 1, Third = 2, - [Display(Name = "4th")] + [{{attr}}"4th")] Fourth = 3 } } @@ -224,29 +229,35 @@ public enum MyEnum Assert.Empty(diagnostics); var settings = Settings(); settings.UseMethodName("CanGenerateEnumExtensionsWithCustomNames"); + settings.UseTextForParameters("_"); + ScrubAttribute(settings, source); return Verifier.Verify(output, settings); } - [Fact] - public Task CanGenerateEnumExtensionsWithDescription() + [Theory] + [InlineData(MetadataSource.DisplayAttribute)] + [InlineData(MetadataSource.DescriptionAttribute)] + [InlineData(MetadataSource.EnumMemberAttribute)] + public Task CanGenerateEnumExtensionsWithNoneMetadataName(MetadataSource source) { - const string input = - """ + var attr = GetAttribute(source); + + var input = + $$""" using NetEscapades.EnumGenerators; - using System.ComponentModel; namespace MyTestNameSpace { - [EnumExtensions] + [EnumExtensions(MetadataSource = MetadataSource.None)] public enum MyEnum { First = 0, - [Description("2nd")] + [{{attr}}"2nd")] Second = 1, Third = 2, - [Description("4th")] + [{{attr}}"4th")] Fourth = 3 } } @@ -255,33 +266,36 @@ public enum MyEnum Assert.Empty(diagnostics); var settings = Settings(); - settings.UseMethodName("CanGenerateEnumExtensionsWithCustomNames"); + settings.UseTextForParameters("_"); + ScrubAttribute(settings, source); return Verifier.Verify(output, settings); } - [Fact] - public Task CanGenerateEnumExtensionsWithDescriptionAndDisplayName() + [Theory] + [InlineData(MetadataSource.DisplayAttribute)] + [InlineData(MetadataSource.DescriptionAttribute)] + [InlineData(MetadataSource.EnumMemberAttribute)] + public Task CanGenerateEnumExtensionsWithWrongMetadataName(MetadataSource source) { - const string input = - """ + var wrongSource = GetWrongSource(source); + var attr = GetAttribute(source); + + var input = + $$""" using NetEscapades.EnumGenerators; - using System.ComponentModel; - using System.ComponentModel.DataAnnotations; namespace MyTestNameSpace { - [EnumExtensions] + [EnumExtensions(MetadataSource = MetadataSource.{{wrongSource}})] public enum MyEnum { First = 0, - [Description("2nd")] // takes precedence - [Display(Name = "Secundo")] + [{{attr}}"2nd")] Second = 1, Third = 2, - [Display(Name = "4th")] // takes precedence - [Description("Number 4")] + [{{attr}}"4th")] Fourth = 3 } } @@ -290,30 +304,38 @@ public enum MyEnum Assert.Empty(diagnostics); var settings = Settings(); - settings.UseMethodName("CanGenerateEnumExtensionsWithCustomNames"); + settings.UseTextForParameters("_"); + ScrubAttribute(settings, wrongSource); return Verifier.Verify(output, settings); } - [Fact] - public Task CanGenerateEnumExtensionsWithSameDisplayName() + [Theory] + [InlineData(MetadataSource.DisplayAttribute)] + [InlineData(MetadataSource.DescriptionAttribute)] + [InlineData(MetadataSource.EnumMemberAttribute)] + public Task CanGenerateEnumExtensionsWithMetadataName_IgnoringOthers(MetadataSource source) { - const string input = - """ + var attr = GetAttribute(source); + var other = GetWrongAttribute(source); + + var input = + $$""" using NetEscapades.EnumGenerators; - using System.ComponentModel.DataAnnotations; namespace MyTestNameSpace { - [EnumExtensions] + [EnumExtensions(MetadataSource = MetadataSource.{{source}})] public enum MyEnum { First = 0, - [Display(Name = "2nd")] + [{{attr}}"2nd")] + [{{other}}"Secundo")] Second = 1, Third = 2, - [Display(Name = "2nd")] + [{{attr}}"4th")] + [{{other}}"Number 4")] Fourth = 3 } } @@ -321,7 +343,47 @@ public enum MyEnum var (diagnostics, output) = TestHelpers.GetGeneratedOutput(Generators(), new(input)); Assert.Empty(diagnostics); - return Verifier.Verify(output, Settings()); + var settings = Settings(); + settings.UseTextForParameters("_"); + ScrubAttribute(settings, source); + return Verifier.Verify(output, settings); + } + + [Theory] + [InlineData(MetadataSource.DisplayAttribute)] + [InlineData(MetadataSource.DescriptionAttribute)] + [InlineData(MetadataSource.EnumMemberAttribute)] + public Task CanGenerateEnumExtensionsWithSameDisplayName(MetadataSource source) + { + var attr = GetAttribute(source); + + var input = + $$""" + using NetEscapades.EnumGenerators; + + namespace MyTestNameSpace + { + [EnumExtensions(MetadataSource = MetadataSource.{{source}})] + public enum MyEnum + { + First = 0, + + [{{attr}}"2nd")] + Second = 1, + Third = 2, + + [{{attr}}"2nd")] + Fourth = 3 + } + } + """; + var (diagnostics, output) = TestHelpers.GetGeneratedOutput(Generators(), new(input)); + + Assert.Empty(diagnostics); + var settings = Settings(); + settings.UseTextForParameters("_"); + ScrubAttribute(settings, source); + return Verifier.Verify(output, settings); } [Theory] @@ -379,30 +441,36 @@ public enum TestEnum return Verifier.Verify(output, Settings()); } - [Fact] - public Task HandlesStringsWithQuotesAndSlashesInDescription() + [Theory] + [InlineData(MetadataSource.DisplayAttribute)] + [InlineData(MetadataSource.DescriptionAttribute)] + [InlineData(MetadataSource.EnumMemberAttribute)] + public Task HandlesStringsWithQuotesAndSlashesInDescription(MetadataSource source) { - const string input = - """" + var attr = GetAttribute(source); + var input = + $$"""" using NetEscapades.EnumGenerators; - using System.ComponentModel; namespace Test; - [EnumExtensions] + [EnumExtensions(MetadataSource = MetadataSource.{{source}})] public enum StringTesting { - [Description("Quotes \"")] Quotes, - [Description(@"Literal Quotes """)] LiteralQuotes, - [Description("Backslash \\")] Backslash, - [Description(@"LiteralBackslash \")] BackslashLiteral, - [Description("New\nLine")] NewLine, + [{{attr}}"Quotes \"")] Quotes, + [{{attr}}@"Literal Quotes """)] LiteralQuotes, + [{{attr}}"Backslash \\")] Backslash, + [{{attr}}@"LiteralBackslash \")] BackslashLiteral, + [{{attr}}"New\nLine")] NewLine, } """"; var (diagnostics, output) = TestHelpers.GetGeneratedOutput(Generators(), new(input)); Assert.Empty(diagnostics); - return Verifier.Verify(output, Settings()); + var settings = Settings(); + settings.UseTextForParameters("_"); + ScrubAttribute(settings, source); + return Verifier.Verify(output, settings); } [Fact] @@ -625,4 +693,45 @@ public enum MyEnum diagnostics.Should().BeEmpty(); output.Should().BeEmpty(); } + + private static void ScrubAttribute(VerifySettings settings, MetadataSource source) + { + var toScrub = source switch + { + MetadataSource.DisplayAttribute => "System.ComponentModel.DataAnnotations.Display", + MetadataSource.DescriptionAttribute => "System.ComponentModel.Description", + MetadataSource.EnumMemberAttribute => "System.Runtime.Serialization.EnumMember", + _ => null, + }; + + if (toScrub is null) + { + return; + } + + settings.ScrubLinesWithReplace(original => original?.Replace(toScrub, "Metadata")); + } + + private static string GetAttribute(MetadataSource source) => + source switch + { + MetadataSource.DisplayAttribute => "System.ComponentModel.DataAnnotations.Display(Name = ", + MetadataSource.DescriptionAttribute => "System.ComponentModel.Description(", + MetadataSource.EnumMemberAttribute => "System.Runtime.Serialization.EnumMember(Value = ", + _ => throw new InvalidOperationException("Unknown source type " + source), + }; + + private static string GetWrongAttribute(MetadataSource source) + => GetAttribute(GetWrongSource(source)); + + private static MetadataSource GetWrongSource(MetadataSource source) + { + return source switch + { + MetadataSource.DisplayAttribute => MetadataSource.EnumMemberAttribute, + MetadataSource.DescriptionAttribute => MetadataSource.DisplayAttribute, + MetadataSource.EnumMemberAttribute => MetadataSource.DescriptionAttribute, + _ => throw new InvalidOperationException("Unknown source type " + source), + }; + } } \ No newline at end of file diff --git a/tests/NetEscapades.EnumGenerators.Tests/EnumInGenericTypeAnalyzerTests.cs b/tests/NetEscapades.EnumGenerators.Tests/EnumInGenericTypeAnalyzerTests.cs index 129d2a0..254ac44 100644 --- a/tests/NetEscapades.EnumGenerators.Tests/EnumInGenericTypeAnalyzerTests.cs +++ b/tests/NetEscapades.EnumGenerators.Tests/EnumInGenericTypeAnalyzerTests.cs @@ -307,5 +307,6 @@ private static string GetTestCode(string testFragment) {{testFragment}} {{TestHelpers.LoadEmbeddedAttribute()}} + {{TestHelpers.LoadEmbeddedMetadataSource()}} """; } \ No newline at end of file diff --git a/tests/NetEscapades.EnumGenerators.Tests/NetEscapades.EnumGenerators.Tests.csproj b/tests/NetEscapades.EnumGenerators.Tests/NetEscapades.EnumGenerators.Tests.csproj index 2019bf0..dc1b55a 100644 --- a/tests/NetEscapades.EnumGenerators.Tests/NetEscapades.EnumGenerators.Tests.csproj +++ b/tests/NetEscapades.EnumGenerators.Tests/NetEscapades.EnumGenerators.Tests.csproj @@ -37,6 +37,9 @@ EnumExtensionsAttribute.cs + + MetadataSource.cs + diff --git a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateEnumExtensionsForFlagsEnum_Params.verified.txt b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateEnumExtensionsForFlagsEnum_Params.verified.txt index 7023687..fddba5f 100644 --- a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateEnumExtensionsForFlagsEnum_Params.verified.txt +++ b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateEnumExtensionsForFlagsEnum_Params.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the NetEscapades.EnumGenerators source generator // @@ -25,20 +25,6 @@ namespace MyTestNameSpace /// public const int Length = 3; - /// - /// Returns the string representation of the value. - /// If the attribute is decorated with a [Display] or [Description]attribute, then - /// uses the provided value. Otherwise uses the name of the member, equivalent to - /// calling ToString() on . - /// - /// The value to retrieve the string value for - /// If uses the value provided in the - /// [Display] or [Description]attribute as the string representation of the member. - /// If , always uses the name of the member, the same as if ToString() was called. - /// The string representation of the value - public static string ToStringFast(this global::MyTestNameSpace.MyEnum value, bool useMetadataAttributes) - => useMetadataAttributes ? value.ToStringFastWithMetadata() : value.ToStringFast(); - /// /// Returns the string representation of the value. /// Directly equivalent to calling ToString() on . @@ -54,6 +40,20 @@ namespace MyTestNameSpace _ => value.ToString(), }; + /// + /// Returns the string representation of the value. + /// If the member is decorated with the attribute + /// then that value is returned. Otherwise returns uses the name of the member, + /// equivalent to calling ToString() on . + /// + /// The value to retrieve the string value for + /// If uses the value provided in the + /// attribute as the string representation of the member. + /// If , always uses the name of the member, the same as if ToString() was called. + /// The string representation of the value + public static string ToStringFast(this global::MyTestNameSpace.MyEnum value, bool useMetadataAttributes) + => useMetadataAttributes ? value.ToStringFastWithMetadata() : value.ToStringFast(); + private static string ToStringFastWithMetadata(this global::MyTestNameSpace.MyEnum value) => value.ToStringFast(); @@ -98,27 +98,28 @@ namespace MyTestNameSpace /// /// The name to check if it's defined /// if a member with the name exists in the enumeration, otherwise - public static bool IsDefined(string name) => IsDefined(name, allowMatchingMetadataAttribute: false); + public static bool IsDefined(string name) + => name switch + { + nameof(global::MyTestNameSpace.MyEnum.First) => true, + nameof(global::MyTestNameSpace.MyEnum.Second) => true, + nameof(global::MyTestNameSpace.MyEnum.Third) => true, + _ => false, + }; /// /// Returns a boolean telling whether an enum with the given name exists in the enumeration, - /// or if a member decorated with a [Display] attribute + /// or if a member decorated with /// with the required name exists. /// /// The name to check if it's defined - /// If , considers the value of metadata attributes,otherwise ignores them + /// If , + /// considers the value of + /// instead of the member name, otherwise ignores them /// if a member with the name exists in the enumeration, or a member is decorated /// with a [Display] attribute with the name, otherwise public static bool IsDefined(string name, bool allowMatchingMetadataAttribute) - { - return name switch - { - nameof(global::MyTestNameSpace.MyEnum.First) => true, - nameof(global::MyTestNameSpace.MyEnum.Second) => true, - nameof(global::MyTestNameSpace.MyEnum.Third) => true, - _ => false, - }; - } + => IsDefined(name); #if NETCOREAPP && !NETCOREAPP2_0 && !NETCOREAPP1_1 && !NETCOREAPP1_0 /// @@ -126,28 +127,26 @@ namespace MyTestNameSpace /// /// The name to check if it's defined /// if a member with the name exists in the enumeration, otherwise - public static bool IsDefined(in global::System.ReadOnlySpan name) => IsDefined(name, allowMatchingMetadataAttribute: false); + public static bool IsDefined(in global::System.ReadOnlySpan name) + => name switch + { + global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.First), global::System.StringComparison.Ordinal) => true, + global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.Second), global::System.StringComparison.Ordinal) => true, + global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.Third), global::System.StringComparison.Ordinal) => true, + _ => false, + }; /// /// Returns a boolean telling whether an enum with the given name exists in the enumeration, - /// or optionally if a member decorated with a [Display] attribute - /// with the required name exists. - /// Slower then the overload, but doesn't allocate memory./> + /// or optionally if a member decorated with + /// exists. Slower then the overload, but doesn't allocate memory./> /// /// The name to check if it's defined /// If , considers the value of metadata attributes,otherwise ignores them /// if a member with the name exists in the enumeration, or a member is decorated /// with a [Display] attribute with the name, otherwise public static bool IsDefined(in global::System.ReadOnlySpan name, bool allowMatchingMetadataAttribute) - { - return name switch - { - global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.First), global::System.StringComparison.Ordinal) => true, - global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.Second), global::System.StringComparison.Ordinal) => true, - global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.Third), global::System.StringComparison.Ordinal) => true, - _ => false, - }; - } + => IsDefined(name); #endif /// @@ -259,8 +258,9 @@ namespace MyTestNameSpace /// If the parse operation fails, contains the default value of the underlying type /// of . This parameter is passed uninitialized. /// to read value in case insensitive mode; to read value in case sensitive mode. - /// If , considers the value included in metadata attributes such as - /// [Display] attribute when parsing, otherwise only considers the member names. + /// If , + /// considers the value included in attribute + /// when parsing, otherwise only considers the member names. /// if the value parameter was converted successfully; otherwise, . public static bool TryParse( #if NETCOREAPP3_0_OR_GREATER @@ -282,7 +282,6 @@ namespace MyTestNameSpace out global::MyTestNameSpace.MyEnum value, bool allowMatchingMetadataAttribute) { - switch (name) { case string s when s.Equals(nameof(global::MyTestNameSpace.MyEnum.First), global::System.StringComparison.OrdinalIgnoreCase): @@ -311,7 +310,6 @@ namespace MyTestNameSpace out global::MyTestNameSpace.MyEnum value, bool allowMatchingMetadataAttribute) { - switch (name) { case nameof(global::MyTestNameSpace.MyEnum.First): @@ -369,8 +367,8 @@ namespace MyTestNameSpace /// /// The case-sensitive string representation of the enumeration name or underlying value to convert /// to read value in case insensitive mode; to read value in case sensitive mode. - /// If , considers the value included in metadata attributes such as - /// [Display] attribute when parsing, otherwise only considers the member names. + /// If , considers the value included in + /// attribute when parsing, otherwise only considers the member names. /// An object of type whose /// value is represented by public static global::MyTestNameSpace.MyEnum Parse( @@ -436,8 +434,8 @@ namespace MyTestNameSpace /// If the parse operation fails, contains the default value of the underlying type /// of . This parameter is passed uninitialized. /// to read value in case insensitive mode; to read value in case sensitive mode. - /// If , considers the value included in metadata attributes such as - /// [Display] attribute when parsing, otherwise only considers the member names. + /// If , considers the value included in + /// attribute when parsing, otherwise only considers the member names. /// if the value parameter was converted successfully; otherwise, . public static bool TryParse( #if NETCOREAPP3_0_OR_GREATER diff --git a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateEnumExtensionsInChildNamespace.verified.txt b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateEnumExtensionsInChildNamespace.verified.txt index 26db5d5..1aac3ed 100644 --- a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateEnumExtensionsInChildNamespace.verified.txt +++ b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateEnumExtensionsInChildNamespace.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the NetEscapades.EnumGenerators source generator // @@ -25,20 +25,6 @@ namespace MyTestNameSpace /// public const int Length = 2; - /// - /// Returns the string representation of the value. - /// If the attribute is decorated with a [Display] or [Description]attribute, then - /// uses the provided value. Otherwise uses the name of the member, equivalent to - /// calling ToString() on . - /// - /// The value to retrieve the string value for - /// If uses the value provided in the - /// [Display] or [Description]attribute as the string representation of the member. - /// If , always uses the name of the member, the same as if ToString() was called. - /// The string representation of the value - public static string ToStringFast(this global::MyTestNameSpace.MyEnum value, bool useMetadataAttributes) - => useMetadataAttributes ? value.ToStringFastWithMetadata() : value.ToStringFast(); - /// /// Returns the string representation of the value. /// Directly equivalent to calling ToString() on . @@ -53,6 +39,20 @@ namespace MyTestNameSpace _ => value.AsUnderlyingType().ToString(), }; + /// + /// Returns the string representation of the value. + /// If the member is decorated with the attribute + /// then that value is returned. Otherwise returns uses the name of the member, + /// equivalent to calling ToString() on . + /// + /// The value to retrieve the string value for + /// If uses the value provided in the + /// attribute as the string representation of the member. + /// If , always uses the name of the member, the same as if ToString() was called. + /// The string representation of the value + public static string ToStringFast(this global::MyTestNameSpace.MyEnum value, bool useMetadataAttributes) + => useMetadataAttributes ? value.ToStringFastWithMetadata() : value.ToStringFast(); + private static string ToStringFastWithMetadata(this global::MyTestNameSpace.MyEnum value) => value.ToStringFast(); @@ -84,26 +84,27 @@ namespace MyTestNameSpace /// /// The name to check if it's defined /// if a member with the name exists in the enumeration, otherwise - public static bool IsDefined(string name) => IsDefined(name, allowMatchingMetadataAttribute: false); + public static bool IsDefined(string name) + => name switch + { + nameof(global::MyTestNameSpace.MyEnum.First) => true, + nameof(global::MyTestNameSpace.MyEnum.Second) => true, + _ => false, + }; /// /// Returns a boolean telling whether an enum with the given name exists in the enumeration, - /// or if a member decorated with a [Display] attribute + /// or if a member decorated with /// with the required name exists. /// /// The name to check if it's defined - /// If , considers the value of metadata attributes,otherwise ignores them + /// If , + /// considers the value of + /// instead of the member name, otherwise ignores them /// if a member with the name exists in the enumeration, or a member is decorated /// with a [Display] attribute with the name, otherwise public static bool IsDefined(string name, bool allowMatchingMetadataAttribute) - { - return name switch - { - nameof(global::MyTestNameSpace.MyEnum.First) => true, - nameof(global::MyTestNameSpace.MyEnum.Second) => true, - _ => false, - }; - } + => IsDefined(name); #if NETCOREAPP && !NETCOREAPP2_0 && !NETCOREAPP1_1 && !NETCOREAPP1_0 /// @@ -111,27 +112,25 @@ namespace MyTestNameSpace /// /// The name to check if it's defined /// if a member with the name exists in the enumeration, otherwise - public static bool IsDefined(in global::System.ReadOnlySpan name) => IsDefined(name, allowMatchingMetadataAttribute: false); + public static bool IsDefined(in global::System.ReadOnlySpan name) + => name switch + { + global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.First), global::System.StringComparison.Ordinal) => true, + global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.Second), global::System.StringComparison.Ordinal) => true, + _ => false, + }; /// /// Returns a boolean telling whether an enum with the given name exists in the enumeration, - /// or optionally if a member decorated with a [Display] attribute - /// with the required name exists. - /// Slower then the overload, but doesn't allocate memory./> + /// or optionally if a member decorated with + /// exists. Slower then the overload, but doesn't allocate memory./> /// /// The name to check if it's defined /// If , considers the value of metadata attributes,otherwise ignores them /// if a member with the name exists in the enumeration, or a member is decorated /// with a [Display] attribute with the name, otherwise public static bool IsDefined(in global::System.ReadOnlySpan name, bool allowMatchingMetadataAttribute) - { - return name switch - { - global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.First), global::System.StringComparison.Ordinal) => true, - global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.Second), global::System.StringComparison.Ordinal) => true, - _ => false, - }; - } + => IsDefined(name); #endif /// @@ -243,8 +242,9 @@ namespace MyTestNameSpace /// If the parse operation fails, contains the default value of the underlying type /// of . This parameter is passed uninitialized. /// to read value in case insensitive mode; to read value in case sensitive mode. - /// If , considers the value included in metadata attributes such as - /// [Display] attribute when parsing, otherwise only considers the member names. + /// If , + /// considers the value included in attribute + /// when parsing, otherwise only considers the member names. /// if the value parameter was converted successfully; otherwise, . public static bool TryParse( #if NETCOREAPP3_0_OR_GREATER @@ -266,7 +266,6 @@ namespace MyTestNameSpace out global::MyTestNameSpace.MyEnum value, bool allowMatchingMetadataAttribute) { - switch (name) { case string s when s.Equals(nameof(global::MyTestNameSpace.MyEnum.First), global::System.StringComparison.OrdinalIgnoreCase): @@ -292,7 +291,6 @@ namespace MyTestNameSpace out global::MyTestNameSpace.MyEnum value, bool allowMatchingMetadataAttribute) { - switch (name) { case nameof(global::MyTestNameSpace.MyEnum.First): @@ -347,8 +345,8 @@ namespace MyTestNameSpace /// /// The case-sensitive string representation of the enumeration name or underlying value to convert /// to read value in case insensitive mode; to read value in case sensitive mode. - /// If , considers the value included in metadata attributes such as - /// [Display] attribute when parsing, otherwise only considers the member names. + /// If , considers the value included in + /// attribute when parsing, otherwise only considers the member names. /// An object of type whose /// value is represented by public static global::MyTestNameSpace.MyEnum Parse( @@ -414,8 +412,8 @@ namespace MyTestNameSpace /// If the parse operation fails, contains the default value of the underlying type /// of . This parameter is passed uninitialized. /// to read value in case insensitive mode; to read value in case sensitive mode. - /// If , considers the value included in metadata attributes such as - /// [Display] attribute when parsing, otherwise only considers the member names. + /// If , considers the value included in + /// attribute when parsing, otherwise only considers the member names. /// if the value parameter was converted successfully; otherwise, . public static bool TryParse( #if NETCOREAPP3_0_OR_GREATER diff --git a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateEnumExtensionsInGlobalNamespace.verified.txt b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateEnumExtensionsInGlobalNamespace.verified.txt index af8f252..e9c8e6e 100644 --- a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateEnumExtensionsInGlobalNamespace.verified.txt +++ b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateEnumExtensionsInGlobalNamespace.verified.txt @@ -23,20 +23,6 @@ /// public const int Length = 2; - /// - /// Returns the string representation of the value. - /// If the attribute is decorated with a [Display] or [Description]attribute, then - /// uses the provided value. Otherwise uses the name of the member, equivalent to - /// calling ToString() on . - /// - /// The value to retrieve the string value for - /// If uses the value provided in the - /// [Display] or [Description]attribute as the string representation of the member. - /// If , always uses the name of the member, the same as if ToString() was called. - /// The string representation of the value - public static string ToStringFast(this global::MyEnum value, bool useMetadataAttributes) - => useMetadataAttributes ? value.ToStringFastWithMetadata() : value.ToStringFast(); - /// /// Returns the string representation of the value. /// Directly equivalent to calling ToString() on . @@ -51,6 +37,20 @@ _ => value.AsUnderlyingType().ToString(), }; + /// + /// Returns the string representation of the value. + /// If the member is decorated with the attribute + /// then that value is returned. Otherwise returns uses the name of the member, + /// equivalent to calling ToString() on . + /// + /// The value to retrieve the string value for + /// If uses the value provided in the + /// attribute as the string representation of the member. + /// If , always uses the name of the member, the same as if ToString() was called. + /// The string representation of the value + public static string ToStringFast(this global::MyEnum value, bool useMetadataAttributes) + => useMetadataAttributes ? value.ToStringFastWithMetadata() : value.ToStringFast(); + private static string ToStringFastWithMetadata(this global::MyEnum value) => value.ToStringFast(); @@ -82,26 +82,27 @@ /// /// The name to check if it's defined /// if a member with the name exists in the enumeration, otherwise - public static bool IsDefined(string name) => IsDefined(name, allowMatchingMetadataAttribute: false); + public static bool IsDefined(string name) + => name switch + { + nameof(global::MyEnum.First) => true, + nameof(global::MyEnum.Second) => true, + _ => false, + }; /// /// Returns a boolean telling whether an enum with the given name exists in the enumeration, - /// or if a member decorated with a [Display] attribute + /// or if a member decorated with /// with the required name exists. /// /// The name to check if it's defined - /// If , considers the value of metadata attributes,otherwise ignores them + /// If , + /// considers the value of + /// instead of the member name, otherwise ignores them /// if a member with the name exists in the enumeration, or a member is decorated /// with a [Display] attribute with the name, otherwise public static bool IsDefined(string name, bool allowMatchingMetadataAttribute) - { - return name switch - { - nameof(global::MyEnum.First) => true, - nameof(global::MyEnum.Second) => true, - _ => false, - }; - } + => IsDefined(name); #if NETCOREAPP && !NETCOREAPP2_0 && !NETCOREAPP1_1 && !NETCOREAPP1_0 /// @@ -109,27 +110,25 @@ /// /// The name to check if it's defined /// if a member with the name exists in the enumeration, otherwise - public static bool IsDefined(in global::System.ReadOnlySpan name) => IsDefined(name, allowMatchingMetadataAttribute: false); + public static bool IsDefined(in global::System.ReadOnlySpan name) + => name switch + { + global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyEnum.First), global::System.StringComparison.Ordinal) => true, + global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyEnum.Second), global::System.StringComparison.Ordinal) => true, + _ => false, + }; /// /// Returns a boolean telling whether an enum with the given name exists in the enumeration, - /// or optionally if a member decorated with a [Display] attribute - /// with the required name exists. - /// Slower then the overload, but doesn't allocate memory./> + /// or optionally if a member decorated with + /// exists. Slower then the overload, but doesn't allocate memory./> /// /// The name to check if it's defined /// If , considers the value of metadata attributes,otherwise ignores them /// if a member with the name exists in the enumeration, or a member is decorated /// with a [Display] attribute with the name, otherwise public static bool IsDefined(in global::System.ReadOnlySpan name, bool allowMatchingMetadataAttribute) - { - return name switch - { - global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyEnum.First), global::System.StringComparison.Ordinal) => true, - global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyEnum.Second), global::System.StringComparison.Ordinal) => true, - _ => false, - }; - } + => IsDefined(name); #endif /// @@ -241,8 +240,9 @@ /// If the parse operation fails, contains the default value of the underlying type /// of . This parameter is passed uninitialized. /// to read value in case insensitive mode; to read value in case sensitive mode. - /// If , considers the value included in metadata attributes such as - /// [Display] attribute when parsing, otherwise only considers the member names. + /// If , + /// considers the value included in attribute + /// when parsing, otherwise only considers the member names. /// if the value parameter was converted successfully; otherwise, . public static bool TryParse( #if NETCOREAPP3_0_OR_GREATER @@ -264,7 +264,6 @@ out global::MyEnum value, bool allowMatchingMetadataAttribute) { - switch (name) { case string s when s.Equals(nameof(global::MyEnum.First), global::System.StringComparison.OrdinalIgnoreCase): @@ -290,7 +289,6 @@ out global::MyEnum value, bool allowMatchingMetadataAttribute) { - switch (name) { case nameof(global::MyEnum.First): @@ -345,8 +343,8 @@ /// /// The case-sensitive string representation of the enumeration name or underlying value to convert /// to read value in case insensitive mode; to read value in case sensitive mode. - /// If , considers the value included in metadata attributes such as - /// [Display] attribute when parsing, otherwise only considers the member names. + /// If , considers the value included in + /// attribute when parsing, otherwise only considers the member names. /// An object of type whose /// value is represented by public static global::MyEnum Parse( @@ -412,8 +410,8 @@ /// If the parse operation fails, contains the default value of the underlying type /// of . This parameter is passed uninitialized. /// to read value in case insensitive mode; to read value in case sensitive mode. - /// If , considers the value included in metadata attributes such as - /// [Display] attribute when parsing, otherwise only considers the member names. + /// If , considers the value included in + /// attribute when parsing, otherwise only considers the member names. /// if the value parameter was converted successfully; otherwise, . public static bool TryParse( #if NETCOREAPP3_0_OR_GREATER diff --git a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateEnumExtensionsInGlobalNamespace_CSharp14.verified.txt b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateEnumExtensionsInGlobalNamespace_CSharp14.verified.txt index 726928f..92e6472 100644 --- a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateEnumExtensionsInGlobalNamespace_CSharp14.verified.txt +++ b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateEnumExtensionsInGlobalNamespace_CSharp14.verified.txt @@ -23,20 +23,6 @@ /// public const int Length = 2; - /// - /// Returns the string representation of the value. - /// If the attribute is decorated with a [Display] or [Description]attribute, then - /// uses the provided value. Otherwise uses the name of the member, equivalent to - /// calling ToString() on . - /// - /// The value to retrieve the string value for - /// If uses the value provided in the - /// [Display] or [Description]attribute as the string representation of the member. - /// If , always uses the name of the member, the same as if ToString() was called. - /// The string representation of the value - public static string ToStringFast(this global::MyEnum value, bool useMetadataAttributes) - => useMetadataAttributes ? value.ToStringFastWithMetadata() : value.ToStringFast(); - /// /// Returns the string representation of the value. /// Directly equivalent to calling ToString() on . @@ -51,6 +37,20 @@ _ => value.AsUnderlyingType().ToString(), }; + /// + /// Returns the string representation of the value. + /// If the member is decorated with the attribute + /// then that value is returned. Otherwise returns uses the name of the member, + /// equivalent to calling ToString() on . + /// + /// The value to retrieve the string value for + /// If uses the value provided in the + /// attribute as the string representation of the member. + /// If , always uses the name of the member, the same as if ToString() was called. + /// The string representation of the value + public static string ToStringFast(this global::MyEnum value, bool useMetadataAttributes) + => useMetadataAttributes ? value.ToStringFastWithMetadata() : value.ToStringFast(); + private static string ToStringFastWithMetadata(this global::MyEnum value) => value.ToStringFast(); @@ -86,26 +86,27 @@ /// /// The name to check if it's defined /// if a member with the name exists in the enumeration, otherwise - public static bool IsDefined(string name) => IsDefined(name, allowMatchingMetadataAttribute: false); + public static bool IsDefined(string name) + => name switch + { + nameof(global::MyEnum.First) => true, + nameof(global::MyEnum.Second) => true, + _ => false, + }; /// /// Returns a boolean telling whether an enum with the given name exists in the enumeration, - /// or if a member decorated with a [Display] attribute + /// or if a member decorated with /// with the required name exists. /// /// The name to check if it's defined - /// If , considers the value of metadata attributes,otherwise ignores them + /// If , + /// considers the value of + /// instead of the member name, otherwise ignores them /// if a member with the name exists in the enumeration, or a member is decorated /// with a [Display] attribute with the name, otherwise public static bool IsDefined(string name, bool allowMatchingMetadataAttribute) - { - return name switch - { - nameof(global::MyEnum.First) => true, - nameof(global::MyEnum.Second) => true, - _ => false, - }; - } + => IsDefined(name); #if NETCOREAPP && !NETCOREAPP2_0 && !NETCOREAPP1_1 && !NETCOREAPP1_0 /// @@ -113,27 +114,25 @@ /// /// The name to check if it's defined /// if a member with the name exists in the enumeration, otherwise - public static bool IsDefined(in global::System.ReadOnlySpan name) => IsDefined(name, allowMatchingMetadataAttribute: false); + public static bool IsDefined(in global::System.ReadOnlySpan name) + => name switch + { + global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyEnum.First), global::System.StringComparison.Ordinal) => true, + global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyEnum.Second), global::System.StringComparison.Ordinal) => true, + _ => false, + }; /// /// Returns a boolean telling whether an enum with the given name exists in the enumeration, - /// or optionally if a member decorated with a [Display] attribute - /// with the required name exists. - /// Slower then the overload, but doesn't allocate memory./> + /// or optionally if a member decorated with + /// exists. Slower then the overload, but doesn't allocate memory./> /// /// The name to check if it's defined /// If , considers the value of metadata attributes,otherwise ignores them /// if a member with the name exists in the enumeration, or a member is decorated /// with a [Display] attribute with the name, otherwise public static bool IsDefined(in global::System.ReadOnlySpan name, bool allowMatchingMetadataAttribute) - { - return name switch - { - global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyEnum.First), global::System.StringComparison.Ordinal) => true, - global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyEnum.Second), global::System.StringComparison.Ordinal) => true, - _ => false, - }; - } + => IsDefined(name); #endif /// @@ -245,8 +244,9 @@ /// If the parse operation fails, contains the default value of the underlying type /// of . This parameter is passed uninitialized. /// to read value in case insensitive mode; to read value in case sensitive mode. - /// If , considers the value included in metadata attributes such as - /// [Display] attribute when parsing, otherwise only considers the member names. + /// If , + /// considers the value included in attribute + /// when parsing, otherwise only considers the member names. /// if the value parameter was converted successfully; otherwise, . public static bool TryParse( #if NETCOREAPP3_0_OR_GREATER @@ -268,7 +268,6 @@ out global::MyEnum value, bool allowMatchingMetadataAttribute) { - switch (name) { case string s when s.Equals(nameof(global::MyEnum.First), global::System.StringComparison.OrdinalIgnoreCase): @@ -294,7 +293,6 @@ out global::MyEnum value, bool allowMatchingMetadataAttribute) { - switch (name) { case nameof(global::MyEnum.First): @@ -349,8 +347,8 @@ /// /// The case-sensitive string representation of the enumeration name or underlying value to convert /// to read value in case insensitive mode; to read value in case sensitive mode. - /// If , considers the value included in metadata attributes such as - /// [Display] attribute when parsing, otherwise only considers the member names. + /// If , considers the value included in + /// attribute when parsing, otherwise only considers the member names. /// An object of type whose /// value is represented by public static global::MyEnum Parse( @@ -416,8 +414,8 @@ /// If the parse operation fails, contains the default value of the underlying type /// of . This parameter is passed uninitialized. /// to read value in case insensitive mode; to read value in case sensitive mode. - /// If , considers the value included in metadata attributes such as - /// [Display] attribute when parsing, otherwise only considers the member names. + /// If , considers the value included in + /// attribute when parsing, otherwise only considers the member names. /// if the value parameter was converted successfully; otherwise, . public static bool TryParse( #if NETCOREAPP3_0_OR_GREATER diff --git a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateEnumExtensionsInNestedClass.verified.txt b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateEnumExtensionsInNestedClass.verified.txt index a7fd3b4..defcc91 100644 --- a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateEnumExtensionsInNestedClass.verified.txt +++ b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateEnumExtensionsInNestedClass.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the NetEscapades.EnumGenerators source generator // @@ -25,20 +25,6 @@ namespace MyTestNameSpace /// public const int Length = 2; - /// - /// Returns the string representation of the value. - /// If the attribute is decorated with a [Display] or [Description]attribute, then - /// uses the provided value. Otherwise uses the name of the member, equivalent to - /// calling ToString() on . - /// - /// The value to retrieve the string value for - /// If uses the value provided in the - /// [Display] or [Description]attribute as the string representation of the member. - /// If , always uses the name of the member, the same as if ToString() was called. - /// The string representation of the value - public static string ToStringFast(this global::MyTestNameSpace.InnerClass.MyEnum value, bool useMetadataAttributes) - => useMetadataAttributes ? value.ToStringFastWithMetadata() : value.ToStringFast(); - /// /// Returns the string representation of the value. /// Directly equivalent to calling ToString() on . @@ -53,6 +39,20 @@ namespace MyTestNameSpace _ => value.AsUnderlyingType().ToString(), }; + /// + /// Returns the string representation of the value. + /// If the member is decorated with the attribute + /// then that value is returned. Otherwise returns uses the name of the member, + /// equivalent to calling ToString() on . + /// + /// The value to retrieve the string value for + /// If uses the value provided in the + /// attribute as the string representation of the member. + /// If , always uses the name of the member, the same as if ToString() was called. + /// The string representation of the value + public static string ToStringFast(this global::MyTestNameSpace.InnerClass.MyEnum value, bool useMetadataAttributes) + => useMetadataAttributes ? value.ToStringFastWithMetadata() : value.ToStringFast(); + private static string ToStringFastWithMetadata(this global::MyTestNameSpace.InnerClass.MyEnum value) => value.ToStringFast(); @@ -84,26 +84,27 @@ namespace MyTestNameSpace /// /// The name to check if it's defined /// if a member with the name exists in the enumeration, otherwise - public static bool IsDefined(string name) => IsDefined(name, allowMatchingMetadataAttribute: false); + public static bool IsDefined(string name) + => name switch + { + nameof(global::MyTestNameSpace.InnerClass.MyEnum.First) => true, + nameof(global::MyTestNameSpace.InnerClass.MyEnum.Second) => true, + _ => false, + }; /// /// Returns a boolean telling whether an enum with the given name exists in the enumeration, - /// or if a member decorated with a [Display] attribute + /// or if a member decorated with /// with the required name exists. /// /// The name to check if it's defined - /// If , considers the value of metadata attributes,otherwise ignores them + /// If , + /// considers the value of + /// instead of the member name, otherwise ignores them /// if a member with the name exists in the enumeration, or a member is decorated /// with a [Display] attribute with the name, otherwise public static bool IsDefined(string name, bool allowMatchingMetadataAttribute) - { - return name switch - { - nameof(global::MyTestNameSpace.InnerClass.MyEnum.First) => true, - nameof(global::MyTestNameSpace.InnerClass.MyEnum.Second) => true, - _ => false, - }; - } + => IsDefined(name); #if NETCOREAPP && !NETCOREAPP2_0 && !NETCOREAPP1_1 && !NETCOREAPP1_0 /// @@ -111,27 +112,25 @@ namespace MyTestNameSpace /// /// The name to check if it's defined /// if a member with the name exists in the enumeration, otherwise - public static bool IsDefined(in global::System.ReadOnlySpan name) => IsDefined(name, allowMatchingMetadataAttribute: false); + public static bool IsDefined(in global::System.ReadOnlySpan name) + => name switch + { + global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.InnerClass.MyEnum.First), global::System.StringComparison.Ordinal) => true, + global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.InnerClass.MyEnum.Second), global::System.StringComparison.Ordinal) => true, + _ => false, + }; /// /// Returns a boolean telling whether an enum with the given name exists in the enumeration, - /// or optionally if a member decorated with a [Display] attribute - /// with the required name exists. - /// Slower then the overload, but doesn't allocate memory./> + /// or optionally if a member decorated with + /// exists. Slower then the overload, but doesn't allocate memory./> /// /// The name to check if it's defined /// If , considers the value of metadata attributes,otherwise ignores them /// if a member with the name exists in the enumeration, or a member is decorated /// with a [Display] attribute with the name, otherwise public static bool IsDefined(in global::System.ReadOnlySpan name, bool allowMatchingMetadataAttribute) - { - return name switch - { - global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.InnerClass.MyEnum.First), global::System.StringComparison.Ordinal) => true, - global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.InnerClass.MyEnum.Second), global::System.StringComparison.Ordinal) => true, - _ => false, - }; - } + => IsDefined(name); #endif /// @@ -243,8 +242,9 @@ namespace MyTestNameSpace /// If the parse operation fails, contains the default value of the underlying type /// of . This parameter is passed uninitialized. /// to read value in case insensitive mode; to read value in case sensitive mode. - /// If , considers the value included in metadata attributes such as - /// [Display] attribute when parsing, otherwise only considers the member names. + /// If , + /// considers the value included in attribute + /// when parsing, otherwise only considers the member names. /// if the value parameter was converted successfully; otherwise, . public static bool TryParse( #if NETCOREAPP3_0_OR_GREATER @@ -266,7 +266,6 @@ namespace MyTestNameSpace out global::MyTestNameSpace.InnerClass.MyEnum value, bool allowMatchingMetadataAttribute) { - switch (name) { case string s when s.Equals(nameof(global::MyTestNameSpace.InnerClass.MyEnum.First), global::System.StringComparison.OrdinalIgnoreCase): @@ -292,7 +291,6 @@ namespace MyTestNameSpace out global::MyTestNameSpace.InnerClass.MyEnum value, bool allowMatchingMetadataAttribute) { - switch (name) { case nameof(global::MyTestNameSpace.InnerClass.MyEnum.First): @@ -347,8 +345,8 @@ namespace MyTestNameSpace /// /// The case-sensitive string representation of the enumeration name or underlying value to convert /// to read value in case insensitive mode; to read value in case sensitive mode. - /// If , considers the value included in metadata attributes such as - /// [Display] attribute when parsing, otherwise only considers the member names. + /// If , considers the value included in + /// attribute when parsing, otherwise only considers the member names. /// An object of type whose /// value is represented by public static global::MyTestNameSpace.InnerClass.MyEnum Parse( @@ -414,8 +412,8 @@ namespace MyTestNameSpace /// If the parse operation fails, contains the default value of the underlying type /// of . This parameter is passed uninitialized. /// to read value in case insensitive mode; to read value in case sensitive mode. - /// If , considers the value included in metadata attributes such as - /// [Display] attribute when parsing, otherwise only considers the member names. + /// If , considers the value included in + /// attribute when parsing, otherwise only considers the member names. /// if the value parameter was converted successfully; otherwise, . public static bool TryParse( #if NETCOREAPP3_0_OR_GREATER diff --git a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateEnumExtensionsWithCustomName.verified.txt b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateEnumExtensionsWithCustomName.verified.txt index 5dab336..20a3553 100644 --- a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateEnumExtensionsWithCustomName.verified.txt +++ b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateEnumExtensionsWithCustomName.verified.txt @@ -25,20 +25,6 @@ namespace MyTestNameSpace /// public const int Length = 2; - /// - /// Returns the string representation of the value. - /// If the attribute is decorated with a [Display] or [Description]attribute, then - /// uses the provided value. Otherwise uses the name of the member, equivalent to - /// calling ToString() on . - /// - /// The value to retrieve the string value for - /// If uses the value provided in the - /// [Display] or [Description]attribute as the string representation of the member. - /// If , always uses the name of the member, the same as if ToString() was called. - /// The string representation of the value - public static string ToStringFast(this global::MyTestNameSpace.MyEnum value, bool useMetadataAttributes) - => useMetadataAttributes ? value.ToStringFastWithMetadata() : value.ToStringFast(); - /// /// Returns the string representation of the value. /// Directly equivalent to calling ToString() on . @@ -53,6 +39,20 @@ namespace MyTestNameSpace _ => value.AsUnderlyingType().ToString(), }; + /// + /// Returns the string representation of the value. + /// If the member is decorated with the attribute + /// then that value is returned. Otherwise returns uses the name of the member, + /// equivalent to calling ToString() on . + /// + /// The value to retrieve the string value for + /// If uses the value provided in the + /// attribute as the string representation of the member. + /// If , always uses the name of the member, the same as if ToString() was called. + /// The string representation of the value + public static string ToStringFast(this global::MyTestNameSpace.MyEnum value, bool useMetadataAttributes) + => useMetadataAttributes ? value.ToStringFastWithMetadata() : value.ToStringFast(); + private static string ToStringFastWithMetadata(this global::MyTestNameSpace.MyEnum value) => value.ToStringFast(); @@ -84,26 +84,27 @@ namespace MyTestNameSpace /// /// The name to check if it's defined /// if a member with the name exists in the enumeration, otherwise - public static bool IsDefined(string name) => IsDefined(name, allowMatchingMetadataAttribute: false); + public static bool IsDefined(string name) + => name switch + { + nameof(global::MyTestNameSpace.MyEnum.First) => true, + nameof(global::MyTestNameSpace.MyEnum.Second) => true, + _ => false, + }; /// /// Returns a boolean telling whether an enum with the given name exists in the enumeration, - /// or if a member decorated with a [Display] attribute + /// or if a member decorated with /// with the required name exists. /// /// The name to check if it's defined - /// If , considers the value of metadata attributes,otherwise ignores them + /// If , + /// considers the value of + /// instead of the member name, otherwise ignores them /// if a member with the name exists in the enumeration, or a member is decorated /// with a [Display] attribute with the name, otherwise public static bool IsDefined(string name, bool allowMatchingMetadataAttribute) - { - return name switch - { - nameof(global::MyTestNameSpace.MyEnum.First) => true, - nameof(global::MyTestNameSpace.MyEnum.Second) => true, - _ => false, - }; - } + => IsDefined(name); #if NETCOREAPP && !NETCOREAPP2_0 && !NETCOREAPP1_1 && !NETCOREAPP1_0 /// @@ -111,27 +112,25 @@ namespace MyTestNameSpace /// /// The name to check if it's defined /// if a member with the name exists in the enumeration, otherwise - public static bool IsDefined(in global::System.ReadOnlySpan name) => IsDefined(name, allowMatchingMetadataAttribute: false); + public static bool IsDefined(in global::System.ReadOnlySpan name) + => name switch + { + global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.First), global::System.StringComparison.Ordinal) => true, + global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.Second), global::System.StringComparison.Ordinal) => true, + _ => false, + }; /// /// Returns a boolean telling whether an enum with the given name exists in the enumeration, - /// or optionally if a member decorated with a [Display] attribute - /// with the required name exists. - /// Slower then the overload, but doesn't allocate memory./> + /// or optionally if a member decorated with + /// exists. Slower then the overload, but doesn't allocate memory./> /// /// The name to check if it's defined /// If , considers the value of metadata attributes,otherwise ignores them /// if a member with the name exists in the enumeration, or a member is decorated /// with a [Display] attribute with the name, otherwise public static bool IsDefined(in global::System.ReadOnlySpan name, bool allowMatchingMetadataAttribute) - { - return name switch - { - global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.First), global::System.StringComparison.Ordinal) => true, - global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.Second), global::System.StringComparison.Ordinal) => true, - _ => false, - }; - } + => IsDefined(name); #endif /// @@ -243,8 +242,9 @@ namespace MyTestNameSpace /// If the parse operation fails, contains the default value of the underlying type /// of . This parameter is passed uninitialized. /// to read value in case insensitive mode; to read value in case sensitive mode. - /// If , considers the value included in metadata attributes such as - /// [Display] attribute when parsing, otherwise only considers the member names. + /// If , + /// considers the value included in attribute + /// when parsing, otherwise only considers the member names. /// if the value parameter was converted successfully; otherwise, . public static bool TryParse( #if NETCOREAPP3_0_OR_GREATER @@ -266,7 +266,6 @@ namespace MyTestNameSpace out global::MyTestNameSpace.MyEnum value, bool allowMatchingMetadataAttribute) { - switch (name) { case string s when s.Equals(nameof(global::MyTestNameSpace.MyEnum.First), global::System.StringComparison.OrdinalIgnoreCase): @@ -292,7 +291,6 @@ namespace MyTestNameSpace out global::MyTestNameSpace.MyEnum value, bool allowMatchingMetadataAttribute) { - switch (name) { case nameof(global::MyTestNameSpace.MyEnum.First): @@ -347,8 +345,8 @@ namespace MyTestNameSpace /// /// The case-sensitive string representation of the enumeration name or underlying value to convert /// to read value in case insensitive mode; to read value in case sensitive mode. - /// If , considers the value included in metadata attributes such as - /// [Display] attribute when parsing, otherwise only considers the member names. + /// If , considers the value included in + /// attribute when parsing, otherwise only considers the member names. /// An object of type whose /// value is represented by public static global::MyTestNameSpace.MyEnum Parse( @@ -414,8 +412,8 @@ namespace MyTestNameSpace /// If the parse operation fails, contains the default value of the underlying type /// of . This parameter is passed uninitialized. /// to read value in case insensitive mode; to read value in case sensitive mode. - /// If , considers the value included in metadata attributes such as - /// [Display] attribute when parsing, otherwise only considers the member names. + /// If , considers the value included in + /// attribute when parsing, otherwise only considers the member names. /// if the value parameter was converted successfully; otherwise, . public static bool TryParse( #if NETCOREAPP3_0_OR_GREATER diff --git a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateEnumExtensionsWithCustomNames.verified.txt b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateEnumExtensionsWithCustomNames__.verified.txt similarity index 92% rename from tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateEnumExtensionsWithCustomNames.verified.txt rename to tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateEnumExtensionsWithCustomNames__.verified.txt index 79fa5fa..c9e3693 100644 --- a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateEnumExtensionsWithCustomNames.verified.txt +++ b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateEnumExtensionsWithCustomNames__.verified.txt @@ -25,20 +25,6 @@ namespace MyTestNameSpace /// public const int Length = 4; - /// - /// Returns the string representation of the value. - /// If the attribute is decorated with a [Display] or [Description]attribute, then - /// uses the provided value. Otherwise uses the name of the member, equivalent to - /// calling ToString() on . - /// - /// The value to retrieve the string value for - /// If uses the value provided in the - /// [Display] or [Description]attribute as the string representation of the member. - /// If , always uses the name of the member, the same as if ToString() was called. - /// The string representation of the value - public static string ToStringFast(this global::MyTestNameSpace.MyEnum value, bool useMetadataAttributes) - => useMetadataAttributes ? value.ToStringFastWithMetadata() : value.ToStringFast(); - /// /// Returns the string representation of the value. /// Directly equivalent to calling ToString() on . @@ -55,6 +41,20 @@ namespace MyTestNameSpace _ => value.AsUnderlyingType().ToString(), }; + /// + /// Returns the string representation of the value. + /// If the member is decorated with the attribute + /// then that value is returned. Otherwise returns uses the name of the member, + /// equivalent to calling ToString() on . + /// + /// The value to retrieve the string value for + /// If uses the value provided in the + /// attribute as the string representation of the member. + /// If , always uses the name of the member, the same as if ToString() was called. + /// The string representation of the value + public static string ToStringFast(this global::MyTestNameSpace.MyEnum value, bool useMetadataAttributes) + => useMetadataAttributes ? value.ToStringFastWithMetadata() : value.ToStringFast(); + private static string ToStringFastWithMetadata(this global::MyTestNameSpace.MyEnum value) => value switch { @@ -95,44 +95,37 @@ namespace MyTestNameSpace /// /// The name to check if it's defined /// if a member with the name exists in the enumeration, otherwise - public static bool IsDefined(string name) => IsDefined(name, allowMatchingMetadataAttribute: false); + public static bool IsDefined(string name) + => name switch + { + nameof(global::MyTestNameSpace.MyEnum.First) => true, + nameof(global::MyTestNameSpace.MyEnum.Second) => true, + nameof(global::MyTestNameSpace.MyEnum.Third) => true, + nameof(global::MyTestNameSpace.MyEnum.Fourth) => true, + _ => false, + }; /// /// Returns a boolean telling whether an enum with the given name exists in the enumeration, - /// or if a member decorated with a [Display] attribute + /// or if a member decorated with /// with the required name exists. /// /// The name to check if it's defined - /// If , considers the value of metadata attributes,otherwise ignores them + /// If , + /// considers the value of + /// instead of the member name, otherwise ignores them /// if a member with the name exists in the enumeration, or a member is decorated /// with a [Display] attribute with the name, otherwise public static bool IsDefined(string name, bool allowMatchingMetadataAttribute) - { - var isDefinedInDisplayAttribute = false; - if (allowMatchingMetadataAttribute) - { - isDefinedInDisplayAttribute = name switch - { - "2nd" => true, - "4th" => true, - _ => false, - }; - } - - if (isDefinedInDisplayAttribute) - { - return true; - } + => allowMatchingMetadataAttribute ? IsMetadataNameDefined(name) : IsDefined(name); - return name switch + private static bool IsMetadataNameDefined(string name) + => name switch { - nameof(global::MyTestNameSpace.MyEnum.First) => true, - nameof(global::MyTestNameSpace.MyEnum.Second) => true, - nameof(global::MyTestNameSpace.MyEnum.Third) => true, - nameof(global::MyTestNameSpace.MyEnum.Fourth) => true, - _ => false, + "2nd" => true, + "4th" => true, + _ => IsDefined(name), }; - } #if NETCOREAPP && !NETCOREAPP2_0 && !NETCOREAPP1_1 && !NETCOREAPP1_0 /// @@ -140,45 +133,34 @@ namespace MyTestNameSpace /// /// The name to check if it's defined /// if a member with the name exists in the enumeration, otherwise - public static bool IsDefined(in global::System.ReadOnlySpan name) => IsDefined(name, allowMatchingMetadataAttribute: false); + public static bool IsDefined(in global::System.ReadOnlySpan name) + => name switch + { + global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.First), global::System.StringComparison.Ordinal) => true, + global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.Second), global::System.StringComparison.Ordinal) => true, + global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.Third), global::System.StringComparison.Ordinal) => true, + global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.Fourth), global::System.StringComparison.Ordinal) => true, + _ => false, + }; /// /// Returns a boolean telling whether an enum with the given name exists in the enumeration, - /// or optionally if a member decorated with a [Display] attribute - /// with the required name exists. - /// Slower then the overload, but doesn't allocate memory./> + /// or optionally if a member decorated with + /// exists. Slower then the overload, but doesn't allocate memory./> /// /// The name to check if it's defined /// If , considers the value of metadata attributes,otherwise ignores them /// if a member with the name exists in the enumeration, or a member is decorated /// with a [Display] attribute with the name, otherwise - public static bool IsDefined(in global::System.ReadOnlySpan name, bool allowMatchingMetadataAttribute) - { - var isDefinedInDisplayAttribute = false; - if (allowMatchingMetadataAttribute) - { - isDefinedInDisplayAttribute = name switch - { - global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, "2nd", global::System.StringComparison.Ordinal) => true, - global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, "4th", global::System.StringComparison.Ordinal) => true, - _ => false, - }; - } - - if (isDefinedInDisplayAttribute) - { - return true; - } + public static bool IsDefined(in global::System.ReadOnlySpan name, bool allowMatchingMetadataAttribute) => allowMatchingMetadataAttribute ? IsMetadataNameDefined(in name) : IsDefined(in name); - return name switch + private static bool IsMetadataNameDefined(in global::System.ReadOnlySpan name) + => name switch { - global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.First), global::System.StringComparison.Ordinal) => true, - global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.Second), global::System.StringComparison.Ordinal) => true, - global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.Third), global::System.StringComparison.Ordinal) => true, - global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.Fourth), global::System.StringComparison.Ordinal) => true, - _ => false, + global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, "2nd", global::System.StringComparison.Ordinal) => true, + global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, "4th", global::System.StringComparison.Ordinal) => true, + _ => IsDefined(name), }; - } #endif /// @@ -290,8 +272,9 @@ namespace MyTestNameSpace /// If the parse operation fails, contains the default value of the underlying type /// of . This parameter is passed uninitialized. /// to read value in case insensitive mode; to read value in case sensitive mode. - /// If , considers the value included in metadata attributes such as - /// [Display] attribute when parsing, otherwise only considers the member names. + /// If , + /// considers the value included in attribute + /// when parsing, otherwise only considers the member names. /// if the value parameter was converted successfully; otherwise, . public static bool TryParse( #if NETCOREAPP3_0_OR_GREATER @@ -434,8 +417,8 @@ namespace MyTestNameSpace /// /// The case-sensitive string representation of the enumeration name or underlying value to convert /// to read value in case insensitive mode; to read value in case sensitive mode. - /// If , considers the value included in metadata attributes such as - /// [Display] attribute when parsing, otherwise only considers the member names. + /// If , considers the value included in + /// attribute when parsing, otherwise only considers the member names. /// An object of type whose /// value is represented by public static global::MyTestNameSpace.MyEnum Parse( @@ -501,8 +484,8 @@ namespace MyTestNameSpace /// If the parse operation fails, contains the default value of the underlying type /// of . This parameter is passed uninitialized. /// to read value in case insensitive mode; to read value in case sensitive mode. - /// If , considers the value included in metadata attributes such as - /// [Display] attribute when parsing, otherwise only considers the member names. + /// If , considers the value included in + /// attribute when parsing, otherwise only considers the member names. /// if the value parameter was converted successfully; otherwise, . public static bool TryParse( #if NETCOREAPP3_0_OR_GREATER diff --git a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateEnumExtensionsWithCustomNamespace.verified.txt b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateEnumExtensionsWithCustomNamespace.verified.txt index f570524..d3045eb 100644 --- a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateEnumExtensionsWithCustomNamespace.verified.txt +++ b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateEnumExtensionsWithCustomNamespace.verified.txt @@ -25,20 +25,6 @@ namespace A.B /// public const int Length = 2; - /// - /// Returns the string representation of the value. - /// If the attribute is decorated with a [Display] or [Description]attribute, then - /// uses the provided value. Otherwise uses the name of the member, equivalent to - /// calling ToString() on . - /// - /// The value to retrieve the string value for - /// If uses the value provided in the - /// [Display] or [Description]attribute as the string representation of the member. - /// If , always uses the name of the member, the same as if ToString() was called. - /// The string representation of the value - public static string ToStringFast(this global::MyTestNameSpace.MyEnum value, bool useMetadataAttributes) - => useMetadataAttributes ? value.ToStringFastWithMetadata() : value.ToStringFast(); - /// /// Returns the string representation of the value. /// Directly equivalent to calling ToString() on . @@ -53,6 +39,20 @@ namespace A.B _ => value.AsUnderlyingType().ToString(), }; + /// + /// Returns the string representation of the value. + /// If the member is decorated with the attribute + /// then that value is returned. Otherwise returns uses the name of the member, + /// equivalent to calling ToString() on . + /// + /// The value to retrieve the string value for + /// If uses the value provided in the + /// attribute as the string representation of the member. + /// If , always uses the name of the member, the same as if ToString() was called. + /// The string representation of the value + public static string ToStringFast(this global::MyTestNameSpace.MyEnum value, bool useMetadataAttributes) + => useMetadataAttributes ? value.ToStringFastWithMetadata() : value.ToStringFast(); + private static string ToStringFastWithMetadata(this global::MyTestNameSpace.MyEnum value) => value.ToStringFast(); @@ -84,26 +84,27 @@ namespace A.B /// /// The name to check if it's defined /// if a member with the name exists in the enumeration, otherwise - public static bool IsDefined(string name) => IsDefined(name, allowMatchingMetadataAttribute: false); + public static bool IsDefined(string name) + => name switch + { + nameof(global::MyTestNameSpace.MyEnum.First) => true, + nameof(global::MyTestNameSpace.MyEnum.Second) => true, + _ => false, + }; /// /// Returns a boolean telling whether an enum with the given name exists in the enumeration, - /// or if a member decorated with a [Display] attribute + /// or if a member decorated with /// with the required name exists. /// /// The name to check if it's defined - /// If , considers the value of metadata attributes,otherwise ignores them + /// If , + /// considers the value of + /// instead of the member name, otherwise ignores them /// if a member with the name exists in the enumeration, or a member is decorated /// with a [Display] attribute with the name, otherwise public static bool IsDefined(string name, bool allowMatchingMetadataAttribute) - { - return name switch - { - nameof(global::MyTestNameSpace.MyEnum.First) => true, - nameof(global::MyTestNameSpace.MyEnum.Second) => true, - _ => false, - }; - } + => IsDefined(name); #if NETCOREAPP && !NETCOREAPP2_0 && !NETCOREAPP1_1 && !NETCOREAPP1_0 /// @@ -111,27 +112,25 @@ namespace A.B /// /// The name to check if it's defined /// if a member with the name exists in the enumeration, otherwise - public static bool IsDefined(in global::System.ReadOnlySpan name) => IsDefined(name, allowMatchingMetadataAttribute: false); + public static bool IsDefined(in global::System.ReadOnlySpan name) + => name switch + { + global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.First), global::System.StringComparison.Ordinal) => true, + global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.Second), global::System.StringComparison.Ordinal) => true, + _ => false, + }; /// /// Returns a boolean telling whether an enum with the given name exists in the enumeration, - /// or optionally if a member decorated with a [Display] attribute - /// with the required name exists. - /// Slower then the overload, but doesn't allocate memory./> + /// or optionally if a member decorated with + /// exists. Slower then the overload, but doesn't allocate memory./> /// /// The name to check if it's defined /// If , considers the value of metadata attributes,otherwise ignores them /// if a member with the name exists in the enumeration, or a member is decorated /// with a [Display] attribute with the name, otherwise public static bool IsDefined(in global::System.ReadOnlySpan name, bool allowMatchingMetadataAttribute) - { - return name switch - { - global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.First), global::System.StringComparison.Ordinal) => true, - global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.Second), global::System.StringComparison.Ordinal) => true, - _ => false, - }; - } + => IsDefined(name); #endif /// @@ -243,8 +242,9 @@ namespace A.B /// If the parse operation fails, contains the default value of the underlying type /// of . This parameter is passed uninitialized. /// to read value in case insensitive mode; to read value in case sensitive mode. - /// If , considers the value included in metadata attributes such as - /// [Display] attribute when parsing, otherwise only considers the member names. + /// If , + /// considers the value included in attribute + /// when parsing, otherwise only considers the member names. /// if the value parameter was converted successfully; otherwise, . public static bool TryParse( #if NETCOREAPP3_0_OR_GREATER @@ -266,7 +266,6 @@ namespace A.B out global::MyTestNameSpace.MyEnum value, bool allowMatchingMetadataAttribute) { - switch (name) { case string s when s.Equals(nameof(global::MyTestNameSpace.MyEnum.First), global::System.StringComparison.OrdinalIgnoreCase): @@ -292,7 +291,6 @@ namespace A.B out global::MyTestNameSpace.MyEnum value, bool allowMatchingMetadataAttribute) { - switch (name) { case nameof(global::MyTestNameSpace.MyEnum.First): @@ -347,8 +345,8 @@ namespace A.B /// /// The case-sensitive string representation of the enumeration name or underlying value to convert /// to read value in case insensitive mode; to read value in case sensitive mode. - /// If , considers the value included in metadata attributes such as - /// [Display] attribute when parsing, otherwise only considers the member names. + /// If , considers the value included in + /// attribute when parsing, otherwise only considers the member names. /// An object of type whose /// value is represented by public static global::MyTestNameSpace.MyEnum Parse( @@ -414,8 +412,8 @@ namespace A.B /// If the parse operation fails, contains the default value of the underlying type /// of . This parameter is passed uninitialized. /// to read value in case insensitive mode; to read value in case sensitive mode. - /// If , considers the value included in metadata attributes such as - /// [Display] attribute when parsing, otherwise only considers the member names. + /// If , considers the value included in + /// attribute when parsing, otherwise only considers the member names. /// if the value parameter was converted successfully; otherwise, . public static bool TryParse( #if NETCOREAPP3_0_OR_GREATER diff --git a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateEnumExtensionsWithCustomNamespaceAndName.verified.txt b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateEnumExtensionsWithCustomNamespaceAndName.verified.txt index fe48913..58dbc32 100644 --- a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateEnumExtensionsWithCustomNamespaceAndName.verified.txt +++ b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateEnumExtensionsWithCustomNamespaceAndName.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the NetEscapades.EnumGenerators source generator // @@ -25,20 +25,6 @@ namespace A.B /// public const int Length = 2; - /// - /// Returns the string representation of the value. - /// If the attribute is decorated with a [Display] or [Description]attribute, then - /// uses the provided value. Otherwise uses the name of the member, equivalent to - /// calling ToString() on . - /// - /// The value to retrieve the string value for - /// If uses the value provided in the - /// [Display] or [Description]attribute as the string representation of the member. - /// If , always uses the name of the member, the same as if ToString() was called. - /// The string representation of the value - public static string ToStringFast(this global::MyTestNameSpace.MyEnum value, bool useMetadataAttributes) - => useMetadataAttributes ? value.ToStringFastWithMetadata() : value.ToStringFast(); - /// /// Returns the string representation of the value. /// Directly equivalent to calling ToString() on . @@ -53,6 +39,20 @@ namespace A.B _ => value.AsUnderlyingType().ToString(), }; + /// + /// Returns the string representation of the value. + /// If the member is decorated with the attribute + /// then that value is returned. Otherwise returns uses the name of the member, + /// equivalent to calling ToString() on . + /// + /// The value to retrieve the string value for + /// If uses the value provided in the + /// attribute as the string representation of the member. + /// If , always uses the name of the member, the same as if ToString() was called. + /// The string representation of the value + public static string ToStringFast(this global::MyTestNameSpace.MyEnum value, bool useMetadataAttributes) + => useMetadataAttributes ? value.ToStringFastWithMetadata() : value.ToStringFast(); + private static string ToStringFastWithMetadata(this global::MyTestNameSpace.MyEnum value) => value.ToStringFast(); @@ -84,26 +84,27 @@ namespace A.B /// /// The name to check if it's defined /// if a member with the name exists in the enumeration, otherwise - public static bool IsDefined(string name) => IsDefined(name, allowMatchingMetadataAttribute: false); + public static bool IsDefined(string name) + => name switch + { + nameof(global::MyTestNameSpace.MyEnum.First) => true, + nameof(global::MyTestNameSpace.MyEnum.Second) => true, + _ => false, + }; /// /// Returns a boolean telling whether an enum with the given name exists in the enumeration, - /// or if a member decorated with a [Display] attribute + /// or if a member decorated with /// with the required name exists. /// /// The name to check if it's defined - /// If , considers the value of metadata attributes,otherwise ignores them + /// If , + /// considers the value of + /// instead of the member name, otherwise ignores them /// if a member with the name exists in the enumeration, or a member is decorated /// with a [Display] attribute with the name, otherwise public static bool IsDefined(string name, bool allowMatchingMetadataAttribute) - { - return name switch - { - nameof(global::MyTestNameSpace.MyEnum.First) => true, - nameof(global::MyTestNameSpace.MyEnum.Second) => true, - _ => false, - }; - } + => IsDefined(name); #if NETCOREAPP && !NETCOREAPP2_0 && !NETCOREAPP1_1 && !NETCOREAPP1_0 /// @@ -111,27 +112,25 @@ namespace A.B /// /// The name to check if it's defined /// if a member with the name exists in the enumeration, otherwise - public static bool IsDefined(in global::System.ReadOnlySpan name) => IsDefined(name, allowMatchingMetadataAttribute: false); + public static bool IsDefined(in global::System.ReadOnlySpan name) + => name switch + { + global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.First), global::System.StringComparison.Ordinal) => true, + global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.Second), global::System.StringComparison.Ordinal) => true, + _ => false, + }; /// /// Returns a boolean telling whether an enum with the given name exists in the enumeration, - /// or optionally if a member decorated with a [Display] attribute - /// with the required name exists. - /// Slower then the overload, but doesn't allocate memory./> + /// or optionally if a member decorated with + /// exists. Slower then the overload, but doesn't allocate memory./> /// /// The name to check if it's defined /// If , considers the value of metadata attributes,otherwise ignores them /// if a member with the name exists in the enumeration, or a member is decorated /// with a [Display] attribute with the name, otherwise public static bool IsDefined(in global::System.ReadOnlySpan name, bool allowMatchingMetadataAttribute) - { - return name switch - { - global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.First), global::System.StringComparison.Ordinal) => true, - global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.Second), global::System.StringComparison.Ordinal) => true, - _ => false, - }; - } + => IsDefined(name); #endif /// @@ -243,8 +242,9 @@ namespace A.B /// If the parse operation fails, contains the default value of the underlying type /// of . This parameter is passed uninitialized. /// to read value in case insensitive mode; to read value in case sensitive mode. - /// If , considers the value included in metadata attributes such as - /// [Display] attribute when parsing, otherwise only considers the member names. + /// If , + /// considers the value included in attribute + /// when parsing, otherwise only considers the member names. /// if the value parameter was converted successfully; otherwise, . public static bool TryParse( #if NETCOREAPP3_0_OR_GREATER @@ -266,7 +266,6 @@ namespace A.B out global::MyTestNameSpace.MyEnum value, bool allowMatchingMetadataAttribute) { - switch (name) { case string s when s.Equals(nameof(global::MyTestNameSpace.MyEnum.First), global::System.StringComparison.OrdinalIgnoreCase): @@ -292,7 +291,6 @@ namespace A.B out global::MyTestNameSpace.MyEnum value, bool allowMatchingMetadataAttribute) { - switch (name) { case nameof(global::MyTestNameSpace.MyEnum.First): @@ -347,8 +345,8 @@ namespace A.B /// /// The case-sensitive string representation of the enumeration name or underlying value to convert /// to read value in case insensitive mode; to read value in case sensitive mode. - /// If , considers the value included in metadata attributes such as - /// [Display] attribute when parsing, otherwise only considers the member names. + /// If , considers the value included in + /// attribute when parsing, otherwise only considers the member names. /// An object of type whose /// value is represented by public static global::MyTestNameSpace.MyEnum Parse( @@ -414,8 +412,8 @@ namespace A.B /// If the parse operation fails, contains the default value of the underlying type /// of . This parameter is passed uninitialized. /// to read value in case insensitive mode; to read value in case sensitive mode. - /// If , considers the value included in metadata attributes such as - /// [Display] attribute when parsing, otherwise only considers the member names. + /// If , considers the value included in + /// attribute when parsing, otherwise only considers the member names. /// if the value parameter was converted successfully; otherwise, . public static bool TryParse( #if NETCOREAPP3_0_OR_GREATER diff --git a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateEnumExtensionsWithMetadataName_IgnoringOthers__.verified.txt b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateEnumExtensionsWithMetadataName_IgnoringOthers__.verified.txt new file mode 100644 index 0000000..c9e3693 --- /dev/null +++ b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateEnumExtensionsWithMetadataName_IgnoringOthers__.verified.txt @@ -0,0 +1,651 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the NetEscapades.EnumGenerators source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#nullable enable + +namespace MyTestNameSpace +{ +#pragma warning disable CS0612 // Ignore usages of obsolete members or enums +#pragma warning disable CS0618 // Ignore usages of obsolete members or enums + /// + /// Extension methods for + /// + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("NetEscapades.EnumGenerators", "FIXED_VERSION")] + public static partial class MyEnumExtensions + { + /// + /// The number of members in the enum. + /// This is a non-distinct count of defined names. + /// + public const int Length = 4; + + /// + /// Returns the string representation of the value. + /// Directly equivalent to calling ToString() on . + /// + /// The value to retrieve the string value for + /// The string representation of the value, the same as that returned by ToString() + public static string ToStringFast(this global::MyTestNameSpace.MyEnum value) + => value switch + { + global::MyTestNameSpace.MyEnum.First => nameof(global::MyTestNameSpace.MyEnum.First), + global::MyTestNameSpace.MyEnum.Second => nameof(global::MyTestNameSpace.MyEnum.Second), + global::MyTestNameSpace.MyEnum.Third => nameof(global::MyTestNameSpace.MyEnum.Third), + global::MyTestNameSpace.MyEnum.Fourth => nameof(global::MyTestNameSpace.MyEnum.Fourth), + _ => value.AsUnderlyingType().ToString(), + }; + + /// + /// Returns the string representation of the value. + /// If the member is decorated with the attribute + /// then that value is returned. Otherwise returns uses the name of the member, + /// equivalent to calling ToString() on . + /// + /// The value to retrieve the string value for + /// If uses the value provided in the + /// attribute as the string representation of the member. + /// If , always uses the name of the member, the same as if ToString() was called. + /// The string representation of the value + public static string ToStringFast(this global::MyTestNameSpace.MyEnum value, bool useMetadataAttributes) + => useMetadataAttributes ? value.ToStringFastWithMetadata() : value.ToStringFast(); + + private static string ToStringFastWithMetadata(this global::MyTestNameSpace.MyEnum value) + => value switch + { + global::MyTestNameSpace.MyEnum.First => nameof(global::MyTestNameSpace.MyEnum.First), + global::MyTestNameSpace.MyEnum.Second => "2nd", + global::MyTestNameSpace.MyEnum.Third => nameof(global::MyTestNameSpace.MyEnum.Third), + global::MyTestNameSpace.MyEnum.Fourth => "4th", + _ => value.AsUnderlyingType().ToString(), + }; + + /// + /// Cast a value of to the underlying type (int). + /// This is mainly a convenience method. + /// + /// The value of cast to the underlying type. + public static int AsUnderlyingType(this global::MyTestNameSpace.MyEnum value) + { + return (int) value; + } + + /// + /// Returns a boolean telling whether the given enum value exists in the enumeration. + /// + /// The value to check if it's defined + /// if the value exists in the enumeration, otherwise + public static bool IsDefined(global::MyTestNameSpace.MyEnum value) + => value switch + { + global::MyTestNameSpace.MyEnum.First => true, + global::MyTestNameSpace.MyEnum.Second => true, + global::MyTestNameSpace.MyEnum.Third => true, + global::MyTestNameSpace.MyEnum.Fourth => true, + _ => false, + }; + + /// + /// Returns a boolean telling whether an enum with the given name exists in the enumeration. + /// + /// The name to check if it's defined + /// if a member with the name exists in the enumeration, otherwise + public static bool IsDefined(string name) + => name switch + { + nameof(global::MyTestNameSpace.MyEnum.First) => true, + nameof(global::MyTestNameSpace.MyEnum.Second) => true, + nameof(global::MyTestNameSpace.MyEnum.Third) => true, + nameof(global::MyTestNameSpace.MyEnum.Fourth) => true, + _ => false, + }; + + /// + /// Returns a boolean telling whether an enum with the given name exists in the enumeration, + /// or if a member decorated with + /// with the required name exists. + /// + /// The name to check if it's defined + /// If , + /// considers the value of + /// instead of the member name, otherwise ignores them + /// if a member with the name exists in the enumeration, or a member is decorated + /// with a [Display] attribute with the name, otherwise + public static bool IsDefined(string name, bool allowMatchingMetadataAttribute) + => allowMatchingMetadataAttribute ? IsMetadataNameDefined(name) : IsDefined(name); + + private static bool IsMetadataNameDefined(string name) + => name switch + { + "2nd" => true, + "4th" => true, + _ => IsDefined(name), + }; + +#if NETCOREAPP && !NETCOREAPP2_0 && !NETCOREAPP1_1 && !NETCOREAPP1_0 + /// + /// Returns a boolean telling whether an enum with the given name exists in the enumeration + /// + /// The name to check if it's defined + /// if a member with the name exists in the enumeration, otherwise + public static bool IsDefined(in global::System.ReadOnlySpan name) + => name switch + { + global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.First), global::System.StringComparison.Ordinal) => true, + global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.Second), global::System.StringComparison.Ordinal) => true, + global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.Third), global::System.StringComparison.Ordinal) => true, + global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.Fourth), global::System.StringComparison.Ordinal) => true, + _ => false, + }; + + /// + /// Returns a boolean telling whether an enum with the given name exists in the enumeration, + /// or optionally if a member decorated with + /// exists. Slower then the overload, but doesn't allocate memory./> + /// + /// The name to check if it's defined + /// If , considers the value of metadata attributes,otherwise ignores them + /// if a member with the name exists in the enumeration, or a member is decorated + /// with a [Display] attribute with the name, otherwise + public static bool IsDefined(in global::System.ReadOnlySpan name, bool allowMatchingMetadataAttribute) => allowMatchingMetadataAttribute ? IsMetadataNameDefined(in name) : IsDefined(in name); + + private static bool IsMetadataNameDefined(in global::System.ReadOnlySpan name) + => name switch + { + global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, "2nd", global::System.StringComparison.Ordinal) => true, + global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, "4th", global::System.StringComparison.Ordinal) => true, + _ => IsDefined(name), + }; +#endif + + /// + /// Converts the string representation of the name or numeric value of + /// an to the equivalent instance. + /// + /// The case-sensitive string representation of the enumeration name or underlying value to convert + /// An object of type whose + /// value is represented by + public static global::MyTestNameSpace.MyEnum Parse( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + string? name) + => TryParse(name, out var value, false, false) ? value : ThrowValueNotFound(name); + + /// + /// Converts the string representation of the name or numeric value of + /// an to the equivalent instance. + /// + /// The case-sensitive string representation of the enumeration name or underlying value to convert + /// to read value in case insensitive mode; to read value in case sensitive mode. + /// An object of type whose + /// value is represented by + public static global::MyTestNameSpace.MyEnum Parse( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + string? name, + bool ignoreCase) + => TryParse(name, out var value, ignoreCase, false) ? value : ThrowValueNotFound(name); + + /// + /// Converts the string representation of the name or numeric value of + /// an to the equivalent instance. + /// + /// The case-sensitive string representation of the enumeration name or underlying value to convert + /// to read value in case insensitive mode; to read value in case sensitive mode. + /// If , considers the value included in metadata attributes such as + /// [Display] attribute when parsing, otherwise only considers the member names. + /// An object of type whose + /// value is represented by + public static global::MyTestNameSpace.MyEnum Parse( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + string? name, + bool ignoreCase, + bool allowMatchingMetadataAttribute) + => TryParse(name, out var value, ignoreCase, allowMatchingMetadataAttribute) ? value : ThrowValueNotFound(name); + +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.DoesNotReturn] +#endif + private static global::MyTestNameSpace.MyEnum ThrowValueNotFound(string? name) + => throw new global::System.ArgumentException($"Requested value '{name}' was not found."); + + /// + /// Converts the string representation of the name or numeric value of + /// an to the equivalent instance. + /// The return value indicates whether the conversion succeeded. + /// + /// The case-sensitive string representation of the enumeration name or underlying value to convert + /// When this method returns, contains an object of type + /// whose + /// value is represented by if the parse operation succeeds. + /// If the parse operation fails, contains the default value of the underlying type + /// of . This parameter is passed uninitialized. + /// if the value parameter was converted successfully; otherwise, . + public static bool TryParse( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + string? name, + out global::MyTestNameSpace.MyEnum value) + => TryParse(name, out value, false, false); + + /// + /// Converts the string representation of the name or numeric value of + /// an to the equivalent instance. + /// The return value indicates whether the conversion succeeded. + /// + /// The string representation of the enumeration name or underlying value to convert + /// When this method returns, contains an object of type + /// whose + /// value is represented by if the parse operation succeeds. + /// If the parse operation fails, contains the default value of the underlying type + /// of . This parameter is passed uninitialized. + /// to read value in case insensitive mode; to read value in case sensitive mode. + /// if the value parameter was converted successfully; otherwise, . + public static bool TryParse( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + string? name, + out global::MyTestNameSpace.MyEnum value, + bool ignoreCase) + => TryParse(name, out value, ignoreCase, false); + + /// + /// Converts the string representation of the name or numeric value of + /// an to the equivalent instance. + /// The return value indicates whether the conversion succeeded. + /// + /// The string representation of the enumeration name or underlying value to convert + /// When this method returns, contains an object of type + /// whose + /// value is represented by if the parse operation succeeds. + /// If the parse operation fails, contains the default value of the underlying type + /// of . This parameter is passed uninitialized. + /// to read value in case insensitive mode; to read value in case sensitive mode. + /// If , + /// considers the value included in attribute + /// when parsing, otherwise only considers the member names. + /// if the value parameter was converted successfully; otherwise, . + public static bool TryParse( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + string? name, + out global::MyTestNameSpace.MyEnum value, + bool ignoreCase, + bool allowMatchingMetadataAttribute) + => ignoreCase + ? TryParseIgnoreCase(name, out value, allowMatchingMetadataAttribute) + : TryParseWithCase(name, out value, allowMatchingMetadataAttribute); + + private static bool TryParseIgnoreCase( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + string? name, + out global::MyTestNameSpace.MyEnum value, + bool allowMatchingMetadataAttribute) + { + if (allowMatchingMetadataAttribute) + { + switch (name) + { + case string s when s.Equals("2nd", global::System.StringComparison.OrdinalIgnoreCase): + value = global::MyTestNameSpace.MyEnum.Second; + return true; + case string s when s.Equals("4th", global::System.StringComparison.OrdinalIgnoreCase): + value = global::MyTestNameSpace.MyEnum.Fourth; + return true; + default: + break; + }; + } + + switch (name) + { + case string s when s.Equals(nameof(global::MyTestNameSpace.MyEnum.First), global::System.StringComparison.OrdinalIgnoreCase): + value = global::MyTestNameSpace.MyEnum.First; + return true; + case string s when s.Equals(nameof(global::MyTestNameSpace.MyEnum.Second), global::System.StringComparison.OrdinalIgnoreCase): + value = global::MyTestNameSpace.MyEnum.Second; + return true; + case string s when s.Equals(nameof(global::MyTestNameSpace.MyEnum.Third), global::System.StringComparison.OrdinalIgnoreCase): + value = global::MyTestNameSpace.MyEnum.Third; + return true; + case string s when s.Equals(nameof(global::MyTestNameSpace.MyEnum.Fourth), global::System.StringComparison.OrdinalIgnoreCase): + value = global::MyTestNameSpace.MyEnum.Fourth; + return true; + case string s when int.TryParse(name, out var val): + value = (global::MyTestNameSpace.MyEnum)val; + return true; + default: + value = default; + return false; + } + } + + private static bool TryParseWithCase( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + string? name, + out global::MyTestNameSpace.MyEnum value, + bool allowMatchingMetadataAttribute) + { + if (allowMatchingMetadataAttribute) + { + switch (name) + { + case "2nd": + value = global::MyTestNameSpace.MyEnum.Second; + return true; + case "4th": + value = global::MyTestNameSpace.MyEnum.Fourth; + return true; + default: + break; + }; + } + + switch (name) + { + case nameof(global::MyTestNameSpace.MyEnum.First): + value = global::MyTestNameSpace.MyEnum.First; + return true; + case nameof(global::MyTestNameSpace.MyEnum.Second): + value = global::MyTestNameSpace.MyEnum.Second; + return true; + case nameof(global::MyTestNameSpace.MyEnum.Third): + value = global::MyTestNameSpace.MyEnum.Third; + return true; + case nameof(global::MyTestNameSpace.MyEnum.Fourth): + value = global::MyTestNameSpace.MyEnum.Fourth; + return true; + case string s when int.TryParse(name, out var val): + value = (global::MyTestNameSpace.MyEnum)val; + return true; + default: + value = default; + return false; + } + } + +#if NETCOREAPP && !NETCOREAPP2_0 && !NETCOREAPP1_1 && !NETCOREAPP1_0 + /// + /// Converts the string representation of the name or numeric value of + /// an to the equivalent instance. + /// + /// The case-sensitive string representation of the enumeration name or underlying value to convert + /// An object of type whose + /// value is represented by + public static global::MyTestNameSpace.MyEnum Parse( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + in global::System.ReadOnlySpan name) + => TryParse(name, out var value, false, false) ? value : ThrowValueNotFound(name.ToString()); + + /// + /// Converts the string representation of the name or numeric value of + /// an to the equivalent instance. + /// + /// The case-sensitive string representation of the enumeration name or underlying value to convert + /// to read value in case insensitive mode; to read value in case sensitive mode. + /// An object of type whose + /// value is represented by + public static global::MyTestNameSpace.MyEnum Parse( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + in global::System.ReadOnlySpan name, + bool ignoreCase) + => TryParse(name, out var value, ignoreCase, false) ? value : ThrowValueNotFound(name.ToString()); + + /// + /// Converts the string representation of the name or numeric value of + /// an to the equivalent instance. + /// + /// The case-sensitive string representation of the enumeration name or underlying value to convert + /// to read value in case insensitive mode; to read value in case sensitive mode. + /// If , considers the value included in + /// attribute when parsing, otherwise only considers the member names. + /// An object of type whose + /// value is represented by + public static global::MyTestNameSpace.MyEnum Parse( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + in global::System.ReadOnlySpan name, + bool ignoreCase, + bool allowMatchingMetadataAttribute) + => TryParse(name, out var value, ignoreCase, allowMatchingMetadataAttribute) ? value : ThrowValueNotFound(name.ToString()); + + /// + /// Converts the span representation of the name or numeric value of + /// an to the equivalent instance. + /// The return value indicates whether the conversion succeeded. + /// + /// The span representation of the enumeration name or underlying value to convert + /// When this method returns, contains an object of type + /// whose + /// value is represented by if the parse operation succeeds. + /// If the parse operation fails, contains the default value of the underlying type + /// of . This parameter is passed uninitialized. + /// if the value parameter was converted successfully; otherwise, . + public static bool TryParse( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + in global::System.ReadOnlySpan name, + out global::MyTestNameSpace.MyEnum value) + => TryParse(name, out value, false, false); + + /// + /// Converts the span representation of the name or numeric value of + /// an to the equivalent instance. + /// The return value indicates whether the conversion succeeded. + /// + /// The span representation of the enumeration name or underlying value to convert + /// When this method returns, contains an object of type + /// whose + /// value is represented by if the parse operation succeeds. + /// If the parse operation fails, contains the default value of the underlying type + /// of . This parameter is passed uninitialized. + /// to read value in case insensitive mode; to read value in case sensitive mode. + /// if the value parameter was converted successfully; otherwise, . + public static bool TryParse( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + in global::System.ReadOnlySpan name, + out global::MyTestNameSpace.MyEnum value, + bool ignoreCase) + => TryParse(name, out value, ignoreCase, false); + + /// + /// Converts the span representation of the name or numeric value of + /// an to the equivalent instance. + /// The return value indicates whether the conversion succeeded. + /// + /// The span representation of the enumeration name or underlying value to convert + /// When this method returns, contains an object of type + /// whose + /// value is represented by if the parse operation succeeds. + /// If the parse operation fails, contains the default value of the underlying type + /// of . This parameter is passed uninitialized. + /// to read value in case insensitive mode; to read value in case sensitive mode. + /// If , considers the value included in + /// attribute when parsing, otherwise only considers the member names. + /// if the value parameter was converted successfully; otherwise, . + public static bool TryParse( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + in global::System.ReadOnlySpan name, + out global::MyTestNameSpace.MyEnum result, + bool ignoreCase, + bool allowMatchingMetadataAttribute) + => ignoreCase + ? TryParseIgnoreCase(in name, out result, allowMatchingMetadataAttribute) + : TryParseWithCase(in name, out result, allowMatchingMetadataAttribute); + + private static bool TryParseIgnoreCase( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + in global::System.ReadOnlySpan name, + out global::MyTestNameSpace.MyEnum result, + bool allowMatchingMetadataAttribute) + { + if (allowMatchingMetadataAttribute) + { + switch (name) + { + case global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, "2nd", global::System.StringComparison.OrdinalIgnoreCase): + result = global::MyTestNameSpace.MyEnum.Second; + return true; + case global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, "4th", global::System.StringComparison.OrdinalIgnoreCase): + result = global::MyTestNameSpace.MyEnum.Fourth; + return true; + default: + break; + }; + } + + switch (name) + { + case global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.First), global::System.StringComparison.OrdinalIgnoreCase): + result = global::MyTestNameSpace.MyEnum.First; + return true; + case global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.Second), global::System.StringComparison.OrdinalIgnoreCase): + result = global::MyTestNameSpace.MyEnum.Second; + return true; + case global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.Third), global::System.StringComparison.OrdinalIgnoreCase): + result = global::MyTestNameSpace.MyEnum.Third; + return true; + case global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.Fourth), global::System.StringComparison.OrdinalIgnoreCase): + result = global::MyTestNameSpace.MyEnum.Fourth; + return true; + case global::System.ReadOnlySpan current when int.TryParse(name, out var numericResult): + result = (global::MyTestNameSpace.MyEnum)numericResult; + return true; + default: + result = default; + return false; + } + } + + private static bool TryParseWithCase( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + in global::System.ReadOnlySpan name, + out global::MyTestNameSpace.MyEnum result, + bool allowMatchingMetadataAttribute) + { + if (allowMatchingMetadataAttribute) + { + switch (name) + { + case global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, "2nd", global::System.StringComparison.Ordinal): + result = global::MyTestNameSpace.MyEnum.Second; + return true; + case global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, "4th", global::System.StringComparison.Ordinal): + result = global::MyTestNameSpace.MyEnum.Fourth; + return true; + default: + break; + }; + } + + switch (name) + { + case global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.First), global::System.StringComparison.Ordinal): + result = global::MyTestNameSpace.MyEnum.First; + return true; + case global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.Second), global::System.StringComparison.Ordinal): + result = global::MyTestNameSpace.MyEnum.Second; + return true; + case global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.Third), global::System.StringComparison.Ordinal): + result = global::MyTestNameSpace.MyEnum.Third; + return true; + case global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.Fourth), global::System.StringComparison.Ordinal): + result = global::MyTestNameSpace.MyEnum.Fourth; + return true; + case global::System.ReadOnlySpan current when int.TryParse(name, out var numericResult): + result = (global::MyTestNameSpace.MyEnum)numericResult; + return true; + default: + result = default; + return false; + } + } +#endif + + /// + /// Retrieves an array of the values of the members defined in + /// . + /// Note that this returns a new array with every invocation, so + /// should be cached if appropriate. + /// + /// An array of the values defined in + public static global::MyTestNameSpace.MyEnum[] GetValues() + { + return new[] + { + global::MyTestNameSpace.MyEnum.First, + global::MyTestNameSpace.MyEnum.Second, + global::MyTestNameSpace.MyEnum.Third, + global::MyTestNameSpace.MyEnum.Fourth, + }; + } + + /// + /// Retrieves an array of the underlying-values of the members defined in + /// . + /// Note that this returns a new array with every invocation, so + /// should be cached if appropriate. + /// + /// An array of the underlying-values defined in + public static int[] GetValuesAsUnderlyingType() + { + return new[] + { + (int) global::MyTestNameSpace.MyEnum.First, + (int) global::MyTestNameSpace.MyEnum.Second, + (int) global::MyTestNameSpace.MyEnum.Third, + (int) global::MyTestNameSpace.MyEnum.Fourth, + }; + } + + /// + /// Retrieves an array of the names of the members defined in + /// . + /// Note that this returns a new array with every invocation, so + /// should be cached if appropriate. + /// + /// An array of the names of the members defined in + public static string[] GetNames() + { + return new[] + { + nameof(global::MyTestNameSpace.MyEnum.First), + nameof(global::MyTestNameSpace.MyEnum.Second), + nameof(global::MyTestNameSpace.MyEnum.Third), + nameof(global::MyTestNameSpace.MyEnum.Fourth), + }; + } + } +#pragma warning restore CS0612 // Ignore usages of obsolete members or enums +#pragma warning restore CS0618 // Ignore usages of obsolete members or enums +} \ No newline at end of file diff --git a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateEnumExtensionsWithNoneMetadataName__.verified.txt b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateEnumExtensionsWithNoneMetadataName__.verified.txt new file mode 100644 index 0000000..006df49 --- /dev/null +++ b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateEnumExtensionsWithNoneMetadataName__.verified.txt @@ -0,0 +1,457 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the NetEscapades.EnumGenerators source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#nullable enable + +namespace MyTestNameSpace +{ +#pragma warning disable CS0612 // Ignore usages of obsolete members or enums +#pragma warning disable CS0618 // Ignore usages of obsolete members or enums + /// + /// Extension methods for + /// + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("NetEscapades.EnumGenerators", "FIXED_VERSION")] + public static partial class MyEnumExtensions + { + /// + /// The number of members in the enum. + /// This is a non-distinct count of defined names. + /// + public const int Length = 4; + + /// + /// Returns the string representation of the value. + /// Directly equivalent to calling ToString() on . + /// + /// The value to retrieve the string value for + /// The string representation of the value, the same as that returned by ToString() + public static string ToStringFast(this global::MyTestNameSpace.MyEnum value) + => value switch + { + global::MyTestNameSpace.MyEnum.First => nameof(global::MyTestNameSpace.MyEnum.First), + global::MyTestNameSpace.MyEnum.Second => nameof(global::MyTestNameSpace.MyEnum.Second), + global::MyTestNameSpace.MyEnum.Third => nameof(global::MyTestNameSpace.MyEnum.Third), + global::MyTestNameSpace.MyEnum.Fourth => nameof(global::MyTestNameSpace.MyEnum.Fourth), + _ => value.AsUnderlyingType().ToString(), + }; + + /// + /// Cast a value of to the underlying type (int). + /// This is mainly a convenience method. + /// + /// The value of cast to the underlying type. + public static int AsUnderlyingType(this global::MyTestNameSpace.MyEnum value) + { + return (int) value; + } + + /// + /// Returns a boolean telling whether the given enum value exists in the enumeration. + /// + /// The value to check if it's defined + /// if the value exists in the enumeration, otherwise + public static bool IsDefined(global::MyTestNameSpace.MyEnum value) + => value switch + { + global::MyTestNameSpace.MyEnum.First => true, + global::MyTestNameSpace.MyEnum.Second => true, + global::MyTestNameSpace.MyEnum.Third => true, + global::MyTestNameSpace.MyEnum.Fourth => true, + _ => false, + }; + + /// + /// Returns a boolean telling whether an enum with the given name exists in the enumeration. + /// + /// The name to check if it's defined + /// if a member with the name exists in the enumeration, otherwise + public static bool IsDefined(string name) + => name switch + { + nameof(global::MyTestNameSpace.MyEnum.First) => true, + nameof(global::MyTestNameSpace.MyEnum.Second) => true, + nameof(global::MyTestNameSpace.MyEnum.Third) => true, + nameof(global::MyTestNameSpace.MyEnum.Fourth) => true, + _ => false, + }; + +#if NETCOREAPP && !NETCOREAPP2_0 && !NETCOREAPP1_1 && !NETCOREAPP1_0 + /// + /// Returns a boolean telling whether an enum with the given name exists in the enumeration + /// + /// The name to check if it's defined + /// if a member with the name exists in the enumeration, otherwise + public static bool IsDefined(in global::System.ReadOnlySpan name) + => name switch + { + global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.First), global::System.StringComparison.Ordinal) => true, + global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.Second), global::System.StringComparison.Ordinal) => true, + global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.Third), global::System.StringComparison.Ordinal) => true, + global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.Fourth), global::System.StringComparison.Ordinal) => true, + _ => false, + }; +#endif + + /// + /// Converts the string representation of the name or numeric value of + /// an to the equivalent instance. + /// + /// The case-sensitive string representation of the enumeration name or underlying value to convert + /// An object of type whose + /// value is represented by + public static global::MyTestNameSpace.MyEnum Parse( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + string? name) + => TryParse(name, out var value, false, false) ? value : ThrowValueNotFound(name); + + /// + /// Converts the string representation of the name or numeric value of + /// an to the equivalent instance. + /// + /// The case-sensitive string representation of the enumeration name or underlying value to convert + /// to read value in case insensitive mode; to read value in case sensitive mode. + /// An object of type whose + /// value is represented by + public static global::MyTestNameSpace.MyEnum Parse( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + string? name, + bool ignoreCase) + => TryParse(name, out var value, ignoreCase, false) ? value : ThrowValueNotFound(name); + +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.DoesNotReturn] +#endif + private static global::MyTestNameSpace.MyEnum ThrowValueNotFound(string? name) + => throw new global::System.ArgumentException($"Requested value '{name}' was not found."); + + /// + /// Converts the string representation of the name or numeric value of + /// an to the equivalent instance. + /// The return value indicates whether the conversion succeeded. + /// + /// The case-sensitive string representation of the enumeration name or underlying value to convert + /// When this method returns, contains an object of type + /// whose + /// value is represented by if the parse operation succeeds. + /// If the parse operation fails, contains the default value of the underlying type + /// of . This parameter is passed uninitialized. + /// if the value parameter was converted successfully; otherwise, . + public static bool TryParse( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + string? name, + out global::MyTestNameSpace.MyEnum value) + => TryParse(name, out value, false, false); + + /// + /// Converts the string representation of the name or numeric value of + /// an to the equivalent instance. + /// The return value indicates whether the conversion succeeded. + /// + /// The string representation of the enumeration name or underlying value to convert + /// When this method returns, contains an object of type + /// whose + /// value is represented by if the parse operation succeeds. + /// If the parse operation fails, contains the default value of the underlying type + /// of . This parameter is passed uninitialized. + /// to read value in case insensitive mode; to read value in case sensitive mode. + /// if the value parameter was converted successfully; otherwise, . + public static bool TryParse( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + string? name, + out global::MyTestNameSpace.MyEnum value, + bool ignoreCase) + => TryParse(name, out value, ignoreCase, false); + + private static bool TryParse( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + string? name, + out global::MyTestNameSpace.MyEnum value, + bool ignoreCase, + bool allowMatchingMetadataAttribute) + => ignoreCase + ? TryParseIgnoreCase(name, out value, allowMatchingMetadataAttribute) + : TryParseWithCase(name, out value, allowMatchingMetadataAttribute); + + private static bool TryParseIgnoreCase( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + string? name, + out global::MyTestNameSpace.MyEnum value, + bool allowMatchingMetadataAttribute) + { + switch (name) + { + case string s when s.Equals(nameof(global::MyTestNameSpace.MyEnum.First), global::System.StringComparison.OrdinalIgnoreCase): + value = global::MyTestNameSpace.MyEnum.First; + return true; + case string s when s.Equals(nameof(global::MyTestNameSpace.MyEnum.Second), global::System.StringComparison.OrdinalIgnoreCase): + value = global::MyTestNameSpace.MyEnum.Second; + return true; + case string s when s.Equals(nameof(global::MyTestNameSpace.MyEnum.Third), global::System.StringComparison.OrdinalIgnoreCase): + value = global::MyTestNameSpace.MyEnum.Third; + return true; + case string s when s.Equals(nameof(global::MyTestNameSpace.MyEnum.Fourth), global::System.StringComparison.OrdinalIgnoreCase): + value = global::MyTestNameSpace.MyEnum.Fourth; + return true; + case string s when int.TryParse(name, out var val): + value = (global::MyTestNameSpace.MyEnum)val; + return true; + default: + value = default; + return false; + } + } + + private static bool TryParseWithCase( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + string? name, + out global::MyTestNameSpace.MyEnum value, + bool allowMatchingMetadataAttribute) + { + switch (name) + { + case nameof(global::MyTestNameSpace.MyEnum.First): + value = global::MyTestNameSpace.MyEnum.First; + return true; + case nameof(global::MyTestNameSpace.MyEnum.Second): + value = global::MyTestNameSpace.MyEnum.Second; + return true; + case nameof(global::MyTestNameSpace.MyEnum.Third): + value = global::MyTestNameSpace.MyEnum.Third; + return true; + case nameof(global::MyTestNameSpace.MyEnum.Fourth): + value = global::MyTestNameSpace.MyEnum.Fourth; + return true; + case string s when int.TryParse(name, out var val): + value = (global::MyTestNameSpace.MyEnum)val; + return true; + default: + value = default; + return false; + } + } + +#if NETCOREAPP && !NETCOREAPP2_0 && !NETCOREAPP1_1 && !NETCOREAPP1_0 + /// + /// Converts the string representation of the name or numeric value of + /// an to the equivalent instance. + /// + /// The case-sensitive string representation of the enumeration name or underlying value to convert + /// An object of type whose + /// value is represented by + public static global::MyTestNameSpace.MyEnum Parse( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + in global::System.ReadOnlySpan name) + => TryParse(name, out var value, false, false) ? value : ThrowValueNotFound(name.ToString()); + + /// + /// Converts the string representation of the name or numeric value of + /// an to the equivalent instance. + /// + /// The case-sensitive string representation of the enumeration name or underlying value to convert + /// to read value in case insensitive mode; to read value in case sensitive mode. + /// An object of type whose + /// value is represented by + public static global::MyTestNameSpace.MyEnum Parse( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + in global::System.ReadOnlySpan name, + bool ignoreCase) + => TryParse(name, out var value, ignoreCase, false) ? value : ThrowValueNotFound(name.ToString()); + + /// + /// Converts the span representation of the name or numeric value of + /// an to the equivalent instance. + /// The return value indicates whether the conversion succeeded. + /// + /// The span representation of the enumeration name or underlying value to convert + /// When this method returns, contains an object of type + /// whose + /// value is represented by if the parse operation succeeds. + /// If the parse operation fails, contains the default value of the underlying type + /// of . This parameter is passed uninitialized. + /// if the value parameter was converted successfully; otherwise, . + public static bool TryParse( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + in global::System.ReadOnlySpan name, + out global::MyTestNameSpace.MyEnum value) + => TryParse(name, out value, false, false); + + /// + /// Converts the span representation of the name or numeric value of + /// an to the equivalent instance. + /// The return value indicates whether the conversion succeeded. + /// + /// The span representation of the enumeration name or underlying value to convert + /// When this method returns, contains an object of type + /// whose + /// value is represented by if the parse operation succeeds. + /// If the parse operation fails, contains the default value of the underlying type + /// of . This parameter is passed uninitialized. + /// to read value in case insensitive mode; to read value in case sensitive mode. + /// if the value parameter was converted successfully; otherwise, . + public static bool TryParse( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + in global::System.ReadOnlySpan name, + out global::MyTestNameSpace.MyEnum value, + bool ignoreCase) + => TryParse(name, out value, ignoreCase, false); + + private static bool TryParse( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + in global::System.ReadOnlySpan name, + out global::MyTestNameSpace.MyEnum value, + bool ignoreCase, + bool allowMatchingMetadataAttribute) + => ignoreCase + ? TryParseIgnoreCase(in name, out value, allowMatchingMetadataAttribute) + : TryParseWithCase(in name, out value, allowMatchingMetadataAttribute); + + private static bool TryParseIgnoreCase( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + in global::System.ReadOnlySpan name, + out global::MyTestNameSpace.MyEnum result, + bool allowMatchingMetadataAttribute) + { + switch (name) + { + case global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.First), global::System.StringComparison.OrdinalIgnoreCase): + result = global::MyTestNameSpace.MyEnum.First; + return true; + case global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.Second), global::System.StringComparison.OrdinalIgnoreCase): + result = global::MyTestNameSpace.MyEnum.Second; + return true; + case global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.Third), global::System.StringComparison.OrdinalIgnoreCase): + result = global::MyTestNameSpace.MyEnum.Third; + return true; + case global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.Fourth), global::System.StringComparison.OrdinalIgnoreCase): + result = global::MyTestNameSpace.MyEnum.Fourth; + return true; + case global::System.ReadOnlySpan current when int.TryParse(name, out var numericResult): + result = (global::MyTestNameSpace.MyEnum)numericResult; + return true; + default: + result = default; + return false; + } + } + + private static bool TryParseWithCase( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + in global::System.ReadOnlySpan name, + out global::MyTestNameSpace.MyEnum result, + bool allowMatchingMetadataAttribute) + { + switch (name) + { + case global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.First), global::System.StringComparison.Ordinal): + result = global::MyTestNameSpace.MyEnum.First; + return true; + case global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.Second), global::System.StringComparison.Ordinal): + result = global::MyTestNameSpace.MyEnum.Second; + return true; + case global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.Third), global::System.StringComparison.Ordinal): + result = global::MyTestNameSpace.MyEnum.Third; + return true; + case global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.Fourth), global::System.StringComparison.Ordinal): + result = global::MyTestNameSpace.MyEnum.Fourth; + return true; + case global::System.ReadOnlySpan current when int.TryParse(name, out var numericResult): + result = (global::MyTestNameSpace.MyEnum)numericResult; + return true; + default: + result = default; + return false; + } + } +#endif + + /// + /// Retrieves an array of the values of the members defined in + /// . + /// Note that this returns a new array with every invocation, so + /// should be cached if appropriate. + /// + /// An array of the values defined in + public static global::MyTestNameSpace.MyEnum[] GetValues() + { + return new[] + { + global::MyTestNameSpace.MyEnum.First, + global::MyTestNameSpace.MyEnum.Second, + global::MyTestNameSpace.MyEnum.Third, + global::MyTestNameSpace.MyEnum.Fourth, + }; + } + + /// + /// Retrieves an array of the underlying-values of the members defined in + /// . + /// Note that this returns a new array with every invocation, so + /// should be cached if appropriate. + /// + /// An array of the underlying-values defined in + public static int[] GetValuesAsUnderlyingType() + { + return new[] + { + (int) global::MyTestNameSpace.MyEnum.First, + (int) global::MyTestNameSpace.MyEnum.Second, + (int) global::MyTestNameSpace.MyEnum.Third, + (int) global::MyTestNameSpace.MyEnum.Fourth, + }; + } + + /// + /// Retrieves an array of the names of the members defined in + /// . + /// Note that this returns a new array with every invocation, so + /// should be cached if appropriate. + /// + /// An array of the names of the members defined in + public static string[] GetNames() + { + return new[] + { + nameof(global::MyTestNameSpace.MyEnum.First), + nameof(global::MyTestNameSpace.MyEnum.Second), + nameof(global::MyTestNameSpace.MyEnum.Third), + nameof(global::MyTestNameSpace.MyEnum.Fourth), + }; + } + } +#pragma warning restore CS0612 // Ignore usages of obsolete members or enums +#pragma warning restore CS0618 // Ignore usages of obsolete members or enums +} \ No newline at end of file diff --git a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateEnumExtensionsWithSameDisplayName.verified.txt b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateEnumExtensionsWithSameDisplayName__.verified.txt similarity index 92% rename from tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateEnumExtensionsWithSameDisplayName.verified.txt rename to tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateEnumExtensionsWithSameDisplayName__.verified.txt index 1dec877..bcb62e3 100644 --- a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateEnumExtensionsWithSameDisplayName.verified.txt +++ b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateEnumExtensionsWithSameDisplayName__.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the NetEscapades.EnumGenerators source generator // @@ -25,20 +25,6 @@ namespace MyTestNameSpace /// public const int Length = 4; - /// - /// Returns the string representation of the value. - /// If the attribute is decorated with a [Display] or [Description]attribute, then - /// uses the provided value. Otherwise uses the name of the member, equivalent to - /// calling ToString() on . - /// - /// The value to retrieve the string value for - /// If uses the value provided in the - /// [Display] or [Description]attribute as the string representation of the member. - /// If , always uses the name of the member, the same as if ToString() was called. - /// The string representation of the value - public static string ToStringFast(this global::MyTestNameSpace.MyEnum value, bool useMetadataAttributes) - => useMetadataAttributes ? value.ToStringFastWithMetadata() : value.ToStringFast(); - /// /// Returns the string representation of the value. /// Directly equivalent to calling ToString() on . @@ -55,6 +41,20 @@ namespace MyTestNameSpace _ => value.AsUnderlyingType().ToString(), }; + /// + /// Returns the string representation of the value. + /// If the member is decorated with the attribute + /// then that value is returned. Otherwise returns uses the name of the member, + /// equivalent to calling ToString() on . + /// + /// The value to retrieve the string value for + /// If uses the value provided in the + /// attribute as the string representation of the member. + /// If , always uses the name of the member, the same as if ToString() was called. + /// The string representation of the value + public static string ToStringFast(this global::MyTestNameSpace.MyEnum value, bool useMetadataAttributes) + => useMetadataAttributes ? value.ToStringFastWithMetadata() : value.ToStringFast(); + private static string ToStringFastWithMetadata(this global::MyTestNameSpace.MyEnum value) => value switch { @@ -95,43 +95,36 @@ namespace MyTestNameSpace /// /// The name to check if it's defined /// if a member with the name exists in the enumeration, otherwise - public static bool IsDefined(string name) => IsDefined(name, allowMatchingMetadataAttribute: false); + public static bool IsDefined(string name) + => name switch + { + nameof(global::MyTestNameSpace.MyEnum.First) => true, + nameof(global::MyTestNameSpace.MyEnum.Second) => true, + nameof(global::MyTestNameSpace.MyEnum.Third) => true, + nameof(global::MyTestNameSpace.MyEnum.Fourth) => true, + _ => false, + }; /// /// Returns a boolean telling whether an enum with the given name exists in the enumeration, - /// or if a member decorated with a [Display] attribute + /// or if a member decorated with /// with the required name exists. /// /// The name to check if it's defined - /// If , considers the value of metadata attributes,otherwise ignores them + /// If , + /// considers the value of + /// instead of the member name, otherwise ignores them /// if a member with the name exists in the enumeration, or a member is decorated /// with a [Display] attribute with the name, otherwise public static bool IsDefined(string name, bool allowMatchingMetadataAttribute) - { - var isDefinedInDisplayAttribute = false; - if (allowMatchingMetadataAttribute) - { - isDefinedInDisplayAttribute = name switch - { - "2nd" => true, - _ => false, - }; - } - - if (isDefinedInDisplayAttribute) - { - return true; - } + => allowMatchingMetadataAttribute ? IsMetadataNameDefined(name) : IsDefined(name); - return name switch + private static bool IsMetadataNameDefined(string name) + => name switch { - nameof(global::MyTestNameSpace.MyEnum.First) => true, - nameof(global::MyTestNameSpace.MyEnum.Second) => true, - nameof(global::MyTestNameSpace.MyEnum.Third) => true, - nameof(global::MyTestNameSpace.MyEnum.Fourth) => true, - _ => false, + "2nd" => true, + _ => IsDefined(name), }; - } #if NETCOREAPP && !NETCOREAPP2_0 && !NETCOREAPP1_1 && !NETCOREAPP1_0 /// @@ -139,44 +132,33 @@ namespace MyTestNameSpace /// /// The name to check if it's defined /// if a member with the name exists in the enumeration, otherwise - public static bool IsDefined(in global::System.ReadOnlySpan name) => IsDefined(name, allowMatchingMetadataAttribute: false); + public static bool IsDefined(in global::System.ReadOnlySpan name) + => name switch + { + global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.First), global::System.StringComparison.Ordinal) => true, + global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.Second), global::System.StringComparison.Ordinal) => true, + global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.Third), global::System.StringComparison.Ordinal) => true, + global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.Fourth), global::System.StringComparison.Ordinal) => true, + _ => false, + }; /// /// Returns a boolean telling whether an enum with the given name exists in the enumeration, - /// or optionally if a member decorated with a [Display] attribute - /// with the required name exists. - /// Slower then the overload, but doesn't allocate memory./> + /// or optionally if a member decorated with + /// exists. Slower then the overload, but doesn't allocate memory./> /// /// The name to check if it's defined /// If , considers the value of metadata attributes,otherwise ignores them /// if a member with the name exists in the enumeration, or a member is decorated /// with a [Display] attribute with the name, otherwise - public static bool IsDefined(in global::System.ReadOnlySpan name, bool allowMatchingMetadataAttribute) - { - var isDefinedInDisplayAttribute = false; - if (allowMatchingMetadataAttribute) - { - isDefinedInDisplayAttribute = name switch - { - global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, "2nd", global::System.StringComparison.Ordinal) => true, - _ => false, - }; - } - - if (isDefinedInDisplayAttribute) - { - return true; - } + public static bool IsDefined(in global::System.ReadOnlySpan name, bool allowMatchingMetadataAttribute) => allowMatchingMetadataAttribute ? IsMetadataNameDefined(in name) : IsDefined(in name); - return name switch + private static bool IsMetadataNameDefined(in global::System.ReadOnlySpan name) + => name switch { - global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.First), global::System.StringComparison.Ordinal) => true, - global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.Second), global::System.StringComparison.Ordinal) => true, - global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.Third), global::System.StringComparison.Ordinal) => true, - global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.Fourth), global::System.StringComparison.Ordinal) => true, - _ => false, + global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, "2nd", global::System.StringComparison.Ordinal) => true, + _ => IsDefined(name), }; - } #endif /// @@ -288,8 +270,9 @@ namespace MyTestNameSpace /// If the parse operation fails, contains the default value of the underlying type /// of . This parameter is passed uninitialized. /// to read value in case insensitive mode; to read value in case sensitive mode. - /// If , considers the value included in metadata attributes such as - /// [Display] attribute when parsing, otherwise only considers the member names. + /// If , + /// considers the value included in attribute + /// when parsing, otherwise only considers the member names. /// if the value parameter was converted successfully; otherwise, . public static bool TryParse( #if NETCOREAPP3_0_OR_GREATER @@ -426,8 +409,8 @@ namespace MyTestNameSpace /// /// The case-sensitive string representation of the enumeration name or underlying value to convert /// to read value in case insensitive mode; to read value in case sensitive mode. - /// If , considers the value included in metadata attributes such as - /// [Display] attribute when parsing, otherwise only considers the member names. + /// If , considers the value included in + /// attribute when parsing, otherwise only considers the member names. /// An object of type whose /// value is represented by public static global::MyTestNameSpace.MyEnum Parse( @@ -493,8 +476,8 @@ namespace MyTestNameSpace /// If the parse operation fails, contains the default value of the underlying type /// of . This parameter is passed uninitialized. /// to read value in case insensitive mode; to read value in case sensitive mode. - /// If , considers the value included in metadata attributes such as - /// [Display] attribute when parsing, otherwise only considers the member names. + /// If , considers the value included in + /// attribute when parsing, otherwise only considers the member names. /// if the value parameter was converted successfully; otherwise, . public static bool TryParse( #if NETCOREAPP3_0_OR_GREATER diff --git a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateEnumExtensionsWithWrongMetadataName__.verified.txt b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateEnumExtensionsWithWrongMetadataName__.verified.txt new file mode 100644 index 0000000..4f950e9 --- /dev/null +++ b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateEnumExtensionsWithWrongMetadataName__.verified.txt @@ -0,0 +1,569 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the NetEscapades.EnumGenerators source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#nullable enable + +namespace MyTestNameSpace +{ +#pragma warning disable CS0612 // Ignore usages of obsolete members or enums +#pragma warning disable CS0618 // Ignore usages of obsolete members or enums + /// + /// Extension methods for + /// + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("NetEscapades.EnumGenerators", "FIXED_VERSION")] + public static partial class MyEnumExtensions + { + /// + /// The number of members in the enum. + /// This is a non-distinct count of defined names. + /// + public const int Length = 4; + + /// + /// Returns the string representation of the value. + /// Directly equivalent to calling ToString() on . + /// + /// The value to retrieve the string value for + /// The string representation of the value, the same as that returned by ToString() + public static string ToStringFast(this global::MyTestNameSpace.MyEnum value) + => value switch + { + global::MyTestNameSpace.MyEnum.First => nameof(global::MyTestNameSpace.MyEnum.First), + global::MyTestNameSpace.MyEnum.Second => nameof(global::MyTestNameSpace.MyEnum.Second), + global::MyTestNameSpace.MyEnum.Third => nameof(global::MyTestNameSpace.MyEnum.Third), + global::MyTestNameSpace.MyEnum.Fourth => nameof(global::MyTestNameSpace.MyEnum.Fourth), + _ => value.AsUnderlyingType().ToString(), + }; + + /// + /// Returns the string representation of the value. + /// If the member is decorated with the attribute + /// then that value is returned. Otherwise returns uses the name of the member, + /// equivalent to calling ToString() on . + /// + /// The value to retrieve the string value for + /// If uses the value provided in the + /// attribute as the string representation of the member. + /// If , always uses the name of the member, the same as if ToString() was called. + /// The string representation of the value + public static string ToStringFast(this global::MyTestNameSpace.MyEnum value, bool useMetadataAttributes) + => useMetadataAttributes ? value.ToStringFastWithMetadata() : value.ToStringFast(); + + private static string ToStringFastWithMetadata(this global::MyTestNameSpace.MyEnum value) + => value.ToStringFast(); + + /// + /// Cast a value of to the underlying type (int). + /// This is mainly a convenience method. + /// + /// The value of cast to the underlying type. + public static int AsUnderlyingType(this global::MyTestNameSpace.MyEnum value) + { + return (int) value; + } + + /// + /// Returns a boolean telling whether the given enum value exists in the enumeration. + /// + /// The value to check if it's defined + /// if the value exists in the enumeration, otherwise + public static bool IsDefined(global::MyTestNameSpace.MyEnum value) + => value switch + { + global::MyTestNameSpace.MyEnum.First => true, + global::MyTestNameSpace.MyEnum.Second => true, + global::MyTestNameSpace.MyEnum.Third => true, + global::MyTestNameSpace.MyEnum.Fourth => true, + _ => false, + }; + + /// + /// Returns a boolean telling whether an enum with the given name exists in the enumeration. + /// + /// The name to check if it's defined + /// if a member with the name exists in the enumeration, otherwise + public static bool IsDefined(string name) + => name switch + { + nameof(global::MyTestNameSpace.MyEnum.First) => true, + nameof(global::MyTestNameSpace.MyEnum.Second) => true, + nameof(global::MyTestNameSpace.MyEnum.Third) => true, + nameof(global::MyTestNameSpace.MyEnum.Fourth) => true, + _ => false, + }; + + /// + /// Returns a boolean telling whether an enum with the given name exists in the enumeration, + /// or if a member decorated with + /// with the required name exists. + /// + /// The name to check if it's defined + /// If , + /// considers the value of + /// instead of the member name, otherwise ignores them + /// if a member with the name exists in the enumeration, or a member is decorated + /// with a [Display] attribute with the name, otherwise + public static bool IsDefined(string name, bool allowMatchingMetadataAttribute) + => IsDefined(name); + +#if NETCOREAPP && !NETCOREAPP2_0 && !NETCOREAPP1_1 && !NETCOREAPP1_0 + /// + /// Returns a boolean telling whether an enum with the given name exists in the enumeration + /// + /// The name to check if it's defined + /// if a member with the name exists in the enumeration, otherwise + public static bool IsDefined(in global::System.ReadOnlySpan name) + => name switch + { + global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.First), global::System.StringComparison.Ordinal) => true, + global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.Second), global::System.StringComparison.Ordinal) => true, + global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.Third), global::System.StringComparison.Ordinal) => true, + global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.Fourth), global::System.StringComparison.Ordinal) => true, + _ => false, + }; + + /// + /// Returns a boolean telling whether an enum with the given name exists in the enumeration, + /// or optionally if a member decorated with + /// exists. Slower then the overload, but doesn't allocate memory./> + /// + /// The name to check if it's defined + /// If , considers the value of metadata attributes,otherwise ignores them + /// if a member with the name exists in the enumeration, or a member is decorated + /// with a [Display] attribute with the name, otherwise + public static bool IsDefined(in global::System.ReadOnlySpan name, bool allowMatchingMetadataAttribute) + => IsDefined(name); +#endif + + /// + /// Converts the string representation of the name or numeric value of + /// an to the equivalent instance. + /// + /// The case-sensitive string representation of the enumeration name or underlying value to convert + /// An object of type whose + /// value is represented by + public static global::MyTestNameSpace.MyEnum Parse( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + string? name) + => TryParse(name, out var value, false, false) ? value : ThrowValueNotFound(name); + + /// + /// Converts the string representation of the name or numeric value of + /// an to the equivalent instance. + /// + /// The case-sensitive string representation of the enumeration name or underlying value to convert + /// to read value in case insensitive mode; to read value in case sensitive mode. + /// An object of type whose + /// value is represented by + public static global::MyTestNameSpace.MyEnum Parse( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + string? name, + bool ignoreCase) + => TryParse(name, out var value, ignoreCase, false) ? value : ThrowValueNotFound(name); + + /// + /// Converts the string representation of the name or numeric value of + /// an to the equivalent instance. + /// + /// The case-sensitive string representation of the enumeration name or underlying value to convert + /// to read value in case insensitive mode; to read value in case sensitive mode. + /// If , considers the value included in metadata attributes such as + /// [Display] attribute when parsing, otherwise only considers the member names. + /// An object of type whose + /// value is represented by + public static global::MyTestNameSpace.MyEnum Parse( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + string? name, + bool ignoreCase, + bool allowMatchingMetadataAttribute) + => TryParse(name, out var value, ignoreCase, allowMatchingMetadataAttribute) ? value : ThrowValueNotFound(name); + +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.DoesNotReturn] +#endif + private static global::MyTestNameSpace.MyEnum ThrowValueNotFound(string? name) + => throw new global::System.ArgumentException($"Requested value '{name}' was not found."); + + /// + /// Converts the string representation of the name or numeric value of + /// an to the equivalent instance. + /// The return value indicates whether the conversion succeeded. + /// + /// The case-sensitive string representation of the enumeration name or underlying value to convert + /// When this method returns, contains an object of type + /// whose + /// value is represented by if the parse operation succeeds. + /// If the parse operation fails, contains the default value of the underlying type + /// of . This parameter is passed uninitialized. + /// if the value parameter was converted successfully; otherwise, . + public static bool TryParse( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + string? name, + out global::MyTestNameSpace.MyEnum value) + => TryParse(name, out value, false, false); + + /// + /// Converts the string representation of the name or numeric value of + /// an to the equivalent instance. + /// The return value indicates whether the conversion succeeded. + /// + /// The string representation of the enumeration name or underlying value to convert + /// When this method returns, contains an object of type + /// whose + /// value is represented by if the parse operation succeeds. + /// If the parse operation fails, contains the default value of the underlying type + /// of . This parameter is passed uninitialized. + /// to read value in case insensitive mode; to read value in case sensitive mode. + /// if the value parameter was converted successfully; otherwise, . + public static bool TryParse( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + string? name, + out global::MyTestNameSpace.MyEnum value, + bool ignoreCase) + => TryParse(name, out value, ignoreCase, false); + + /// + /// Converts the string representation of the name or numeric value of + /// an to the equivalent instance. + /// The return value indicates whether the conversion succeeded. + /// + /// The string representation of the enumeration name or underlying value to convert + /// When this method returns, contains an object of type + /// whose + /// value is represented by if the parse operation succeeds. + /// If the parse operation fails, contains the default value of the underlying type + /// of . This parameter is passed uninitialized. + /// to read value in case insensitive mode; to read value in case sensitive mode. + /// If , + /// considers the value included in attribute + /// when parsing, otherwise only considers the member names. + /// if the value parameter was converted successfully; otherwise, . + public static bool TryParse( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + string? name, + out global::MyTestNameSpace.MyEnum value, + bool ignoreCase, + bool allowMatchingMetadataAttribute) + => ignoreCase + ? TryParseIgnoreCase(name, out value, allowMatchingMetadataAttribute) + : TryParseWithCase(name, out value, allowMatchingMetadataAttribute); + + private static bool TryParseIgnoreCase( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + string? name, + out global::MyTestNameSpace.MyEnum value, + bool allowMatchingMetadataAttribute) + { + switch (name) + { + case string s when s.Equals(nameof(global::MyTestNameSpace.MyEnum.First), global::System.StringComparison.OrdinalIgnoreCase): + value = global::MyTestNameSpace.MyEnum.First; + return true; + case string s when s.Equals(nameof(global::MyTestNameSpace.MyEnum.Second), global::System.StringComparison.OrdinalIgnoreCase): + value = global::MyTestNameSpace.MyEnum.Second; + return true; + case string s when s.Equals(nameof(global::MyTestNameSpace.MyEnum.Third), global::System.StringComparison.OrdinalIgnoreCase): + value = global::MyTestNameSpace.MyEnum.Third; + return true; + case string s when s.Equals(nameof(global::MyTestNameSpace.MyEnum.Fourth), global::System.StringComparison.OrdinalIgnoreCase): + value = global::MyTestNameSpace.MyEnum.Fourth; + return true; + case string s when int.TryParse(name, out var val): + value = (global::MyTestNameSpace.MyEnum)val; + return true; + default: + value = default; + return false; + } + } + + private static bool TryParseWithCase( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + string? name, + out global::MyTestNameSpace.MyEnum value, + bool allowMatchingMetadataAttribute) + { + switch (name) + { + case nameof(global::MyTestNameSpace.MyEnum.First): + value = global::MyTestNameSpace.MyEnum.First; + return true; + case nameof(global::MyTestNameSpace.MyEnum.Second): + value = global::MyTestNameSpace.MyEnum.Second; + return true; + case nameof(global::MyTestNameSpace.MyEnum.Third): + value = global::MyTestNameSpace.MyEnum.Third; + return true; + case nameof(global::MyTestNameSpace.MyEnum.Fourth): + value = global::MyTestNameSpace.MyEnum.Fourth; + return true; + case string s when int.TryParse(name, out var val): + value = (global::MyTestNameSpace.MyEnum)val; + return true; + default: + value = default; + return false; + } + } + +#if NETCOREAPP && !NETCOREAPP2_0 && !NETCOREAPP1_1 && !NETCOREAPP1_0 + /// + /// Converts the string representation of the name or numeric value of + /// an to the equivalent instance. + /// + /// The case-sensitive string representation of the enumeration name or underlying value to convert + /// An object of type whose + /// value is represented by + public static global::MyTestNameSpace.MyEnum Parse( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + in global::System.ReadOnlySpan name) + => TryParse(name, out var value, false, false) ? value : ThrowValueNotFound(name.ToString()); + + /// + /// Converts the string representation of the name or numeric value of + /// an to the equivalent instance. + /// + /// The case-sensitive string representation of the enumeration name or underlying value to convert + /// to read value in case insensitive mode; to read value in case sensitive mode. + /// An object of type whose + /// value is represented by + public static global::MyTestNameSpace.MyEnum Parse( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + in global::System.ReadOnlySpan name, + bool ignoreCase) + => TryParse(name, out var value, ignoreCase, false) ? value : ThrowValueNotFound(name.ToString()); + + /// + /// Converts the string representation of the name or numeric value of + /// an to the equivalent instance. + /// + /// The case-sensitive string representation of the enumeration name or underlying value to convert + /// to read value in case insensitive mode; to read value in case sensitive mode. + /// If , considers the value included in + /// attribute when parsing, otherwise only considers the member names. + /// An object of type whose + /// value is represented by + public static global::MyTestNameSpace.MyEnum Parse( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + in global::System.ReadOnlySpan name, + bool ignoreCase, + bool allowMatchingMetadataAttribute) + => TryParse(name, out var value, ignoreCase, allowMatchingMetadataAttribute) ? value : ThrowValueNotFound(name.ToString()); + + /// + /// Converts the span representation of the name or numeric value of + /// an to the equivalent instance. + /// The return value indicates whether the conversion succeeded. + /// + /// The span representation of the enumeration name or underlying value to convert + /// When this method returns, contains an object of type + /// whose + /// value is represented by if the parse operation succeeds. + /// If the parse operation fails, contains the default value of the underlying type + /// of . This parameter is passed uninitialized. + /// if the value parameter was converted successfully; otherwise, . + public static bool TryParse( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + in global::System.ReadOnlySpan name, + out global::MyTestNameSpace.MyEnum value) + => TryParse(name, out value, false, false); + + /// + /// Converts the span representation of the name or numeric value of + /// an to the equivalent instance. + /// The return value indicates whether the conversion succeeded. + /// + /// The span representation of the enumeration name or underlying value to convert + /// When this method returns, contains an object of type + /// whose + /// value is represented by if the parse operation succeeds. + /// If the parse operation fails, contains the default value of the underlying type + /// of . This parameter is passed uninitialized. + /// to read value in case insensitive mode; to read value in case sensitive mode. + /// if the value parameter was converted successfully; otherwise, . + public static bool TryParse( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + in global::System.ReadOnlySpan name, + out global::MyTestNameSpace.MyEnum value, + bool ignoreCase) + => TryParse(name, out value, ignoreCase, false); + + /// + /// Converts the span representation of the name or numeric value of + /// an to the equivalent instance. + /// The return value indicates whether the conversion succeeded. + /// + /// The span representation of the enumeration name or underlying value to convert + /// When this method returns, contains an object of type + /// whose + /// value is represented by if the parse operation succeeds. + /// If the parse operation fails, contains the default value of the underlying type + /// of . This parameter is passed uninitialized. + /// to read value in case insensitive mode; to read value in case sensitive mode. + /// If , considers the value included in + /// attribute when parsing, otherwise only considers the member names. + /// if the value parameter was converted successfully; otherwise, . + public static bool TryParse( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + in global::System.ReadOnlySpan name, + out global::MyTestNameSpace.MyEnum result, + bool ignoreCase, + bool allowMatchingMetadataAttribute) + => ignoreCase + ? TryParseIgnoreCase(in name, out result, allowMatchingMetadataAttribute) + : TryParseWithCase(in name, out result, allowMatchingMetadataAttribute); + + private static bool TryParseIgnoreCase( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + in global::System.ReadOnlySpan name, + out global::MyTestNameSpace.MyEnum result, + bool allowMatchingMetadataAttribute) + { + switch (name) + { + case global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.First), global::System.StringComparison.OrdinalIgnoreCase): + result = global::MyTestNameSpace.MyEnum.First; + return true; + case global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.Second), global::System.StringComparison.OrdinalIgnoreCase): + result = global::MyTestNameSpace.MyEnum.Second; + return true; + case global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.Third), global::System.StringComparison.OrdinalIgnoreCase): + result = global::MyTestNameSpace.MyEnum.Third; + return true; + case global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.Fourth), global::System.StringComparison.OrdinalIgnoreCase): + result = global::MyTestNameSpace.MyEnum.Fourth; + return true; + case global::System.ReadOnlySpan current when int.TryParse(name, out var numericResult): + result = (global::MyTestNameSpace.MyEnum)numericResult; + return true; + default: + result = default; + return false; + } + } + + private static bool TryParseWithCase( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + in global::System.ReadOnlySpan name, + out global::MyTestNameSpace.MyEnum result, + bool allowMatchingMetadataAttribute) + { + switch (name) + { + case global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.First), global::System.StringComparison.Ordinal): + result = global::MyTestNameSpace.MyEnum.First; + return true; + case global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.Second), global::System.StringComparison.Ordinal): + result = global::MyTestNameSpace.MyEnum.Second; + return true; + case global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.Third), global::System.StringComparison.Ordinal): + result = global::MyTestNameSpace.MyEnum.Third; + return true; + case global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.Fourth), global::System.StringComparison.Ordinal): + result = global::MyTestNameSpace.MyEnum.Fourth; + return true; + case global::System.ReadOnlySpan current when int.TryParse(name, out var numericResult): + result = (global::MyTestNameSpace.MyEnum)numericResult; + return true; + default: + result = default; + return false; + } + } +#endif + + /// + /// Retrieves an array of the values of the members defined in + /// . + /// Note that this returns a new array with every invocation, so + /// should be cached if appropriate. + /// + /// An array of the values defined in + public static global::MyTestNameSpace.MyEnum[] GetValues() + { + return new[] + { + global::MyTestNameSpace.MyEnum.First, + global::MyTestNameSpace.MyEnum.Second, + global::MyTestNameSpace.MyEnum.Third, + global::MyTestNameSpace.MyEnum.Fourth, + }; + } + + /// + /// Retrieves an array of the underlying-values of the members defined in + /// . + /// Note that this returns a new array with every invocation, so + /// should be cached if appropriate. + /// + /// An array of the underlying-values defined in + public static int[] GetValuesAsUnderlyingType() + { + return new[] + { + (int) global::MyTestNameSpace.MyEnum.First, + (int) global::MyTestNameSpace.MyEnum.Second, + (int) global::MyTestNameSpace.MyEnum.Third, + (int) global::MyTestNameSpace.MyEnum.Fourth, + }; + } + + /// + /// Retrieves an array of the names of the members defined in + /// . + /// Note that this returns a new array with every invocation, so + /// should be cached if appropriate. + /// + /// An array of the names of the members defined in + public static string[] GetNames() + { + return new[] + { + nameof(global::MyTestNameSpace.MyEnum.First), + nameof(global::MyTestNameSpace.MyEnum.Second), + nameof(global::MyTestNameSpace.MyEnum.Third), + nameof(global::MyTestNameSpace.MyEnum.Fourth), + }; + } + } +#pragma warning restore CS0612 // Ignore usages of obsolete members or enums +#pragma warning restore CS0618 // Ignore usages of obsolete members or enums +} \ No newline at end of file diff --git a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateExternalEnumExtensionsWithCustomName.verified.txt b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateExternalEnumExtensionsWithCustomName.verified.txt index f8fe225..8d08534 100644 --- a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateExternalEnumExtensionsWithCustomName.verified.txt +++ b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateExternalEnumExtensionsWithCustomName.verified.txt @@ -25,20 +25,6 @@ namespace System /// public const int Length = 3; - /// - /// Returns the string representation of the value. - /// If the attribute is decorated with a [Display] or [Description]attribute, then - /// uses the provided value. Otherwise uses the name of the member, equivalent to - /// calling ToString() on . - /// - /// The value to retrieve the string value for - /// If uses the value provided in the - /// [Display] or [Description]attribute as the string representation of the member. - /// If , always uses the name of the member, the same as if ToString() was called. - /// The string representation of the value - public static string ToStringFast(this global::System.DateTimeKind value, bool useMetadataAttributes) - => useMetadataAttributes ? value.ToStringFastWithMetadata() : value.ToStringFast(); - /// /// Returns the string representation of the value. /// Directly equivalent to calling ToString() on . @@ -54,6 +40,20 @@ namespace System _ => value.AsUnderlyingType().ToString(), }; + /// + /// Returns the string representation of the value. + /// If the member is decorated with the attribute + /// then that value is returned. Otherwise returns uses the name of the member, + /// equivalent to calling ToString() on . + /// + /// The value to retrieve the string value for + /// If uses the value provided in the + /// attribute as the string representation of the member. + /// If , always uses the name of the member, the same as if ToString() was called. + /// The string representation of the value + public static string ToStringFast(this global::System.DateTimeKind value, bool useMetadataAttributes) + => useMetadataAttributes ? value.ToStringFastWithMetadata() : value.ToStringFast(); + private static string ToStringFastWithMetadata(this global::System.DateTimeKind value) => value.ToStringFast(); @@ -86,27 +86,28 @@ namespace System /// /// The name to check if it's defined /// if a member with the name exists in the enumeration, otherwise - public static bool IsDefined(string name) => IsDefined(name, allowMatchingMetadataAttribute: false); + public static bool IsDefined(string name) + => name switch + { + nameof(global::System.DateTimeKind.Unspecified) => true, + nameof(global::System.DateTimeKind.Utc) => true, + nameof(global::System.DateTimeKind.Local) => true, + _ => false, + }; /// /// Returns a boolean telling whether an enum with the given name exists in the enumeration, - /// or if a member decorated with a [Display] attribute + /// or if a member decorated with /// with the required name exists. /// /// The name to check if it's defined - /// If , considers the value of metadata attributes,otherwise ignores them + /// If , + /// considers the value of + /// instead of the member name, otherwise ignores them /// if a member with the name exists in the enumeration, or a member is decorated /// with a [Display] attribute with the name, otherwise public static bool IsDefined(string name, bool allowMatchingMetadataAttribute) - { - return name switch - { - nameof(global::System.DateTimeKind.Unspecified) => true, - nameof(global::System.DateTimeKind.Utc) => true, - nameof(global::System.DateTimeKind.Local) => true, - _ => false, - }; - } + => IsDefined(name); #if NETCOREAPP && !NETCOREAPP2_0 && !NETCOREAPP1_1 && !NETCOREAPP1_0 /// @@ -114,28 +115,26 @@ namespace System /// /// The name to check if it's defined /// if a member with the name exists in the enumeration, otherwise - public static bool IsDefined(in global::System.ReadOnlySpan name) => IsDefined(name, allowMatchingMetadataAttribute: false); + public static bool IsDefined(in global::System.ReadOnlySpan name) + => name switch + { + global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::System.DateTimeKind.Unspecified), global::System.StringComparison.Ordinal) => true, + global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::System.DateTimeKind.Utc), global::System.StringComparison.Ordinal) => true, + global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::System.DateTimeKind.Local), global::System.StringComparison.Ordinal) => true, + _ => false, + }; /// /// Returns a boolean telling whether an enum with the given name exists in the enumeration, - /// or optionally if a member decorated with a [Display] attribute - /// with the required name exists. - /// Slower then the overload, but doesn't allocate memory./> + /// or optionally if a member decorated with + /// exists. Slower then the overload, but doesn't allocate memory./> /// /// The name to check if it's defined /// If , considers the value of metadata attributes,otherwise ignores them /// if a member with the name exists in the enumeration, or a member is decorated /// with a [Display] attribute with the name, otherwise public static bool IsDefined(in global::System.ReadOnlySpan name, bool allowMatchingMetadataAttribute) - { - return name switch - { - global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::System.DateTimeKind.Unspecified), global::System.StringComparison.Ordinal) => true, - global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::System.DateTimeKind.Utc), global::System.StringComparison.Ordinal) => true, - global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::System.DateTimeKind.Local), global::System.StringComparison.Ordinal) => true, - _ => false, - }; - } + => IsDefined(name); #endif /// @@ -247,8 +246,9 @@ namespace System /// If the parse operation fails, contains the default value of the underlying type /// of . This parameter is passed uninitialized. /// to read value in case insensitive mode; to read value in case sensitive mode. - /// If , considers the value included in metadata attributes such as - /// [Display] attribute when parsing, otherwise only considers the member names. + /// If , + /// considers the value included in attribute + /// when parsing, otherwise only considers the member names. /// if the value parameter was converted successfully; otherwise, . public static bool TryParse( #if NETCOREAPP3_0_OR_GREATER @@ -270,7 +270,6 @@ namespace System out global::System.DateTimeKind value, bool allowMatchingMetadataAttribute) { - switch (name) { case string s when s.Equals(nameof(global::System.DateTimeKind.Unspecified), global::System.StringComparison.OrdinalIgnoreCase): @@ -299,7 +298,6 @@ namespace System out global::System.DateTimeKind value, bool allowMatchingMetadataAttribute) { - switch (name) { case nameof(global::System.DateTimeKind.Unspecified): @@ -357,8 +355,8 @@ namespace System /// /// The case-sensitive string representation of the enumeration name or underlying value to convert /// to read value in case insensitive mode; to read value in case sensitive mode. - /// If , considers the value included in metadata attributes such as - /// [Display] attribute when parsing, otherwise only considers the member names. + /// If , considers the value included in + /// attribute when parsing, otherwise only considers the member names. /// An object of type whose /// value is represented by public static global::System.DateTimeKind Parse( @@ -424,8 +422,8 @@ namespace System /// If the parse operation fails, contains the default value of the underlying type /// of . This parameter is passed uninitialized. /// to read value in case insensitive mode; to read value in case sensitive mode. - /// If , considers the value included in metadata attributes such as - /// [Display] attribute when parsing, otherwise only considers the member names. + /// If , considers the value included in + /// attribute when parsing, otherwise only considers the member names. /// if the value parameter was converted successfully; otherwise, . public static bool TryParse( #if NETCOREAPP3_0_OR_GREATER diff --git a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateExternalEnumExtensionsWithCustomNamespace.verified.txt b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateExternalEnumExtensionsWithCustomNamespace.verified.txt index 3d85a85..9881f97 100644 --- a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateExternalEnumExtensionsWithCustomNamespace.verified.txt +++ b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateExternalEnumExtensionsWithCustomNamespace.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the NetEscapades.EnumGenerators source generator // @@ -25,20 +25,6 @@ namespace A.B /// public const int Length = 3; - /// - /// Returns the string representation of the value. - /// If the attribute is decorated with a [Display] or [Description]attribute, then - /// uses the provided value. Otherwise uses the name of the member, equivalent to - /// calling ToString() on . - /// - /// The value to retrieve the string value for - /// If uses the value provided in the - /// [Display] or [Description]attribute as the string representation of the member. - /// If , always uses the name of the member, the same as if ToString() was called. - /// The string representation of the value - public static string ToStringFast(this global::System.DateTimeKind value, bool useMetadataAttributes) - => useMetadataAttributes ? value.ToStringFastWithMetadata() : value.ToStringFast(); - /// /// Returns the string representation of the value. /// Directly equivalent to calling ToString() on . @@ -54,6 +40,20 @@ namespace A.B _ => value.AsUnderlyingType().ToString(), }; + /// + /// Returns the string representation of the value. + /// If the member is decorated with the attribute + /// then that value is returned. Otherwise returns uses the name of the member, + /// equivalent to calling ToString() on . + /// + /// The value to retrieve the string value for + /// If uses the value provided in the + /// attribute as the string representation of the member. + /// If , always uses the name of the member, the same as if ToString() was called. + /// The string representation of the value + public static string ToStringFast(this global::System.DateTimeKind value, bool useMetadataAttributes) + => useMetadataAttributes ? value.ToStringFastWithMetadata() : value.ToStringFast(); + private static string ToStringFastWithMetadata(this global::System.DateTimeKind value) => value.ToStringFast(); @@ -86,27 +86,28 @@ namespace A.B /// /// The name to check if it's defined /// if a member with the name exists in the enumeration, otherwise - public static bool IsDefined(string name) => IsDefined(name, allowMatchingMetadataAttribute: false); + public static bool IsDefined(string name) + => name switch + { + nameof(global::System.DateTimeKind.Unspecified) => true, + nameof(global::System.DateTimeKind.Utc) => true, + nameof(global::System.DateTimeKind.Local) => true, + _ => false, + }; /// /// Returns a boolean telling whether an enum with the given name exists in the enumeration, - /// or if a member decorated with a [Display] attribute + /// or if a member decorated with /// with the required name exists. /// /// The name to check if it's defined - /// If , considers the value of metadata attributes,otherwise ignores them + /// If , + /// considers the value of + /// instead of the member name, otherwise ignores them /// if a member with the name exists in the enumeration, or a member is decorated /// with a [Display] attribute with the name, otherwise public static bool IsDefined(string name, bool allowMatchingMetadataAttribute) - { - return name switch - { - nameof(global::System.DateTimeKind.Unspecified) => true, - nameof(global::System.DateTimeKind.Utc) => true, - nameof(global::System.DateTimeKind.Local) => true, - _ => false, - }; - } + => IsDefined(name); #if NETCOREAPP && !NETCOREAPP2_0 && !NETCOREAPP1_1 && !NETCOREAPP1_0 /// @@ -114,28 +115,26 @@ namespace A.B /// /// The name to check if it's defined /// if a member with the name exists in the enumeration, otherwise - public static bool IsDefined(in global::System.ReadOnlySpan name) => IsDefined(name, allowMatchingMetadataAttribute: false); + public static bool IsDefined(in global::System.ReadOnlySpan name) + => name switch + { + global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::System.DateTimeKind.Unspecified), global::System.StringComparison.Ordinal) => true, + global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::System.DateTimeKind.Utc), global::System.StringComparison.Ordinal) => true, + global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::System.DateTimeKind.Local), global::System.StringComparison.Ordinal) => true, + _ => false, + }; /// /// Returns a boolean telling whether an enum with the given name exists in the enumeration, - /// or optionally if a member decorated with a [Display] attribute - /// with the required name exists. - /// Slower then the overload, but doesn't allocate memory./> + /// or optionally if a member decorated with + /// exists. Slower then the overload, but doesn't allocate memory./> /// /// The name to check if it's defined /// If , considers the value of metadata attributes,otherwise ignores them /// if a member with the name exists in the enumeration, or a member is decorated /// with a [Display] attribute with the name, otherwise public static bool IsDefined(in global::System.ReadOnlySpan name, bool allowMatchingMetadataAttribute) - { - return name switch - { - global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::System.DateTimeKind.Unspecified), global::System.StringComparison.Ordinal) => true, - global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::System.DateTimeKind.Utc), global::System.StringComparison.Ordinal) => true, - global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::System.DateTimeKind.Local), global::System.StringComparison.Ordinal) => true, - _ => false, - }; - } + => IsDefined(name); #endif /// @@ -247,8 +246,9 @@ namespace A.B /// If the parse operation fails, contains the default value of the underlying type /// of . This parameter is passed uninitialized. /// to read value in case insensitive mode; to read value in case sensitive mode. - /// If , considers the value included in metadata attributes such as - /// [Display] attribute when parsing, otherwise only considers the member names. + /// If , + /// considers the value included in attribute + /// when parsing, otherwise only considers the member names. /// if the value parameter was converted successfully; otherwise, . public static bool TryParse( #if NETCOREAPP3_0_OR_GREATER @@ -270,7 +270,6 @@ namespace A.B out global::System.DateTimeKind value, bool allowMatchingMetadataAttribute) { - switch (name) { case string s when s.Equals(nameof(global::System.DateTimeKind.Unspecified), global::System.StringComparison.OrdinalIgnoreCase): @@ -299,7 +298,6 @@ namespace A.B out global::System.DateTimeKind value, bool allowMatchingMetadataAttribute) { - switch (name) { case nameof(global::System.DateTimeKind.Unspecified): @@ -357,8 +355,8 @@ namespace A.B /// /// The case-sensitive string representation of the enumeration name or underlying value to convert /// to read value in case insensitive mode; to read value in case sensitive mode. - /// If , considers the value included in metadata attributes such as - /// [Display] attribute when parsing, otherwise only considers the member names. + /// If , considers the value included in + /// attribute when parsing, otherwise only considers the member names. /// An object of type whose /// value is represented by public static global::System.DateTimeKind Parse( @@ -424,8 +422,8 @@ namespace A.B /// If the parse operation fails, contains the default value of the underlying type /// of . This parameter is passed uninitialized. /// to read value in case insensitive mode; to read value in case sensitive mode. - /// If , considers the value included in metadata attributes such as - /// [Display] attribute when parsing, otherwise only considers the member names. + /// If , considers the value included in + /// attribute when parsing, otherwise only considers the member names. /// if the value parameter was converted successfully; otherwise, . public static bool TryParse( #if NETCOREAPP3_0_OR_GREATER diff --git a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateExternalEnumExtensionsWithCustomNamespaceAndName.verified.txt b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateExternalEnumExtensionsWithCustomNamespaceAndName.verified.txt index 973e79d..9c0eaec 100644 --- a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateExternalEnumExtensionsWithCustomNamespaceAndName.verified.txt +++ b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateExternalEnumExtensionsWithCustomNamespaceAndName.verified.txt @@ -25,20 +25,6 @@ namespace A.B /// public const int Length = 3; - /// - /// Returns the string representation of the value. - /// If the attribute is decorated with a [Display] or [Description]attribute, then - /// uses the provided value. Otherwise uses the name of the member, equivalent to - /// calling ToString() on . - /// - /// The value to retrieve the string value for - /// If uses the value provided in the - /// [Display] or [Description]attribute as the string representation of the member. - /// If , always uses the name of the member, the same as if ToString() was called. - /// The string representation of the value - public static string ToStringFast(this global::System.DateTimeKind value, bool useMetadataAttributes) - => useMetadataAttributes ? value.ToStringFastWithMetadata() : value.ToStringFast(); - /// /// Returns the string representation of the value. /// Directly equivalent to calling ToString() on . @@ -54,6 +40,20 @@ namespace A.B _ => value.AsUnderlyingType().ToString(), }; + /// + /// Returns the string representation of the value. + /// If the member is decorated with the attribute + /// then that value is returned. Otherwise returns uses the name of the member, + /// equivalent to calling ToString() on . + /// + /// The value to retrieve the string value for + /// If uses the value provided in the + /// attribute as the string representation of the member. + /// If , always uses the name of the member, the same as if ToString() was called. + /// The string representation of the value + public static string ToStringFast(this global::System.DateTimeKind value, bool useMetadataAttributes) + => useMetadataAttributes ? value.ToStringFastWithMetadata() : value.ToStringFast(); + private static string ToStringFastWithMetadata(this global::System.DateTimeKind value) => value.ToStringFast(); @@ -86,27 +86,28 @@ namespace A.B /// /// The name to check if it's defined /// if a member with the name exists in the enumeration, otherwise - public static bool IsDefined(string name) => IsDefined(name, allowMatchingMetadataAttribute: false); + public static bool IsDefined(string name) + => name switch + { + nameof(global::System.DateTimeKind.Unspecified) => true, + nameof(global::System.DateTimeKind.Utc) => true, + nameof(global::System.DateTimeKind.Local) => true, + _ => false, + }; /// /// Returns a boolean telling whether an enum with the given name exists in the enumeration, - /// or if a member decorated with a [Display] attribute + /// or if a member decorated with /// with the required name exists. /// /// The name to check if it's defined - /// If , considers the value of metadata attributes,otherwise ignores them + /// If , + /// considers the value of + /// instead of the member name, otherwise ignores them /// if a member with the name exists in the enumeration, or a member is decorated /// with a [Display] attribute with the name, otherwise public static bool IsDefined(string name, bool allowMatchingMetadataAttribute) - { - return name switch - { - nameof(global::System.DateTimeKind.Unspecified) => true, - nameof(global::System.DateTimeKind.Utc) => true, - nameof(global::System.DateTimeKind.Local) => true, - _ => false, - }; - } + => IsDefined(name); #if NETCOREAPP && !NETCOREAPP2_0 && !NETCOREAPP1_1 && !NETCOREAPP1_0 /// @@ -114,28 +115,26 @@ namespace A.B /// /// The name to check if it's defined /// if a member with the name exists in the enumeration, otherwise - public static bool IsDefined(in global::System.ReadOnlySpan name) => IsDefined(name, allowMatchingMetadataAttribute: false); + public static bool IsDefined(in global::System.ReadOnlySpan name) + => name switch + { + global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::System.DateTimeKind.Unspecified), global::System.StringComparison.Ordinal) => true, + global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::System.DateTimeKind.Utc), global::System.StringComparison.Ordinal) => true, + global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::System.DateTimeKind.Local), global::System.StringComparison.Ordinal) => true, + _ => false, + }; /// /// Returns a boolean telling whether an enum with the given name exists in the enumeration, - /// or optionally if a member decorated with a [Display] attribute - /// with the required name exists. - /// Slower then the overload, but doesn't allocate memory./> + /// or optionally if a member decorated with + /// exists. Slower then the overload, but doesn't allocate memory./> /// /// The name to check if it's defined /// If , considers the value of metadata attributes,otherwise ignores them /// if a member with the name exists in the enumeration, or a member is decorated /// with a [Display] attribute with the name, otherwise public static bool IsDefined(in global::System.ReadOnlySpan name, bool allowMatchingMetadataAttribute) - { - return name switch - { - global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::System.DateTimeKind.Unspecified), global::System.StringComparison.Ordinal) => true, - global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::System.DateTimeKind.Utc), global::System.StringComparison.Ordinal) => true, - global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::System.DateTimeKind.Local), global::System.StringComparison.Ordinal) => true, - _ => false, - }; - } + => IsDefined(name); #endif /// @@ -247,8 +246,9 @@ namespace A.B /// If the parse operation fails, contains the default value of the underlying type /// of . This parameter is passed uninitialized. /// to read value in case insensitive mode; to read value in case sensitive mode. - /// If , considers the value included in metadata attributes such as - /// [Display] attribute when parsing, otherwise only considers the member names. + /// If , + /// considers the value included in attribute + /// when parsing, otherwise only considers the member names. /// if the value parameter was converted successfully; otherwise, . public static bool TryParse( #if NETCOREAPP3_0_OR_GREATER @@ -270,7 +270,6 @@ namespace A.B out global::System.DateTimeKind value, bool allowMatchingMetadataAttribute) { - switch (name) { case string s when s.Equals(nameof(global::System.DateTimeKind.Unspecified), global::System.StringComparison.OrdinalIgnoreCase): @@ -299,7 +298,6 @@ namespace A.B out global::System.DateTimeKind value, bool allowMatchingMetadataAttribute) { - switch (name) { case nameof(global::System.DateTimeKind.Unspecified): @@ -357,8 +355,8 @@ namespace A.B /// /// The case-sensitive string representation of the enumeration name or underlying value to convert /// to read value in case insensitive mode; to read value in case sensitive mode. - /// If , considers the value included in metadata attributes such as - /// [Display] attribute when parsing, otherwise only considers the member names. + /// If , considers the value included in + /// attribute when parsing, otherwise only considers the member names. /// An object of type whose /// value is represented by public static global::System.DateTimeKind Parse( @@ -424,8 +422,8 @@ namespace A.B /// If the parse operation fails, contains the default value of the underlying type /// of . This parameter is passed uninitialized. /// to read value in case insensitive mode; to read value in case sensitive mode. - /// If , considers the value included in metadata attributes such as - /// [Display] attribute when parsing, otherwise only considers the member names. + /// If , considers the value included in + /// attribute when parsing, otherwise only considers the member names. /// if the value parameter was converted successfully; otherwise, . public static bool TryParse( #if NETCOREAPP3_0_OR_GREATER diff --git a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateForExternalEnum.verified.txt b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateForExternalEnum.verified.txt index 6b27c51..4623763 100644 --- a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateForExternalEnum.verified.txt +++ b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateForExternalEnum.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the NetEscapades.EnumGenerators source generator // @@ -25,20 +25,6 @@ namespace System /// public const int Length = 6; - /// - /// Returns the string representation of the value. - /// If the attribute is decorated with a [Display] or [Description]attribute, then - /// uses the provided value. Otherwise uses the name of the member, equivalent to - /// calling ToString() on . - /// - /// The value to retrieve the string value for - /// If uses the value provided in the - /// [Display] or [Description]attribute as the string representation of the member. - /// If , always uses the name of the member, the same as if ToString() was called. - /// The string representation of the value - public static string ToStringFast(this global::System.StringComparison value, bool useMetadataAttributes) - => useMetadataAttributes ? value.ToStringFastWithMetadata() : value.ToStringFast(); - /// /// Returns the string representation of the value. /// Directly equivalent to calling ToString() on . @@ -57,6 +43,20 @@ namespace System _ => value.AsUnderlyingType().ToString(), }; + /// + /// Returns the string representation of the value. + /// If the member is decorated with the attribute + /// then that value is returned. Otherwise returns uses the name of the member, + /// equivalent to calling ToString() on . + /// + /// The value to retrieve the string value for + /// If uses the value provided in the + /// attribute as the string representation of the member. + /// If , always uses the name of the member, the same as if ToString() was called. + /// The string representation of the value + public static string ToStringFast(this global::System.StringComparison value, bool useMetadataAttributes) + => useMetadataAttributes ? value.ToStringFastWithMetadata() : value.ToStringFast(); + private static string ToStringFastWithMetadata(this global::System.StringComparison value) => value.ToStringFast(); @@ -92,30 +92,31 @@ namespace System /// /// The name to check if it's defined /// if a member with the name exists in the enumeration, otherwise - public static bool IsDefined(string name) => IsDefined(name, allowMatchingMetadataAttribute: false); + public static bool IsDefined(string name) + => name switch + { + nameof(global::System.StringComparison.CurrentCulture) => true, + nameof(global::System.StringComparison.CurrentCultureIgnoreCase) => true, + nameof(global::System.StringComparison.InvariantCulture) => true, + nameof(global::System.StringComparison.InvariantCultureIgnoreCase) => true, + nameof(global::System.StringComparison.Ordinal) => true, + nameof(global::System.StringComparison.OrdinalIgnoreCase) => true, + _ => false, + }; /// /// Returns a boolean telling whether an enum with the given name exists in the enumeration, - /// or if a member decorated with a [Display] attribute + /// or if a member decorated with /// with the required name exists. /// /// The name to check if it's defined - /// If , considers the value of metadata attributes,otherwise ignores them + /// If , + /// considers the value of + /// instead of the member name, otherwise ignores them /// if a member with the name exists in the enumeration, or a member is decorated /// with a [Display] attribute with the name, otherwise public static bool IsDefined(string name, bool allowMatchingMetadataAttribute) - { - return name switch - { - nameof(global::System.StringComparison.CurrentCulture) => true, - nameof(global::System.StringComparison.CurrentCultureIgnoreCase) => true, - nameof(global::System.StringComparison.InvariantCulture) => true, - nameof(global::System.StringComparison.InvariantCultureIgnoreCase) => true, - nameof(global::System.StringComparison.Ordinal) => true, - nameof(global::System.StringComparison.OrdinalIgnoreCase) => true, - _ => false, - }; - } + => IsDefined(name); #if NETCOREAPP && !NETCOREAPP2_0 && !NETCOREAPP1_1 && !NETCOREAPP1_0 /// @@ -123,21 +124,8 @@ namespace System /// /// The name to check if it's defined /// if a member with the name exists in the enumeration, otherwise - public static bool IsDefined(in global::System.ReadOnlySpan name) => IsDefined(name, allowMatchingMetadataAttribute: false); - - /// - /// Returns a boolean telling whether an enum with the given name exists in the enumeration, - /// or optionally if a member decorated with a [Display] attribute - /// with the required name exists. - /// Slower then the overload, but doesn't allocate memory./> - /// - /// The name to check if it's defined - /// If , considers the value of metadata attributes,otherwise ignores them - /// if a member with the name exists in the enumeration, or a member is decorated - /// with a [Display] attribute with the name, otherwise - public static bool IsDefined(in global::System.ReadOnlySpan name, bool allowMatchingMetadataAttribute) - { - return name switch + public static bool IsDefined(in global::System.ReadOnlySpan name) + => name switch { global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::System.StringComparison.CurrentCulture), global::System.StringComparison.Ordinal) => true, global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::System.StringComparison.CurrentCultureIgnoreCase), global::System.StringComparison.Ordinal) => true, @@ -147,7 +135,18 @@ namespace System global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::System.StringComparison.OrdinalIgnoreCase), global::System.StringComparison.Ordinal) => true, _ => false, }; - } + + /// + /// Returns a boolean telling whether an enum with the given name exists in the enumeration, + /// or optionally if a member decorated with + /// exists. Slower then the overload, but doesn't allocate memory./> + /// + /// The name to check if it's defined + /// If , considers the value of metadata attributes,otherwise ignores them + /// if a member with the name exists in the enumeration, or a member is decorated + /// with a [Display] attribute with the name, otherwise + public static bool IsDefined(in global::System.ReadOnlySpan name, bool allowMatchingMetadataAttribute) + => IsDefined(name); #endif /// @@ -259,8 +258,9 @@ namespace System /// If the parse operation fails, contains the default value of the underlying type /// of . This parameter is passed uninitialized. /// to read value in case insensitive mode; to read value in case sensitive mode. - /// If , considers the value included in metadata attributes such as - /// [Display] attribute when parsing, otherwise only considers the member names. + /// If , + /// considers the value included in attribute + /// when parsing, otherwise only considers the member names. /// if the value parameter was converted successfully; otherwise, . public static bool TryParse( #if NETCOREAPP3_0_OR_GREATER @@ -282,7 +282,6 @@ namespace System out global::System.StringComparison value, bool allowMatchingMetadataAttribute) { - switch (name) { case string s when s.Equals(nameof(global::System.StringComparison.CurrentCulture), global::System.StringComparison.OrdinalIgnoreCase): @@ -320,7 +319,6 @@ namespace System out global::System.StringComparison value, bool allowMatchingMetadataAttribute) { - switch (name) { case nameof(global::System.StringComparison.CurrentCulture): @@ -387,8 +385,8 @@ namespace System /// /// The case-sensitive string representation of the enumeration name or underlying value to convert /// to read value in case insensitive mode; to read value in case sensitive mode. - /// If , considers the value included in metadata attributes such as - /// [Display] attribute when parsing, otherwise only considers the member names. + /// If , considers the value included in + /// attribute when parsing, otherwise only considers the member names. /// An object of type whose /// value is represented by public static global::System.StringComparison Parse( @@ -454,8 +452,8 @@ namespace System /// If the parse operation fails, contains the default value of the underlying type /// of . This parameter is passed uninitialized. /// to read value in case insensitive mode; to read value in case sensitive mode. - /// If , considers the value included in metadata attributes such as - /// [Display] attribute when parsing, otherwise only considers the member names. + /// If , considers the value included in + /// attribute when parsing, otherwise only considers the member names. /// if the value parameter was converted successfully; otherwise, . public static bool TryParse( #if NETCOREAPP3_0_OR_GREATER diff --git a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateForExternalFlagsEnum.verified.txt b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateForExternalFlagsEnum.verified.txt index f778f85..e1f1dbe 100644 --- a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateForExternalFlagsEnum.verified.txt +++ b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateForExternalFlagsEnum.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the NetEscapades.EnumGenerators source generator // @@ -25,20 +25,6 @@ namespace System.IO /// public const int Length = 6; - /// - /// Returns the string representation of the value. - /// If the attribute is decorated with a [Display] or [Description]attribute, then - /// uses the provided value. Otherwise uses the name of the member, equivalent to - /// calling ToString() on . - /// - /// The value to retrieve the string value for - /// If uses the value provided in the - /// [Display] or [Description]attribute as the string representation of the member. - /// If , always uses the name of the member, the same as if ToString() was called. - /// The string representation of the value - public static string ToStringFast(this global::System.IO.FileShare value, bool useMetadataAttributes) - => useMetadataAttributes ? value.ToStringFastWithMetadata() : value.ToStringFast(); - /// /// Returns the string representation of the value. /// Directly equivalent to calling ToString() on . @@ -57,6 +43,20 @@ namespace System.IO _ => value.ToString(), }; + /// + /// Returns the string representation of the value. + /// If the member is decorated with the attribute + /// then that value is returned. Otherwise returns uses the name of the member, + /// equivalent to calling ToString() on . + /// + /// The value to retrieve the string value for + /// If uses the value provided in the + /// attribute as the string representation of the member. + /// If , always uses the name of the member, the same as if ToString() was called. + /// The string representation of the value + public static string ToStringFast(this global::System.IO.FileShare value, bool useMetadataAttributes) + => useMetadataAttributes ? value.ToStringFastWithMetadata() : value.ToStringFast(); + private static string ToStringFastWithMetadata(this global::System.IO.FileShare value) => value.ToStringFast(); @@ -104,30 +104,31 @@ namespace System.IO /// /// The name to check if it's defined /// if a member with the name exists in the enumeration, otherwise - public static bool IsDefined(string name) => IsDefined(name, allowMatchingMetadataAttribute: false); + public static bool IsDefined(string name) + => name switch + { + nameof(global::System.IO.FileShare.None) => true, + nameof(global::System.IO.FileShare.Read) => true, + nameof(global::System.IO.FileShare.Write) => true, + nameof(global::System.IO.FileShare.ReadWrite) => true, + nameof(global::System.IO.FileShare.Delete) => true, + nameof(global::System.IO.FileShare.Inheritable) => true, + _ => false, + }; /// /// Returns a boolean telling whether an enum with the given name exists in the enumeration, - /// or if a member decorated with a [Display] attribute + /// or if a member decorated with /// with the required name exists. /// /// The name to check if it's defined - /// If , considers the value of metadata attributes,otherwise ignores them + /// If , + /// considers the value of + /// instead of the member name, otherwise ignores them /// if a member with the name exists in the enumeration, or a member is decorated /// with a [Display] attribute with the name, otherwise public static bool IsDefined(string name, bool allowMatchingMetadataAttribute) - { - return name switch - { - nameof(global::System.IO.FileShare.None) => true, - nameof(global::System.IO.FileShare.Read) => true, - nameof(global::System.IO.FileShare.Write) => true, - nameof(global::System.IO.FileShare.ReadWrite) => true, - nameof(global::System.IO.FileShare.Delete) => true, - nameof(global::System.IO.FileShare.Inheritable) => true, - _ => false, - }; - } + => IsDefined(name); #if NETCOREAPP && !NETCOREAPP2_0 && !NETCOREAPP1_1 && !NETCOREAPP1_0 /// @@ -135,21 +136,8 @@ namespace System.IO /// /// The name to check if it's defined /// if a member with the name exists in the enumeration, otherwise - public static bool IsDefined(in global::System.ReadOnlySpan name) => IsDefined(name, allowMatchingMetadataAttribute: false); - - /// - /// Returns a boolean telling whether an enum with the given name exists in the enumeration, - /// or optionally if a member decorated with a [Display] attribute - /// with the required name exists. - /// Slower then the overload, but doesn't allocate memory./> - /// - /// The name to check if it's defined - /// If , considers the value of metadata attributes,otherwise ignores them - /// if a member with the name exists in the enumeration, or a member is decorated - /// with a [Display] attribute with the name, otherwise - public static bool IsDefined(in global::System.ReadOnlySpan name, bool allowMatchingMetadataAttribute) - { - return name switch + public static bool IsDefined(in global::System.ReadOnlySpan name) + => name switch { global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::System.IO.FileShare.None), global::System.StringComparison.Ordinal) => true, global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::System.IO.FileShare.Read), global::System.StringComparison.Ordinal) => true, @@ -159,7 +147,18 @@ namespace System.IO global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::System.IO.FileShare.Inheritable), global::System.StringComparison.Ordinal) => true, _ => false, }; - } + + /// + /// Returns a boolean telling whether an enum with the given name exists in the enumeration, + /// or optionally if a member decorated with + /// exists. Slower then the overload, but doesn't allocate memory./> + /// + /// The name to check if it's defined + /// If , considers the value of metadata attributes,otherwise ignores them + /// if a member with the name exists in the enumeration, or a member is decorated + /// with a [Display] attribute with the name, otherwise + public static bool IsDefined(in global::System.ReadOnlySpan name, bool allowMatchingMetadataAttribute) + => IsDefined(name); #endif /// @@ -271,8 +270,9 @@ namespace System.IO /// If the parse operation fails, contains the default value of the underlying type /// of . This parameter is passed uninitialized. /// to read value in case insensitive mode; to read value in case sensitive mode. - /// If , considers the value included in metadata attributes such as - /// [Display] attribute when parsing, otherwise only considers the member names. + /// If , + /// considers the value included in attribute + /// when parsing, otherwise only considers the member names. /// if the value parameter was converted successfully; otherwise, . public static bool TryParse( #if NETCOREAPP3_0_OR_GREATER @@ -294,7 +294,6 @@ namespace System.IO out global::System.IO.FileShare value, bool allowMatchingMetadataAttribute) { - switch (name) { case string s when s.Equals(nameof(global::System.IO.FileShare.None), global::System.StringComparison.OrdinalIgnoreCase): @@ -332,7 +331,6 @@ namespace System.IO out global::System.IO.FileShare value, bool allowMatchingMetadataAttribute) { - switch (name) { case nameof(global::System.IO.FileShare.None): @@ -399,8 +397,8 @@ namespace System.IO /// /// The case-sensitive string representation of the enumeration name or underlying value to convert /// to read value in case insensitive mode; to read value in case sensitive mode. - /// If , considers the value included in metadata attributes such as - /// [Display] attribute when parsing, otherwise only considers the member names. + /// If , considers the value included in + /// attribute when parsing, otherwise only considers the member names. /// An object of type whose /// value is represented by public static global::System.IO.FileShare Parse( @@ -466,8 +464,8 @@ namespace System.IO /// If the parse operation fails, contains the default value of the underlying type /// of . This parameter is passed uninitialized. /// to read value in case insensitive mode; to read value in case sensitive mode. - /// If , considers the value included in metadata attributes such as - /// [Display] attribute when parsing, otherwise only considers the member names. + /// If , considers the value included in + /// attribute when parsing, otherwise only considers the member names. /// if the value parameter was converted successfully; otherwise, . public static bool TryParse( #if NETCOREAPP3_0_OR_GREATER diff --git a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateForMultipleExternalEnums.verified.txt b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateForMultipleExternalEnums.verified.txt index 577e496..849ef73 100644 --- a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateForMultipleExternalEnums.verified.txt +++ b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateForMultipleExternalEnums.verified.txt @@ -1,4 +1,4 @@ -[ +[ //------------------------------------------------------------------------------ // // This code was generated by the NetEscapades.EnumGenerators source generator @@ -26,20 +26,6 @@ namespace System /// public const int Length = 16; - /// - /// Returns the string representation of the value. - /// If the attribute is decorated with a [Display] or [Description]attribute, then - /// uses the provided value. Otherwise uses the name of the member, equivalent to - /// calling ToString() on . - /// - /// The value to retrieve the string value for - /// If uses the value provided in the - /// [Display] or [Description]attribute as the string representation of the member. - /// If , always uses the name of the member, the same as if ToString() was called. - /// The string representation of the value - public static string ToStringFast(this global::System.ConsoleColor value, bool useMetadataAttributes) - => useMetadataAttributes ? value.ToStringFastWithMetadata() : value.ToStringFast(); - /// /// Returns the string representation of the value. /// Directly equivalent to calling ToString() on . @@ -68,6 +54,20 @@ namespace System _ => value.AsUnderlyingType().ToString(), }; + /// + /// Returns the string representation of the value. + /// If the member is decorated with the attribute + /// then that value is returned. Otherwise returns uses the name of the member, + /// equivalent to calling ToString() on . + /// + /// The value to retrieve the string value for + /// If uses the value provided in the + /// attribute as the string representation of the member. + /// If , always uses the name of the member, the same as if ToString() was called. + /// The string representation of the value + public static string ToStringFast(this global::System.ConsoleColor value, bool useMetadataAttributes) + => useMetadataAttributes ? value.ToStringFastWithMetadata() : value.ToStringFast(); + private static string ToStringFastWithMetadata(this global::System.ConsoleColor value) => value.ToStringFast(); @@ -113,20 +113,8 @@ namespace System /// /// The name to check if it's defined /// if a member with the name exists in the enumeration, otherwise - public static bool IsDefined(string name) => IsDefined(name, allowMatchingMetadataAttribute: false); - - /// - /// Returns a boolean telling whether an enum with the given name exists in the enumeration, - /// or if a member decorated with a [Display] attribute - /// with the required name exists. - /// - /// The name to check if it's defined - /// If , considers the value of metadata attributes,otherwise ignores them - /// if a member with the name exists in the enumeration, or a member is decorated - /// with a [Display] attribute with the name, otherwise - public static bool IsDefined(string name, bool allowMatchingMetadataAttribute) - { - return name switch + public static bool IsDefined(string name) + => name switch { nameof(global::System.ConsoleColor.Black) => true, nameof(global::System.ConsoleColor.DarkBlue) => true, @@ -144,31 +132,31 @@ namespace System nameof(global::System.ConsoleColor.Magenta) => true, nameof(global::System.ConsoleColor.Yellow) => true, nameof(global::System.ConsoleColor.White) => true, - _ => false, - }; - } - -#if NETCOREAPP && !NETCOREAPP2_0 && !NETCOREAPP1_1 && !NETCOREAPP1_0 - /// - /// Returns a boolean telling whether an enum with the given name exists in the enumeration - /// - /// The name to check if it's defined - /// if a member with the name exists in the enumeration, otherwise - public static bool IsDefined(in global::System.ReadOnlySpan name) => IsDefined(name, allowMatchingMetadataAttribute: false); + _ => false, + }; /// /// Returns a boolean telling whether an enum with the given name exists in the enumeration, - /// or optionally if a member decorated with a [Display] attribute + /// or if a member decorated with /// with the required name exists. - /// Slower then the overload, but doesn't allocate memory./> /// /// The name to check if it's defined - /// If , considers the value of metadata attributes,otherwise ignores them + /// If , + /// considers the value of + /// instead of the member name, otherwise ignores them /// if a member with the name exists in the enumeration, or a member is decorated /// with a [Display] attribute with the name, otherwise - public static bool IsDefined(in global::System.ReadOnlySpan name, bool allowMatchingMetadataAttribute) - { - return name switch + public static bool IsDefined(string name, bool allowMatchingMetadataAttribute) + => IsDefined(name); + +#if NETCOREAPP && !NETCOREAPP2_0 && !NETCOREAPP1_1 && !NETCOREAPP1_0 + /// + /// Returns a boolean telling whether an enum with the given name exists in the enumeration + /// + /// The name to check if it's defined + /// if a member with the name exists in the enumeration, otherwise + public static bool IsDefined(in global::System.ReadOnlySpan name) + => name switch { global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::System.ConsoleColor.Black), global::System.StringComparison.Ordinal) => true, global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::System.ConsoleColor.DarkBlue), global::System.StringComparison.Ordinal) => true, @@ -188,7 +176,18 @@ namespace System global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::System.ConsoleColor.White), global::System.StringComparison.Ordinal) => true, _ => false, }; - } + + /// + /// Returns a boolean telling whether an enum with the given name exists in the enumeration, + /// or optionally if a member decorated with + /// exists. Slower then the overload, but doesn't allocate memory./> + /// + /// The name to check if it's defined + /// If , considers the value of metadata attributes,otherwise ignores them + /// if a member with the name exists in the enumeration, or a member is decorated + /// with a [Display] attribute with the name, otherwise + public static bool IsDefined(in global::System.ReadOnlySpan name, bool allowMatchingMetadataAttribute) + => IsDefined(name); #endif /// @@ -300,8 +299,9 @@ namespace System /// If the parse operation fails, contains the default value of the underlying type /// of . This parameter is passed uninitialized. /// to read value in case insensitive mode; to read value in case sensitive mode. - /// If , considers the value included in metadata attributes such as - /// [Display] attribute when parsing, otherwise only considers the member names. + /// If , + /// considers the value included in attribute + /// when parsing, otherwise only considers the member names. /// if the value parameter was converted successfully; otherwise, . public static bool TryParse( #if NETCOREAPP3_0_OR_GREATER @@ -323,7 +323,6 @@ namespace System out global::System.ConsoleColor value, bool allowMatchingMetadataAttribute) { - switch (name) { case string s when s.Equals(nameof(global::System.ConsoleColor.Black), global::System.StringComparison.OrdinalIgnoreCase): @@ -391,7 +390,6 @@ namespace System out global::System.ConsoleColor value, bool allowMatchingMetadataAttribute) { - switch (name) { case nameof(global::System.ConsoleColor.Black): @@ -488,8 +486,8 @@ namespace System /// /// The case-sensitive string representation of the enumeration name or underlying value to convert /// to read value in case insensitive mode; to read value in case sensitive mode. - /// If , considers the value included in metadata attributes such as - /// [Display] attribute when parsing, otherwise only considers the member names. + /// If , considers the value included in + /// attribute when parsing, otherwise only considers the member names. /// An object of type whose /// value is represented by public static global::System.ConsoleColor Parse( @@ -555,8 +553,8 @@ namespace System /// If the parse operation fails, contains the default value of the underlying type /// of . This parameter is passed uninitialized. /// to read value in case insensitive mode; to read value in case sensitive mode. - /// If , considers the value included in metadata attributes such as - /// [Display] attribute when parsing, otherwise only considers the member names. + /// If , considers the value included in + /// attribute when parsing, otherwise only considers the member names. /// if the value parameter was converted successfully; otherwise, . public static bool TryParse( #if NETCOREAPP3_0_OR_GREATER @@ -826,20 +824,6 @@ namespace System /// public const int Length = 3; - /// - /// Returns the string representation of the value. - /// If the attribute is decorated with a [Display] or [Description]attribute, then - /// uses the provided value. Otherwise uses the name of the member, equivalent to - /// calling ToString() on . - /// - /// The value to retrieve the string value for - /// If uses the value provided in the - /// [Display] or [Description]attribute as the string representation of the member. - /// If , always uses the name of the member, the same as if ToString() was called. - /// The string representation of the value - public static string ToStringFast(this global::System.DateTimeKind value, bool useMetadataAttributes) - => useMetadataAttributes ? value.ToStringFastWithMetadata() : value.ToStringFast(); - /// /// Returns the string representation of the value. /// Directly equivalent to calling ToString() on . @@ -855,6 +839,20 @@ namespace System _ => value.AsUnderlyingType().ToString(), }; + /// + /// Returns the string representation of the value. + /// If the member is decorated with the attribute + /// then that value is returned. Otherwise returns uses the name of the member, + /// equivalent to calling ToString() on . + /// + /// The value to retrieve the string value for + /// If uses the value provided in the + /// attribute as the string representation of the member. + /// If , always uses the name of the member, the same as if ToString() was called. + /// The string representation of the value + public static string ToStringFast(this global::System.DateTimeKind value, bool useMetadataAttributes) + => useMetadataAttributes ? value.ToStringFastWithMetadata() : value.ToStringFast(); + private static string ToStringFastWithMetadata(this global::System.DateTimeKind value) => value.ToStringFast(); @@ -887,27 +885,28 @@ namespace System /// /// The name to check if it's defined /// if a member with the name exists in the enumeration, otherwise - public static bool IsDefined(string name) => IsDefined(name, allowMatchingMetadataAttribute: false); + public static bool IsDefined(string name) + => name switch + { + nameof(global::System.DateTimeKind.Unspecified) => true, + nameof(global::System.DateTimeKind.Utc) => true, + nameof(global::System.DateTimeKind.Local) => true, + _ => false, + }; /// /// Returns a boolean telling whether an enum with the given name exists in the enumeration, - /// or if a member decorated with a [Display] attribute + /// or if a member decorated with /// with the required name exists. /// /// The name to check if it's defined - /// If , considers the value of metadata attributes,otherwise ignores them + /// If , + /// considers the value of + /// instead of the member name, otherwise ignores them /// if a member with the name exists in the enumeration, or a member is decorated /// with a [Display] attribute with the name, otherwise public static bool IsDefined(string name, bool allowMatchingMetadataAttribute) - { - return name switch - { - nameof(global::System.DateTimeKind.Unspecified) => true, - nameof(global::System.DateTimeKind.Utc) => true, - nameof(global::System.DateTimeKind.Local) => true, - _ => false, - }; - } + => IsDefined(name); #if NETCOREAPP && !NETCOREAPP2_0 && !NETCOREAPP1_1 && !NETCOREAPP1_0 /// @@ -915,28 +914,26 @@ namespace System /// /// The name to check if it's defined /// if a member with the name exists in the enumeration, otherwise - public static bool IsDefined(in global::System.ReadOnlySpan name) => IsDefined(name, allowMatchingMetadataAttribute: false); + public static bool IsDefined(in global::System.ReadOnlySpan name) + => name switch + { + global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::System.DateTimeKind.Unspecified), global::System.StringComparison.Ordinal) => true, + global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::System.DateTimeKind.Utc), global::System.StringComparison.Ordinal) => true, + global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::System.DateTimeKind.Local), global::System.StringComparison.Ordinal) => true, + _ => false, + }; /// /// Returns a boolean telling whether an enum with the given name exists in the enumeration, - /// or optionally if a member decorated with a [Display] attribute - /// with the required name exists. - /// Slower then the overload, but doesn't allocate memory./> + /// or optionally if a member decorated with + /// exists. Slower then the overload, but doesn't allocate memory./> /// /// The name to check if it's defined /// If , considers the value of metadata attributes,otherwise ignores them /// if a member with the name exists in the enumeration, or a member is decorated /// with a [Display] attribute with the name, otherwise public static bool IsDefined(in global::System.ReadOnlySpan name, bool allowMatchingMetadataAttribute) - { - return name switch - { - global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::System.DateTimeKind.Unspecified), global::System.StringComparison.Ordinal) => true, - global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::System.DateTimeKind.Utc), global::System.StringComparison.Ordinal) => true, - global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::System.DateTimeKind.Local), global::System.StringComparison.Ordinal) => true, - _ => false, - }; - } + => IsDefined(name); #endif /// @@ -1048,8 +1045,9 @@ namespace System /// If the parse operation fails, contains the default value of the underlying type /// of . This parameter is passed uninitialized. /// to read value in case insensitive mode; to read value in case sensitive mode. - /// If , considers the value included in metadata attributes such as - /// [Display] attribute when parsing, otherwise only considers the member names. + /// If , + /// considers the value included in attribute + /// when parsing, otherwise only considers the member names. /// if the value parameter was converted successfully; otherwise, . public static bool TryParse( #if NETCOREAPP3_0_OR_GREATER @@ -1071,7 +1069,6 @@ namespace System out global::System.DateTimeKind value, bool allowMatchingMetadataAttribute) { - switch (name) { case string s when s.Equals(nameof(global::System.DateTimeKind.Unspecified), global::System.StringComparison.OrdinalIgnoreCase): @@ -1100,7 +1097,6 @@ namespace System out global::System.DateTimeKind value, bool allowMatchingMetadataAttribute) { - switch (name) { case nameof(global::System.DateTimeKind.Unspecified): @@ -1158,8 +1154,8 @@ namespace System /// /// The case-sensitive string representation of the enumeration name or underlying value to convert /// to read value in case insensitive mode; to read value in case sensitive mode. - /// If , considers the value included in metadata attributes such as - /// [Display] attribute when parsing, otherwise only considers the member names. + /// If , considers the value included in + /// attribute when parsing, otherwise only considers the member names. /// An object of type whose /// value is represented by public static global::System.DateTimeKind Parse( @@ -1225,8 +1221,8 @@ namespace System /// If the parse operation fails, contains the default value of the underlying type /// of . This parameter is passed uninitialized. /// to read value in case insensitive mode; to read value in case sensitive mode. - /// If , considers the value included in metadata attributes such as - /// [Display] attribute when parsing, otherwise only considers the member names. + /// If , considers the value included in + /// attribute when parsing, otherwise only considers the member names. /// if the value parameter was converted successfully; otherwise, . public static bool TryParse( #if NETCOREAPP3_0_OR_GREATER diff --git a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanHandleNamespaceAndClassNameAreTheSame.verified.txt b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanHandleNamespaceAndClassNameAreTheSame.verified.txt index 825f89c..461133e 100644 --- a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanHandleNamespaceAndClassNameAreTheSame.verified.txt +++ b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanHandleNamespaceAndClassNameAreTheSame.verified.txt @@ -25,20 +25,6 @@ namespace Foo /// public const int Length = 1; - /// - /// Returns the string representation of the value. - /// If the attribute is decorated with a [Display] or [Description]attribute, then - /// uses the provided value. Otherwise uses the name of the member, equivalent to - /// calling ToString() on . - /// - /// The value to retrieve the string value for - /// If uses the value provided in the - /// [Display] or [Description]attribute as the string representation of the member. - /// If , always uses the name of the member, the same as if ToString() was called. - /// The string representation of the value - public static string ToStringFast(this global::Foo.TestEnum value, bool useMetadataAttributes) - => useMetadataAttributes ? value.ToStringFastWithMetadata() : value.ToStringFast(); - /// /// Returns the string representation of the value. /// Directly equivalent to calling ToString() on . @@ -52,6 +38,20 @@ namespace Foo _ => value.AsUnderlyingType().ToString(), }; + /// + /// Returns the string representation of the value. + /// If the member is decorated with the attribute + /// then that value is returned. Otherwise returns uses the name of the member, + /// equivalent to calling ToString() on . + /// + /// The value to retrieve the string value for + /// If uses the value provided in the + /// attribute as the string representation of the member. + /// If , always uses the name of the member, the same as if ToString() was called. + /// The string representation of the value + public static string ToStringFast(this global::Foo.TestEnum value, bool useMetadataAttributes) + => useMetadataAttributes ? value.ToStringFastWithMetadata() : value.ToStringFast(); + private static string ToStringFastWithMetadata(this global::Foo.TestEnum value) => value.ToStringFast(); @@ -82,25 +82,26 @@ namespace Foo /// /// The name to check if it's defined /// if a member with the name exists in the enumeration, otherwise - public static bool IsDefined(string name) => IsDefined(name, allowMatchingMetadataAttribute: false); + public static bool IsDefined(string name) + => name switch + { + nameof(global::Foo.TestEnum.Value1) => true, + _ => false, + }; /// /// Returns a boolean telling whether an enum with the given name exists in the enumeration, - /// or if a member decorated with a [Display] attribute + /// or if a member decorated with /// with the required name exists. /// /// The name to check if it's defined - /// If , considers the value of metadata attributes,otherwise ignores them + /// If , + /// considers the value of + /// instead of the member name, otherwise ignores them /// if a member with the name exists in the enumeration, or a member is decorated /// with a [Display] attribute with the name, otherwise public static bool IsDefined(string name, bool allowMatchingMetadataAttribute) - { - return name switch - { - nameof(global::Foo.TestEnum.Value1) => true, - _ => false, - }; - } + => IsDefined(name); #if NETCOREAPP && !NETCOREAPP2_0 && !NETCOREAPP1_1 && !NETCOREAPP1_0 /// @@ -108,26 +109,24 @@ namespace Foo /// /// The name to check if it's defined /// if a member with the name exists in the enumeration, otherwise - public static bool IsDefined(in global::System.ReadOnlySpan name) => IsDefined(name, allowMatchingMetadataAttribute: false); + public static bool IsDefined(in global::System.ReadOnlySpan name) + => name switch + { + global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::Foo.TestEnum.Value1), global::System.StringComparison.Ordinal) => true, + _ => false, + }; /// /// Returns a boolean telling whether an enum with the given name exists in the enumeration, - /// or optionally if a member decorated with a [Display] attribute - /// with the required name exists. - /// Slower then the overload, but doesn't allocate memory./> + /// or optionally if a member decorated with + /// exists. Slower then the overload, but doesn't allocate memory./> /// /// The name to check if it's defined /// If , considers the value of metadata attributes,otherwise ignores them /// if a member with the name exists in the enumeration, or a member is decorated /// with a [Display] attribute with the name, otherwise public static bool IsDefined(in global::System.ReadOnlySpan name, bool allowMatchingMetadataAttribute) - { - return name switch - { - global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::Foo.TestEnum.Value1), global::System.StringComparison.Ordinal) => true, - _ => false, - }; - } + => IsDefined(name); #endif /// @@ -239,8 +238,9 @@ namespace Foo /// If the parse operation fails, contains the default value of the underlying type /// of . This parameter is passed uninitialized. /// to read value in case insensitive mode; to read value in case sensitive mode. - /// If , considers the value included in metadata attributes such as - /// [Display] attribute when parsing, otherwise only considers the member names. + /// If , + /// considers the value included in attribute + /// when parsing, otherwise only considers the member names. /// if the value parameter was converted successfully; otherwise, . public static bool TryParse( #if NETCOREAPP3_0_OR_GREATER @@ -262,7 +262,6 @@ namespace Foo out global::Foo.TestEnum value, bool allowMatchingMetadataAttribute) { - switch (name) { case string s when s.Equals(nameof(global::Foo.TestEnum.Value1), global::System.StringComparison.OrdinalIgnoreCase): @@ -285,7 +284,6 @@ namespace Foo out global::Foo.TestEnum value, bool allowMatchingMetadataAttribute) { - switch (name) { case nameof(global::Foo.TestEnum.Value1): @@ -337,8 +335,8 @@ namespace Foo /// /// The case-sensitive string representation of the enumeration name or underlying value to convert /// to read value in case insensitive mode; to read value in case sensitive mode. - /// If , considers the value included in metadata attributes such as - /// [Display] attribute when parsing, otherwise only considers the member names. + /// If , considers the value included in + /// attribute when parsing, otherwise only considers the member names. /// An object of type whose /// value is represented by public static global::Foo.TestEnum Parse( @@ -404,8 +402,8 @@ namespace Foo /// If the parse operation fails, contains the default value of the underlying type /// of . This parameter is passed uninitialized. /// to read value in case insensitive mode; to read value in case sensitive mode. - /// If , considers the value included in metadata attributes such as - /// [Display] attribute when parsing, otherwise only considers the member names. + /// If , considers the value included in + /// attribute when parsing, otherwise only considers the member names. /// if the value parameter was converted successfully; otherwise, . public static bool TryParse( #if NETCOREAPP3_0_OR_GREATER diff --git a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.DoesNotGenerateWarningsForObsoleteEnums_CS0612_Issue97.verified.txt b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.DoesNotGenerateWarningsForObsoleteEnums_CS0612_Issue97.verified.txt index 0be1be2..e9c8e6e 100644 --- a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.DoesNotGenerateWarningsForObsoleteEnums_CS0612_Issue97.verified.txt +++ b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.DoesNotGenerateWarningsForObsoleteEnums_CS0612_Issue97.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the NetEscapades.EnumGenerators source generator // @@ -23,20 +23,6 @@ /// public const int Length = 2; - /// - /// Returns the string representation of the value. - /// If the attribute is decorated with a [Display] or [Description]attribute, then - /// uses the provided value. Otherwise uses the name of the member, equivalent to - /// calling ToString() on . - /// - /// The value to retrieve the string value for - /// If uses the value provided in the - /// [Display] or [Description]attribute as the string representation of the member. - /// If , always uses the name of the member, the same as if ToString() was called. - /// The string representation of the value - public static string ToStringFast(this global::MyEnum value, bool useMetadataAttributes) - => useMetadataAttributes ? value.ToStringFastWithMetadata() : value.ToStringFast(); - /// /// Returns the string representation of the value. /// Directly equivalent to calling ToString() on . @@ -51,6 +37,20 @@ _ => value.AsUnderlyingType().ToString(), }; + /// + /// Returns the string representation of the value. + /// If the member is decorated with the attribute + /// then that value is returned. Otherwise returns uses the name of the member, + /// equivalent to calling ToString() on . + /// + /// The value to retrieve the string value for + /// If uses the value provided in the + /// attribute as the string representation of the member. + /// If , always uses the name of the member, the same as if ToString() was called. + /// The string representation of the value + public static string ToStringFast(this global::MyEnum value, bool useMetadataAttributes) + => useMetadataAttributes ? value.ToStringFastWithMetadata() : value.ToStringFast(); + private static string ToStringFastWithMetadata(this global::MyEnum value) => value.ToStringFast(); @@ -82,26 +82,27 @@ /// /// The name to check if it's defined /// if a member with the name exists in the enumeration, otherwise - public static bool IsDefined(string name) => IsDefined(name, allowMatchingMetadataAttribute: false); + public static bool IsDefined(string name) + => name switch + { + nameof(global::MyEnum.First) => true, + nameof(global::MyEnum.Second) => true, + _ => false, + }; /// /// Returns a boolean telling whether an enum with the given name exists in the enumeration, - /// or if a member decorated with a [Display] attribute + /// or if a member decorated with /// with the required name exists. /// /// The name to check if it's defined - /// If , considers the value of metadata attributes,otherwise ignores them + /// If , + /// considers the value of + /// instead of the member name, otherwise ignores them /// if a member with the name exists in the enumeration, or a member is decorated /// with a [Display] attribute with the name, otherwise public static bool IsDefined(string name, bool allowMatchingMetadataAttribute) - { - return name switch - { - nameof(global::MyEnum.First) => true, - nameof(global::MyEnum.Second) => true, - _ => false, - }; - } + => IsDefined(name); #if NETCOREAPP && !NETCOREAPP2_0 && !NETCOREAPP1_1 && !NETCOREAPP1_0 /// @@ -109,27 +110,25 @@ /// /// The name to check if it's defined /// if a member with the name exists in the enumeration, otherwise - public static bool IsDefined(in global::System.ReadOnlySpan name) => IsDefined(name, allowMatchingMetadataAttribute: false); + public static bool IsDefined(in global::System.ReadOnlySpan name) + => name switch + { + global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyEnum.First), global::System.StringComparison.Ordinal) => true, + global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyEnum.Second), global::System.StringComparison.Ordinal) => true, + _ => false, + }; /// /// Returns a boolean telling whether an enum with the given name exists in the enumeration, - /// or optionally if a member decorated with a [Display] attribute - /// with the required name exists. - /// Slower then the overload, but doesn't allocate memory./> + /// or optionally if a member decorated with + /// exists. Slower then the overload, but doesn't allocate memory./> /// /// The name to check if it's defined /// If , considers the value of metadata attributes,otherwise ignores them /// if a member with the name exists in the enumeration, or a member is decorated /// with a [Display] attribute with the name, otherwise public static bool IsDefined(in global::System.ReadOnlySpan name, bool allowMatchingMetadataAttribute) - { - return name switch - { - global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyEnum.First), global::System.StringComparison.Ordinal) => true, - global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyEnum.Second), global::System.StringComparison.Ordinal) => true, - _ => false, - }; - } + => IsDefined(name); #endif /// @@ -241,8 +240,9 @@ /// If the parse operation fails, contains the default value of the underlying type /// of . This parameter is passed uninitialized. /// to read value in case insensitive mode; to read value in case sensitive mode. - /// If , considers the value included in metadata attributes such as - /// [Display] attribute when parsing, otherwise only considers the member names. + /// If , + /// considers the value included in attribute + /// when parsing, otherwise only considers the member names. /// if the value parameter was converted successfully; otherwise, . public static bool TryParse( #if NETCOREAPP3_0_OR_GREATER @@ -264,7 +264,6 @@ out global::MyEnum value, bool allowMatchingMetadataAttribute) { - switch (name) { case string s when s.Equals(nameof(global::MyEnum.First), global::System.StringComparison.OrdinalIgnoreCase): @@ -290,7 +289,6 @@ out global::MyEnum value, bool allowMatchingMetadataAttribute) { - switch (name) { case nameof(global::MyEnum.First): @@ -345,8 +343,8 @@ /// /// The case-sensitive string representation of the enumeration name or underlying value to convert /// to read value in case insensitive mode; to read value in case sensitive mode. - /// If , considers the value included in metadata attributes such as - /// [Display] attribute when parsing, otherwise only considers the member names. + /// If , considers the value included in + /// attribute when parsing, otherwise only considers the member names. /// An object of type whose /// value is represented by public static global::MyEnum Parse( @@ -412,8 +410,8 @@ /// If the parse operation fails, contains the default value of the underlying type /// of . This parameter is passed uninitialized. /// to read value in case insensitive mode; to read value in case sensitive mode. - /// If , considers the value included in metadata attributes such as - /// [Display] attribute when parsing, otherwise only considers the member names. + /// If , considers the value included in + /// attribute when parsing, otherwise only considers the member names. /// if the value parameter was converted successfully; otherwise, . public static bool TryParse( #if NETCOREAPP3_0_OR_GREATER diff --git a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.DoesNotGenerateWarningsForObsoleteEnums_CS0618_Issue97.verified.txt b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.DoesNotGenerateWarningsForObsoleteEnums_CS0618_Issue97.verified.txt index 0be1be2..e9c8e6e 100644 --- a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.DoesNotGenerateWarningsForObsoleteEnums_CS0618_Issue97.verified.txt +++ b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.DoesNotGenerateWarningsForObsoleteEnums_CS0618_Issue97.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the NetEscapades.EnumGenerators source generator // @@ -23,20 +23,6 @@ /// public const int Length = 2; - /// - /// Returns the string representation of the value. - /// If the attribute is decorated with a [Display] or [Description]attribute, then - /// uses the provided value. Otherwise uses the name of the member, equivalent to - /// calling ToString() on . - /// - /// The value to retrieve the string value for - /// If uses the value provided in the - /// [Display] or [Description]attribute as the string representation of the member. - /// If , always uses the name of the member, the same as if ToString() was called. - /// The string representation of the value - public static string ToStringFast(this global::MyEnum value, bool useMetadataAttributes) - => useMetadataAttributes ? value.ToStringFastWithMetadata() : value.ToStringFast(); - /// /// Returns the string representation of the value. /// Directly equivalent to calling ToString() on . @@ -51,6 +37,20 @@ _ => value.AsUnderlyingType().ToString(), }; + /// + /// Returns the string representation of the value. + /// If the member is decorated with the attribute + /// then that value is returned. Otherwise returns uses the name of the member, + /// equivalent to calling ToString() on . + /// + /// The value to retrieve the string value for + /// If uses the value provided in the + /// attribute as the string representation of the member. + /// If , always uses the name of the member, the same as if ToString() was called. + /// The string representation of the value + public static string ToStringFast(this global::MyEnum value, bool useMetadataAttributes) + => useMetadataAttributes ? value.ToStringFastWithMetadata() : value.ToStringFast(); + private static string ToStringFastWithMetadata(this global::MyEnum value) => value.ToStringFast(); @@ -82,26 +82,27 @@ /// /// The name to check if it's defined /// if a member with the name exists in the enumeration, otherwise - public static bool IsDefined(string name) => IsDefined(name, allowMatchingMetadataAttribute: false); + public static bool IsDefined(string name) + => name switch + { + nameof(global::MyEnum.First) => true, + nameof(global::MyEnum.Second) => true, + _ => false, + }; /// /// Returns a boolean telling whether an enum with the given name exists in the enumeration, - /// or if a member decorated with a [Display] attribute + /// or if a member decorated with /// with the required name exists. /// /// The name to check if it's defined - /// If , considers the value of metadata attributes,otherwise ignores them + /// If , + /// considers the value of + /// instead of the member name, otherwise ignores them /// if a member with the name exists in the enumeration, or a member is decorated /// with a [Display] attribute with the name, otherwise public static bool IsDefined(string name, bool allowMatchingMetadataAttribute) - { - return name switch - { - nameof(global::MyEnum.First) => true, - nameof(global::MyEnum.Second) => true, - _ => false, - }; - } + => IsDefined(name); #if NETCOREAPP && !NETCOREAPP2_0 && !NETCOREAPP1_1 && !NETCOREAPP1_0 /// @@ -109,27 +110,25 @@ /// /// The name to check if it's defined /// if a member with the name exists in the enumeration, otherwise - public static bool IsDefined(in global::System.ReadOnlySpan name) => IsDefined(name, allowMatchingMetadataAttribute: false); + public static bool IsDefined(in global::System.ReadOnlySpan name) + => name switch + { + global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyEnum.First), global::System.StringComparison.Ordinal) => true, + global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyEnum.Second), global::System.StringComparison.Ordinal) => true, + _ => false, + }; /// /// Returns a boolean telling whether an enum with the given name exists in the enumeration, - /// or optionally if a member decorated with a [Display] attribute - /// with the required name exists. - /// Slower then the overload, but doesn't allocate memory./> + /// or optionally if a member decorated with + /// exists. Slower then the overload, but doesn't allocate memory./> /// /// The name to check if it's defined /// If , considers the value of metadata attributes,otherwise ignores them /// if a member with the name exists in the enumeration, or a member is decorated /// with a [Display] attribute with the name, otherwise public static bool IsDefined(in global::System.ReadOnlySpan name, bool allowMatchingMetadataAttribute) - { - return name switch - { - global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyEnum.First), global::System.StringComparison.Ordinal) => true, - global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyEnum.Second), global::System.StringComparison.Ordinal) => true, - _ => false, - }; - } + => IsDefined(name); #endif /// @@ -241,8 +240,9 @@ /// If the parse operation fails, contains the default value of the underlying type /// of . This parameter is passed uninitialized. /// to read value in case insensitive mode; to read value in case sensitive mode. - /// If , considers the value included in metadata attributes such as - /// [Display] attribute when parsing, otherwise only considers the member names. + /// If , + /// considers the value included in attribute + /// when parsing, otherwise only considers the member names. /// if the value parameter was converted successfully; otherwise, . public static bool TryParse( #if NETCOREAPP3_0_OR_GREATER @@ -264,7 +264,6 @@ out global::MyEnum value, bool allowMatchingMetadataAttribute) { - switch (name) { case string s when s.Equals(nameof(global::MyEnum.First), global::System.StringComparison.OrdinalIgnoreCase): @@ -290,7 +289,6 @@ out global::MyEnum value, bool allowMatchingMetadataAttribute) { - switch (name) { case nameof(global::MyEnum.First): @@ -345,8 +343,8 @@ /// /// The case-sensitive string representation of the enumeration name or underlying value to convert /// to read value in case insensitive mode; to read value in case sensitive mode. - /// If , considers the value included in metadata attributes such as - /// [Display] attribute when parsing, otherwise only considers the member names. + /// If , considers the value included in + /// attribute when parsing, otherwise only considers the member names. /// An object of type whose /// value is represented by public static global::MyEnum Parse( @@ -412,8 +410,8 @@ /// If the parse operation fails, contains the default value of the underlying type /// of . This parameter is passed uninitialized. /// to read value in case insensitive mode; to read value in case sensitive mode. - /// If , considers the value included in metadata attributes such as - /// [Display] attribute when parsing, otherwise only considers the member names. + /// If , considers the value included in + /// attribute when parsing, otherwise only considers the member names. /// if the value parameter was converted successfully; otherwise, . public static bool TryParse( #if NETCOREAPP3_0_OR_GREATER diff --git a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.DoesNotGenerateWarningsForObsoleteMembers_CS0612_Issue97.verified.txt b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.DoesNotGenerateWarningsForObsoleteMembers_CS0612_Issue97.verified.txt index 0be1be2..e9c8e6e 100644 --- a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.DoesNotGenerateWarningsForObsoleteMembers_CS0612_Issue97.verified.txt +++ b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.DoesNotGenerateWarningsForObsoleteMembers_CS0612_Issue97.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the NetEscapades.EnumGenerators source generator // @@ -23,20 +23,6 @@ /// public const int Length = 2; - /// - /// Returns the string representation of the value. - /// If the attribute is decorated with a [Display] or [Description]attribute, then - /// uses the provided value. Otherwise uses the name of the member, equivalent to - /// calling ToString() on . - /// - /// The value to retrieve the string value for - /// If uses the value provided in the - /// [Display] or [Description]attribute as the string representation of the member. - /// If , always uses the name of the member, the same as if ToString() was called. - /// The string representation of the value - public static string ToStringFast(this global::MyEnum value, bool useMetadataAttributes) - => useMetadataAttributes ? value.ToStringFastWithMetadata() : value.ToStringFast(); - /// /// Returns the string representation of the value. /// Directly equivalent to calling ToString() on . @@ -51,6 +37,20 @@ _ => value.AsUnderlyingType().ToString(), }; + /// + /// Returns the string representation of the value. + /// If the member is decorated with the attribute + /// then that value is returned. Otherwise returns uses the name of the member, + /// equivalent to calling ToString() on . + /// + /// The value to retrieve the string value for + /// If uses the value provided in the + /// attribute as the string representation of the member. + /// If , always uses the name of the member, the same as if ToString() was called. + /// The string representation of the value + public static string ToStringFast(this global::MyEnum value, bool useMetadataAttributes) + => useMetadataAttributes ? value.ToStringFastWithMetadata() : value.ToStringFast(); + private static string ToStringFastWithMetadata(this global::MyEnum value) => value.ToStringFast(); @@ -82,26 +82,27 @@ /// /// The name to check if it's defined /// if a member with the name exists in the enumeration, otherwise - public static bool IsDefined(string name) => IsDefined(name, allowMatchingMetadataAttribute: false); + public static bool IsDefined(string name) + => name switch + { + nameof(global::MyEnum.First) => true, + nameof(global::MyEnum.Second) => true, + _ => false, + }; /// /// Returns a boolean telling whether an enum with the given name exists in the enumeration, - /// or if a member decorated with a [Display] attribute + /// or if a member decorated with /// with the required name exists. /// /// The name to check if it's defined - /// If , considers the value of metadata attributes,otherwise ignores them + /// If , + /// considers the value of + /// instead of the member name, otherwise ignores them /// if a member with the name exists in the enumeration, or a member is decorated /// with a [Display] attribute with the name, otherwise public static bool IsDefined(string name, bool allowMatchingMetadataAttribute) - { - return name switch - { - nameof(global::MyEnum.First) => true, - nameof(global::MyEnum.Second) => true, - _ => false, - }; - } + => IsDefined(name); #if NETCOREAPP && !NETCOREAPP2_0 && !NETCOREAPP1_1 && !NETCOREAPP1_0 /// @@ -109,27 +110,25 @@ /// /// The name to check if it's defined /// if a member with the name exists in the enumeration, otherwise - public static bool IsDefined(in global::System.ReadOnlySpan name) => IsDefined(name, allowMatchingMetadataAttribute: false); + public static bool IsDefined(in global::System.ReadOnlySpan name) + => name switch + { + global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyEnum.First), global::System.StringComparison.Ordinal) => true, + global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyEnum.Second), global::System.StringComparison.Ordinal) => true, + _ => false, + }; /// /// Returns a boolean telling whether an enum with the given name exists in the enumeration, - /// or optionally if a member decorated with a [Display] attribute - /// with the required name exists. - /// Slower then the overload, but doesn't allocate memory./> + /// or optionally if a member decorated with + /// exists. Slower then the overload, but doesn't allocate memory./> /// /// The name to check if it's defined /// If , considers the value of metadata attributes,otherwise ignores them /// if a member with the name exists in the enumeration, or a member is decorated /// with a [Display] attribute with the name, otherwise public static bool IsDefined(in global::System.ReadOnlySpan name, bool allowMatchingMetadataAttribute) - { - return name switch - { - global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyEnum.First), global::System.StringComparison.Ordinal) => true, - global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyEnum.Second), global::System.StringComparison.Ordinal) => true, - _ => false, - }; - } + => IsDefined(name); #endif /// @@ -241,8 +240,9 @@ /// If the parse operation fails, contains the default value of the underlying type /// of . This parameter is passed uninitialized. /// to read value in case insensitive mode; to read value in case sensitive mode. - /// If , considers the value included in metadata attributes such as - /// [Display] attribute when parsing, otherwise only considers the member names. + /// If , + /// considers the value included in attribute + /// when parsing, otherwise only considers the member names. /// if the value parameter was converted successfully; otherwise, . public static bool TryParse( #if NETCOREAPP3_0_OR_GREATER @@ -264,7 +264,6 @@ out global::MyEnum value, bool allowMatchingMetadataAttribute) { - switch (name) { case string s when s.Equals(nameof(global::MyEnum.First), global::System.StringComparison.OrdinalIgnoreCase): @@ -290,7 +289,6 @@ out global::MyEnum value, bool allowMatchingMetadataAttribute) { - switch (name) { case nameof(global::MyEnum.First): @@ -345,8 +343,8 @@ /// /// The case-sensitive string representation of the enumeration name or underlying value to convert /// to read value in case insensitive mode; to read value in case sensitive mode. - /// If , considers the value included in metadata attributes such as - /// [Display] attribute when parsing, otherwise only considers the member names. + /// If , considers the value included in + /// attribute when parsing, otherwise only considers the member names. /// An object of type whose /// value is represented by public static global::MyEnum Parse( @@ -412,8 +410,8 @@ /// If the parse operation fails, contains the default value of the underlying type /// of . This parameter is passed uninitialized. /// to read value in case insensitive mode; to read value in case sensitive mode. - /// If , considers the value included in metadata attributes such as - /// [Display] attribute when parsing, otherwise only considers the member names. + /// If , considers the value included in + /// attribute when parsing, otherwise only considers the member names. /// if the value parameter was converted successfully; otherwise, . public static bool TryParse( #if NETCOREAPP3_0_OR_GREATER diff --git a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.DoesNotGenerateWarningsForObsoleteMembers_CS0618_Issue97.verified.txt b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.DoesNotGenerateWarningsForObsoleteMembers_CS0618_Issue97.verified.txt index 0be1be2..e9c8e6e 100644 --- a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.DoesNotGenerateWarningsForObsoleteMembers_CS0618_Issue97.verified.txt +++ b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.DoesNotGenerateWarningsForObsoleteMembers_CS0618_Issue97.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the NetEscapades.EnumGenerators source generator // @@ -23,20 +23,6 @@ /// public const int Length = 2; - /// - /// Returns the string representation of the value. - /// If the attribute is decorated with a [Display] or [Description]attribute, then - /// uses the provided value. Otherwise uses the name of the member, equivalent to - /// calling ToString() on . - /// - /// The value to retrieve the string value for - /// If uses the value provided in the - /// [Display] or [Description]attribute as the string representation of the member. - /// If , always uses the name of the member, the same as if ToString() was called. - /// The string representation of the value - public static string ToStringFast(this global::MyEnum value, bool useMetadataAttributes) - => useMetadataAttributes ? value.ToStringFastWithMetadata() : value.ToStringFast(); - /// /// Returns the string representation of the value. /// Directly equivalent to calling ToString() on . @@ -51,6 +37,20 @@ _ => value.AsUnderlyingType().ToString(), }; + /// + /// Returns the string representation of the value. + /// If the member is decorated with the attribute + /// then that value is returned. Otherwise returns uses the name of the member, + /// equivalent to calling ToString() on . + /// + /// The value to retrieve the string value for + /// If uses the value provided in the + /// attribute as the string representation of the member. + /// If , always uses the name of the member, the same as if ToString() was called. + /// The string representation of the value + public static string ToStringFast(this global::MyEnum value, bool useMetadataAttributes) + => useMetadataAttributes ? value.ToStringFastWithMetadata() : value.ToStringFast(); + private static string ToStringFastWithMetadata(this global::MyEnum value) => value.ToStringFast(); @@ -82,26 +82,27 @@ /// /// The name to check if it's defined /// if a member with the name exists in the enumeration, otherwise - public static bool IsDefined(string name) => IsDefined(name, allowMatchingMetadataAttribute: false); + public static bool IsDefined(string name) + => name switch + { + nameof(global::MyEnum.First) => true, + nameof(global::MyEnum.Second) => true, + _ => false, + }; /// /// Returns a boolean telling whether an enum with the given name exists in the enumeration, - /// or if a member decorated with a [Display] attribute + /// or if a member decorated with /// with the required name exists. /// /// The name to check if it's defined - /// If , considers the value of metadata attributes,otherwise ignores them + /// If , + /// considers the value of + /// instead of the member name, otherwise ignores them /// if a member with the name exists in the enumeration, or a member is decorated /// with a [Display] attribute with the name, otherwise public static bool IsDefined(string name, bool allowMatchingMetadataAttribute) - { - return name switch - { - nameof(global::MyEnum.First) => true, - nameof(global::MyEnum.Second) => true, - _ => false, - }; - } + => IsDefined(name); #if NETCOREAPP && !NETCOREAPP2_0 && !NETCOREAPP1_1 && !NETCOREAPP1_0 /// @@ -109,27 +110,25 @@ /// /// The name to check if it's defined /// if a member with the name exists in the enumeration, otherwise - public static bool IsDefined(in global::System.ReadOnlySpan name) => IsDefined(name, allowMatchingMetadataAttribute: false); + public static bool IsDefined(in global::System.ReadOnlySpan name) + => name switch + { + global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyEnum.First), global::System.StringComparison.Ordinal) => true, + global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyEnum.Second), global::System.StringComparison.Ordinal) => true, + _ => false, + }; /// /// Returns a boolean telling whether an enum with the given name exists in the enumeration, - /// or optionally if a member decorated with a [Display] attribute - /// with the required name exists. - /// Slower then the overload, but doesn't allocate memory./> + /// or optionally if a member decorated with + /// exists. Slower then the overload, but doesn't allocate memory./> /// /// The name to check if it's defined /// If , considers the value of metadata attributes,otherwise ignores them /// if a member with the name exists in the enumeration, or a member is decorated /// with a [Display] attribute with the name, otherwise public static bool IsDefined(in global::System.ReadOnlySpan name, bool allowMatchingMetadataAttribute) - { - return name switch - { - global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyEnum.First), global::System.StringComparison.Ordinal) => true, - global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyEnum.Second), global::System.StringComparison.Ordinal) => true, - _ => false, - }; - } + => IsDefined(name); #endif /// @@ -241,8 +240,9 @@ /// If the parse operation fails, contains the default value of the underlying type /// of . This parameter is passed uninitialized. /// to read value in case insensitive mode; to read value in case sensitive mode. - /// If , considers the value included in metadata attributes such as - /// [Display] attribute when parsing, otherwise only considers the member names. + /// If , + /// considers the value included in attribute + /// when parsing, otherwise only considers the member names. /// if the value parameter was converted successfully; otherwise, . public static bool TryParse( #if NETCOREAPP3_0_OR_GREATER @@ -264,7 +264,6 @@ out global::MyEnum value, bool allowMatchingMetadataAttribute) { - switch (name) { case string s when s.Equals(nameof(global::MyEnum.First), global::System.StringComparison.OrdinalIgnoreCase): @@ -290,7 +289,6 @@ out global::MyEnum value, bool allowMatchingMetadataAttribute) { - switch (name) { case nameof(global::MyEnum.First): @@ -345,8 +343,8 @@ /// /// The case-sensitive string representation of the enumeration name or underlying value to convert /// to read value in case insensitive mode; to read value in case sensitive mode. - /// If , considers the value included in metadata attributes such as - /// [Display] attribute when parsing, otherwise only considers the member names. + /// If , considers the value included in + /// attribute when parsing, otherwise only considers the member names. /// An object of type whose /// value is represented by public static global::MyEnum Parse( @@ -412,8 +410,8 @@ /// If the parse operation fails, contains the default value of the underlying type /// of . This parameter is passed uninitialized. /// to read value in case insensitive mode; to read value in case sensitive mode. - /// If , considers the value included in metadata attributes such as - /// [Display] attribute when parsing, otherwise only considers the member names. + /// If , considers the value included in + /// attribute when parsing, otherwise only considers the member names. /// if the value parameter was converted successfully; otherwise, . public static bool TryParse( #if NETCOREAPP3_0_OR_GREATER diff --git a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.HandlesStringsWithQuotesAndSlashesInDescription.verified.txt b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.HandlesStringsWithQuotesAndSlashesInDescription__.verified.txt similarity index 91% rename from tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.HandlesStringsWithQuotesAndSlashesInDescription.verified.txt rename to tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.HandlesStringsWithQuotesAndSlashesInDescription__.verified.txt index ae8e383..b9b3e9b 100644 --- a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.HandlesStringsWithQuotesAndSlashesInDescription.verified.txt +++ b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.HandlesStringsWithQuotesAndSlashesInDescription__.verified.txt @@ -25,20 +25,6 @@ namespace Test /// public const int Length = 5; - /// - /// Returns the string representation of the value. - /// If the attribute is decorated with a [Display] or [Description]attribute, then - /// uses the provided value. Otherwise uses the name of the member, equivalent to - /// calling ToString() on . - /// - /// The value to retrieve the string value for - /// If uses the value provided in the - /// [Display] or [Description]attribute as the string representation of the member. - /// If , always uses the name of the member, the same as if ToString() was called. - /// The string representation of the value - public static string ToStringFast(this global::Test.StringTesting value, bool useMetadataAttributes) - => useMetadataAttributes ? value.ToStringFastWithMetadata() : value.ToStringFast(); - /// /// Returns the string representation of the value. /// Directly equivalent to calling ToString() on . @@ -56,6 +42,20 @@ namespace Test _ => value.AsUnderlyingType().ToString(), }; + /// + /// Returns the string representation of the value. + /// If the member is decorated with the attribute + /// then that value is returned. Otherwise returns uses the name of the member, + /// equivalent to calling ToString() on . + /// + /// The value to retrieve the string value for + /// If uses the value provided in the + /// attribute as the string representation of the member. + /// If , always uses the name of the member, the same as if ToString() was called. + /// The string representation of the value + public static string ToStringFast(this global::Test.StringTesting value, bool useMetadataAttributes) + => useMetadataAttributes ? value.ToStringFastWithMetadata() : value.ToStringFast(); + private static string ToStringFastWithMetadata(this global::Test.StringTesting value) => value switch { @@ -98,48 +98,41 @@ namespace Test /// /// The name to check if it's defined /// if a member with the name exists in the enumeration, otherwise - public static bool IsDefined(string name) => IsDefined(name, allowMatchingMetadataAttribute: false); + public static bool IsDefined(string name) + => name switch + { + nameof(global::Test.StringTesting.Quotes) => true, + nameof(global::Test.StringTesting.LiteralQuotes) => true, + nameof(global::Test.StringTesting.Backslash) => true, + nameof(global::Test.StringTesting.BackslashLiteral) => true, + nameof(global::Test.StringTesting.NewLine) => true, + _ => false, + }; /// /// Returns a boolean telling whether an enum with the given name exists in the enumeration, - /// or if a member decorated with a [Display] attribute + /// or if a member decorated with /// with the required name exists. /// /// The name to check if it's defined - /// If , considers the value of metadata attributes,otherwise ignores them + /// If , + /// considers the value of + /// instead of the member name, otherwise ignores them /// if a member with the name exists in the enumeration, or a member is decorated /// with a [Display] attribute with the name, otherwise public static bool IsDefined(string name, bool allowMatchingMetadataAttribute) - { - var isDefinedInDisplayAttribute = false; - if (allowMatchingMetadataAttribute) - { - isDefinedInDisplayAttribute = name switch - { - "Quotes \"" => true, - "Literal Quotes \"" => true, - "Backslash \\" => true, - "LiteralBackslash \\" => true, - "New\nLine" => true, - _ => false, - }; - } + => allowMatchingMetadataAttribute ? IsMetadataNameDefined(name) : IsDefined(name); - if (isDefinedInDisplayAttribute) + private static bool IsMetadataNameDefined(string name) + => name switch { - return true; - } - - return name switch - { - nameof(global::Test.StringTesting.Quotes) => true, - nameof(global::Test.StringTesting.LiteralQuotes) => true, - nameof(global::Test.StringTesting.Backslash) => true, - nameof(global::Test.StringTesting.BackslashLiteral) => true, - nameof(global::Test.StringTesting.NewLine) => true, - _ => false, + "Quotes \"" => true, + "Literal Quotes \"" => true, + "Backslash \\" => true, + "LiteralBackslash \\" => true, + "New\nLine" => true, + _ => IsDefined(name), }; - } #if NETCOREAPP && !NETCOREAPP2_0 && !NETCOREAPP1_1 && !NETCOREAPP1_0 /// @@ -147,49 +140,38 @@ namespace Test /// /// The name to check if it's defined /// if a member with the name exists in the enumeration, otherwise - public static bool IsDefined(in global::System.ReadOnlySpan name) => IsDefined(name, allowMatchingMetadataAttribute: false); + public static bool IsDefined(in global::System.ReadOnlySpan name) + => name switch + { + global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::Test.StringTesting.Quotes), global::System.StringComparison.Ordinal) => true, + global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::Test.StringTesting.LiteralQuotes), global::System.StringComparison.Ordinal) => true, + global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::Test.StringTesting.Backslash), global::System.StringComparison.Ordinal) => true, + global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::Test.StringTesting.BackslashLiteral), global::System.StringComparison.Ordinal) => true, + global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::Test.StringTesting.NewLine), global::System.StringComparison.Ordinal) => true, + _ => false, + }; /// /// Returns a boolean telling whether an enum with the given name exists in the enumeration, - /// or optionally if a member decorated with a [Display] attribute - /// with the required name exists. - /// Slower then the overload, but doesn't allocate memory./> + /// or optionally if a member decorated with + /// exists. Slower then the overload, but doesn't allocate memory./> /// /// The name to check if it's defined /// If , considers the value of metadata attributes,otherwise ignores them /// if a member with the name exists in the enumeration, or a member is decorated /// with a [Display] attribute with the name, otherwise - public static bool IsDefined(in global::System.ReadOnlySpan name, bool allowMatchingMetadataAttribute) - { - var isDefinedInDisplayAttribute = false; - if (allowMatchingMetadataAttribute) - { - isDefinedInDisplayAttribute = name switch - { - global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, "Quotes \"", global::System.StringComparison.Ordinal) => true, - global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, "Literal Quotes \"", global::System.StringComparison.Ordinal) => true, - global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, "Backslash \\", global::System.StringComparison.Ordinal) => true, - global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, "LiteralBackslash \\", global::System.StringComparison.Ordinal) => true, - global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, "New\nLine", global::System.StringComparison.Ordinal) => true, - _ => false, - }; - } - - if (isDefinedInDisplayAttribute) - { - return true; - } + public static bool IsDefined(in global::System.ReadOnlySpan name, bool allowMatchingMetadataAttribute) => allowMatchingMetadataAttribute ? IsMetadataNameDefined(in name) : IsDefined(in name); - return name switch + private static bool IsMetadataNameDefined(in global::System.ReadOnlySpan name) + => name switch { - global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::Test.StringTesting.Quotes), global::System.StringComparison.Ordinal) => true, - global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::Test.StringTesting.LiteralQuotes), global::System.StringComparison.Ordinal) => true, - global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::Test.StringTesting.Backslash), global::System.StringComparison.Ordinal) => true, - global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::Test.StringTesting.BackslashLiteral), global::System.StringComparison.Ordinal) => true, - global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::Test.StringTesting.NewLine), global::System.StringComparison.Ordinal) => true, - _ => false, + global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, "Quotes \"", global::System.StringComparison.Ordinal) => true, + global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, "Literal Quotes \"", global::System.StringComparison.Ordinal) => true, + global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, "Backslash \\", global::System.StringComparison.Ordinal) => true, + global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, "LiteralBackslash \\", global::System.StringComparison.Ordinal) => true, + global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, "New\nLine", global::System.StringComparison.Ordinal) => true, + _ => IsDefined(name), }; - } #endif /// @@ -301,8 +283,9 @@ namespace Test /// If the parse operation fails, contains the default value of the underlying type /// of . This parameter is passed uninitialized. /// to read value in case insensitive mode; to read value in case sensitive mode. - /// If , considers the value included in metadata attributes such as - /// [Display] attribute when parsing, otherwise only considers the member names. + /// If , + /// considers the value included in attribute + /// when parsing, otherwise only considers the member names. /// if the value parameter was converted successfully; otherwise, . public static bool TryParse( #if NETCOREAPP3_0_OR_GREATER @@ -469,8 +452,8 @@ namespace Test /// /// The case-sensitive string representation of the enumeration name or underlying value to convert /// to read value in case insensitive mode; to read value in case sensitive mode. - /// If , considers the value included in metadata attributes such as - /// [Display] attribute when parsing, otherwise only considers the member names. + /// If , considers the value included in + /// attribute when parsing, otherwise only considers the member names. /// An object of type whose /// value is represented by public static global::Test.StringTesting Parse( @@ -536,8 +519,8 @@ namespace Test /// If the parse operation fails, contains the default value of the underlying type /// of . This parameter is passed uninitialized. /// to read value in case insensitive mode; to read value in case sensitive mode. - /// If , considers the value included in metadata attributes such as - /// [Display] attribute when parsing, otherwise only considers the member names. + /// If , considers the value included in + /// attribute when parsing, otherwise only considers the member names. /// if the value parameter was converted successfully; otherwise, . public static bool TryParse( #if NETCOREAPP3_0_OR_GREATER diff --git a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/InterceptorTests.CanHandleEnumsWithSameNameInDifferentNamespaces.verified.txt b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/InterceptorTests.CanHandleEnumsWithSameNameInDifferentNamespaces.verified.txt index 1bf1174..500cdd3 100644 --- a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/InterceptorTests.CanHandleEnumsWithSameNameInDifferentNamespaces.verified.txt +++ b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/InterceptorTests.CanHandleEnumsWithSameNameInDifferentNamespaces.verified.txt @@ -1,4 +1,4 @@ -[ +[ //------------------------------------------------------------------------------ // // This code was generated by the NetEscapades.EnumGenerators source generator @@ -26,20 +26,6 @@ namespace Foo /// public const int Length = 3; - /// - /// Returns the string representation of the value. - /// If the attribute is decorated with a [Display] or [Description]attribute, then - /// uses the provided value. Otherwise uses the name of the member, equivalent to - /// calling ToString() on . - /// - /// The value to retrieve the string value for - /// If uses the value provided in the - /// [Display] or [Description]attribute as the string representation of the member. - /// If , always uses the name of the member, the same as if ToString() was called. - /// The string representation of the value - public static string ToStringFast(this global::Foo.MyEnum value, bool useMetadataAttributes) - => useMetadataAttributes ? value.ToStringFastWithMetadata() : value.ToStringFast(); - /// /// Returns the string representation of the value. /// Directly equivalent to calling ToString() on . @@ -55,6 +41,20 @@ namespace Foo _ => value.ToString(), }; + /// + /// Returns the string representation of the value. + /// If the member is decorated with the attribute + /// then that value is returned. Otherwise returns uses the name of the member, + /// equivalent to calling ToString() on . + /// + /// The value to retrieve the string value for + /// If uses the value provided in the + /// attribute as the string representation of the member. + /// If , always uses the name of the member, the same as if ToString() was called. + /// The string representation of the value + public static string ToStringFast(this global::Foo.MyEnum value, bool useMetadataAttributes) + => useMetadataAttributes ? value.ToStringFastWithMetadata() : value.ToStringFast(); + private static string ToStringFastWithMetadata(this global::Foo.MyEnum value) => value.ToStringFast(); @@ -99,27 +99,28 @@ namespace Foo /// /// The name to check if it's defined /// if a member with the name exists in the enumeration, otherwise - public static bool IsDefined(string name) => IsDefined(name, allowMatchingMetadataAttribute: false); + public static bool IsDefined(string name) + => name switch + { + nameof(global::Foo.MyEnum.First) => true, + nameof(global::Foo.MyEnum.Second) => true, + nameof(global::Foo.MyEnum.Third) => true, + _ => false, + }; /// /// Returns a boolean telling whether an enum with the given name exists in the enumeration, - /// or if a member decorated with a [Display] attribute + /// or if a member decorated with /// with the required name exists. /// /// The name to check if it's defined - /// If , considers the value of metadata attributes,otherwise ignores them + /// If , + /// considers the value of + /// instead of the member name, otherwise ignores them /// if a member with the name exists in the enumeration, or a member is decorated /// with a [Display] attribute with the name, otherwise public static bool IsDefined(string name, bool allowMatchingMetadataAttribute) - { - return name switch - { - nameof(global::Foo.MyEnum.First) => true, - nameof(global::Foo.MyEnum.Second) => true, - nameof(global::Foo.MyEnum.Third) => true, - _ => false, - }; - } + => IsDefined(name); #if NETCOREAPP && !NETCOREAPP2_0 && !NETCOREAPP1_1 && !NETCOREAPP1_0 /// @@ -127,28 +128,26 @@ namespace Foo /// /// The name to check if it's defined /// if a member with the name exists in the enumeration, otherwise - public static bool IsDefined(in global::System.ReadOnlySpan name) => IsDefined(name, allowMatchingMetadataAttribute: false); + public static bool IsDefined(in global::System.ReadOnlySpan name) + => name switch + { + global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::Foo.MyEnum.First), global::System.StringComparison.Ordinal) => true, + global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::Foo.MyEnum.Second), global::System.StringComparison.Ordinal) => true, + global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::Foo.MyEnum.Third), global::System.StringComparison.Ordinal) => true, + _ => false, + }; /// /// Returns a boolean telling whether an enum with the given name exists in the enumeration, - /// or optionally if a member decorated with a [Display] attribute - /// with the required name exists. - /// Slower then the overload, but doesn't allocate memory./> + /// or optionally if a member decorated with + /// exists. Slower then the overload, but doesn't allocate memory./> /// /// The name to check if it's defined /// If , considers the value of metadata attributes,otherwise ignores them /// if a member with the name exists in the enumeration, or a member is decorated /// with a [Display] attribute with the name, otherwise public static bool IsDefined(in global::System.ReadOnlySpan name, bool allowMatchingMetadataAttribute) - { - return name switch - { - global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::Foo.MyEnum.First), global::System.StringComparison.Ordinal) => true, - global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::Foo.MyEnum.Second), global::System.StringComparison.Ordinal) => true, - global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::Foo.MyEnum.Third), global::System.StringComparison.Ordinal) => true, - _ => false, - }; - } + => IsDefined(name); #endif /// @@ -260,8 +259,9 @@ namespace Foo /// If the parse operation fails, contains the default value of the underlying type /// of . This parameter is passed uninitialized. /// to read value in case insensitive mode; to read value in case sensitive mode. - /// If , considers the value included in metadata attributes such as - /// [Display] attribute when parsing, otherwise only considers the member names. + /// If , + /// considers the value included in attribute + /// when parsing, otherwise only considers the member names. /// if the value parameter was converted successfully; otherwise, . public static bool TryParse( #if NETCOREAPP3_0_OR_GREATER @@ -283,7 +283,6 @@ namespace Foo out global::Foo.MyEnum value, bool allowMatchingMetadataAttribute) { - switch (name) { case string s when s.Equals(nameof(global::Foo.MyEnum.First), global::System.StringComparison.OrdinalIgnoreCase): @@ -312,7 +311,6 @@ namespace Foo out global::Foo.MyEnum value, bool allowMatchingMetadataAttribute) { - switch (name) { case nameof(global::Foo.MyEnum.First): @@ -370,8 +368,8 @@ namespace Foo /// /// The case-sensitive string representation of the enumeration name or underlying value to convert /// to read value in case insensitive mode; to read value in case sensitive mode. - /// If , considers the value included in metadata attributes such as - /// [Display] attribute when parsing, otherwise only considers the member names. + /// If , considers the value included in + /// attribute when parsing, otherwise only considers the member names. /// An object of type whose /// value is represented by public static global::Foo.MyEnum Parse( @@ -437,8 +435,8 @@ namespace Foo /// If the parse operation fails, contains the default value of the underlying type /// of . This parameter is passed uninitialized. /// to read value in case insensitive mode; to read value in case sensitive mode. - /// If , considers the value included in metadata attributes such as - /// [Display] attribute when parsing, otherwise only considers the member names. + /// If , considers the value included in + /// attribute when parsing, otherwise only considers the member names. /// if the value parameter was converted successfully; otherwise, . public static bool TryParse( #if NETCOREAPP3_0_OR_GREATER @@ -591,20 +589,6 @@ namespace Bar /// public const int Length = 3; - /// - /// Returns the string representation of the value. - /// If the attribute is decorated with a [Display] or [Description]attribute, then - /// uses the provided value. Otherwise uses the name of the member, equivalent to - /// calling ToString() on . - /// - /// The value to retrieve the string value for - /// If uses the value provided in the - /// [Display] or [Description]attribute as the string representation of the member. - /// If , always uses the name of the member, the same as if ToString() was called. - /// The string representation of the value - public static string ToStringFast(this global::Bar.MyEnum value, bool useMetadataAttributes) - => useMetadataAttributes ? value.ToStringFastWithMetadata() : value.ToStringFast(); - /// /// Returns the string representation of the value. /// Directly equivalent to calling ToString() on . @@ -620,6 +604,20 @@ namespace Bar _ => value.ToString(), }; + /// + /// Returns the string representation of the value. + /// If the member is decorated with the attribute + /// then that value is returned. Otherwise returns uses the name of the member, + /// equivalent to calling ToString() on . + /// + /// The value to retrieve the string value for + /// If uses the value provided in the + /// attribute as the string representation of the member. + /// If , always uses the name of the member, the same as if ToString() was called. + /// The string representation of the value + public static string ToStringFast(this global::Bar.MyEnum value, bool useMetadataAttributes) + => useMetadataAttributes ? value.ToStringFastWithMetadata() : value.ToStringFast(); + private static string ToStringFastWithMetadata(this global::Bar.MyEnum value) => value.ToStringFast(); @@ -664,27 +662,28 @@ namespace Bar /// /// The name to check if it's defined /// if a member with the name exists in the enumeration, otherwise - public static bool IsDefined(string name) => IsDefined(name, allowMatchingMetadataAttribute: false); + public static bool IsDefined(string name) + => name switch + { + nameof(global::Bar.MyEnum.First) => true, + nameof(global::Bar.MyEnum.Second) => true, + nameof(global::Bar.MyEnum.Third) => true, + _ => false, + }; /// /// Returns a boolean telling whether an enum with the given name exists in the enumeration, - /// or if a member decorated with a [Display] attribute + /// or if a member decorated with /// with the required name exists. /// /// The name to check if it's defined - /// If , considers the value of metadata attributes,otherwise ignores them + /// If , + /// considers the value of + /// instead of the member name, otherwise ignores them /// if a member with the name exists in the enumeration, or a member is decorated /// with a [Display] attribute with the name, otherwise public static bool IsDefined(string name, bool allowMatchingMetadataAttribute) - { - return name switch - { - nameof(global::Bar.MyEnum.First) => true, - nameof(global::Bar.MyEnum.Second) => true, - nameof(global::Bar.MyEnum.Third) => true, - _ => false, - }; - } + => IsDefined(name); #if NETCOREAPP && !NETCOREAPP2_0 && !NETCOREAPP1_1 && !NETCOREAPP1_0 /// @@ -692,28 +691,26 @@ namespace Bar /// /// The name to check if it's defined /// if a member with the name exists in the enumeration, otherwise - public static bool IsDefined(in global::System.ReadOnlySpan name) => IsDefined(name, allowMatchingMetadataAttribute: false); + public static bool IsDefined(in global::System.ReadOnlySpan name) + => name switch + { + global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::Bar.MyEnum.First), global::System.StringComparison.Ordinal) => true, + global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::Bar.MyEnum.Second), global::System.StringComparison.Ordinal) => true, + global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::Bar.MyEnum.Third), global::System.StringComparison.Ordinal) => true, + _ => false, + }; /// /// Returns a boolean telling whether an enum with the given name exists in the enumeration, - /// or optionally if a member decorated with a [Display] attribute - /// with the required name exists. - /// Slower then the overload, but doesn't allocate memory./> + /// or optionally if a member decorated with + /// exists. Slower then the overload, but doesn't allocate memory./> /// /// The name to check if it's defined /// If , considers the value of metadata attributes,otherwise ignores them /// if a member with the name exists in the enumeration, or a member is decorated /// with a [Display] attribute with the name, otherwise public static bool IsDefined(in global::System.ReadOnlySpan name, bool allowMatchingMetadataAttribute) - { - return name switch - { - global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::Bar.MyEnum.First), global::System.StringComparison.Ordinal) => true, - global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::Bar.MyEnum.Second), global::System.StringComparison.Ordinal) => true, - global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::Bar.MyEnum.Third), global::System.StringComparison.Ordinal) => true, - _ => false, - }; - } + => IsDefined(name); #endif /// @@ -825,8 +822,9 @@ namespace Bar /// If the parse operation fails, contains the default value of the underlying type /// of . This parameter is passed uninitialized. /// to read value in case insensitive mode; to read value in case sensitive mode. - /// If , considers the value included in metadata attributes such as - /// [Display] attribute when parsing, otherwise only considers the member names. + /// If , + /// considers the value included in attribute + /// when parsing, otherwise only considers the member names. /// if the value parameter was converted successfully; otherwise, . public static bool TryParse( #if NETCOREAPP3_0_OR_GREATER @@ -848,7 +846,6 @@ namespace Bar out global::Bar.MyEnum value, bool allowMatchingMetadataAttribute) { - switch (name) { case string s when s.Equals(nameof(global::Bar.MyEnum.First), global::System.StringComparison.OrdinalIgnoreCase): @@ -877,7 +874,6 @@ namespace Bar out global::Bar.MyEnum value, bool allowMatchingMetadataAttribute) { - switch (name) { case nameof(global::Bar.MyEnum.First): @@ -935,8 +931,8 @@ namespace Bar /// /// The case-sensitive string representation of the enumeration name or underlying value to convert /// to read value in case insensitive mode; to read value in case sensitive mode. - /// If , considers the value included in metadata attributes such as - /// [Display] attribute when parsing, otherwise only considers the member names. + /// If , considers the value included in + /// attribute when parsing, otherwise only considers the member names. /// An object of type whose /// value is represented by public static global::Bar.MyEnum Parse( @@ -1002,8 +998,8 @@ namespace Bar /// If the parse operation fails, contains the default value of the underlying type /// of . This parameter is passed uninitialized. /// to read value in case insensitive mode; to read value in case sensitive mode. - /// If , considers the value included in metadata attributes such as - /// [Display] attribute when parsing, otherwise only considers the member names. + /// If , considers the value included in + /// attribute when parsing, otherwise only considers the member names. /// if the value parameter was converted successfully; otherwise, . public static bool TryParse( #if NETCOREAPP3_0_OR_GREATER diff --git a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/InterceptorTests.CanInterceptEnumInDifferentNamespace.verified.txt b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/InterceptorTests.CanInterceptEnumInDifferentNamespace.verified.txt index 3497c80..13cff0b 100644 --- a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/InterceptorTests.CanInterceptEnumInDifferentNamespace.verified.txt +++ b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/InterceptorTests.CanInterceptEnumInDifferentNamespace.verified.txt @@ -1,4 +1,4 @@ -[ +[ //------------------------------------------------------------------------------ // // This code was generated by the NetEscapades.EnumGenerators source generator @@ -26,20 +26,6 @@ namespace Bar /// public const int Length = 3; - /// - /// Returns the string representation of the value. - /// If the attribute is decorated with a [Display] or [Description]attribute, then - /// uses the provided value. Otherwise uses the name of the member, equivalent to - /// calling ToString() on . - /// - /// The value to retrieve the string value for - /// If uses the value provided in the - /// [Display] or [Description]attribute as the string representation of the member. - /// If , always uses the name of the member, the same as if ToString() was called. - /// The string representation of the value - public static string ToStringFast(this global::Foo.MyEnum value, bool useMetadataAttributes) - => useMetadataAttributes ? value.ToStringFastWithMetadata() : value.ToStringFast(); - /// /// Returns the string representation of the value. /// Directly equivalent to calling ToString() on . @@ -55,6 +41,20 @@ namespace Bar _ => value.ToString(), }; + /// + /// Returns the string representation of the value. + /// If the member is decorated with the attribute + /// then that value is returned. Otherwise returns uses the name of the member, + /// equivalent to calling ToString() on . + /// + /// The value to retrieve the string value for + /// If uses the value provided in the + /// attribute as the string representation of the member. + /// If , always uses the name of the member, the same as if ToString() was called. + /// The string representation of the value + public static string ToStringFast(this global::Foo.MyEnum value, bool useMetadataAttributes) + => useMetadataAttributes ? value.ToStringFastWithMetadata() : value.ToStringFast(); + private static string ToStringFastWithMetadata(this global::Foo.MyEnum value) => value.ToStringFast(); @@ -99,27 +99,28 @@ namespace Bar /// /// The name to check if it's defined /// if a member with the name exists in the enumeration, otherwise - public static bool IsDefined(string name) => IsDefined(name, allowMatchingMetadataAttribute: false); + public static bool IsDefined(string name) + => name switch + { + nameof(global::Foo.MyEnum.First) => true, + nameof(global::Foo.MyEnum.Second) => true, + nameof(global::Foo.MyEnum.Third) => true, + _ => false, + }; /// /// Returns a boolean telling whether an enum with the given name exists in the enumeration, - /// or if a member decorated with a [Display] attribute + /// or if a member decorated with /// with the required name exists. /// /// The name to check if it's defined - /// If , considers the value of metadata attributes,otherwise ignores them + /// If , + /// considers the value of + /// instead of the member name, otherwise ignores them /// if a member with the name exists in the enumeration, or a member is decorated /// with a [Display] attribute with the name, otherwise public static bool IsDefined(string name, bool allowMatchingMetadataAttribute) - { - return name switch - { - nameof(global::Foo.MyEnum.First) => true, - nameof(global::Foo.MyEnum.Second) => true, - nameof(global::Foo.MyEnum.Third) => true, - _ => false, - }; - } + => IsDefined(name); #if NETCOREAPP && !NETCOREAPP2_0 && !NETCOREAPP1_1 && !NETCOREAPP1_0 /// @@ -127,28 +128,26 @@ namespace Bar /// /// The name to check if it's defined /// if a member with the name exists in the enumeration, otherwise - public static bool IsDefined(in global::System.ReadOnlySpan name) => IsDefined(name, allowMatchingMetadataAttribute: false); + public static bool IsDefined(in global::System.ReadOnlySpan name) + => name switch + { + global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::Foo.MyEnum.First), global::System.StringComparison.Ordinal) => true, + global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::Foo.MyEnum.Second), global::System.StringComparison.Ordinal) => true, + global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::Foo.MyEnum.Third), global::System.StringComparison.Ordinal) => true, + _ => false, + }; /// /// Returns a boolean telling whether an enum with the given name exists in the enumeration, - /// or optionally if a member decorated with a [Display] attribute - /// with the required name exists. - /// Slower then the overload, but doesn't allocate memory./> + /// or optionally if a member decorated with + /// exists. Slower then the overload, but doesn't allocate memory./> /// /// The name to check if it's defined /// If , considers the value of metadata attributes,otherwise ignores them /// if a member with the name exists in the enumeration, or a member is decorated /// with a [Display] attribute with the name, otherwise public static bool IsDefined(in global::System.ReadOnlySpan name, bool allowMatchingMetadataAttribute) - { - return name switch - { - global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::Foo.MyEnum.First), global::System.StringComparison.Ordinal) => true, - global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::Foo.MyEnum.Second), global::System.StringComparison.Ordinal) => true, - global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::Foo.MyEnum.Third), global::System.StringComparison.Ordinal) => true, - _ => false, - }; - } + => IsDefined(name); #endif /// @@ -260,8 +259,9 @@ namespace Bar /// If the parse operation fails, contains the default value of the underlying type /// of . This parameter is passed uninitialized. /// to read value in case insensitive mode; to read value in case sensitive mode. - /// If , considers the value included in metadata attributes such as - /// [Display] attribute when parsing, otherwise only considers the member names. + /// If , + /// considers the value included in attribute + /// when parsing, otherwise only considers the member names. /// if the value parameter was converted successfully; otherwise, . public static bool TryParse( #if NETCOREAPP3_0_OR_GREATER @@ -283,7 +283,6 @@ namespace Bar out global::Foo.MyEnum value, bool allowMatchingMetadataAttribute) { - switch (name) { case string s when s.Equals(nameof(global::Foo.MyEnum.First), global::System.StringComparison.OrdinalIgnoreCase): @@ -312,7 +311,6 @@ namespace Bar out global::Foo.MyEnum value, bool allowMatchingMetadataAttribute) { - switch (name) { case nameof(global::Foo.MyEnum.First): @@ -370,8 +368,8 @@ namespace Bar /// /// The case-sensitive string representation of the enumeration name or underlying value to convert /// to read value in case insensitive mode; to read value in case sensitive mode. - /// If , considers the value included in metadata attributes such as - /// [Display] attribute when parsing, otherwise only considers the member names. + /// If , considers the value included in + /// attribute when parsing, otherwise only considers the member names. /// An object of type whose /// value is represented by public static global::Foo.MyEnum Parse( @@ -437,8 +435,8 @@ namespace Bar /// If the parse operation fails, contains the default value of the underlying type /// of . This parameter is passed uninitialized. /// to read value in case insensitive mode; to read value in case sensitive mode. - /// If , considers the value included in metadata attributes such as - /// [Display] attribute when parsing, otherwise only considers the member names. + /// If , considers the value included in + /// attribute when parsing, otherwise only considers the member names. /// if the value parameter was converted successfully; otherwise, . public static bool TryParse( #if NETCOREAPP3_0_OR_GREATER diff --git a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/InterceptorTests.CanInterceptEnumInGlobalNamespace.verified.txt b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/InterceptorTests.CanInterceptEnumInGlobalNamespace.verified.txt index 8424c1b..c485eba 100644 --- a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/InterceptorTests.CanInterceptEnumInGlobalNamespace.verified.txt +++ b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/InterceptorTests.CanInterceptEnumInGlobalNamespace.verified.txt @@ -1,4 +1,4 @@ -[ +[ //------------------------------------------------------------------------------ // // This code was generated by the NetEscapades.EnumGenerators source generator @@ -24,20 +24,6 @@ /// public const int Length = 3; - /// - /// Returns the string representation of the value. - /// If the attribute is decorated with a [Display] or [Description]attribute, then - /// uses the provided value. Otherwise uses the name of the member, equivalent to - /// calling ToString() on . - /// - /// The value to retrieve the string value for - /// If uses the value provided in the - /// [Display] or [Description]attribute as the string representation of the member. - /// If , always uses the name of the member, the same as if ToString() was called. - /// The string representation of the value - public static string ToStringFast(this global::MyEnum value, bool useMetadataAttributes) - => useMetadataAttributes ? value.ToStringFastWithMetadata() : value.ToStringFast(); - /// /// Returns the string representation of the value. /// Directly equivalent to calling ToString() on . @@ -53,6 +39,20 @@ _ => value.ToString(), }; + /// + /// Returns the string representation of the value. + /// If the member is decorated with the attribute + /// then that value is returned. Otherwise returns uses the name of the member, + /// equivalent to calling ToString() on . + /// + /// The value to retrieve the string value for + /// If uses the value provided in the + /// attribute as the string representation of the member. + /// If , always uses the name of the member, the same as if ToString() was called. + /// The string representation of the value + public static string ToStringFast(this global::MyEnum value, bool useMetadataAttributes) + => useMetadataAttributes ? value.ToStringFastWithMetadata() : value.ToStringFast(); + private static string ToStringFastWithMetadata(this global::MyEnum value) => value.ToStringFast(); @@ -97,27 +97,28 @@ /// /// The name to check if it's defined /// if a member with the name exists in the enumeration, otherwise - public static bool IsDefined(string name) => IsDefined(name, allowMatchingMetadataAttribute: false); + public static bool IsDefined(string name) + => name switch + { + nameof(global::MyEnum.First) => true, + nameof(global::MyEnum.Second) => true, + nameof(global::MyEnum.Third) => true, + _ => false, + }; /// /// Returns a boolean telling whether an enum with the given name exists in the enumeration, - /// or if a member decorated with a [Display] attribute + /// or if a member decorated with /// with the required name exists. /// /// The name to check if it's defined - /// If , considers the value of metadata attributes,otherwise ignores them + /// If , + /// considers the value of + /// instead of the member name, otherwise ignores them /// if a member with the name exists in the enumeration, or a member is decorated /// with a [Display] attribute with the name, otherwise public static bool IsDefined(string name, bool allowMatchingMetadataAttribute) - { - return name switch - { - nameof(global::MyEnum.First) => true, - nameof(global::MyEnum.Second) => true, - nameof(global::MyEnum.Third) => true, - _ => false, - }; - } + => IsDefined(name); #if NETCOREAPP && !NETCOREAPP2_0 && !NETCOREAPP1_1 && !NETCOREAPP1_0 /// @@ -125,28 +126,26 @@ /// /// The name to check if it's defined /// if a member with the name exists in the enumeration, otherwise - public static bool IsDefined(in global::System.ReadOnlySpan name) => IsDefined(name, allowMatchingMetadataAttribute: false); + public static bool IsDefined(in global::System.ReadOnlySpan name) + => name switch + { + global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyEnum.First), global::System.StringComparison.Ordinal) => true, + global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyEnum.Second), global::System.StringComparison.Ordinal) => true, + global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyEnum.Third), global::System.StringComparison.Ordinal) => true, + _ => false, + }; /// /// Returns a boolean telling whether an enum with the given name exists in the enumeration, - /// or optionally if a member decorated with a [Display] attribute - /// with the required name exists. - /// Slower then the overload, but doesn't allocate memory./> + /// or optionally if a member decorated with + /// exists. Slower then the overload, but doesn't allocate memory./> /// /// The name to check if it's defined /// If , considers the value of metadata attributes,otherwise ignores them /// if a member with the name exists in the enumeration, or a member is decorated /// with a [Display] attribute with the name, otherwise public static bool IsDefined(in global::System.ReadOnlySpan name, bool allowMatchingMetadataAttribute) - { - return name switch - { - global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyEnum.First), global::System.StringComparison.Ordinal) => true, - global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyEnum.Second), global::System.StringComparison.Ordinal) => true, - global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyEnum.Third), global::System.StringComparison.Ordinal) => true, - _ => false, - }; - } + => IsDefined(name); #endif /// @@ -258,8 +257,9 @@ /// If the parse operation fails, contains the default value of the underlying type /// of . This parameter is passed uninitialized. /// to read value in case insensitive mode; to read value in case sensitive mode. - /// If , considers the value included in metadata attributes such as - /// [Display] attribute when parsing, otherwise only considers the member names. + /// If , + /// considers the value included in attribute + /// when parsing, otherwise only considers the member names. /// if the value parameter was converted successfully; otherwise, . public static bool TryParse( #if NETCOREAPP3_0_OR_GREATER @@ -281,7 +281,6 @@ out global::MyEnum value, bool allowMatchingMetadataAttribute) { - switch (name) { case string s when s.Equals(nameof(global::MyEnum.First), global::System.StringComparison.OrdinalIgnoreCase): @@ -310,7 +309,6 @@ out global::MyEnum value, bool allowMatchingMetadataAttribute) { - switch (name) { case nameof(global::MyEnum.First): @@ -368,8 +366,8 @@ /// /// The case-sensitive string representation of the enumeration name or underlying value to convert /// to read value in case insensitive mode; to read value in case sensitive mode. - /// If , considers the value included in metadata attributes such as - /// [Display] attribute when parsing, otherwise only considers the member names. + /// If , considers the value included in + /// attribute when parsing, otherwise only considers the member names. /// An object of type whose /// value is represented by public static global::MyEnum Parse( @@ -435,8 +433,8 @@ /// If the parse operation fails, contains the default value of the underlying type /// of . This parameter is passed uninitialized. /// to read value in case insensitive mode; to read value in case sensitive mode. - /// If , considers the value included in metadata attributes such as - /// [Display] attribute when parsing, otherwise only considers the member names. + /// If , considers the value included in + /// attribute when parsing, otherwise only considers the member names. /// if the value parameter was converted successfully; otherwise, . public static bool TryParse( #if NETCOREAPP3_0_OR_GREATER diff --git a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/InterceptorTests.CanInterceptExternalEnumToString.verified.txt b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/InterceptorTests.CanInterceptExternalEnumToString.verified.txt index 15263e0..2e9ffae 100644 --- a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/InterceptorTests.CanInterceptExternalEnumToString.verified.txt +++ b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/InterceptorTests.CanInterceptExternalEnumToString.verified.txt @@ -1,4 +1,4 @@ -[ +[ //------------------------------------------------------------------------------ // // This code was generated by the NetEscapades.EnumGenerators source generator @@ -26,20 +26,6 @@ namespace System /// public const int Length = 6; - /// - /// Returns the string representation of the value. - /// If the attribute is decorated with a [Display] or [Description]attribute, then - /// uses the provided value. Otherwise uses the name of the member, equivalent to - /// calling ToString() on . - /// - /// The value to retrieve the string value for - /// If uses the value provided in the - /// [Display] or [Description]attribute as the string representation of the member. - /// If , always uses the name of the member, the same as if ToString() was called. - /// The string representation of the value - public static string ToStringFast(this global::System.StringComparison value, bool useMetadataAttributes) - => useMetadataAttributes ? value.ToStringFastWithMetadata() : value.ToStringFast(); - /// /// Returns the string representation of the value. /// Directly equivalent to calling ToString() on . @@ -58,6 +44,20 @@ namespace System _ => value.AsUnderlyingType().ToString(), }; + /// + /// Returns the string representation of the value. + /// If the member is decorated with the attribute + /// then that value is returned. Otherwise returns uses the name of the member, + /// equivalent to calling ToString() on . + /// + /// The value to retrieve the string value for + /// If uses the value provided in the + /// attribute as the string representation of the member. + /// If , always uses the name of the member, the same as if ToString() was called. + /// The string representation of the value + public static string ToStringFast(this global::System.StringComparison value, bool useMetadataAttributes) + => useMetadataAttributes ? value.ToStringFastWithMetadata() : value.ToStringFast(); + private static string ToStringFastWithMetadata(this global::System.StringComparison value) => value.ToStringFast(); @@ -93,30 +93,31 @@ namespace System /// /// The name to check if it's defined /// if a member with the name exists in the enumeration, otherwise - public static bool IsDefined(string name) => IsDefined(name, allowMatchingMetadataAttribute: false); + public static bool IsDefined(string name) + => name switch + { + nameof(global::System.StringComparison.CurrentCulture) => true, + nameof(global::System.StringComparison.CurrentCultureIgnoreCase) => true, + nameof(global::System.StringComparison.InvariantCulture) => true, + nameof(global::System.StringComparison.InvariantCultureIgnoreCase) => true, + nameof(global::System.StringComparison.Ordinal) => true, + nameof(global::System.StringComparison.OrdinalIgnoreCase) => true, + _ => false, + }; /// /// Returns a boolean telling whether an enum with the given name exists in the enumeration, - /// or if a member decorated with a [Display] attribute + /// or if a member decorated with /// with the required name exists. /// /// The name to check if it's defined - /// If , considers the value of metadata attributes,otherwise ignores them + /// If , + /// considers the value of + /// instead of the member name, otherwise ignores them /// if a member with the name exists in the enumeration, or a member is decorated /// with a [Display] attribute with the name, otherwise public static bool IsDefined(string name, bool allowMatchingMetadataAttribute) - { - return name switch - { - nameof(global::System.StringComparison.CurrentCulture) => true, - nameof(global::System.StringComparison.CurrentCultureIgnoreCase) => true, - nameof(global::System.StringComparison.InvariantCulture) => true, - nameof(global::System.StringComparison.InvariantCultureIgnoreCase) => true, - nameof(global::System.StringComparison.Ordinal) => true, - nameof(global::System.StringComparison.OrdinalIgnoreCase) => true, - _ => false, - }; - } + => IsDefined(name); #if NETCOREAPP && !NETCOREAPP2_0 && !NETCOREAPP1_1 && !NETCOREAPP1_0 /// @@ -124,21 +125,8 @@ namespace System /// /// The name to check if it's defined /// if a member with the name exists in the enumeration, otherwise - public static bool IsDefined(in global::System.ReadOnlySpan name) => IsDefined(name, allowMatchingMetadataAttribute: false); - - /// - /// Returns a boolean telling whether an enum with the given name exists in the enumeration, - /// or optionally if a member decorated with a [Display] attribute - /// with the required name exists. - /// Slower then the overload, but doesn't allocate memory./> - /// - /// The name to check if it's defined - /// If , considers the value of metadata attributes,otherwise ignores them - /// if a member with the name exists in the enumeration, or a member is decorated - /// with a [Display] attribute with the name, otherwise - public static bool IsDefined(in global::System.ReadOnlySpan name, bool allowMatchingMetadataAttribute) - { - return name switch + public static bool IsDefined(in global::System.ReadOnlySpan name) + => name switch { global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::System.StringComparison.CurrentCulture), global::System.StringComparison.Ordinal) => true, global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::System.StringComparison.CurrentCultureIgnoreCase), global::System.StringComparison.Ordinal) => true, @@ -148,7 +136,18 @@ namespace System global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::System.StringComparison.OrdinalIgnoreCase), global::System.StringComparison.Ordinal) => true, _ => false, }; - } + + /// + /// Returns a boolean telling whether an enum with the given name exists in the enumeration, + /// or optionally if a member decorated with + /// exists. Slower then the overload, but doesn't allocate memory./> + /// + /// The name to check if it's defined + /// If , considers the value of metadata attributes,otherwise ignores them + /// if a member with the name exists in the enumeration, or a member is decorated + /// with a [Display] attribute with the name, otherwise + public static bool IsDefined(in global::System.ReadOnlySpan name, bool allowMatchingMetadataAttribute) + => IsDefined(name); #endif /// @@ -260,8 +259,9 @@ namespace System /// If the parse operation fails, contains the default value of the underlying type /// of . This parameter is passed uninitialized. /// to read value in case insensitive mode; to read value in case sensitive mode. - /// If , considers the value included in metadata attributes such as - /// [Display] attribute when parsing, otherwise only considers the member names. + /// If , + /// considers the value included in attribute + /// when parsing, otherwise only considers the member names. /// if the value parameter was converted successfully; otherwise, . public static bool TryParse( #if NETCOREAPP3_0_OR_GREATER @@ -283,7 +283,6 @@ namespace System out global::System.StringComparison value, bool allowMatchingMetadataAttribute) { - switch (name) { case string s when s.Equals(nameof(global::System.StringComparison.CurrentCulture), global::System.StringComparison.OrdinalIgnoreCase): @@ -321,7 +320,6 @@ namespace System out global::System.StringComparison value, bool allowMatchingMetadataAttribute) { - switch (name) { case nameof(global::System.StringComparison.CurrentCulture): @@ -388,8 +386,8 @@ namespace System /// /// The case-sensitive string representation of the enumeration name or underlying value to convert /// to read value in case insensitive mode; to read value in case sensitive mode. - /// If , considers the value included in metadata attributes such as - /// [Display] attribute when parsing, otherwise only considers the member names. + /// If , considers the value included in + /// attribute when parsing, otherwise only considers the member names. /// An object of type whose /// value is represented by public static global::System.StringComparison Parse( @@ -455,8 +453,8 @@ namespace System /// If the parse operation fails, contains the default value of the underlying type /// of . This parameter is passed uninitialized. /// to read value in case insensitive mode; to read value in case sensitive mode. - /// If , considers the value included in metadata attributes such as - /// [Display] attribute when parsing, otherwise only considers the member names. + /// If , considers the value included in + /// attribute when parsing, otherwise only considers the member names. /// if the value parameter was converted successfully; otherwise, . public static bool TryParse( #if NETCOREAPP3_0_OR_GREATER diff --git a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/InterceptorTests.CanInterceptHasFlag.verified.txt b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/InterceptorTests.CanInterceptHasFlag.verified.txt index 40585ac..0a74d6f 100644 --- a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/InterceptorTests.CanInterceptHasFlag.verified.txt +++ b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/InterceptorTests.CanInterceptHasFlag.verified.txt @@ -1,4 +1,4 @@ -[ +[ //------------------------------------------------------------------------------ // // This code was generated by the NetEscapades.EnumGenerators source generator @@ -26,20 +26,6 @@ namespace MyTestNameSpace /// public const int Length = 3; - /// - /// Returns the string representation of the value. - /// If the attribute is decorated with a [Display] or [Description]attribute, then - /// uses the provided value. Otherwise uses the name of the member, equivalent to - /// calling ToString() on . - /// - /// The value to retrieve the string value for - /// If uses the value provided in the - /// [Display] or [Description]attribute as the string representation of the member. - /// If , always uses the name of the member, the same as if ToString() was called. - /// The string representation of the value - public static string ToStringFast(this global::MyTestNameSpace.MyEnum value, bool useMetadataAttributes) - => useMetadataAttributes ? value.ToStringFastWithMetadata() : value.ToStringFast(); - /// /// Returns the string representation of the value. /// Directly equivalent to calling ToString() on . @@ -55,6 +41,20 @@ namespace MyTestNameSpace _ => value.ToString(), }; + /// + /// Returns the string representation of the value. + /// If the member is decorated with the attribute + /// then that value is returned. Otherwise returns uses the name of the member, + /// equivalent to calling ToString() on . + /// + /// The value to retrieve the string value for + /// If uses the value provided in the + /// attribute as the string representation of the member. + /// If , always uses the name of the member, the same as if ToString() was called. + /// The string representation of the value + public static string ToStringFast(this global::MyTestNameSpace.MyEnum value, bool useMetadataAttributes) + => useMetadataAttributes ? value.ToStringFastWithMetadata() : value.ToStringFast(); + private static string ToStringFastWithMetadata(this global::MyTestNameSpace.MyEnum value) => value.ToStringFast(); @@ -99,27 +99,28 @@ namespace MyTestNameSpace /// /// The name to check if it's defined /// if a member with the name exists in the enumeration, otherwise - public static bool IsDefined(string name) => IsDefined(name, allowMatchingMetadataAttribute: false); + public static bool IsDefined(string name) + => name switch + { + nameof(global::MyTestNameSpace.MyEnum.First) => true, + nameof(global::MyTestNameSpace.MyEnum.Second) => true, + nameof(global::MyTestNameSpace.MyEnum.Third) => true, + _ => false, + }; /// /// Returns a boolean telling whether an enum with the given name exists in the enumeration, - /// or if a member decorated with a [Display] attribute + /// or if a member decorated with /// with the required name exists. /// /// The name to check if it's defined - /// If , considers the value of metadata attributes,otherwise ignores them + /// If , + /// considers the value of + /// instead of the member name, otherwise ignores them /// if a member with the name exists in the enumeration, or a member is decorated /// with a [Display] attribute with the name, otherwise public static bool IsDefined(string name, bool allowMatchingMetadataAttribute) - { - return name switch - { - nameof(global::MyTestNameSpace.MyEnum.First) => true, - nameof(global::MyTestNameSpace.MyEnum.Second) => true, - nameof(global::MyTestNameSpace.MyEnum.Third) => true, - _ => false, - }; - } + => IsDefined(name); #if NETCOREAPP && !NETCOREAPP2_0 && !NETCOREAPP1_1 && !NETCOREAPP1_0 /// @@ -127,28 +128,26 @@ namespace MyTestNameSpace /// /// The name to check if it's defined /// if a member with the name exists in the enumeration, otherwise - public static bool IsDefined(in global::System.ReadOnlySpan name) => IsDefined(name, allowMatchingMetadataAttribute: false); + public static bool IsDefined(in global::System.ReadOnlySpan name) + => name switch + { + global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.First), global::System.StringComparison.Ordinal) => true, + global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.Second), global::System.StringComparison.Ordinal) => true, + global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.Third), global::System.StringComparison.Ordinal) => true, + _ => false, + }; /// /// Returns a boolean telling whether an enum with the given name exists in the enumeration, - /// or optionally if a member decorated with a [Display] attribute - /// with the required name exists. - /// Slower then the overload, but doesn't allocate memory./> + /// or optionally if a member decorated with + /// exists. Slower then the overload, but doesn't allocate memory./> /// /// The name to check if it's defined /// If , considers the value of metadata attributes,otherwise ignores them /// if a member with the name exists in the enumeration, or a member is decorated /// with a [Display] attribute with the name, otherwise public static bool IsDefined(in global::System.ReadOnlySpan name, bool allowMatchingMetadataAttribute) - { - return name switch - { - global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.First), global::System.StringComparison.Ordinal) => true, - global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.Second), global::System.StringComparison.Ordinal) => true, - global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.Third), global::System.StringComparison.Ordinal) => true, - _ => false, - }; - } + => IsDefined(name); #endif /// @@ -260,8 +259,9 @@ namespace MyTestNameSpace /// If the parse operation fails, contains the default value of the underlying type /// of . This parameter is passed uninitialized. /// to read value in case insensitive mode; to read value in case sensitive mode. - /// If , considers the value included in metadata attributes such as - /// [Display] attribute when parsing, otherwise only considers the member names. + /// If , + /// considers the value included in attribute + /// when parsing, otherwise only considers the member names. /// if the value parameter was converted successfully; otherwise, . public static bool TryParse( #if NETCOREAPP3_0_OR_GREATER @@ -283,7 +283,6 @@ namespace MyTestNameSpace out global::MyTestNameSpace.MyEnum value, bool allowMatchingMetadataAttribute) { - switch (name) { case string s when s.Equals(nameof(global::MyTestNameSpace.MyEnum.First), global::System.StringComparison.OrdinalIgnoreCase): @@ -312,7 +311,6 @@ namespace MyTestNameSpace out global::MyTestNameSpace.MyEnum value, bool allowMatchingMetadataAttribute) { - switch (name) { case nameof(global::MyTestNameSpace.MyEnum.First): @@ -370,8 +368,8 @@ namespace MyTestNameSpace /// /// The case-sensitive string representation of the enumeration name or underlying value to convert /// to read value in case insensitive mode; to read value in case sensitive mode. - /// If , considers the value included in metadata attributes such as - /// [Display] attribute when parsing, otherwise only considers the member names. + /// If , considers the value included in + /// attribute when parsing, otherwise only considers the member names. /// An object of type whose /// value is represented by public static global::MyTestNameSpace.MyEnum Parse( @@ -437,8 +435,8 @@ namespace MyTestNameSpace /// If the parse operation fails, contains the default value of the underlying type /// of . This parameter is passed uninitialized. /// to read value in case insensitive mode; to read value in case sensitive mode. - /// If , considers the value included in metadata attributes such as - /// [Display] attribute when parsing, otherwise only considers the member names. + /// If , considers the value included in + /// attribute when parsing, otherwise only considers the member names. /// if the value parameter was converted successfully; otherwise, . public static bool TryParse( #if NETCOREAPP3_0_OR_GREATER diff --git a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/InterceptorTests.CanInterceptMultipleEnumsToString.verified.txt b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/InterceptorTests.CanInterceptMultipleEnumsToString.verified.txt index 3e2b692..88ef307 100644 --- a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/InterceptorTests.CanInterceptMultipleEnumsToString.verified.txt +++ b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/InterceptorTests.CanInterceptMultipleEnumsToString.verified.txt @@ -1,4 +1,4 @@ -[ +[ //------------------------------------------------------------------------------ // // This code was generated by the NetEscapades.EnumGenerators source generator @@ -26,20 +26,6 @@ namespace MyTestNameSpace /// public const int Length = 3; - /// - /// Returns the string representation of the value. - /// If the attribute is decorated with a [Display] or [Description]attribute, then - /// uses the provided value. Otherwise uses the name of the member, equivalent to - /// calling ToString() on . - /// - /// The value to retrieve the string value for - /// If uses the value provided in the - /// [Display] or [Description]attribute as the string representation of the member. - /// If , always uses the name of the member, the same as if ToString() was called. - /// The string representation of the value - public static string ToStringFast(this global::MyTestNameSpace.MyEnum value, bool useMetadataAttributes) - => useMetadataAttributes ? value.ToStringFastWithMetadata() : value.ToStringFast(); - /// /// Returns the string representation of the value. /// Directly equivalent to calling ToString() on . @@ -55,6 +41,20 @@ namespace MyTestNameSpace _ => value.AsUnderlyingType().ToString(), }; + /// + /// Returns the string representation of the value. + /// If the member is decorated with the attribute + /// then that value is returned. Otherwise returns uses the name of the member, + /// equivalent to calling ToString() on . + /// + /// The value to retrieve the string value for + /// If uses the value provided in the + /// attribute as the string representation of the member. + /// If , always uses the name of the member, the same as if ToString() was called. + /// The string representation of the value + public static string ToStringFast(this global::MyTestNameSpace.MyEnum value, bool useMetadataAttributes) + => useMetadataAttributes ? value.ToStringFastWithMetadata() : value.ToStringFast(); + private static string ToStringFastWithMetadata(this global::MyTestNameSpace.MyEnum value) => value.ToStringFast(); @@ -87,27 +87,28 @@ namespace MyTestNameSpace /// /// The name to check if it's defined /// if a member with the name exists in the enumeration, otherwise - public static bool IsDefined(string name) => IsDefined(name, allowMatchingMetadataAttribute: false); + public static bool IsDefined(string name) + => name switch + { + nameof(global::MyTestNameSpace.MyEnum.First) => true, + nameof(global::MyTestNameSpace.MyEnum.Second) => true, + nameof(global::MyTestNameSpace.MyEnum.Third) => true, + _ => false, + }; /// /// Returns a boolean telling whether an enum with the given name exists in the enumeration, - /// or if a member decorated with a [Display] attribute + /// or if a member decorated with /// with the required name exists. /// /// The name to check if it's defined - /// If , considers the value of metadata attributes,otherwise ignores them + /// If , + /// considers the value of + /// instead of the member name, otherwise ignores them /// if a member with the name exists in the enumeration, or a member is decorated /// with a [Display] attribute with the name, otherwise public static bool IsDefined(string name, bool allowMatchingMetadataAttribute) - { - return name switch - { - nameof(global::MyTestNameSpace.MyEnum.First) => true, - nameof(global::MyTestNameSpace.MyEnum.Second) => true, - nameof(global::MyTestNameSpace.MyEnum.Third) => true, - _ => false, - }; - } + => IsDefined(name); #if NETCOREAPP && !NETCOREAPP2_0 && !NETCOREAPP1_1 && !NETCOREAPP1_0 /// @@ -115,28 +116,26 @@ namespace MyTestNameSpace /// /// The name to check if it's defined /// if a member with the name exists in the enumeration, otherwise - public static bool IsDefined(in global::System.ReadOnlySpan name) => IsDefined(name, allowMatchingMetadataAttribute: false); + public static bool IsDefined(in global::System.ReadOnlySpan name) + => name switch + { + global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.First), global::System.StringComparison.Ordinal) => true, + global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.Second), global::System.StringComparison.Ordinal) => true, + global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.Third), global::System.StringComparison.Ordinal) => true, + _ => false, + }; /// /// Returns a boolean telling whether an enum with the given name exists in the enumeration, - /// or optionally if a member decorated with a [Display] attribute - /// with the required name exists. - /// Slower then the overload, but doesn't allocate memory./> + /// or optionally if a member decorated with + /// exists. Slower then the overload, but doesn't allocate memory./> /// /// The name to check if it's defined /// If , considers the value of metadata attributes,otherwise ignores them /// if a member with the name exists in the enumeration, or a member is decorated /// with a [Display] attribute with the name, otherwise public static bool IsDefined(in global::System.ReadOnlySpan name, bool allowMatchingMetadataAttribute) - { - return name switch - { - global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.First), global::System.StringComparison.Ordinal) => true, - global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.Second), global::System.StringComparison.Ordinal) => true, - global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.Third), global::System.StringComparison.Ordinal) => true, - _ => false, - }; - } + => IsDefined(name); #endif /// @@ -248,8 +247,9 @@ namespace MyTestNameSpace /// If the parse operation fails, contains the default value of the underlying type /// of . This parameter is passed uninitialized. /// to read value in case insensitive mode; to read value in case sensitive mode. - /// If , considers the value included in metadata attributes such as - /// [Display] attribute when parsing, otherwise only considers the member names. + /// If , + /// considers the value included in attribute + /// when parsing, otherwise only considers the member names. /// if the value parameter was converted successfully; otherwise, . public static bool TryParse( #if NETCOREAPP3_0_OR_GREATER @@ -271,7 +271,6 @@ namespace MyTestNameSpace out global::MyTestNameSpace.MyEnum value, bool allowMatchingMetadataAttribute) { - switch (name) { case string s when s.Equals(nameof(global::MyTestNameSpace.MyEnum.First), global::System.StringComparison.OrdinalIgnoreCase): @@ -300,7 +299,6 @@ namespace MyTestNameSpace out global::MyTestNameSpace.MyEnum value, bool allowMatchingMetadataAttribute) { - switch (name) { case nameof(global::MyTestNameSpace.MyEnum.First): @@ -358,8 +356,8 @@ namespace MyTestNameSpace /// /// The case-sensitive string representation of the enumeration name or underlying value to convert /// to read value in case insensitive mode; to read value in case sensitive mode. - /// If , considers the value included in metadata attributes such as - /// [Display] attribute when parsing, otherwise only considers the member names. + /// If , considers the value included in + /// attribute when parsing, otherwise only considers the member names. /// An object of type whose /// value is represented by public static global::MyTestNameSpace.MyEnum Parse( @@ -425,8 +423,8 @@ namespace MyTestNameSpace /// If the parse operation fails, contains the default value of the underlying type /// of . This parameter is passed uninitialized. /// to read value in case insensitive mode; to read value in case sensitive mode. - /// If , considers the value included in metadata attributes such as - /// [Display] attribute when parsing, otherwise only considers the member names. + /// If , considers the value included in + /// attribute when parsing, otherwise only considers the member names. /// if the value parameter was converted successfully; otherwise, . public static bool TryParse( #if NETCOREAPP3_0_OR_GREATER @@ -579,20 +577,6 @@ namespace MyTestNameSpace /// public const int Length = 3; - /// - /// Returns the string representation of the value. - /// If the attribute is decorated with a [Display] or [Description]attribute, then - /// uses the provided value. Otherwise uses the name of the member, equivalent to - /// calling ToString() on . - /// - /// The value to retrieve the string value for - /// If uses the value provided in the - /// [Display] or [Description]attribute as the string representation of the member. - /// If , always uses the name of the member, the same as if ToString() was called. - /// The string representation of the value - public static string ToStringFast(this global::MyTestNameSpace.AnotherEnum value, bool useMetadataAttributes) - => useMetadataAttributes ? value.ToStringFastWithMetadata() : value.ToStringFast(); - /// /// Returns the string representation of the value. /// Directly equivalent to calling ToString() on . @@ -608,6 +592,20 @@ namespace MyTestNameSpace _ => value.AsUnderlyingType().ToString(), }; + /// + /// Returns the string representation of the value. + /// If the member is decorated with the attribute + /// then that value is returned. Otherwise returns uses the name of the member, + /// equivalent to calling ToString() on . + /// + /// The value to retrieve the string value for + /// If uses the value provided in the + /// attribute as the string representation of the member. + /// If , always uses the name of the member, the same as if ToString() was called. + /// The string representation of the value + public static string ToStringFast(this global::MyTestNameSpace.AnotherEnum value, bool useMetadataAttributes) + => useMetadataAttributes ? value.ToStringFastWithMetadata() : value.ToStringFast(); + private static string ToStringFastWithMetadata(this global::MyTestNameSpace.AnotherEnum value) => value.ToStringFast(); @@ -640,27 +638,28 @@ namespace MyTestNameSpace /// /// The name to check if it's defined /// if a member with the name exists in the enumeration, otherwise - public static bool IsDefined(string name) => IsDefined(name, allowMatchingMetadataAttribute: false); + public static bool IsDefined(string name) + => name switch + { + nameof(global::MyTestNameSpace.AnotherEnum.First) => true, + nameof(global::MyTestNameSpace.AnotherEnum.Second) => true, + nameof(global::MyTestNameSpace.AnotherEnum.Third) => true, + _ => false, + }; /// /// Returns a boolean telling whether an enum with the given name exists in the enumeration, - /// or if a member decorated with a [Display] attribute + /// or if a member decorated with /// with the required name exists. /// /// The name to check if it's defined - /// If , considers the value of metadata attributes,otherwise ignores them + /// If , + /// considers the value of + /// instead of the member name, otherwise ignores them /// if a member with the name exists in the enumeration, or a member is decorated /// with a [Display] attribute with the name, otherwise public static bool IsDefined(string name, bool allowMatchingMetadataAttribute) - { - return name switch - { - nameof(global::MyTestNameSpace.AnotherEnum.First) => true, - nameof(global::MyTestNameSpace.AnotherEnum.Second) => true, - nameof(global::MyTestNameSpace.AnotherEnum.Third) => true, - _ => false, - }; - } + => IsDefined(name); #if NETCOREAPP && !NETCOREAPP2_0 && !NETCOREAPP1_1 && !NETCOREAPP1_0 /// @@ -668,28 +667,26 @@ namespace MyTestNameSpace /// /// The name to check if it's defined /// if a member with the name exists in the enumeration, otherwise - public static bool IsDefined(in global::System.ReadOnlySpan name) => IsDefined(name, allowMatchingMetadataAttribute: false); + public static bool IsDefined(in global::System.ReadOnlySpan name) + => name switch + { + global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.AnotherEnum.First), global::System.StringComparison.Ordinal) => true, + global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.AnotherEnum.Second), global::System.StringComparison.Ordinal) => true, + global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.AnotherEnum.Third), global::System.StringComparison.Ordinal) => true, + _ => false, + }; /// /// Returns a boolean telling whether an enum with the given name exists in the enumeration, - /// or optionally if a member decorated with a [Display] attribute - /// with the required name exists. - /// Slower then the overload, but doesn't allocate memory./> + /// or optionally if a member decorated with + /// exists. Slower then the overload, but doesn't allocate memory./> /// /// The name to check if it's defined /// If , considers the value of metadata attributes,otherwise ignores them /// if a member with the name exists in the enumeration, or a member is decorated /// with a [Display] attribute with the name, otherwise public static bool IsDefined(in global::System.ReadOnlySpan name, bool allowMatchingMetadataAttribute) - { - return name switch - { - global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.AnotherEnum.First), global::System.StringComparison.Ordinal) => true, - global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.AnotherEnum.Second), global::System.StringComparison.Ordinal) => true, - global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.AnotherEnum.Third), global::System.StringComparison.Ordinal) => true, - _ => false, - }; - } + => IsDefined(name); #endif /// @@ -801,8 +798,9 @@ namespace MyTestNameSpace /// If the parse operation fails, contains the default value of the underlying type /// of . This parameter is passed uninitialized. /// to read value in case insensitive mode; to read value in case sensitive mode. - /// If , considers the value included in metadata attributes such as - /// [Display] attribute when parsing, otherwise only considers the member names. + /// If , + /// considers the value included in attribute + /// when parsing, otherwise only considers the member names. /// if the value parameter was converted successfully; otherwise, . public static bool TryParse( #if NETCOREAPP3_0_OR_GREATER @@ -824,7 +822,6 @@ namespace MyTestNameSpace out global::MyTestNameSpace.AnotherEnum value, bool allowMatchingMetadataAttribute) { - switch (name) { case string s when s.Equals(nameof(global::MyTestNameSpace.AnotherEnum.First), global::System.StringComparison.OrdinalIgnoreCase): @@ -853,7 +850,6 @@ namespace MyTestNameSpace out global::MyTestNameSpace.AnotherEnum value, bool allowMatchingMetadataAttribute) { - switch (name) { case nameof(global::MyTestNameSpace.AnotherEnum.First): @@ -911,8 +907,8 @@ namespace MyTestNameSpace /// /// The case-sensitive string representation of the enumeration name or underlying value to convert /// to read value in case insensitive mode; to read value in case sensitive mode. - /// If , considers the value included in metadata attributes such as - /// [Display] attribute when parsing, otherwise only considers the member names. + /// If , considers the value included in + /// attribute when parsing, otherwise only considers the member names. /// An object of type whose /// value is represented by public static global::MyTestNameSpace.AnotherEnum Parse( @@ -978,8 +974,8 @@ namespace MyTestNameSpace /// If the parse operation fails, contains the default value of the underlying type /// of . This parameter is passed uninitialized. /// to read value in case insensitive mode; to read value in case sensitive mode. - /// If , considers the value included in metadata attributes such as - /// [Display] attribute when parsing, otherwise only considers the member names. + /// If , considers the value included in + /// attribute when parsing, otherwise only considers the member names. /// if the value parameter was converted successfully; otherwise, . public static bool TryParse( #if NETCOREAPP3_0_OR_GREATER diff --git a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/InterceptorTests.CanInterceptToString.verified.txt b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/InterceptorTests.CanInterceptToString.verified.txt index 4e95299..b128a9b 100644 --- a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/InterceptorTests.CanInterceptToString.verified.txt +++ b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/InterceptorTests.CanInterceptToString.verified.txt @@ -1,4 +1,4 @@ -[ +[ //------------------------------------------------------------------------------ // // This code was generated by the NetEscapades.EnumGenerators source generator @@ -26,20 +26,6 @@ namespace MyTestNameSpace /// public const int Length = 2; - /// - /// Returns the string representation of the value. - /// If the attribute is decorated with a [Display] or [Description]attribute, then - /// uses the provided value. Otherwise uses the name of the member, equivalent to - /// calling ToString() on . - /// - /// The value to retrieve the string value for - /// If uses the value provided in the - /// [Display] or [Description]attribute as the string representation of the member. - /// If , always uses the name of the member, the same as if ToString() was called. - /// The string representation of the value - public static string ToStringFast(this global::MyTestNameSpace.MyEnum value, bool useMetadataAttributes) - => useMetadataAttributes ? value.ToStringFastWithMetadata() : value.ToStringFast(); - /// /// Returns the string representation of the value. /// Directly equivalent to calling ToString() on . @@ -54,6 +40,20 @@ namespace MyTestNameSpace _ => value.AsUnderlyingType().ToString(), }; + /// + /// Returns the string representation of the value. + /// If the member is decorated with the attribute + /// then that value is returned. Otherwise returns uses the name of the member, + /// equivalent to calling ToString() on . + /// + /// The value to retrieve the string value for + /// If uses the value provided in the + /// attribute as the string representation of the member. + /// If , always uses the name of the member, the same as if ToString() was called. + /// The string representation of the value + public static string ToStringFast(this global::MyTestNameSpace.MyEnum value, bool useMetadataAttributes) + => useMetadataAttributes ? value.ToStringFastWithMetadata() : value.ToStringFast(); + private static string ToStringFastWithMetadata(this global::MyTestNameSpace.MyEnum value) => value.ToStringFast(); @@ -85,26 +85,27 @@ namespace MyTestNameSpace /// /// The name to check if it's defined /// if a member with the name exists in the enumeration, otherwise - public static bool IsDefined(string name) => IsDefined(name, allowMatchingMetadataAttribute: false); + public static bool IsDefined(string name) + => name switch + { + nameof(global::MyTestNameSpace.MyEnum.First) => true, + nameof(global::MyTestNameSpace.MyEnum.Second) => true, + _ => false, + }; /// /// Returns a boolean telling whether an enum with the given name exists in the enumeration, - /// or if a member decorated with a [Display] attribute + /// or if a member decorated with /// with the required name exists. /// /// The name to check if it's defined - /// If , considers the value of metadata attributes,otherwise ignores them + /// If , + /// considers the value of + /// instead of the member name, otherwise ignores them /// if a member with the name exists in the enumeration, or a member is decorated /// with a [Display] attribute with the name, otherwise public static bool IsDefined(string name, bool allowMatchingMetadataAttribute) - { - return name switch - { - nameof(global::MyTestNameSpace.MyEnum.First) => true, - nameof(global::MyTestNameSpace.MyEnum.Second) => true, - _ => false, - }; - } + => IsDefined(name); #if NETCOREAPP && !NETCOREAPP2_0 && !NETCOREAPP1_1 && !NETCOREAPP1_0 /// @@ -112,27 +113,25 @@ namespace MyTestNameSpace /// /// The name to check if it's defined /// if a member with the name exists in the enumeration, otherwise - public static bool IsDefined(in global::System.ReadOnlySpan name) => IsDefined(name, allowMatchingMetadataAttribute: false); + public static bool IsDefined(in global::System.ReadOnlySpan name) + => name switch + { + global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.First), global::System.StringComparison.Ordinal) => true, + global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.Second), global::System.StringComparison.Ordinal) => true, + _ => false, + }; /// /// Returns a boolean telling whether an enum with the given name exists in the enumeration, - /// or optionally if a member decorated with a [Display] attribute - /// with the required name exists. - /// Slower then the overload, but doesn't allocate memory./> + /// or optionally if a member decorated with + /// exists. Slower then the overload, but doesn't allocate memory./> /// /// The name to check if it's defined /// If , considers the value of metadata attributes,otherwise ignores them /// if a member with the name exists in the enumeration, or a member is decorated /// with a [Display] attribute with the name, otherwise public static bool IsDefined(in global::System.ReadOnlySpan name, bool allowMatchingMetadataAttribute) - { - return name switch - { - global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.First), global::System.StringComparison.Ordinal) => true, - global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.Second), global::System.StringComparison.Ordinal) => true, - _ => false, - }; - } + => IsDefined(name); #endif /// @@ -244,8 +243,9 @@ namespace MyTestNameSpace /// If the parse operation fails, contains the default value of the underlying type /// of . This parameter is passed uninitialized. /// to read value in case insensitive mode; to read value in case sensitive mode. - /// If , considers the value included in metadata attributes such as - /// [Display] attribute when parsing, otherwise only considers the member names. + /// If , + /// considers the value included in attribute + /// when parsing, otherwise only considers the member names. /// if the value parameter was converted successfully; otherwise, . public static bool TryParse( #if NETCOREAPP3_0_OR_GREATER @@ -267,7 +267,6 @@ namespace MyTestNameSpace out global::MyTestNameSpace.MyEnum value, bool allowMatchingMetadataAttribute) { - switch (name) { case string s when s.Equals(nameof(global::MyTestNameSpace.MyEnum.First), global::System.StringComparison.OrdinalIgnoreCase): @@ -293,7 +292,6 @@ namespace MyTestNameSpace out global::MyTestNameSpace.MyEnum value, bool allowMatchingMetadataAttribute) { - switch (name) { case nameof(global::MyTestNameSpace.MyEnum.First): @@ -348,8 +346,8 @@ namespace MyTestNameSpace /// /// The case-sensitive string representation of the enumeration name or underlying value to convert /// to read value in case insensitive mode; to read value in case sensitive mode. - /// If , considers the value included in metadata attributes such as - /// [Display] attribute when parsing, otherwise only considers the member names. + /// If , considers the value included in + /// attribute when parsing, otherwise only considers the member names. /// An object of type whose /// value is represented by public static global::MyTestNameSpace.MyEnum Parse( @@ -415,8 +413,8 @@ namespace MyTestNameSpace /// If the parse operation fails, contains the default value of the underlying type /// of . This parameter is passed uninitialized. /// to read value in case insensitive mode; to read value in case sensitive mode. - /// If , considers the value included in metadata attributes such as - /// [Display] attribute when parsing, otherwise only considers the member names. + /// If , considers the value included in + /// attribute when parsing, otherwise only considers the member names. /// if the value parameter was converted successfully; otherwise, . public static bool TryParse( #if NETCOREAPP3_0_OR_GREATER diff --git a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/InterceptorTests.CanInterceptToStringWhenCsharp11.verified.txt b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/InterceptorTests.CanInterceptToStringWhenCsharp11.verified.txt index 4e95299..b128a9b 100644 --- a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/InterceptorTests.CanInterceptToStringWhenCsharp11.verified.txt +++ b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/InterceptorTests.CanInterceptToStringWhenCsharp11.verified.txt @@ -1,4 +1,4 @@ -[ +[ //------------------------------------------------------------------------------ // // This code was generated by the NetEscapades.EnumGenerators source generator @@ -26,20 +26,6 @@ namespace MyTestNameSpace /// public const int Length = 2; - /// - /// Returns the string representation of the value. - /// If the attribute is decorated with a [Display] or [Description]attribute, then - /// uses the provided value. Otherwise uses the name of the member, equivalent to - /// calling ToString() on . - /// - /// The value to retrieve the string value for - /// If uses the value provided in the - /// [Display] or [Description]attribute as the string representation of the member. - /// If , always uses the name of the member, the same as if ToString() was called. - /// The string representation of the value - public static string ToStringFast(this global::MyTestNameSpace.MyEnum value, bool useMetadataAttributes) - => useMetadataAttributes ? value.ToStringFastWithMetadata() : value.ToStringFast(); - /// /// Returns the string representation of the value. /// Directly equivalent to calling ToString() on . @@ -54,6 +40,20 @@ namespace MyTestNameSpace _ => value.AsUnderlyingType().ToString(), }; + /// + /// Returns the string representation of the value. + /// If the member is decorated with the attribute + /// then that value is returned. Otherwise returns uses the name of the member, + /// equivalent to calling ToString() on . + /// + /// The value to retrieve the string value for + /// If uses the value provided in the + /// attribute as the string representation of the member. + /// If , always uses the name of the member, the same as if ToString() was called. + /// The string representation of the value + public static string ToStringFast(this global::MyTestNameSpace.MyEnum value, bool useMetadataAttributes) + => useMetadataAttributes ? value.ToStringFastWithMetadata() : value.ToStringFast(); + private static string ToStringFastWithMetadata(this global::MyTestNameSpace.MyEnum value) => value.ToStringFast(); @@ -85,26 +85,27 @@ namespace MyTestNameSpace /// /// The name to check if it's defined /// if a member with the name exists in the enumeration, otherwise - public static bool IsDefined(string name) => IsDefined(name, allowMatchingMetadataAttribute: false); + public static bool IsDefined(string name) + => name switch + { + nameof(global::MyTestNameSpace.MyEnum.First) => true, + nameof(global::MyTestNameSpace.MyEnum.Second) => true, + _ => false, + }; /// /// Returns a boolean telling whether an enum with the given name exists in the enumeration, - /// or if a member decorated with a [Display] attribute + /// or if a member decorated with /// with the required name exists. /// /// The name to check if it's defined - /// If , considers the value of metadata attributes,otherwise ignores them + /// If , + /// considers the value of + /// instead of the member name, otherwise ignores them /// if a member with the name exists in the enumeration, or a member is decorated /// with a [Display] attribute with the name, otherwise public static bool IsDefined(string name, bool allowMatchingMetadataAttribute) - { - return name switch - { - nameof(global::MyTestNameSpace.MyEnum.First) => true, - nameof(global::MyTestNameSpace.MyEnum.Second) => true, - _ => false, - }; - } + => IsDefined(name); #if NETCOREAPP && !NETCOREAPP2_0 && !NETCOREAPP1_1 && !NETCOREAPP1_0 /// @@ -112,27 +113,25 @@ namespace MyTestNameSpace /// /// The name to check if it's defined /// if a member with the name exists in the enumeration, otherwise - public static bool IsDefined(in global::System.ReadOnlySpan name) => IsDefined(name, allowMatchingMetadataAttribute: false); + public static bool IsDefined(in global::System.ReadOnlySpan name) + => name switch + { + global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.First), global::System.StringComparison.Ordinal) => true, + global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.Second), global::System.StringComparison.Ordinal) => true, + _ => false, + }; /// /// Returns a boolean telling whether an enum with the given name exists in the enumeration, - /// or optionally if a member decorated with a [Display] attribute - /// with the required name exists. - /// Slower then the overload, but doesn't allocate memory./> + /// or optionally if a member decorated with + /// exists. Slower then the overload, but doesn't allocate memory./> /// /// The name to check if it's defined /// If , considers the value of metadata attributes,otherwise ignores them /// if a member with the name exists in the enumeration, or a member is decorated /// with a [Display] attribute with the name, otherwise public static bool IsDefined(in global::System.ReadOnlySpan name, bool allowMatchingMetadataAttribute) - { - return name switch - { - global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.First), global::System.StringComparison.Ordinal) => true, - global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.Second), global::System.StringComparison.Ordinal) => true, - _ => false, - }; - } + => IsDefined(name); #endif /// @@ -244,8 +243,9 @@ namespace MyTestNameSpace /// If the parse operation fails, contains the default value of the underlying type /// of . This parameter is passed uninitialized. /// to read value in case insensitive mode; to read value in case sensitive mode. - /// If , considers the value included in metadata attributes such as - /// [Display] attribute when parsing, otherwise only considers the member names. + /// If , + /// considers the value included in attribute + /// when parsing, otherwise only considers the member names. /// if the value parameter was converted successfully; otherwise, . public static bool TryParse( #if NETCOREAPP3_0_OR_GREATER @@ -267,7 +267,6 @@ namespace MyTestNameSpace out global::MyTestNameSpace.MyEnum value, bool allowMatchingMetadataAttribute) { - switch (name) { case string s when s.Equals(nameof(global::MyTestNameSpace.MyEnum.First), global::System.StringComparison.OrdinalIgnoreCase): @@ -293,7 +292,6 @@ namespace MyTestNameSpace out global::MyTestNameSpace.MyEnum value, bool allowMatchingMetadataAttribute) { - switch (name) { case nameof(global::MyTestNameSpace.MyEnum.First): @@ -348,8 +346,8 @@ namespace MyTestNameSpace /// /// The case-sensitive string representation of the enumeration name or underlying value to convert /// to read value in case insensitive mode; to read value in case sensitive mode. - /// If , considers the value included in metadata attributes such as - /// [Display] attribute when parsing, otherwise only considers the member names. + /// If , considers the value included in + /// attribute when parsing, otherwise only considers the member names. /// An object of type whose /// value is represented by public static global::MyTestNameSpace.MyEnum Parse( @@ -415,8 +413,8 @@ namespace MyTestNameSpace /// If the parse operation fails, contains the default value of the underlying type /// of . This parameter is passed uninitialized. /// to read value in case insensitive mode; to read value in case sensitive mode. - /// If , considers the value included in metadata attributes such as - /// [Display] attribute when parsing, otherwise only considers the member names. + /// If , considers the value included in + /// attribute when parsing, otherwise only considers the member names. /// if the value parameter was converted successfully; otherwise, . public static bool TryParse( #if NETCOREAPP3_0_OR_GREATER diff --git a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/InterceptorTests.CanInterceptUsingInterceptableAttributeToString.verified.txt b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/InterceptorTests.CanInterceptUsingInterceptableAttributeToString.verified.txt index ce84010..f0d8729 100644 --- a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/InterceptorTests.CanInterceptUsingInterceptableAttributeToString.verified.txt +++ b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/InterceptorTests.CanInterceptUsingInterceptableAttributeToString.verified.txt @@ -1,4 +1,4 @@ -[ +[ //------------------------------------------------------------------------------ // // This code was generated by the NetEscapades.EnumGenerators source generator @@ -26,20 +26,6 @@ namespace System /// public const int Length = 6; - /// - /// Returns the string representation of the value. - /// If the attribute is decorated with a [Display] or [Description]attribute, then - /// uses the provided value. Otherwise uses the name of the member, equivalent to - /// calling ToString() on . - /// - /// The value to retrieve the string value for - /// If uses the value provided in the - /// [Display] or [Description]attribute as the string representation of the member. - /// If , always uses the name of the member, the same as if ToString() was called. - /// The string representation of the value - public static string ToStringFast(this global::System.StringComparison value, bool useMetadataAttributes) - => useMetadataAttributes ? value.ToStringFastWithMetadata() : value.ToStringFast(); - /// /// Returns the string representation of the value. /// Directly equivalent to calling ToString() on . @@ -58,6 +44,20 @@ namespace System _ => value.AsUnderlyingType().ToString(), }; + /// + /// Returns the string representation of the value. + /// If the member is decorated with the attribute + /// then that value is returned. Otherwise returns uses the name of the member, + /// equivalent to calling ToString() on . + /// + /// The value to retrieve the string value for + /// If uses the value provided in the + /// attribute as the string representation of the member. + /// If , always uses the name of the member, the same as if ToString() was called. + /// The string representation of the value + public static string ToStringFast(this global::System.StringComparison value, bool useMetadataAttributes) + => useMetadataAttributes ? value.ToStringFastWithMetadata() : value.ToStringFast(); + private static string ToStringFastWithMetadata(this global::System.StringComparison value) => value.ToStringFast(); @@ -93,30 +93,31 @@ namespace System /// /// The name to check if it's defined /// if a member with the name exists in the enumeration, otherwise - public static bool IsDefined(string name) => IsDefined(name, allowMatchingMetadataAttribute: false); + public static bool IsDefined(string name) + => name switch + { + nameof(global::System.StringComparison.CurrentCulture) => true, + nameof(global::System.StringComparison.CurrentCultureIgnoreCase) => true, + nameof(global::System.StringComparison.InvariantCulture) => true, + nameof(global::System.StringComparison.InvariantCultureIgnoreCase) => true, + nameof(global::System.StringComparison.Ordinal) => true, + nameof(global::System.StringComparison.OrdinalIgnoreCase) => true, + _ => false, + }; /// /// Returns a boolean telling whether an enum with the given name exists in the enumeration, - /// or if a member decorated with a [Display] attribute + /// or if a member decorated with /// with the required name exists. /// /// The name to check if it's defined - /// If , considers the value of metadata attributes,otherwise ignores them + /// If , + /// considers the value of + /// instead of the member name, otherwise ignores them /// if a member with the name exists in the enumeration, or a member is decorated /// with a [Display] attribute with the name, otherwise public static bool IsDefined(string name, bool allowMatchingMetadataAttribute) - { - return name switch - { - nameof(global::System.StringComparison.CurrentCulture) => true, - nameof(global::System.StringComparison.CurrentCultureIgnoreCase) => true, - nameof(global::System.StringComparison.InvariantCulture) => true, - nameof(global::System.StringComparison.InvariantCultureIgnoreCase) => true, - nameof(global::System.StringComparison.Ordinal) => true, - nameof(global::System.StringComparison.OrdinalIgnoreCase) => true, - _ => false, - }; - } + => IsDefined(name); #if NETCOREAPP && !NETCOREAPP2_0 && !NETCOREAPP1_1 && !NETCOREAPP1_0 /// @@ -124,21 +125,8 @@ namespace System /// /// The name to check if it's defined /// if a member with the name exists in the enumeration, otherwise - public static bool IsDefined(in global::System.ReadOnlySpan name) => IsDefined(name, allowMatchingMetadataAttribute: false); - - /// - /// Returns a boolean telling whether an enum with the given name exists in the enumeration, - /// or optionally if a member decorated with a [Display] attribute - /// with the required name exists. - /// Slower then the overload, but doesn't allocate memory./> - /// - /// The name to check if it's defined - /// If , considers the value of metadata attributes,otherwise ignores them - /// if a member with the name exists in the enumeration, or a member is decorated - /// with a [Display] attribute with the name, otherwise - public static bool IsDefined(in global::System.ReadOnlySpan name, bool allowMatchingMetadataAttribute) - { - return name switch + public static bool IsDefined(in global::System.ReadOnlySpan name) + => name switch { global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::System.StringComparison.CurrentCulture), global::System.StringComparison.Ordinal) => true, global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::System.StringComparison.CurrentCultureIgnoreCase), global::System.StringComparison.Ordinal) => true, @@ -148,7 +136,18 @@ namespace System global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::System.StringComparison.OrdinalIgnoreCase), global::System.StringComparison.Ordinal) => true, _ => false, }; - } + + /// + /// Returns a boolean telling whether an enum with the given name exists in the enumeration, + /// or optionally if a member decorated with + /// exists. Slower then the overload, but doesn't allocate memory./> + /// + /// The name to check if it's defined + /// If , considers the value of metadata attributes,otherwise ignores them + /// if a member with the name exists in the enumeration, or a member is decorated + /// with a [Display] attribute with the name, otherwise + public static bool IsDefined(in global::System.ReadOnlySpan name, bool allowMatchingMetadataAttribute) + => IsDefined(name); #endif /// @@ -260,8 +259,9 @@ namespace System /// If the parse operation fails, contains the default value of the underlying type /// of . This parameter is passed uninitialized. /// to read value in case insensitive mode; to read value in case sensitive mode. - /// If , considers the value included in metadata attributes such as - /// [Display] attribute when parsing, otherwise only considers the member names. + /// If , + /// considers the value included in attribute + /// when parsing, otherwise only considers the member names. /// if the value parameter was converted successfully; otherwise, . public static bool TryParse( #if NETCOREAPP3_0_OR_GREATER @@ -283,7 +283,6 @@ namespace System out global::System.StringComparison value, bool allowMatchingMetadataAttribute) { - switch (name) { case string s when s.Equals(nameof(global::System.StringComparison.CurrentCulture), global::System.StringComparison.OrdinalIgnoreCase): @@ -321,7 +320,6 @@ namespace System out global::System.StringComparison value, bool allowMatchingMetadataAttribute) { - switch (name) { case nameof(global::System.StringComparison.CurrentCulture): @@ -388,8 +386,8 @@ namespace System /// /// The case-sensitive string representation of the enumeration name or underlying value to convert /// to read value in case insensitive mode; to read value in case sensitive mode. - /// If , considers the value included in metadata attributes such as - /// [Display] attribute when parsing, otherwise only considers the member names. + /// If , considers the value included in + /// attribute when parsing, otherwise only considers the member names. /// An object of type whose /// value is represented by public static global::System.StringComparison Parse( @@ -455,8 +453,8 @@ namespace System /// If the parse operation fails, contains the default value of the underlying type /// of . This parameter is passed uninitialized. /// to read value in case insensitive mode; to read value in case sensitive mode. - /// If , considers the value included in metadata attributes such as - /// [Display] attribute when parsing, otherwise only considers the member names. + /// If , considers the value included in + /// attribute when parsing, otherwise only considers the member names. /// if the value parameter was converted successfully; otherwise, . public static bool TryParse( #if NETCOREAPP3_0_OR_GREATER diff --git a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/InterceptorTests.CanNotInterceptEnumWithoutInterceptorPackage.verified.txt b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/InterceptorTests.CanNotInterceptEnumWithoutInterceptorPackage.verified.txt index 11509dd..79fb729 100644 --- a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/InterceptorTests.CanNotInterceptEnumWithoutInterceptorPackage.verified.txt +++ b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/InterceptorTests.CanNotInterceptEnumWithoutInterceptorPackage.verified.txt @@ -1,4 +1,4 @@ -[ +[ //------------------------------------------------------------------------------ // // This code was generated by the NetEscapades.EnumGenerators source generator @@ -26,20 +26,6 @@ namespace MyTestNameSpace /// public const int Length = 2; - /// - /// Returns the string representation of the value. - /// If the attribute is decorated with a [Display] or [Description]attribute, then - /// uses the provided value. Otherwise uses the name of the member, equivalent to - /// calling ToString() on . - /// - /// The value to retrieve the string value for - /// If uses the value provided in the - /// [Display] or [Description]attribute as the string representation of the member. - /// If , always uses the name of the member, the same as if ToString() was called. - /// The string representation of the value - public static string ToStringFast(this global::MyTestNameSpace.MyEnum value, bool useMetadataAttributes) - => useMetadataAttributes ? value.ToStringFastWithMetadata() : value.ToStringFast(); - /// /// Returns the string representation of the value. /// Directly equivalent to calling ToString() on . @@ -54,6 +40,20 @@ namespace MyTestNameSpace _ => value.AsUnderlyingType().ToString(), }; + /// + /// Returns the string representation of the value. + /// If the member is decorated with the attribute + /// then that value is returned. Otherwise returns uses the name of the member, + /// equivalent to calling ToString() on . + /// + /// The value to retrieve the string value for + /// If uses the value provided in the + /// attribute as the string representation of the member. + /// If , always uses the name of the member, the same as if ToString() was called. + /// The string representation of the value + public static string ToStringFast(this global::MyTestNameSpace.MyEnum value, bool useMetadataAttributes) + => useMetadataAttributes ? value.ToStringFastWithMetadata() : value.ToStringFast(); + private static string ToStringFastWithMetadata(this global::MyTestNameSpace.MyEnum value) => value.ToStringFast(); @@ -85,26 +85,27 @@ namespace MyTestNameSpace /// /// The name to check if it's defined /// if a member with the name exists in the enumeration, otherwise - public static bool IsDefined(string name) => IsDefined(name, allowMatchingMetadataAttribute: false); + public static bool IsDefined(string name) + => name switch + { + nameof(global::MyTestNameSpace.MyEnum.First) => true, + nameof(global::MyTestNameSpace.MyEnum.Second) => true, + _ => false, + }; /// /// Returns a boolean telling whether an enum with the given name exists in the enumeration, - /// or if a member decorated with a [Display] attribute + /// or if a member decorated with /// with the required name exists. /// /// The name to check if it's defined - /// If , considers the value of metadata attributes,otherwise ignores them + /// If , + /// considers the value of + /// instead of the member name, otherwise ignores them /// if a member with the name exists in the enumeration, or a member is decorated /// with a [Display] attribute with the name, otherwise public static bool IsDefined(string name, bool allowMatchingMetadataAttribute) - { - return name switch - { - nameof(global::MyTestNameSpace.MyEnum.First) => true, - nameof(global::MyTestNameSpace.MyEnum.Second) => true, - _ => false, - }; - } + => IsDefined(name); #if NETCOREAPP && !NETCOREAPP2_0 && !NETCOREAPP1_1 && !NETCOREAPP1_0 /// @@ -112,27 +113,25 @@ namespace MyTestNameSpace /// /// The name to check if it's defined /// if a member with the name exists in the enumeration, otherwise - public static bool IsDefined(in global::System.ReadOnlySpan name) => IsDefined(name, allowMatchingMetadataAttribute: false); + public static bool IsDefined(in global::System.ReadOnlySpan name) + => name switch + { + global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.First), global::System.StringComparison.Ordinal) => true, + global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.Second), global::System.StringComparison.Ordinal) => true, + _ => false, + }; /// /// Returns a boolean telling whether an enum with the given name exists in the enumeration, - /// or optionally if a member decorated with a [Display] attribute - /// with the required name exists. - /// Slower then the overload, but doesn't allocate memory./> + /// or optionally if a member decorated with + /// exists. Slower then the overload, but doesn't allocate memory./> /// /// The name to check if it's defined /// If , considers the value of metadata attributes,otherwise ignores them /// if a member with the name exists in the enumeration, or a member is decorated /// with a [Display] attribute with the name, otherwise public static bool IsDefined(in global::System.ReadOnlySpan name, bool allowMatchingMetadataAttribute) - { - return name switch - { - global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.First), global::System.StringComparison.Ordinal) => true, - global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.Second), global::System.StringComparison.Ordinal) => true, - _ => false, - }; - } + => IsDefined(name); #endif /// @@ -244,8 +243,9 @@ namespace MyTestNameSpace /// If the parse operation fails, contains the default value of the underlying type /// of . This parameter is passed uninitialized. /// to read value in case insensitive mode; to read value in case sensitive mode. - /// If , considers the value included in metadata attributes such as - /// [Display] attribute when parsing, otherwise only considers the member names. + /// If , + /// considers the value included in attribute + /// when parsing, otherwise only considers the member names. /// if the value parameter was converted successfully; otherwise, . public static bool TryParse( #if NETCOREAPP3_0_OR_GREATER @@ -267,7 +267,6 @@ namespace MyTestNameSpace out global::MyTestNameSpace.MyEnum value, bool allowMatchingMetadataAttribute) { - switch (name) { case string s when s.Equals(nameof(global::MyTestNameSpace.MyEnum.First), global::System.StringComparison.OrdinalIgnoreCase): @@ -293,7 +292,6 @@ namespace MyTestNameSpace out global::MyTestNameSpace.MyEnum value, bool allowMatchingMetadataAttribute) { - switch (name) { case nameof(global::MyTestNameSpace.MyEnum.First): @@ -348,8 +346,8 @@ namespace MyTestNameSpace /// /// The case-sensitive string representation of the enumeration name or underlying value to convert /// to read value in case insensitive mode; to read value in case sensitive mode. - /// If , considers the value included in metadata attributes such as - /// [Display] attribute when parsing, otherwise only considers the member names. + /// If , considers the value included in + /// attribute when parsing, otherwise only considers the member names. /// An object of type whose /// value is represented by public static global::MyTestNameSpace.MyEnum Parse( @@ -415,8 +413,8 @@ namespace MyTestNameSpace /// If the parse operation fails, contains the default value of the underlying type /// of . This parameter is passed uninitialized. /// to read value in case insensitive mode; to read value in case sensitive mode. - /// If , considers the value included in metadata attributes such as - /// [Display] attribute when parsing, otherwise only considers the member names. + /// If , considers the value included in + /// attribute when parsing, otherwise only considers the member names. /// if the value parameter was converted successfully; otherwise, . public static bool TryParse( #if NETCOREAPP3_0_OR_GREATER diff --git a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/InterceptorTests.CanNotInterceptUsageInStringInterpolation.verified.txt b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/InterceptorTests.CanNotInterceptUsageInStringInterpolation.verified.txt index 11509dd..79fb729 100644 --- a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/InterceptorTests.CanNotInterceptUsageInStringInterpolation.verified.txt +++ b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/InterceptorTests.CanNotInterceptUsageInStringInterpolation.verified.txt @@ -1,4 +1,4 @@ -[ +[ //------------------------------------------------------------------------------ // // This code was generated by the NetEscapades.EnumGenerators source generator @@ -26,20 +26,6 @@ namespace MyTestNameSpace /// public const int Length = 2; - /// - /// Returns the string representation of the value. - /// If the attribute is decorated with a [Display] or [Description]attribute, then - /// uses the provided value. Otherwise uses the name of the member, equivalent to - /// calling ToString() on . - /// - /// The value to retrieve the string value for - /// If uses the value provided in the - /// [Display] or [Description]attribute as the string representation of the member. - /// If , always uses the name of the member, the same as if ToString() was called. - /// The string representation of the value - public static string ToStringFast(this global::MyTestNameSpace.MyEnum value, bool useMetadataAttributes) - => useMetadataAttributes ? value.ToStringFastWithMetadata() : value.ToStringFast(); - /// /// Returns the string representation of the value. /// Directly equivalent to calling ToString() on . @@ -54,6 +40,20 @@ namespace MyTestNameSpace _ => value.AsUnderlyingType().ToString(), }; + /// + /// Returns the string representation of the value. + /// If the member is decorated with the attribute + /// then that value is returned. Otherwise returns uses the name of the member, + /// equivalent to calling ToString() on . + /// + /// The value to retrieve the string value for + /// If uses the value provided in the + /// attribute as the string representation of the member. + /// If , always uses the name of the member, the same as if ToString() was called. + /// The string representation of the value + public static string ToStringFast(this global::MyTestNameSpace.MyEnum value, bool useMetadataAttributes) + => useMetadataAttributes ? value.ToStringFastWithMetadata() : value.ToStringFast(); + private static string ToStringFastWithMetadata(this global::MyTestNameSpace.MyEnum value) => value.ToStringFast(); @@ -85,26 +85,27 @@ namespace MyTestNameSpace /// /// The name to check if it's defined /// if a member with the name exists in the enumeration, otherwise - public static bool IsDefined(string name) => IsDefined(name, allowMatchingMetadataAttribute: false); + public static bool IsDefined(string name) + => name switch + { + nameof(global::MyTestNameSpace.MyEnum.First) => true, + nameof(global::MyTestNameSpace.MyEnum.Second) => true, + _ => false, + }; /// /// Returns a boolean telling whether an enum with the given name exists in the enumeration, - /// or if a member decorated with a [Display] attribute + /// or if a member decorated with /// with the required name exists. /// /// The name to check if it's defined - /// If , considers the value of metadata attributes,otherwise ignores them + /// If , + /// considers the value of + /// instead of the member name, otherwise ignores them /// if a member with the name exists in the enumeration, or a member is decorated /// with a [Display] attribute with the name, otherwise public static bool IsDefined(string name, bool allowMatchingMetadataAttribute) - { - return name switch - { - nameof(global::MyTestNameSpace.MyEnum.First) => true, - nameof(global::MyTestNameSpace.MyEnum.Second) => true, - _ => false, - }; - } + => IsDefined(name); #if NETCOREAPP && !NETCOREAPP2_0 && !NETCOREAPP1_1 && !NETCOREAPP1_0 /// @@ -112,27 +113,25 @@ namespace MyTestNameSpace /// /// The name to check if it's defined /// if a member with the name exists in the enumeration, otherwise - public static bool IsDefined(in global::System.ReadOnlySpan name) => IsDefined(name, allowMatchingMetadataAttribute: false); + public static bool IsDefined(in global::System.ReadOnlySpan name) + => name switch + { + global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.First), global::System.StringComparison.Ordinal) => true, + global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.Second), global::System.StringComparison.Ordinal) => true, + _ => false, + }; /// /// Returns a boolean telling whether an enum with the given name exists in the enumeration, - /// or optionally if a member decorated with a [Display] attribute - /// with the required name exists. - /// Slower then the overload, but doesn't allocate memory./> + /// or optionally if a member decorated with + /// exists. Slower then the overload, but doesn't allocate memory./> /// /// The name to check if it's defined /// If , considers the value of metadata attributes,otherwise ignores them /// if a member with the name exists in the enumeration, or a member is decorated /// with a [Display] attribute with the name, otherwise public static bool IsDefined(in global::System.ReadOnlySpan name, bool allowMatchingMetadataAttribute) - { - return name switch - { - global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.First), global::System.StringComparison.Ordinal) => true, - global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.Second), global::System.StringComparison.Ordinal) => true, - _ => false, - }; - } + => IsDefined(name); #endif /// @@ -244,8 +243,9 @@ namespace MyTestNameSpace /// If the parse operation fails, contains the default value of the underlying type /// of . This parameter is passed uninitialized. /// to read value in case insensitive mode; to read value in case sensitive mode. - /// If , considers the value included in metadata attributes such as - /// [Display] attribute when parsing, otherwise only considers the member names. + /// If , + /// considers the value included in attribute + /// when parsing, otherwise only considers the member names. /// if the value parameter was converted successfully; otherwise, . public static bool TryParse( #if NETCOREAPP3_0_OR_GREATER @@ -267,7 +267,6 @@ namespace MyTestNameSpace out global::MyTestNameSpace.MyEnum value, bool allowMatchingMetadataAttribute) { - switch (name) { case string s when s.Equals(nameof(global::MyTestNameSpace.MyEnum.First), global::System.StringComparison.OrdinalIgnoreCase): @@ -293,7 +292,6 @@ namespace MyTestNameSpace out global::MyTestNameSpace.MyEnum value, bool allowMatchingMetadataAttribute) { - switch (name) { case nameof(global::MyTestNameSpace.MyEnum.First): @@ -348,8 +346,8 @@ namespace MyTestNameSpace /// /// The case-sensitive string representation of the enumeration name or underlying value to convert /// to read value in case insensitive mode; to read value in case sensitive mode. - /// If , considers the value included in metadata attributes such as - /// [Display] attribute when parsing, otherwise only considers the member names. + /// If , considers the value included in + /// attribute when parsing, otherwise only considers the member names. /// An object of type whose /// value is represented by public static global::MyTestNameSpace.MyEnum Parse( @@ -415,8 +413,8 @@ namespace MyTestNameSpace /// If the parse operation fails, contains the default value of the underlying type /// of . This parameter is passed uninitialized. /// to read value in case insensitive mode; to read value in case sensitive mode. - /// If , considers the value included in metadata attributes such as - /// [Display] attribute when parsing, otherwise only considers the member names. + /// If , considers the value included in + /// attribute when parsing, otherwise only considers the member names. /// if the value parameter was converted successfully; otherwise, . public static bool TryParse( #if NETCOREAPP3_0_OR_GREATER diff --git a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/InterceptorTests.CanNotInterceptUsageWithEnumDirectly.verified.txt b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/InterceptorTests.CanNotInterceptUsageWithEnumDirectly.verified.txt index 11509dd..79fb729 100644 --- a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/InterceptorTests.CanNotInterceptUsageWithEnumDirectly.verified.txt +++ b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/InterceptorTests.CanNotInterceptUsageWithEnumDirectly.verified.txt @@ -1,4 +1,4 @@ -[ +[ //------------------------------------------------------------------------------ // // This code was generated by the NetEscapades.EnumGenerators source generator @@ -26,20 +26,6 @@ namespace MyTestNameSpace /// public const int Length = 2; - /// - /// Returns the string representation of the value. - /// If the attribute is decorated with a [Display] or [Description]attribute, then - /// uses the provided value. Otherwise uses the name of the member, equivalent to - /// calling ToString() on . - /// - /// The value to retrieve the string value for - /// If uses the value provided in the - /// [Display] or [Description]attribute as the string representation of the member. - /// If , always uses the name of the member, the same as if ToString() was called. - /// The string representation of the value - public static string ToStringFast(this global::MyTestNameSpace.MyEnum value, bool useMetadataAttributes) - => useMetadataAttributes ? value.ToStringFastWithMetadata() : value.ToStringFast(); - /// /// Returns the string representation of the value. /// Directly equivalent to calling ToString() on . @@ -54,6 +40,20 @@ namespace MyTestNameSpace _ => value.AsUnderlyingType().ToString(), }; + /// + /// Returns the string representation of the value. + /// If the member is decorated with the attribute + /// then that value is returned. Otherwise returns uses the name of the member, + /// equivalent to calling ToString() on . + /// + /// The value to retrieve the string value for + /// If uses the value provided in the + /// attribute as the string representation of the member. + /// If , always uses the name of the member, the same as if ToString() was called. + /// The string representation of the value + public static string ToStringFast(this global::MyTestNameSpace.MyEnum value, bool useMetadataAttributes) + => useMetadataAttributes ? value.ToStringFastWithMetadata() : value.ToStringFast(); + private static string ToStringFastWithMetadata(this global::MyTestNameSpace.MyEnum value) => value.ToStringFast(); @@ -85,26 +85,27 @@ namespace MyTestNameSpace /// /// The name to check if it's defined /// if a member with the name exists in the enumeration, otherwise - public static bool IsDefined(string name) => IsDefined(name, allowMatchingMetadataAttribute: false); + public static bool IsDefined(string name) + => name switch + { + nameof(global::MyTestNameSpace.MyEnum.First) => true, + nameof(global::MyTestNameSpace.MyEnum.Second) => true, + _ => false, + }; /// /// Returns a boolean telling whether an enum with the given name exists in the enumeration, - /// or if a member decorated with a [Display] attribute + /// or if a member decorated with /// with the required name exists. /// /// The name to check if it's defined - /// If , considers the value of metadata attributes,otherwise ignores them + /// If , + /// considers the value of + /// instead of the member name, otherwise ignores them /// if a member with the name exists in the enumeration, or a member is decorated /// with a [Display] attribute with the name, otherwise public static bool IsDefined(string name, bool allowMatchingMetadataAttribute) - { - return name switch - { - nameof(global::MyTestNameSpace.MyEnum.First) => true, - nameof(global::MyTestNameSpace.MyEnum.Second) => true, - _ => false, - }; - } + => IsDefined(name); #if NETCOREAPP && !NETCOREAPP2_0 && !NETCOREAPP1_1 && !NETCOREAPP1_0 /// @@ -112,27 +113,25 @@ namespace MyTestNameSpace /// /// The name to check if it's defined /// if a member with the name exists in the enumeration, otherwise - public static bool IsDefined(in global::System.ReadOnlySpan name) => IsDefined(name, allowMatchingMetadataAttribute: false); + public static bool IsDefined(in global::System.ReadOnlySpan name) + => name switch + { + global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.First), global::System.StringComparison.Ordinal) => true, + global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.Second), global::System.StringComparison.Ordinal) => true, + _ => false, + }; /// /// Returns a boolean telling whether an enum with the given name exists in the enumeration, - /// or optionally if a member decorated with a [Display] attribute - /// with the required name exists. - /// Slower then the overload, but doesn't allocate memory./> + /// or optionally if a member decorated with + /// exists. Slower then the overload, but doesn't allocate memory./> /// /// The name to check if it's defined /// If , considers the value of metadata attributes,otherwise ignores them /// if a member with the name exists in the enumeration, or a member is decorated /// with a [Display] attribute with the name, otherwise public static bool IsDefined(in global::System.ReadOnlySpan name, bool allowMatchingMetadataAttribute) - { - return name switch - { - global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.First), global::System.StringComparison.Ordinal) => true, - global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.Second), global::System.StringComparison.Ordinal) => true, - _ => false, - }; - } + => IsDefined(name); #endif /// @@ -244,8 +243,9 @@ namespace MyTestNameSpace /// If the parse operation fails, contains the default value of the underlying type /// of . This parameter is passed uninitialized. /// to read value in case insensitive mode; to read value in case sensitive mode. - /// If , considers the value included in metadata attributes such as - /// [Display] attribute when parsing, otherwise only considers the member names. + /// If , + /// considers the value included in attribute + /// when parsing, otherwise only considers the member names. /// if the value parameter was converted successfully; otherwise, . public static bool TryParse( #if NETCOREAPP3_0_OR_GREATER @@ -267,7 +267,6 @@ namespace MyTestNameSpace out global::MyTestNameSpace.MyEnum value, bool allowMatchingMetadataAttribute) { - switch (name) { case string s when s.Equals(nameof(global::MyTestNameSpace.MyEnum.First), global::System.StringComparison.OrdinalIgnoreCase): @@ -293,7 +292,6 @@ namespace MyTestNameSpace out global::MyTestNameSpace.MyEnum value, bool allowMatchingMetadataAttribute) { - switch (name) { case nameof(global::MyTestNameSpace.MyEnum.First): @@ -348,8 +346,8 @@ namespace MyTestNameSpace /// /// The case-sensitive string representation of the enumeration name or underlying value to convert /// to read value in case insensitive mode; to read value in case sensitive mode. - /// If , considers the value included in metadata attributes such as - /// [Display] attribute when parsing, otherwise only considers the member names. + /// If , considers the value included in + /// attribute when parsing, otherwise only considers the member names. /// An object of type whose /// value is represented by public static global::MyTestNameSpace.MyEnum Parse( @@ -415,8 +413,8 @@ namespace MyTestNameSpace /// If the parse operation fails, contains the default value of the underlying type /// of . This parameter is passed uninitialized. /// to read value in case insensitive mode; to read value in case sensitive mode. - /// If , considers the value included in metadata attributes such as - /// [Display] attribute when parsing, otherwise only considers the member names. + /// If , considers the value included in + /// attribute when parsing, otherwise only considers the member names. /// if the value parameter was converted successfully; otherwise, . public static bool TryParse( #if NETCOREAPP3_0_OR_GREATER diff --git a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/InterceptorTests.DoesNotGenerateWarningsForUseOfObsoleteEnums_CS0612_Issue97.verified.txt b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/InterceptorTests.DoesNotGenerateWarningsForUseOfObsoleteEnums_CS0612_Issue97.verified.txt index b973781..70880e2 100644 --- a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/InterceptorTests.DoesNotGenerateWarningsForUseOfObsoleteEnums_CS0612_Issue97.verified.txt +++ b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/InterceptorTests.DoesNotGenerateWarningsForUseOfObsoleteEnums_CS0612_Issue97.verified.txt @@ -23,20 +23,6 @@ /// public const int Length = 2; - /// - /// Returns the string representation of the value. - /// If the attribute is decorated with a [Display] or [Description]attribute, then - /// uses the provided value. Otherwise uses the name of the member, equivalent to - /// calling ToString() on . - /// - /// The value to retrieve the string value for - /// If uses the value provided in the - /// [Display] or [Description]attribute as the string representation of the member. - /// If , always uses the name of the member, the same as if ToString() was called. - /// The string representation of the value - public static string ToStringFast(this global::OtherEnum value, bool useMetadataAttributes) - => useMetadataAttributes ? value.ToStringFastWithMetadata() : value.ToStringFast(); - /// /// Returns the string representation of the value. /// Directly equivalent to calling ToString() on . @@ -51,6 +37,20 @@ _ => value.AsUnderlyingType().ToString(), }; + /// + /// Returns the string representation of the value. + /// If the member is decorated with the attribute + /// then that value is returned. Otherwise returns uses the name of the member, + /// equivalent to calling ToString() on . + /// + /// The value to retrieve the string value for + /// If uses the value provided in the + /// attribute as the string representation of the member. + /// If , always uses the name of the member, the same as if ToString() was called. + /// The string representation of the value + public static string ToStringFast(this global::OtherEnum value, bool useMetadataAttributes) + => useMetadataAttributes ? value.ToStringFastWithMetadata() : value.ToStringFast(); + private static string ToStringFastWithMetadata(this global::OtherEnum value) => value.ToStringFast(); @@ -82,26 +82,27 @@ /// /// The name to check if it's defined /// if a member with the name exists in the enumeration, otherwise - public static bool IsDefined(string name) => IsDefined(name, allowMatchingMetadataAttribute: false); + public static bool IsDefined(string name) + => name switch + { + nameof(global::OtherEnum.First) => true, + nameof(global::OtherEnum.Second) => true, + _ => false, + }; /// /// Returns a boolean telling whether an enum with the given name exists in the enumeration, - /// or if a member decorated with a [Display] attribute + /// or if a member decorated with /// with the required name exists. /// /// The name to check if it's defined - /// If , considers the value of metadata attributes,otherwise ignores them + /// If , + /// considers the value of + /// instead of the member name, otherwise ignores them /// if a member with the name exists in the enumeration, or a member is decorated /// with a [Display] attribute with the name, otherwise public static bool IsDefined(string name, bool allowMatchingMetadataAttribute) - { - return name switch - { - nameof(global::OtherEnum.First) => true, - nameof(global::OtherEnum.Second) => true, - _ => false, - }; - } + => IsDefined(name); #if NETCOREAPP && !NETCOREAPP2_0 && !NETCOREAPP1_1 && !NETCOREAPP1_0 /// @@ -109,27 +110,25 @@ /// /// The name to check if it's defined /// if a member with the name exists in the enumeration, otherwise - public static bool IsDefined(in global::System.ReadOnlySpan name) => IsDefined(name, allowMatchingMetadataAttribute: false); + public static bool IsDefined(in global::System.ReadOnlySpan name) + => name switch + { + global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::OtherEnum.First), global::System.StringComparison.Ordinal) => true, + global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::OtherEnum.Second), global::System.StringComparison.Ordinal) => true, + _ => false, + }; /// /// Returns a boolean telling whether an enum with the given name exists in the enumeration, - /// or optionally if a member decorated with a [Display] attribute - /// with the required name exists. - /// Slower then the overload, but doesn't allocate memory./> + /// or optionally if a member decorated with + /// exists. Slower then the overload, but doesn't allocate memory./> /// /// The name to check if it's defined /// If , considers the value of metadata attributes,otherwise ignores them /// if a member with the name exists in the enumeration, or a member is decorated /// with a [Display] attribute with the name, otherwise public static bool IsDefined(in global::System.ReadOnlySpan name, bool allowMatchingMetadataAttribute) - { - return name switch - { - global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::OtherEnum.First), global::System.StringComparison.Ordinal) => true, - global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::OtherEnum.Second), global::System.StringComparison.Ordinal) => true, - _ => false, - }; - } + => IsDefined(name); #endif /// @@ -241,8 +240,9 @@ /// If the parse operation fails, contains the default value of the underlying type /// of . This parameter is passed uninitialized. /// to read value in case insensitive mode; to read value in case sensitive mode. - /// If , considers the value included in metadata attributes such as - /// [Display] attribute when parsing, otherwise only considers the member names. + /// If , + /// considers the value included in attribute + /// when parsing, otherwise only considers the member names. /// if the value parameter was converted successfully; otherwise, . public static bool TryParse( #if NETCOREAPP3_0_OR_GREATER @@ -264,7 +264,6 @@ out global::OtherEnum value, bool allowMatchingMetadataAttribute) { - switch (name) { case string s when s.Equals(nameof(global::OtherEnum.First), global::System.StringComparison.OrdinalIgnoreCase): @@ -290,7 +289,6 @@ out global::OtherEnum value, bool allowMatchingMetadataAttribute) { - switch (name) { case nameof(global::OtherEnum.First): @@ -345,8 +343,8 @@ /// /// The case-sensitive string representation of the enumeration name or underlying value to convert /// to read value in case insensitive mode; to read value in case sensitive mode. - /// If , considers the value included in metadata attributes such as - /// [Display] attribute when parsing, otherwise only considers the member names. + /// If , considers the value included in + /// attribute when parsing, otherwise only considers the member names. /// An object of type whose /// value is represented by public static global::OtherEnum Parse( @@ -412,8 +410,8 @@ /// If the parse operation fails, contains the default value of the underlying type /// of . This parameter is passed uninitialized. /// to read value in case insensitive mode; to read value in case sensitive mode. - /// If , considers the value included in metadata attributes such as - /// [Display] attribute when parsing, otherwise only considers the member names. + /// If , considers the value included in + /// attribute when parsing, otherwise only considers the member names. /// if the value parameter was converted successfully; otherwise, . public static bool TryParse( #if NETCOREAPP3_0_OR_GREATER diff --git a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/InterceptorTests.DoesNotGenerateWarningsForUseOfObsoleteEnums_CS0618_Issue97.verified.txt b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/InterceptorTests.DoesNotGenerateWarningsForUseOfObsoleteEnums_CS0618_Issue97.verified.txt index 4fe0e50..70880e2 100644 --- a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/InterceptorTests.DoesNotGenerateWarningsForUseOfObsoleteEnums_CS0618_Issue97.verified.txt +++ b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/InterceptorTests.DoesNotGenerateWarningsForUseOfObsoleteEnums_CS0618_Issue97.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the NetEscapades.EnumGenerators source generator // @@ -23,20 +23,6 @@ /// public const int Length = 2; - /// - /// Returns the string representation of the value. - /// If the attribute is decorated with a [Display] or [Description]attribute, then - /// uses the provided value. Otherwise uses the name of the member, equivalent to - /// calling ToString() on . - /// - /// The value to retrieve the string value for - /// If uses the value provided in the - /// [Display] or [Description]attribute as the string representation of the member. - /// If , always uses the name of the member, the same as if ToString() was called. - /// The string representation of the value - public static string ToStringFast(this global::OtherEnum value, bool useMetadataAttributes) - => useMetadataAttributes ? value.ToStringFastWithMetadata() : value.ToStringFast(); - /// /// Returns the string representation of the value. /// Directly equivalent to calling ToString() on . @@ -51,6 +37,20 @@ _ => value.AsUnderlyingType().ToString(), }; + /// + /// Returns the string representation of the value. + /// If the member is decorated with the attribute + /// then that value is returned. Otherwise returns uses the name of the member, + /// equivalent to calling ToString() on . + /// + /// The value to retrieve the string value for + /// If uses the value provided in the + /// attribute as the string representation of the member. + /// If , always uses the name of the member, the same as if ToString() was called. + /// The string representation of the value + public static string ToStringFast(this global::OtherEnum value, bool useMetadataAttributes) + => useMetadataAttributes ? value.ToStringFastWithMetadata() : value.ToStringFast(); + private static string ToStringFastWithMetadata(this global::OtherEnum value) => value.ToStringFast(); @@ -82,26 +82,27 @@ /// /// The name to check if it's defined /// if a member with the name exists in the enumeration, otherwise - public static bool IsDefined(string name) => IsDefined(name, allowMatchingMetadataAttribute: false); + public static bool IsDefined(string name) + => name switch + { + nameof(global::OtherEnum.First) => true, + nameof(global::OtherEnum.Second) => true, + _ => false, + }; /// /// Returns a boolean telling whether an enum with the given name exists in the enumeration, - /// or if a member decorated with a [Display] attribute + /// or if a member decorated with /// with the required name exists. /// /// The name to check if it's defined - /// If , considers the value of metadata attributes,otherwise ignores them + /// If , + /// considers the value of + /// instead of the member name, otherwise ignores them /// if a member with the name exists in the enumeration, or a member is decorated /// with a [Display] attribute with the name, otherwise public static bool IsDefined(string name, bool allowMatchingMetadataAttribute) - { - return name switch - { - nameof(global::OtherEnum.First) => true, - nameof(global::OtherEnum.Second) => true, - _ => false, - }; - } + => IsDefined(name); #if NETCOREAPP && !NETCOREAPP2_0 && !NETCOREAPP1_1 && !NETCOREAPP1_0 /// @@ -109,27 +110,25 @@ /// /// The name to check if it's defined /// if a member with the name exists in the enumeration, otherwise - public static bool IsDefined(in global::System.ReadOnlySpan name) => IsDefined(name, allowMatchingMetadataAttribute: false); + public static bool IsDefined(in global::System.ReadOnlySpan name) + => name switch + { + global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::OtherEnum.First), global::System.StringComparison.Ordinal) => true, + global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::OtherEnum.Second), global::System.StringComparison.Ordinal) => true, + _ => false, + }; /// /// Returns a boolean telling whether an enum with the given name exists in the enumeration, - /// or optionally if a member decorated with a [Display] attribute - /// with the required name exists. - /// Slower then the overload, but doesn't allocate memory./> + /// or optionally if a member decorated with + /// exists. Slower then the overload, but doesn't allocate memory./> /// /// The name to check if it's defined /// If , considers the value of metadata attributes,otherwise ignores them /// if a member with the name exists in the enumeration, or a member is decorated /// with a [Display] attribute with the name, otherwise public static bool IsDefined(in global::System.ReadOnlySpan name, bool allowMatchingMetadataAttribute) - { - return name switch - { - global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::OtherEnum.First), global::System.StringComparison.Ordinal) => true, - global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::OtherEnum.Second), global::System.StringComparison.Ordinal) => true, - _ => false, - }; - } + => IsDefined(name); #endif /// @@ -241,8 +240,9 @@ /// If the parse operation fails, contains the default value of the underlying type /// of . This parameter is passed uninitialized. /// to read value in case insensitive mode; to read value in case sensitive mode. - /// If , considers the value included in metadata attributes such as - /// [Display] attribute when parsing, otherwise only considers the member names. + /// If , + /// considers the value included in attribute + /// when parsing, otherwise only considers the member names. /// if the value parameter was converted successfully; otherwise, . public static bool TryParse( #if NETCOREAPP3_0_OR_GREATER @@ -264,7 +264,6 @@ out global::OtherEnum value, bool allowMatchingMetadataAttribute) { - switch (name) { case string s when s.Equals(nameof(global::OtherEnum.First), global::System.StringComparison.OrdinalIgnoreCase): @@ -290,7 +289,6 @@ out global::OtherEnum value, bool allowMatchingMetadataAttribute) { - switch (name) { case nameof(global::OtherEnum.First): @@ -345,8 +343,8 @@ /// /// The case-sensitive string representation of the enumeration name or underlying value to convert /// to read value in case insensitive mode; to read value in case sensitive mode. - /// If , considers the value included in metadata attributes such as - /// [Display] attribute when parsing, otherwise only considers the member names. + /// If , considers the value included in + /// attribute when parsing, otherwise only considers the member names. /// An object of type whose /// value is represented by public static global::OtherEnum Parse( @@ -412,8 +410,8 @@ /// If the parse operation fails, contains the default value of the underlying type /// of . This parameter is passed uninitialized. /// to read value in case insensitive mode; to read value in case sensitive mode. - /// If , considers the value included in metadata attributes such as - /// [Display] attribute when parsing, otherwise only considers the member names. + /// If , considers the value included in + /// attribute when parsing, otherwise only considers the member names. /// if the value parameter was converted successfully; otherwise, . public static bool TryParse( #if NETCOREAPP3_0_OR_GREATER diff --git a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/InterceptorTests.DoesNotInterceptEnumMarkedAsNotInterceptable.verified.txt b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/InterceptorTests.DoesNotInterceptEnumMarkedAsNotInterceptable.verified.txt index 06c722e..46bff39 100644 --- a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/InterceptorTests.DoesNotInterceptEnumMarkedAsNotInterceptable.verified.txt +++ b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/InterceptorTests.DoesNotInterceptEnumMarkedAsNotInterceptable.verified.txt @@ -1,4 +1,4 @@ -[ +[ //------------------------------------------------------------------------------ // // This code was generated by the NetEscapades.EnumGenerators source generator @@ -26,20 +26,6 @@ namespace MyTestNameSpace /// public const int Length = 3; - /// - /// Returns the string representation of the value. - /// If the attribute is decorated with a [Display] or [Description]attribute, then - /// uses the provided value. Otherwise uses the name of the member, equivalent to - /// calling ToString() on . - /// - /// The value to retrieve the string value for - /// If uses the value provided in the - /// [Display] or [Description]attribute as the string representation of the member. - /// If , always uses the name of the member, the same as if ToString() was called. - /// The string representation of the value - public static string ToStringFast(this global::MyTestNameSpace.MyEnum value, bool useMetadataAttributes) - => useMetadataAttributes ? value.ToStringFastWithMetadata() : value.ToStringFast(); - /// /// Returns the string representation of the value. /// Directly equivalent to calling ToString() on . @@ -55,6 +41,20 @@ namespace MyTestNameSpace _ => value.AsUnderlyingType().ToString(), }; + /// + /// Returns the string representation of the value. + /// If the member is decorated with the attribute + /// then that value is returned. Otherwise returns uses the name of the member, + /// equivalent to calling ToString() on . + /// + /// The value to retrieve the string value for + /// If uses the value provided in the + /// attribute as the string representation of the member. + /// If , always uses the name of the member, the same as if ToString() was called. + /// The string representation of the value + public static string ToStringFast(this global::MyTestNameSpace.MyEnum value, bool useMetadataAttributes) + => useMetadataAttributes ? value.ToStringFastWithMetadata() : value.ToStringFast(); + private static string ToStringFastWithMetadata(this global::MyTestNameSpace.MyEnum value) => value.ToStringFast(); @@ -87,27 +87,28 @@ namespace MyTestNameSpace /// /// The name to check if it's defined /// if a member with the name exists in the enumeration, otherwise - public static bool IsDefined(string name) => IsDefined(name, allowMatchingMetadataAttribute: false); + public static bool IsDefined(string name) + => name switch + { + nameof(global::MyTestNameSpace.MyEnum.First) => true, + nameof(global::MyTestNameSpace.MyEnum.Second) => true, + nameof(global::MyTestNameSpace.MyEnum.Third) => true, + _ => false, + }; /// /// Returns a boolean telling whether an enum with the given name exists in the enumeration, - /// or if a member decorated with a [Display] attribute + /// or if a member decorated with /// with the required name exists. /// /// The name to check if it's defined - /// If , considers the value of metadata attributes,otherwise ignores them + /// If , + /// considers the value of + /// instead of the member name, otherwise ignores them /// if a member with the name exists in the enumeration, or a member is decorated /// with a [Display] attribute with the name, otherwise public static bool IsDefined(string name, bool allowMatchingMetadataAttribute) - { - return name switch - { - nameof(global::MyTestNameSpace.MyEnum.First) => true, - nameof(global::MyTestNameSpace.MyEnum.Second) => true, - nameof(global::MyTestNameSpace.MyEnum.Third) => true, - _ => false, - }; - } + => IsDefined(name); #if NETCOREAPP && !NETCOREAPP2_0 && !NETCOREAPP1_1 && !NETCOREAPP1_0 /// @@ -115,28 +116,26 @@ namespace MyTestNameSpace /// /// The name to check if it's defined /// if a member with the name exists in the enumeration, otherwise - public static bool IsDefined(in global::System.ReadOnlySpan name) => IsDefined(name, allowMatchingMetadataAttribute: false); + public static bool IsDefined(in global::System.ReadOnlySpan name) + => name switch + { + global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.First), global::System.StringComparison.Ordinal) => true, + global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.Second), global::System.StringComparison.Ordinal) => true, + global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.Third), global::System.StringComparison.Ordinal) => true, + _ => false, + }; /// /// Returns a boolean telling whether an enum with the given name exists in the enumeration, - /// or optionally if a member decorated with a [Display] attribute - /// with the required name exists. - /// Slower then the overload, but doesn't allocate memory./> + /// or optionally if a member decorated with + /// exists. Slower then the overload, but doesn't allocate memory./> /// /// The name to check if it's defined /// If , considers the value of metadata attributes,otherwise ignores them /// if a member with the name exists in the enumeration, or a member is decorated /// with a [Display] attribute with the name, otherwise public static bool IsDefined(in global::System.ReadOnlySpan name, bool allowMatchingMetadataAttribute) - { - return name switch - { - global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.First), global::System.StringComparison.Ordinal) => true, - global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.Second), global::System.StringComparison.Ordinal) => true, - global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.Third), global::System.StringComparison.Ordinal) => true, - _ => false, - }; - } + => IsDefined(name); #endif /// @@ -248,8 +247,9 @@ namespace MyTestNameSpace /// If the parse operation fails, contains the default value of the underlying type /// of . This parameter is passed uninitialized. /// to read value in case insensitive mode; to read value in case sensitive mode. - /// If , considers the value included in metadata attributes such as - /// [Display] attribute when parsing, otherwise only considers the member names. + /// If , + /// considers the value included in attribute + /// when parsing, otherwise only considers the member names. /// if the value parameter was converted successfully; otherwise, . public static bool TryParse( #if NETCOREAPP3_0_OR_GREATER @@ -271,7 +271,6 @@ namespace MyTestNameSpace out global::MyTestNameSpace.MyEnum value, bool allowMatchingMetadataAttribute) { - switch (name) { case string s when s.Equals(nameof(global::MyTestNameSpace.MyEnum.First), global::System.StringComparison.OrdinalIgnoreCase): @@ -300,7 +299,6 @@ namespace MyTestNameSpace out global::MyTestNameSpace.MyEnum value, bool allowMatchingMetadataAttribute) { - switch (name) { case nameof(global::MyTestNameSpace.MyEnum.First): @@ -358,8 +356,8 @@ namespace MyTestNameSpace /// /// The case-sensitive string representation of the enumeration name or underlying value to convert /// to read value in case insensitive mode; to read value in case sensitive mode. - /// If , considers the value included in metadata attributes such as - /// [Display] attribute when parsing, otherwise only considers the member names. + /// If , considers the value included in + /// attribute when parsing, otherwise only considers the member names. /// An object of type whose /// value is represented by public static global::MyTestNameSpace.MyEnum Parse( @@ -425,8 +423,8 @@ namespace MyTestNameSpace /// If the parse operation fails, contains the default value of the underlying type /// of . This parameter is passed uninitialized. /// to read value in case insensitive mode; to read value in case sensitive mode. - /// If , considers the value included in metadata attributes such as - /// [Display] attribute when parsing, otherwise only considers the member names. + /// If , considers the value included in + /// attribute when parsing, otherwise only considers the member names. /// if the value parameter was converted successfully; otherwise, . public static bool TryParse( #if NETCOREAPP3_0_OR_GREATER @@ -579,20 +577,6 @@ namespace MyTestNameSpace /// public const int Length = 3; - /// - /// Returns the string representation of the value. - /// If the attribute is decorated with a [Display] or [Description]attribute, then - /// uses the provided value. Otherwise uses the name of the member, equivalent to - /// calling ToString() on . - /// - /// The value to retrieve the string value for - /// If uses the value provided in the - /// [Display] or [Description]attribute as the string representation of the member. - /// If , always uses the name of the member, the same as if ToString() was called. - /// The string representation of the value - public static string ToStringFast(this global::MyTestNameSpace.AnotherEnum value, bool useMetadataAttributes) - => useMetadataAttributes ? value.ToStringFastWithMetadata() : value.ToStringFast(); - /// /// Returns the string representation of the value. /// Directly equivalent to calling ToString() on . @@ -608,6 +592,20 @@ namespace MyTestNameSpace _ => value.ToString(), }; + /// + /// Returns the string representation of the value. + /// If the member is decorated with the attribute + /// then that value is returned. Otherwise returns uses the name of the member, + /// equivalent to calling ToString() on . + /// + /// The value to retrieve the string value for + /// If uses the value provided in the + /// attribute as the string representation of the member. + /// If , always uses the name of the member, the same as if ToString() was called. + /// The string representation of the value + public static string ToStringFast(this global::MyTestNameSpace.AnotherEnum value, bool useMetadataAttributes) + => useMetadataAttributes ? value.ToStringFastWithMetadata() : value.ToStringFast(); + private static string ToStringFastWithMetadata(this global::MyTestNameSpace.AnotherEnum value) => value.ToStringFast(); @@ -652,27 +650,28 @@ namespace MyTestNameSpace /// /// The name to check if it's defined /// if a member with the name exists in the enumeration, otherwise - public static bool IsDefined(string name) => IsDefined(name, allowMatchingMetadataAttribute: false); + public static bool IsDefined(string name) + => name switch + { + nameof(global::MyTestNameSpace.AnotherEnum.First) => true, + nameof(global::MyTestNameSpace.AnotherEnum.Second) => true, + nameof(global::MyTestNameSpace.AnotherEnum.Third) => true, + _ => false, + }; /// /// Returns a boolean telling whether an enum with the given name exists in the enumeration, - /// or if a member decorated with a [Display] attribute + /// or if a member decorated with /// with the required name exists. /// /// The name to check if it's defined - /// If , considers the value of metadata attributes,otherwise ignores them + /// If , + /// considers the value of + /// instead of the member name, otherwise ignores them /// if a member with the name exists in the enumeration, or a member is decorated /// with a [Display] attribute with the name, otherwise public static bool IsDefined(string name, bool allowMatchingMetadataAttribute) - { - return name switch - { - nameof(global::MyTestNameSpace.AnotherEnum.First) => true, - nameof(global::MyTestNameSpace.AnotherEnum.Second) => true, - nameof(global::MyTestNameSpace.AnotherEnum.Third) => true, - _ => false, - }; - } + => IsDefined(name); #if NETCOREAPP && !NETCOREAPP2_0 && !NETCOREAPP1_1 && !NETCOREAPP1_0 /// @@ -680,28 +679,26 @@ namespace MyTestNameSpace /// /// The name to check if it's defined /// if a member with the name exists in the enumeration, otherwise - public static bool IsDefined(in global::System.ReadOnlySpan name) => IsDefined(name, allowMatchingMetadataAttribute: false); + public static bool IsDefined(in global::System.ReadOnlySpan name) + => name switch + { + global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.AnotherEnum.First), global::System.StringComparison.Ordinal) => true, + global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.AnotherEnum.Second), global::System.StringComparison.Ordinal) => true, + global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.AnotherEnum.Third), global::System.StringComparison.Ordinal) => true, + _ => false, + }; /// /// Returns a boolean telling whether an enum with the given name exists in the enumeration, - /// or optionally if a member decorated with a [Display] attribute - /// with the required name exists. - /// Slower then the overload, but doesn't allocate memory./> + /// or optionally if a member decorated with + /// exists. Slower then the overload, but doesn't allocate memory./> /// /// The name to check if it's defined /// If , considers the value of metadata attributes,otherwise ignores them /// if a member with the name exists in the enumeration, or a member is decorated /// with a [Display] attribute with the name, otherwise public static bool IsDefined(in global::System.ReadOnlySpan name, bool allowMatchingMetadataAttribute) - { - return name switch - { - global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.AnotherEnum.First), global::System.StringComparison.Ordinal) => true, - global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.AnotherEnum.Second), global::System.StringComparison.Ordinal) => true, - global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.AnotherEnum.Third), global::System.StringComparison.Ordinal) => true, - _ => false, - }; - } + => IsDefined(name); #endif /// @@ -813,8 +810,9 @@ namespace MyTestNameSpace /// If the parse operation fails, contains the default value of the underlying type /// of . This parameter is passed uninitialized. /// to read value in case insensitive mode; to read value in case sensitive mode. - /// If , considers the value included in metadata attributes such as - /// [Display] attribute when parsing, otherwise only considers the member names. + /// If , + /// considers the value included in attribute + /// when parsing, otherwise only considers the member names. /// if the value parameter was converted successfully; otherwise, . public static bool TryParse( #if NETCOREAPP3_0_OR_GREATER @@ -836,7 +834,6 @@ namespace MyTestNameSpace out global::MyTestNameSpace.AnotherEnum value, bool allowMatchingMetadataAttribute) { - switch (name) { case string s when s.Equals(nameof(global::MyTestNameSpace.AnotherEnum.First), global::System.StringComparison.OrdinalIgnoreCase): @@ -865,7 +862,6 @@ namespace MyTestNameSpace out global::MyTestNameSpace.AnotherEnum value, bool allowMatchingMetadataAttribute) { - switch (name) { case nameof(global::MyTestNameSpace.AnotherEnum.First): @@ -923,8 +919,8 @@ namespace MyTestNameSpace /// /// The case-sensitive string representation of the enumeration name or underlying value to convert /// to read value in case insensitive mode; to read value in case sensitive mode. - /// If , considers the value included in metadata attributes such as - /// [Display] attribute when parsing, otherwise only considers the member names. + /// If , considers the value included in + /// attribute when parsing, otherwise only considers the member names. /// An object of type whose /// value is represented by public static global::MyTestNameSpace.AnotherEnum Parse( @@ -990,8 +986,8 @@ namespace MyTestNameSpace /// If the parse operation fails, contains the default value of the underlying type /// of . This parameter is passed uninitialized. /// to read value in case insensitive mode; to read value in case sensitive mode. - /// If , considers the value included in metadata attributes such as - /// [Display] attribute when parsing, otherwise only considers the member names. + /// If , considers the value included in + /// attribute when parsing, otherwise only considers the member names. /// if the value parameter was converted successfully; otherwise, . public static bool TryParse( #if NETCOREAPP3_0_OR_GREATER diff --git a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/InterceptorTests.DoesNotInterceptExternalEnumMarkedAsNotInterceptable.verified.txt b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/InterceptorTests.DoesNotInterceptExternalEnumMarkedAsNotInterceptable.verified.txt index e67f3c4..1c667f9 100644 --- a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/InterceptorTests.DoesNotInterceptExternalEnumMarkedAsNotInterceptable.verified.txt +++ b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/InterceptorTests.DoesNotInterceptExternalEnumMarkedAsNotInterceptable.verified.txt @@ -1,4 +1,4 @@ -[ +[ //------------------------------------------------------------------------------ // // This code was generated by the NetEscapades.EnumGenerators source generator @@ -26,20 +26,6 @@ namespace System /// public const int Length = 6; - /// - /// Returns the string representation of the value. - /// If the attribute is decorated with a [Display] or [Description]attribute, then - /// uses the provided value. Otherwise uses the name of the member, equivalent to - /// calling ToString() on . - /// - /// The value to retrieve the string value for - /// If uses the value provided in the - /// [Display] or [Description]attribute as the string representation of the member. - /// If , always uses the name of the member, the same as if ToString() was called. - /// The string representation of the value - public static string ToStringFast(this global::System.StringComparison value, bool useMetadataAttributes) - => useMetadataAttributes ? value.ToStringFastWithMetadata() : value.ToStringFast(); - /// /// Returns the string representation of the value. /// Directly equivalent to calling ToString() on . @@ -58,6 +44,20 @@ namespace System _ => value.AsUnderlyingType().ToString(), }; + /// + /// Returns the string representation of the value. + /// If the member is decorated with the attribute + /// then that value is returned. Otherwise returns uses the name of the member, + /// equivalent to calling ToString() on . + /// + /// The value to retrieve the string value for + /// If uses the value provided in the + /// attribute as the string representation of the member. + /// If , always uses the name of the member, the same as if ToString() was called. + /// The string representation of the value + public static string ToStringFast(this global::System.StringComparison value, bool useMetadataAttributes) + => useMetadataAttributes ? value.ToStringFastWithMetadata() : value.ToStringFast(); + private static string ToStringFastWithMetadata(this global::System.StringComparison value) => value.ToStringFast(); @@ -93,30 +93,31 @@ namespace System /// /// The name to check if it's defined /// if a member with the name exists in the enumeration, otherwise - public static bool IsDefined(string name) => IsDefined(name, allowMatchingMetadataAttribute: false); + public static bool IsDefined(string name) + => name switch + { + nameof(global::System.StringComparison.CurrentCulture) => true, + nameof(global::System.StringComparison.CurrentCultureIgnoreCase) => true, + nameof(global::System.StringComparison.InvariantCulture) => true, + nameof(global::System.StringComparison.InvariantCultureIgnoreCase) => true, + nameof(global::System.StringComparison.Ordinal) => true, + nameof(global::System.StringComparison.OrdinalIgnoreCase) => true, + _ => false, + }; /// /// Returns a boolean telling whether an enum with the given name exists in the enumeration, - /// or if a member decorated with a [Display] attribute + /// or if a member decorated with /// with the required name exists. /// /// The name to check if it's defined - /// If , considers the value of metadata attributes,otherwise ignores them + /// If , + /// considers the value of + /// instead of the member name, otherwise ignores them /// if a member with the name exists in the enumeration, or a member is decorated /// with a [Display] attribute with the name, otherwise public static bool IsDefined(string name, bool allowMatchingMetadataAttribute) - { - return name switch - { - nameof(global::System.StringComparison.CurrentCulture) => true, - nameof(global::System.StringComparison.CurrentCultureIgnoreCase) => true, - nameof(global::System.StringComparison.InvariantCulture) => true, - nameof(global::System.StringComparison.InvariantCultureIgnoreCase) => true, - nameof(global::System.StringComparison.Ordinal) => true, - nameof(global::System.StringComparison.OrdinalIgnoreCase) => true, - _ => false, - }; - } + => IsDefined(name); #if NETCOREAPP && !NETCOREAPP2_0 && !NETCOREAPP1_1 && !NETCOREAPP1_0 /// @@ -124,21 +125,8 @@ namespace System /// /// The name to check if it's defined /// if a member with the name exists in the enumeration, otherwise - public static bool IsDefined(in global::System.ReadOnlySpan name) => IsDefined(name, allowMatchingMetadataAttribute: false); - - /// - /// Returns a boolean telling whether an enum with the given name exists in the enumeration, - /// or optionally if a member decorated with a [Display] attribute - /// with the required name exists. - /// Slower then the overload, but doesn't allocate memory./> - /// - /// The name to check if it's defined - /// If , considers the value of metadata attributes,otherwise ignores them - /// if a member with the name exists in the enumeration, or a member is decorated - /// with a [Display] attribute with the name, otherwise - public static bool IsDefined(in global::System.ReadOnlySpan name, bool allowMatchingMetadataAttribute) - { - return name switch + public static bool IsDefined(in global::System.ReadOnlySpan name) + => name switch { global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::System.StringComparison.CurrentCulture), global::System.StringComparison.Ordinal) => true, global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::System.StringComparison.CurrentCultureIgnoreCase), global::System.StringComparison.Ordinal) => true, @@ -148,7 +136,18 @@ namespace System global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::System.StringComparison.OrdinalIgnoreCase), global::System.StringComparison.Ordinal) => true, _ => false, }; - } + + /// + /// Returns a boolean telling whether an enum with the given name exists in the enumeration, + /// or optionally if a member decorated with + /// exists. Slower then the overload, but doesn't allocate memory./> + /// + /// The name to check if it's defined + /// If , considers the value of metadata attributes,otherwise ignores them + /// if a member with the name exists in the enumeration, or a member is decorated + /// with a [Display] attribute with the name, otherwise + public static bool IsDefined(in global::System.ReadOnlySpan name, bool allowMatchingMetadataAttribute) + => IsDefined(name); #endif /// @@ -260,8 +259,9 @@ namespace System /// If the parse operation fails, contains the default value of the underlying type /// of . This parameter is passed uninitialized. /// to read value in case insensitive mode; to read value in case sensitive mode. - /// If , considers the value included in metadata attributes such as - /// [Display] attribute when parsing, otherwise only considers the member names. + /// If , + /// considers the value included in attribute + /// when parsing, otherwise only considers the member names. /// if the value parameter was converted successfully; otherwise, . public static bool TryParse( #if NETCOREAPP3_0_OR_GREATER @@ -283,7 +283,6 @@ namespace System out global::System.StringComparison value, bool allowMatchingMetadataAttribute) { - switch (name) { case string s when s.Equals(nameof(global::System.StringComparison.CurrentCulture), global::System.StringComparison.OrdinalIgnoreCase): @@ -321,7 +320,6 @@ namespace System out global::System.StringComparison value, bool allowMatchingMetadataAttribute) { - switch (name) { case nameof(global::System.StringComparison.CurrentCulture): @@ -388,8 +386,8 @@ namespace System /// /// The case-sensitive string representation of the enumeration name or underlying value to convert /// to read value in case insensitive mode; to read value in case sensitive mode. - /// If , considers the value included in metadata attributes such as - /// [Display] attribute when parsing, otherwise only considers the member names. + /// If , considers the value included in + /// attribute when parsing, otherwise only considers the member names. /// An object of type whose /// value is represented by public static global::System.StringComparison Parse( @@ -455,8 +453,8 @@ namespace System /// If the parse operation fails, contains the default value of the underlying type /// of . This parameter is passed uninitialized. /// to read value in case insensitive mode; to read value in case sensitive mode. - /// If , considers the value included in metadata attributes such as - /// [Display] attribute when parsing, otherwise only considers the member names. + /// If , considers the value included in + /// attribute when parsing, otherwise only considers the member names. /// if the value parameter was converted successfully; otherwise, . public static bool TryParse( #if NETCOREAPP3_0_OR_GREATER @@ -636,20 +634,6 @@ namespace System /// public const int Length = 3; - /// - /// Returns the string representation of the value. - /// If the attribute is decorated with a [Display] or [Description]attribute, then - /// uses the provided value. Otherwise uses the name of the member, equivalent to - /// calling ToString() on . - /// - /// The value to retrieve the string value for - /// If uses the value provided in the - /// [Display] or [Description]attribute as the string representation of the member. - /// If , always uses the name of the member, the same as if ToString() was called. - /// The string representation of the value - public static string ToStringFast(this global::System.DateTimeKind value, bool useMetadataAttributes) - => useMetadataAttributes ? value.ToStringFastWithMetadata() : value.ToStringFast(); - /// /// Returns the string representation of the value. /// Directly equivalent to calling ToString() on . @@ -665,6 +649,20 @@ namespace System _ => value.AsUnderlyingType().ToString(), }; + /// + /// Returns the string representation of the value. + /// If the member is decorated with the attribute + /// then that value is returned. Otherwise returns uses the name of the member, + /// equivalent to calling ToString() on . + /// + /// The value to retrieve the string value for + /// If uses the value provided in the + /// attribute as the string representation of the member. + /// If , always uses the name of the member, the same as if ToString() was called. + /// The string representation of the value + public static string ToStringFast(this global::System.DateTimeKind value, bool useMetadataAttributes) + => useMetadataAttributes ? value.ToStringFastWithMetadata() : value.ToStringFast(); + private static string ToStringFastWithMetadata(this global::System.DateTimeKind value) => value.ToStringFast(); @@ -697,27 +695,28 @@ namespace System /// /// The name to check if it's defined /// if a member with the name exists in the enumeration, otherwise - public static bool IsDefined(string name) => IsDefined(name, allowMatchingMetadataAttribute: false); + public static bool IsDefined(string name) + => name switch + { + nameof(global::System.DateTimeKind.Unspecified) => true, + nameof(global::System.DateTimeKind.Utc) => true, + nameof(global::System.DateTimeKind.Local) => true, + _ => false, + }; /// /// Returns a boolean telling whether an enum with the given name exists in the enumeration, - /// or if a member decorated with a [Display] attribute + /// or if a member decorated with /// with the required name exists. /// /// The name to check if it's defined - /// If , considers the value of metadata attributes,otherwise ignores them + /// If , + /// considers the value of + /// instead of the member name, otherwise ignores them /// if a member with the name exists in the enumeration, or a member is decorated /// with a [Display] attribute with the name, otherwise public static bool IsDefined(string name, bool allowMatchingMetadataAttribute) - { - return name switch - { - nameof(global::System.DateTimeKind.Unspecified) => true, - nameof(global::System.DateTimeKind.Utc) => true, - nameof(global::System.DateTimeKind.Local) => true, - _ => false, - }; - } + => IsDefined(name); #if NETCOREAPP && !NETCOREAPP2_0 && !NETCOREAPP1_1 && !NETCOREAPP1_0 /// @@ -725,28 +724,26 @@ namespace System /// /// The name to check if it's defined /// if a member with the name exists in the enumeration, otherwise - public static bool IsDefined(in global::System.ReadOnlySpan name) => IsDefined(name, allowMatchingMetadataAttribute: false); + public static bool IsDefined(in global::System.ReadOnlySpan name) + => name switch + { + global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::System.DateTimeKind.Unspecified), global::System.StringComparison.Ordinal) => true, + global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::System.DateTimeKind.Utc), global::System.StringComparison.Ordinal) => true, + global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::System.DateTimeKind.Local), global::System.StringComparison.Ordinal) => true, + _ => false, + }; /// /// Returns a boolean telling whether an enum with the given name exists in the enumeration, - /// or optionally if a member decorated with a [Display] attribute - /// with the required name exists. - /// Slower then the overload, but doesn't allocate memory./> + /// or optionally if a member decorated with + /// exists. Slower then the overload, but doesn't allocate memory./> /// /// The name to check if it's defined /// If , considers the value of metadata attributes,otherwise ignores them /// if a member with the name exists in the enumeration, or a member is decorated /// with a [Display] attribute with the name, otherwise public static bool IsDefined(in global::System.ReadOnlySpan name, bool allowMatchingMetadataAttribute) - { - return name switch - { - global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::System.DateTimeKind.Unspecified), global::System.StringComparison.Ordinal) => true, - global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::System.DateTimeKind.Utc), global::System.StringComparison.Ordinal) => true, - global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::System.DateTimeKind.Local), global::System.StringComparison.Ordinal) => true, - _ => false, - }; - } + => IsDefined(name); #endif /// @@ -858,8 +855,9 @@ namespace System /// If the parse operation fails, contains the default value of the underlying type /// of . This parameter is passed uninitialized. /// to read value in case insensitive mode; to read value in case sensitive mode. - /// If , considers the value included in metadata attributes such as - /// [Display] attribute when parsing, otherwise only considers the member names. + /// If , + /// considers the value included in attribute + /// when parsing, otherwise only considers the member names. /// if the value parameter was converted successfully; otherwise, . public static bool TryParse( #if NETCOREAPP3_0_OR_GREATER @@ -881,7 +879,6 @@ namespace System out global::System.DateTimeKind value, bool allowMatchingMetadataAttribute) { - switch (name) { case string s when s.Equals(nameof(global::System.DateTimeKind.Unspecified), global::System.StringComparison.OrdinalIgnoreCase): @@ -910,7 +907,6 @@ namespace System out global::System.DateTimeKind value, bool allowMatchingMetadataAttribute) { - switch (name) { case nameof(global::System.DateTimeKind.Unspecified): @@ -968,8 +964,8 @@ namespace System /// /// The case-sensitive string representation of the enumeration name or underlying value to convert /// to read value in case insensitive mode; to read value in case sensitive mode. - /// If , considers the value included in metadata attributes such as - /// [Display] attribute when parsing, otherwise only considers the member names. + /// If , considers the value included in + /// attribute when parsing, otherwise only considers the member names. /// An object of type whose /// value is represented by public static global::System.DateTimeKind Parse( @@ -1035,8 +1031,8 @@ namespace System /// If the parse operation fails, contains the default value of the underlying type /// of . This parameter is passed uninitialized. /// to read value in case insensitive mode; to read value in case sensitive mode. - /// If , considers the value included in metadata attributes such as - /// [Display] attribute when parsing, otherwise only considers the member names. + /// If , considers the value included in + /// attribute when parsing, otherwise only considers the member names. /// if the value parameter was converted successfully; otherwise, . public static bool TryParse( #if NETCOREAPP3_0_OR_GREATER diff --git a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/InterceptorTests.DoesNotInterceptToStringWhenDisabled.verified.txt b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/InterceptorTests.DoesNotInterceptToStringWhenDisabled.verified.txt index 11509dd..79fb729 100644 --- a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/InterceptorTests.DoesNotInterceptToStringWhenDisabled.verified.txt +++ b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/InterceptorTests.DoesNotInterceptToStringWhenDisabled.verified.txt @@ -1,4 +1,4 @@ -[ +[ //------------------------------------------------------------------------------ // // This code was generated by the NetEscapades.EnumGenerators source generator @@ -26,20 +26,6 @@ namespace MyTestNameSpace /// public const int Length = 2; - /// - /// Returns the string representation of the value. - /// If the attribute is decorated with a [Display] or [Description]attribute, then - /// uses the provided value. Otherwise uses the name of the member, equivalent to - /// calling ToString() on . - /// - /// The value to retrieve the string value for - /// If uses the value provided in the - /// [Display] or [Description]attribute as the string representation of the member. - /// If , always uses the name of the member, the same as if ToString() was called. - /// The string representation of the value - public static string ToStringFast(this global::MyTestNameSpace.MyEnum value, bool useMetadataAttributes) - => useMetadataAttributes ? value.ToStringFastWithMetadata() : value.ToStringFast(); - /// /// Returns the string representation of the value. /// Directly equivalent to calling ToString() on . @@ -54,6 +40,20 @@ namespace MyTestNameSpace _ => value.AsUnderlyingType().ToString(), }; + /// + /// Returns the string representation of the value. + /// If the member is decorated with the attribute + /// then that value is returned. Otherwise returns uses the name of the member, + /// equivalent to calling ToString() on . + /// + /// The value to retrieve the string value for + /// If uses the value provided in the + /// attribute as the string representation of the member. + /// If , always uses the name of the member, the same as if ToString() was called. + /// The string representation of the value + public static string ToStringFast(this global::MyTestNameSpace.MyEnum value, bool useMetadataAttributes) + => useMetadataAttributes ? value.ToStringFastWithMetadata() : value.ToStringFast(); + private static string ToStringFastWithMetadata(this global::MyTestNameSpace.MyEnum value) => value.ToStringFast(); @@ -85,26 +85,27 @@ namespace MyTestNameSpace /// /// The name to check if it's defined /// if a member with the name exists in the enumeration, otherwise - public static bool IsDefined(string name) => IsDefined(name, allowMatchingMetadataAttribute: false); + public static bool IsDefined(string name) + => name switch + { + nameof(global::MyTestNameSpace.MyEnum.First) => true, + nameof(global::MyTestNameSpace.MyEnum.Second) => true, + _ => false, + }; /// /// Returns a boolean telling whether an enum with the given name exists in the enumeration, - /// or if a member decorated with a [Display] attribute + /// or if a member decorated with /// with the required name exists. /// /// The name to check if it's defined - /// If , considers the value of metadata attributes,otherwise ignores them + /// If , + /// considers the value of + /// instead of the member name, otherwise ignores them /// if a member with the name exists in the enumeration, or a member is decorated /// with a [Display] attribute with the name, otherwise public static bool IsDefined(string name, bool allowMatchingMetadataAttribute) - { - return name switch - { - nameof(global::MyTestNameSpace.MyEnum.First) => true, - nameof(global::MyTestNameSpace.MyEnum.Second) => true, - _ => false, - }; - } + => IsDefined(name); #if NETCOREAPP && !NETCOREAPP2_0 && !NETCOREAPP1_1 && !NETCOREAPP1_0 /// @@ -112,27 +113,25 @@ namespace MyTestNameSpace /// /// The name to check if it's defined /// if a member with the name exists in the enumeration, otherwise - public static bool IsDefined(in global::System.ReadOnlySpan name) => IsDefined(name, allowMatchingMetadataAttribute: false); + public static bool IsDefined(in global::System.ReadOnlySpan name) + => name switch + { + global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.First), global::System.StringComparison.Ordinal) => true, + global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.Second), global::System.StringComparison.Ordinal) => true, + _ => false, + }; /// /// Returns a boolean telling whether an enum with the given name exists in the enumeration, - /// or optionally if a member decorated with a [Display] attribute - /// with the required name exists. - /// Slower then the overload, but doesn't allocate memory./> + /// or optionally if a member decorated with + /// exists. Slower then the overload, but doesn't allocate memory./> /// /// The name to check if it's defined /// If , considers the value of metadata attributes,otherwise ignores them /// if a member with the name exists in the enumeration, or a member is decorated /// with a [Display] attribute with the name, otherwise public static bool IsDefined(in global::System.ReadOnlySpan name, bool allowMatchingMetadataAttribute) - { - return name switch - { - global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.First), global::System.StringComparison.Ordinal) => true, - global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.Second), global::System.StringComparison.Ordinal) => true, - _ => false, - }; - } + => IsDefined(name); #endif /// @@ -244,8 +243,9 @@ namespace MyTestNameSpace /// If the parse operation fails, contains the default value of the underlying type /// of . This parameter is passed uninitialized. /// to read value in case insensitive mode; to read value in case sensitive mode. - /// If , considers the value included in metadata attributes such as - /// [Display] attribute when parsing, otherwise only considers the member names. + /// If , + /// considers the value included in attribute + /// when parsing, otherwise only considers the member names. /// if the value parameter was converted successfully; otherwise, . public static bool TryParse( #if NETCOREAPP3_0_OR_GREATER @@ -267,7 +267,6 @@ namespace MyTestNameSpace out global::MyTestNameSpace.MyEnum value, bool allowMatchingMetadataAttribute) { - switch (name) { case string s when s.Equals(nameof(global::MyTestNameSpace.MyEnum.First), global::System.StringComparison.OrdinalIgnoreCase): @@ -293,7 +292,6 @@ namespace MyTestNameSpace out global::MyTestNameSpace.MyEnum value, bool allowMatchingMetadataAttribute) { - switch (name) { case nameof(global::MyTestNameSpace.MyEnum.First): @@ -348,8 +346,8 @@ namespace MyTestNameSpace /// /// The case-sensitive string representation of the enumeration name or underlying value to convert /// to read value in case insensitive mode; to read value in case sensitive mode. - /// If , considers the value included in metadata attributes such as - /// [Display] attribute when parsing, otherwise only considers the member names. + /// If , considers the value included in + /// attribute when parsing, otherwise only considers the member names. /// An object of type whose /// value is represented by public static global::MyTestNameSpace.MyEnum Parse( @@ -415,8 +413,8 @@ namespace MyTestNameSpace /// If the parse operation fails, contains the default value of the underlying type /// of . This parameter is passed uninitialized. /// to read value in case insensitive mode; to read value in case sensitive mode. - /// If , considers the value included in metadata attributes such as - /// [Display] attribute when parsing, otherwise only considers the member names. + /// If , considers the value included in + /// attribute when parsing, otherwise only considers the member names. /// if the value parameter was converted successfully; otherwise, . public static bool TryParse( #if NETCOREAPP3_0_OR_GREATER diff --git a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/InterceptorTests.DoesNotInterceptToStringWhenOldCsharp.verified.txt b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/InterceptorTests.DoesNotInterceptToStringWhenOldCsharp.verified.txt index 11509dd..79fb729 100644 --- a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/InterceptorTests.DoesNotInterceptToStringWhenOldCsharp.verified.txt +++ b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/InterceptorTests.DoesNotInterceptToStringWhenOldCsharp.verified.txt @@ -1,4 +1,4 @@ -[ +[ //------------------------------------------------------------------------------ // // This code was generated by the NetEscapades.EnumGenerators source generator @@ -26,20 +26,6 @@ namespace MyTestNameSpace /// public const int Length = 2; - /// - /// Returns the string representation of the value. - /// If the attribute is decorated with a [Display] or [Description]attribute, then - /// uses the provided value. Otherwise uses the name of the member, equivalent to - /// calling ToString() on . - /// - /// The value to retrieve the string value for - /// If uses the value provided in the - /// [Display] or [Description]attribute as the string representation of the member. - /// If , always uses the name of the member, the same as if ToString() was called. - /// The string representation of the value - public static string ToStringFast(this global::MyTestNameSpace.MyEnum value, bool useMetadataAttributes) - => useMetadataAttributes ? value.ToStringFastWithMetadata() : value.ToStringFast(); - /// /// Returns the string representation of the value. /// Directly equivalent to calling ToString() on . @@ -54,6 +40,20 @@ namespace MyTestNameSpace _ => value.AsUnderlyingType().ToString(), }; + /// + /// Returns the string representation of the value. + /// If the member is decorated with the attribute + /// then that value is returned. Otherwise returns uses the name of the member, + /// equivalent to calling ToString() on . + /// + /// The value to retrieve the string value for + /// If uses the value provided in the + /// attribute as the string representation of the member. + /// If , always uses the name of the member, the same as if ToString() was called. + /// The string representation of the value + public static string ToStringFast(this global::MyTestNameSpace.MyEnum value, bool useMetadataAttributes) + => useMetadataAttributes ? value.ToStringFastWithMetadata() : value.ToStringFast(); + private static string ToStringFastWithMetadata(this global::MyTestNameSpace.MyEnum value) => value.ToStringFast(); @@ -85,26 +85,27 @@ namespace MyTestNameSpace /// /// The name to check if it's defined /// if a member with the name exists in the enumeration, otherwise - public static bool IsDefined(string name) => IsDefined(name, allowMatchingMetadataAttribute: false); + public static bool IsDefined(string name) + => name switch + { + nameof(global::MyTestNameSpace.MyEnum.First) => true, + nameof(global::MyTestNameSpace.MyEnum.Second) => true, + _ => false, + }; /// /// Returns a boolean telling whether an enum with the given name exists in the enumeration, - /// or if a member decorated with a [Display] attribute + /// or if a member decorated with /// with the required name exists. /// /// The name to check if it's defined - /// If , considers the value of metadata attributes,otherwise ignores them + /// If , + /// considers the value of + /// instead of the member name, otherwise ignores them /// if a member with the name exists in the enumeration, or a member is decorated /// with a [Display] attribute with the name, otherwise public static bool IsDefined(string name, bool allowMatchingMetadataAttribute) - { - return name switch - { - nameof(global::MyTestNameSpace.MyEnum.First) => true, - nameof(global::MyTestNameSpace.MyEnum.Second) => true, - _ => false, - }; - } + => IsDefined(name); #if NETCOREAPP && !NETCOREAPP2_0 && !NETCOREAPP1_1 && !NETCOREAPP1_0 /// @@ -112,27 +113,25 @@ namespace MyTestNameSpace /// /// The name to check if it's defined /// if a member with the name exists in the enumeration, otherwise - public static bool IsDefined(in global::System.ReadOnlySpan name) => IsDefined(name, allowMatchingMetadataAttribute: false); + public static bool IsDefined(in global::System.ReadOnlySpan name) + => name switch + { + global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.First), global::System.StringComparison.Ordinal) => true, + global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.Second), global::System.StringComparison.Ordinal) => true, + _ => false, + }; /// /// Returns a boolean telling whether an enum with the given name exists in the enumeration, - /// or optionally if a member decorated with a [Display] attribute - /// with the required name exists. - /// Slower then the overload, but doesn't allocate memory./> + /// or optionally if a member decorated with + /// exists. Slower then the overload, but doesn't allocate memory./> /// /// The name to check if it's defined /// If , considers the value of metadata attributes,otherwise ignores them /// if a member with the name exists in the enumeration, or a member is decorated /// with a [Display] attribute with the name, otherwise public static bool IsDefined(in global::System.ReadOnlySpan name, bool allowMatchingMetadataAttribute) - { - return name switch - { - global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.First), global::System.StringComparison.Ordinal) => true, - global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.Second), global::System.StringComparison.Ordinal) => true, - _ => false, - }; - } + => IsDefined(name); #endif /// @@ -244,8 +243,9 @@ namespace MyTestNameSpace /// If the parse operation fails, contains the default value of the underlying type /// of . This parameter is passed uninitialized. /// to read value in case insensitive mode; to read value in case sensitive mode. - /// If , considers the value included in metadata attributes such as - /// [Display] attribute when parsing, otherwise only considers the member names. + /// If , + /// considers the value included in attribute + /// when parsing, otherwise only considers the member names. /// if the value parameter was converted successfully; otherwise, . public static bool TryParse( #if NETCOREAPP3_0_OR_GREATER @@ -267,7 +267,6 @@ namespace MyTestNameSpace out global::MyTestNameSpace.MyEnum value, bool allowMatchingMetadataAttribute) { - switch (name) { case string s when s.Equals(nameof(global::MyTestNameSpace.MyEnum.First), global::System.StringComparison.OrdinalIgnoreCase): @@ -293,7 +292,6 @@ namespace MyTestNameSpace out global::MyTestNameSpace.MyEnum value, bool allowMatchingMetadataAttribute) { - switch (name) { case nameof(global::MyTestNameSpace.MyEnum.First): @@ -348,8 +346,8 @@ namespace MyTestNameSpace /// /// The case-sensitive string representation of the enumeration name or underlying value to convert /// to read value in case insensitive mode; to read value in case sensitive mode. - /// If , considers the value included in metadata attributes such as - /// [Display] attribute when parsing, otherwise only considers the member names. + /// If , considers the value included in + /// attribute when parsing, otherwise only considers the member names. /// An object of type whose /// value is represented by public static global::MyTestNameSpace.MyEnum Parse( @@ -415,8 +413,8 @@ namespace MyTestNameSpace /// If the parse operation fails, contains the default value of the underlying type /// of . This parameter is passed uninitialized. /// to read value in case insensitive mode; to read value in case sensitive mode. - /// If , considers the value included in metadata attributes such as - /// [Display] attribute when parsing, otherwise only considers the member names. + /// If , considers the value included in + /// attribute when parsing, otherwise only considers the member names. /// if the value parameter was converted successfully; otherwise, . public static bool TryParse( #if NETCOREAPP3_0_OR_GREATER diff --git a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesEnumCorrectly_csharp14IsSupported=False.verified.txt b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesEnumCorrectly_csharp14IsSupported=False_defaultSource=EnumMemberAttribute.verified.txt similarity index 94% rename from tests/NetEscapades.EnumGenerators.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesEnumCorrectly_csharp14IsSupported=False.verified.txt rename to tests/NetEscapades.EnumGenerators.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesEnumCorrectly_csharp14IsSupported=False_defaultSource=EnumMemberAttribute.verified.txt index 8e8fd53..95c7c3a 100644 --- a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesEnumCorrectly_csharp14IsSupported=False.verified.txt +++ b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesEnumCorrectly_csharp14IsSupported=False_defaultSource=EnumMemberAttribute.verified.txt @@ -25,20 +25,6 @@ namespace Something.Blah /// public const int Length = 2; - /// - /// Returns the string representation of the value. - /// If the attribute is decorated with a [Display] or [Description]attribute, then - /// uses the provided value. Otherwise uses the name of the member, equivalent to - /// calling ToString() on . - /// - /// The value to retrieve the string value for - /// If uses the value provided in the - /// [Display] or [Description]attribute as the string representation of the member. - /// If , always uses the name of the member, the same as if ToString() was called. - /// The string representation of the value - public static string ToStringFast(this global::Something.Blah.ShortName value, bool useMetadataAttributes) - => useMetadataAttributes ? value.ToStringFastWithMetadata() : value.ToStringFast(); - /// /// Returns the string representation of the value. /// Directly equivalent to calling ToString() on . @@ -53,6 +39,20 @@ namespace Something.Blah _ => value.AsUnderlyingType().ToString(), }; + /// + /// Returns the string representation of the value. + /// If the member is decorated with the attribute + /// then that value is returned. Otherwise returns uses the name of the member, + /// equivalent to calling ToString() on . + /// + /// The value to retrieve the string value for + /// If uses the value provided in the + /// attribute as the string representation of the member. + /// If , always uses the name of the member, the same as if ToString() was called. + /// The string representation of the value + public static string ToStringFast(this global::Something.Blah.ShortName value, bool useMetadataAttributes) + => useMetadataAttributes ? value.ToStringFastWithMetadata() : value.ToStringFast(); + private static string ToStringFastWithMetadata(this global::Something.Blah.ShortName value) => value.ToStringFast(); @@ -84,26 +84,27 @@ namespace Something.Blah /// /// The name to check if it's defined /// if a member with the name exists in the enumeration, otherwise - public static bool IsDefined(string name) => IsDefined(name, allowMatchingMetadataAttribute: false); + public static bool IsDefined(string name) + => name switch + { + nameof(global::Something.Blah.ShortName.First) => true, + nameof(global::Something.Blah.ShortName.Second) => true, + _ => false, + }; /// /// Returns a boolean telling whether an enum with the given name exists in the enumeration, - /// or if a member decorated with a [Display] attribute + /// or if a member decorated with /// with the required name exists. /// /// The name to check if it's defined - /// If , considers the value of metadata attributes,otherwise ignores them + /// If , + /// considers the value of + /// instead of the member name, otherwise ignores them /// if a member with the name exists in the enumeration, or a member is decorated /// with a [Display] attribute with the name, otherwise public static bool IsDefined(string name, bool allowMatchingMetadataAttribute) - { - return name switch - { - nameof(global::Something.Blah.ShortName.First) => true, - nameof(global::Something.Blah.ShortName.Second) => true, - _ => false, - }; - } + => IsDefined(name); #if NETCOREAPP && !NETCOREAPP2_0 && !NETCOREAPP1_1 && !NETCOREAPP1_0 /// @@ -111,27 +112,25 @@ namespace Something.Blah /// /// The name to check if it's defined /// if a member with the name exists in the enumeration, otherwise - public static bool IsDefined(in global::System.ReadOnlySpan name) => IsDefined(name, allowMatchingMetadataAttribute: false); + public static bool IsDefined(in global::System.ReadOnlySpan name) + => name switch + { + global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::Something.Blah.ShortName.First), global::System.StringComparison.Ordinal) => true, + global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::Something.Blah.ShortName.Second), global::System.StringComparison.Ordinal) => true, + _ => false, + }; /// /// Returns a boolean telling whether an enum with the given name exists in the enumeration, - /// or optionally if a member decorated with a [Display] attribute - /// with the required name exists. - /// Slower then the overload, but doesn't allocate memory./> + /// or optionally if a member decorated with + /// exists. Slower then the overload, but doesn't allocate memory./> /// /// The name to check if it's defined /// If , considers the value of metadata attributes,otherwise ignores them /// if a member with the name exists in the enumeration, or a member is decorated /// with a [Display] attribute with the name, otherwise public static bool IsDefined(in global::System.ReadOnlySpan name, bool allowMatchingMetadataAttribute) - { - return name switch - { - global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::Something.Blah.ShortName.First), global::System.StringComparison.Ordinal) => true, - global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::Something.Blah.ShortName.Second), global::System.StringComparison.Ordinal) => true, - _ => false, - }; - } + => IsDefined(name); #endif /// @@ -243,8 +242,9 @@ namespace Something.Blah /// If the parse operation fails, contains the default value of the underlying type /// of . This parameter is passed uninitialized. /// to read value in case insensitive mode; to read value in case sensitive mode. - /// If , considers the value included in metadata attributes such as - /// [Display] attribute when parsing, otherwise only considers the member names. + /// If , + /// considers the value included in attribute + /// when parsing, otherwise only considers the member names. /// if the value parameter was converted successfully; otherwise, . public static bool TryParse( #if NETCOREAPP3_0_OR_GREATER @@ -266,7 +266,6 @@ namespace Something.Blah out global::Something.Blah.ShortName value, bool allowMatchingMetadataAttribute) { - switch (name) { case string s when s.Equals(nameof(global::Something.Blah.ShortName.First), global::System.StringComparison.OrdinalIgnoreCase): @@ -292,7 +291,6 @@ namespace Something.Blah out global::Something.Blah.ShortName value, bool allowMatchingMetadataAttribute) { - switch (name) { case nameof(global::Something.Blah.ShortName.First): @@ -347,8 +345,8 @@ namespace Something.Blah /// /// The case-sensitive string representation of the enumeration name or underlying value to convert /// to read value in case insensitive mode; to read value in case sensitive mode. - /// If , considers the value included in metadata attributes such as - /// [Display] attribute when parsing, otherwise only considers the member names. + /// If , considers the value included in + /// attribute when parsing, otherwise only considers the member names. /// An object of type whose /// value is represented by public static global::Something.Blah.ShortName Parse( @@ -414,8 +412,8 @@ namespace Something.Blah /// If the parse operation fails, contains the default value of the underlying type /// of . This parameter is passed uninitialized. /// to read value in case insensitive mode; to read value in case sensitive mode. - /// If , considers the value included in metadata attributes such as - /// [Display] attribute when parsing, otherwise only considers the member names. + /// If , considers the value included in + /// attribute when parsing, otherwise only considers the member names. /// if the value parameter was converted successfully; otherwise, . public static bool TryParse( #if NETCOREAPP3_0_OR_GREATER diff --git a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesEnumCorrectly_csharp14IsSupported=False_defaultSource=None.verified.txt b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesEnumCorrectly_csharp14IsSupported=False_defaultSource=None.verified.txt new file mode 100644 index 0000000..383d84d --- /dev/null +++ b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesEnumCorrectly_csharp14IsSupported=False_defaultSource=None.verified.txt @@ -0,0 +1,419 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the NetEscapades.EnumGenerators source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#nullable enable + +namespace Something.Blah +{ +#pragma warning disable CS0612 // Ignore usages of obsolete members or enums +#pragma warning disable CS0618 // Ignore usages of obsolete members or enums + /// + /// Extension methods for + /// + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("NetEscapades.EnumGenerators", "FIXED_VERSION")] + public static partial class ShortName + { + /// + /// The number of members in the enum. + /// This is a non-distinct count of defined names. + /// + public const int Length = 2; + + /// + /// Returns the string representation of the value. + /// Directly equivalent to calling ToString() on . + /// + /// The value to retrieve the string value for + /// The string representation of the value, the same as that returned by ToString() + public static string ToStringFast(this global::Something.Blah.ShortName value) + => value switch + { + global::Something.Blah.ShortName.First => nameof(global::Something.Blah.ShortName.First), + global::Something.Blah.ShortName.Second => nameof(global::Something.Blah.ShortName.Second), + _ => value.AsUnderlyingType().ToString(), + }; + + /// + /// Cast a value of to the underlying type (int). + /// This is mainly a convenience method. + /// + /// The value of cast to the underlying type. + public static int AsUnderlyingType(this global::Something.Blah.ShortName value) + { + return (int) value; + } + + /// + /// Returns a boolean telling whether the given enum value exists in the enumeration. + /// + /// The value to check if it's defined + /// if the value exists in the enumeration, otherwise + public static bool IsDefined(global::Something.Blah.ShortName value) + => value switch + { + global::Something.Blah.ShortName.First => true, + global::Something.Blah.ShortName.Second => true, + _ => false, + }; + + /// + /// Returns a boolean telling whether an enum with the given name exists in the enumeration. + /// + /// The name to check if it's defined + /// if a member with the name exists in the enumeration, otherwise + public static bool IsDefined(string name) + => name switch + { + nameof(global::Something.Blah.ShortName.First) => true, + nameof(global::Something.Blah.ShortName.Second) => true, + _ => false, + }; + +#if NETCOREAPP && !NETCOREAPP2_0 && !NETCOREAPP1_1 && !NETCOREAPP1_0 + /// + /// Returns a boolean telling whether an enum with the given name exists in the enumeration + /// + /// The name to check if it's defined + /// if a member with the name exists in the enumeration, otherwise + public static bool IsDefined(in global::System.ReadOnlySpan name) + => name switch + { + global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::Something.Blah.ShortName.First), global::System.StringComparison.Ordinal) => true, + global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::Something.Blah.ShortName.Second), global::System.StringComparison.Ordinal) => true, + _ => false, + }; +#endif + + /// + /// Converts the string representation of the name or numeric value of + /// an to the equivalent instance. + /// + /// The case-sensitive string representation of the enumeration name or underlying value to convert + /// An object of type whose + /// value is represented by + public static global::Something.Blah.ShortName Parse( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + string? name) + => TryParse(name, out var value, false, false) ? value : ThrowValueNotFound(name); + + /// + /// Converts the string representation of the name or numeric value of + /// an to the equivalent instance. + /// + /// The case-sensitive string representation of the enumeration name or underlying value to convert + /// to read value in case insensitive mode; to read value in case sensitive mode. + /// An object of type whose + /// value is represented by + public static global::Something.Blah.ShortName Parse( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + string? name, + bool ignoreCase) + => TryParse(name, out var value, ignoreCase, false) ? value : ThrowValueNotFound(name); + +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.DoesNotReturn] +#endif + private static global::Something.Blah.ShortName ThrowValueNotFound(string? name) + => throw new global::System.ArgumentException($"Requested value '{name}' was not found."); + + /// + /// Converts the string representation of the name or numeric value of + /// an to the equivalent instance. + /// The return value indicates whether the conversion succeeded. + /// + /// The case-sensitive string representation of the enumeration name or underlying value to convert + /// When this method returns, contains an object of type + /// whose + /// value is represented by if the parse operation succeeds. + /// If the parse operation fails, contains the default value of the underlying type + /// of . This parameter is passed uninitialized. + /// if the value parameter was converted successfully; otherwise, . + public static bool TryParse( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + string? name, + out global::Something.Blah.ShortName value) + => TryParse(name, out value, false, false); + + /// + /// Converts the string representation of the name or numeric value of + /// an to the equivalent instance. + /// The return value indicates whether the conversion succeeded. + /// + /// The string representation of the enumeration name or underlying value to convert + /// When this method returns, contains an object of type + /// whose + /// value is represented by if the parse operation succeeds. + /// If the parse operation fails, contains the default value of the underlying type + /// of . This parameter is passed uninitialized. + /// to read value in case insensitive mode; to read value in case sensitive mode. + /// if the value parameter was converted successfully; otherwise, . + public static bool TryParse( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + string? name, + out global::Something.Blah.ShortName value, + bool ignoreCase) + => TryParse(name, out value, ignoreCase, false); + + private static bool TryParse( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + string? name, + out global::Something.Blah.ShortName value, + bool ignoreCase, + bool allowMatchingMetadataAttribute) + => ignoreCase + ? TryParseIgnoreCase(name, out value, allowMatchingMetadataAttribute) + : TryParseWithCase(name, out value, allowMatchingMetadataAttribute); + + private static bool TryParseIgnoreCase( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + string? name, + out global::Something.Blah.ShortName value, + bool allowMatchingMetadataAttribute) + { + switch (name) + { + case string s when s.Equals(nameof(global::Something.Blah.ShortName.First), global::System.StringComparison.OrdinalIgnoreCase): + value = global::Something.Blah.ShortName.First; + return true; + case string s when s.Equals(nameof(global::Something.Blah.ShortName.Second), global::System.StringComparison.OrdinalIgnoreCase): + value = global::Something.Blah.ShortName.Second; + return true; + case string s when int.TryParse(name, out var val): + value = (global::Something.Blah.ShortName)val; + return true; + default: + value = default; + return false; + } + } + + private static bool TryParseWithCase( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + string? name, + out global::Something.Blah.ShortName value, + bool allowMatchingMetadataAttribute) + { + switch (name) + { + case nameof(global::Something.Blah.ShortName.First): + value = global::Something.Blah.ShortName.First; + return true; + case nameof(global::Something.Blah.ShortName.Second): + value = global::Something.Blah.ShortName.Second; + return true; + case string s when int.TryParse(name, out var val): + value = (global::Something.Blah.ShortName)val; + return true; + default: + value = default; + return false; + } + } + +#if NETCOREAPP && !NETCOREAPP2_0 && !NETCOREAPP1_1 && !NETCOREAPP1_0 + /// + /// Converts the string representation of the name or numeric value of + /// an to the equivalent instance. + /// + /// The case-sensitive string representation of the enumeration name or underlying value to convert + /// An object of type whose + /// value is represented by + public static global::Something.Blah.ShortName Parse( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + in global::System.ReadOnlySpan name) + => TryParse(name, out var value, false, false) ? value : ThrowValueNotFound(name.ToString()); + + /// + /// Converts the string representation of the name or numeric value of + /// an to the equivalent instance. + /// + /// The case-sensitive string representation of the enumeration name or underlying value to convert + /// to read value in case insensitive mode; to read value in case sensitive mode. + /// An object of type whose + /// value is represented by + public static global::Something.Blah.ShortName Parse( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + in global::System.ReadOnlySpan name, + bool ignoreCase) + => TryParse(name, out var value, ignoreCase, false) ? value : ThrowValueNotFound(name.ToString()); + + /// + /// Converts the span representation of the name or numeric value of + /// an to the equivalent instance. + /// The return value indicates whether the conversion succeeded. + /// + /// The span representation of the enumeration name or underlying value to convert + /// When this method returns, contains an object of type + /// whose + /// value is represented by if the parse operation succeeds. + /// If the parse operation fails, contains the default value of the underlying type + /// of . This parameter is passed uninitialized. + /// if the value parameter was converted successfully; otherwise, . + public static bool TryParse( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + in global::System.ReadOnlySpan name, + out global::Something.Blah.ShortName value) + => TryParse(name, out value, false, false); + + /// + /// Converts the span representation of the name or numeric value of + /// an to the equivalent instance. + /// The return value indicates whether the conversion succeeded. + /// + /// The span representation of the enumeration name or underlying value to convert + /// When this method returns, contains an object of type + /// whose + /// value is represented by if the parse operation succeeds. + /// If the parse operation fails, contains the default value of the underlying type + /// of . This parameter is passed uninitialized. + /// to read value in case insensitive mode; to read value in case sensitive mode. + /// if the value parameter was converted successfully; otherwise, . + public static bool TryParse( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + in global::System.ReadOnlySpan name, + out global::Something.Blah.ShortName value, + bool ignoreCase) + => TryParse(name, out value, ignoreCase, false); + + private static bool TryParse( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + in global::System.ReadOnlySpan name, + out global::Something.Blah.ShortName value, + bool ignoreCase, + bool allowMatchingMetadataAttribute) + => ignoreCase + ? TryParseIgnoreCase(in name, out value, allowMatchingMetadataAttribute) + : TryParseWithCase(in name, out value, allowMatchingMetadataAttribute); + + private static bool TryParseIgnoreCase( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + in global::System.ReadOnlySpan name, + out global::Something.Blah.ShortName result, + bool allowMatchingMetadataAttribute) + { + switch (name) + { + case global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::Something.Blah.ShortName.First), global::System.StringComparison.OrdinalIgnoreCase): + result = global::Something.Blah.ShortName.First; + return true; + case global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::Something.Blah.ShortName.Second), global::System.StringComparison.OrdinalIgnoreCase): + result = global::Something.Blah.ShortName.Second; + return true; + case global::System.ReadOnlySpan current when int.TryParse(name, out var numericResult): + result = (global::Something.Blah.ShortName)numericResult; + return true; + default: + result = default; + return false; + } + } + + private static bool TryParseWithCase( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + in global::System.ReadOnlySpan name, + out global::Something.Blah.ShortName result, + bool allowMatchingMetadataAttribute) + { + switch (name) + { + case global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::Something.Blah.ShortName.First), global::System.StringComparison.Ordinal): + result = global::Something.Blah.ShortName.First; + return true; + case global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::Something.Blah.ShortName.Second), global::System.StringComparison.Ordinal): + result = global::Something.Blah.ShortName.Second; + return true; + case global::System.ReadOnlySpan current when int.TryParse(name, out var numericResult): + result = (global::Something.Blah.ShortName)numericResult; + return true; + default: + result = default; + return false; + } + } +#endif + + /// + /// Retrieves an array of the values of the members defined in + /// . + /// Note that this returns a new array with every invocation, so + /// should be cached if appropriate. + /// + /// An array of the values defined in + public static global::Something.Blah.ShortName[] GetValues() + { + return new[] + { + global::Something.Blah.ShortName.First, + global::Something.Blah.ShortName.Second, + }; + } + + /// + /// Retrieves an array of the underlying-values of the members defined in + /// . + /// Note that this returns a new array with every invocation, so + /// should be cached if appropriate. + /// + /// An array of the underlying-values defined in + public static int[] GetValuesAsUnderlyingType() + { + return new[] + { + (int) global::Something.Blah.ShortName.First, + (int) global::Something.Blah.ShortName.Second, + }; + } + + /// + /// Retrieves an array of the names of the members defined in + /// . + /// Note that this returns a new array with every invocation, so + /// should be cached if appropriate. + /// + /// An array of the names of the members defined in + public static string[] GetNames() + { + return new[] + { + nameof(global::Something.Blah.ShortName.First), + nameof(global::Something.Blah.ShortName.Second), + }; + } + } +#pragma warning restore CS0612 // Ignore usages of obsolete members or enums +#pragma warning restore CS0618 // Ignore usages of obsolete members or enums +} \ No newline at end of file diff --git a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesEnumCorrectly_csharp14IsSupported=True.verified.txt b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesEnumCorrectly_csharp14IsSupported=True_defaultSource=EnumMemberAttribute.verified.txt similarity index 94% rename from tests/NetEscapades.EnumGenerators.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesEnumCorrectly_csharp14IsSupported=True.verified.txt rename to tests/NetEscapades.EnumGenerators.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesEnumCorrectly_csharp14IsSupported=True_defaultSource=EnumMemberAttribute.verified.txt index 36bfb80..c30d6a4 100644 --- a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesEnumCorrectly_csharp14IsSupported=True.verified.txt +++ b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesEnumCorrectly_csharp14IsSupported=True_defaultSource=EnumMemberAttribute.verified.txt @@ -25,20 +25,6 @@ namespace Something.Blah /// public const int Length = 2; - /// - /// Returns the string representation of the value. - /// If the attribute is decorated with a [Display] or [Description]attribute, then - /// uses the provided value. Otherwise uses the name of the member, equivalent to - /// calling ToString() on . - /// - /// The value to retrieve the string value for - /// If uses the value provided in the - /// [Display] or [Description]attribute as the string representation of the member. - /// If , always uses the name of the member, the same as if ToString() was called. - /// The string representation of the value - public static string ToStringFast(this global::Something.Blah.ShortName value, bool useMetadataAttributes) - => useMetadataAttributes ? value.ToStringFastWithMetadata() : value.ToStringFast(); - /// /// Returns the string representation of the value. /// Directly equivalent to calling ToString() on . @@ -53,6 +39,20 @@ namespace Something.Blah _ => value.AsUnderlyingType().ToString(), }; + /// + /// Returns the string representation of the value. + /// If the member is decorated with the attribute + /// then that value is returned. Otherwise returns uses the name of the member, + /// equivalent to calling ToString() on . + /// + /// The value to retrieve the string value for + /// If uses the value provided in the + /// attribute as the string representation of the member. + /// If , always uses the name of the member, the same as if ToString() was called. + /// The string representation of the value + public static string ToStringFast(this global::Something.Blah.ShortName value, bool useMetadataAttributes) + => useMetadataAttributes ? value.ToStringFastWithMetadata() : value.ToStringFast(); + private static string ToStringFastWithMetadata(this global::Something.Blah.ShortName value) => value.ToStringFast(); @@ -88,26 +88,27 @@ namespace Something.Blah /// /// The name to check if it's defined /// if a member with the name exists in the enumeration, otherwise - public static bool IsDefined(string name) => IsDefined(name, allowMatchingMetadataAttribute: false); + public static bool IsDefined(string name) + => name switch + { + nameof(global::Something.Blah.ShortName.First) => true, + nameof(global::Something.Blah.ShortName.Second) => true, + _ => false, + }; /// /// Returns a boolean telling whether an enum with the given name exists in the enumeration, - /// or if a member decorated with a [Display] attribute + /// or if a member decorated with /// with the required name exists. /// /// The name to check if it's defined - /// If , considers the value of metadata attributes,otherwise ignores them + /// If , + /// considers the value of + /// instead of the member name, otherwise ignores them /// if a member with the name exists in the enumeration, or a member is decorated /// with a [Display] attribute with the name, otherwise public static bool IsDefined(string name, bool allowMatchingMetadataAttribute) - { - return name switch - { - nameof(global::Something.Blah.ShortName.First) => true, - nameof(global::Something.Blah.ShortName.Second) => true, - _ => false, - }; - } + => IsDefined(name); #if NETCOREAPP && !NETCOREAPP2_0 && !NETCOREAPP1_1 && !NETCOREAPP1_0 /// @@ -115,27 +116,25 @@ namespace Something.Blah /// /// The name to check if it's defined /// if a member with the name exists in the enumeration, otherwise - public static bool IsDefined(in global::System.ReadOnlySpan name) => IsDefined(name, allowMatchingMetadataAttribute: false); + public static bool IsDefined(in global::System.ReadOnlySpan name) + => name switch + { + global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::Something.Blah.ShortName.First), global::System.StringComparison.Ordinal) => true, + global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::Something.Blah.ShortName.Second), global::System.StringComparison.Ordinal) => true, + _ => false, + }; /// /// Returns a boolean telling whether an enum with the given name exists in the enumeration, - /// or optionally if a member decorated with a [Display] attribute - /// with the required name exists. - /// Slower then the overload, but doesn't allocate memory./> + /// or optionally if a member decorated with + /// exists. Slower then the overload, but doesn't allocate memory./> /// /// The name to check if it's defined /// If , considers the value of metadata attributes,otherwise ignores them /// if a member with the name exists in the enumeration, or a member is decorated /// with a [Display] attribute with the name, otherwise public static bool IsDefined(in global::System.ReadOnlySpan name, bool allowMatchingMetadataAttribute) - { - return name switch - { - global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::Something.Blah.ShortName.First), global::System.StringComparison.Ordinal) => true, - global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::Something.Blah.ShortName.Second), global::System.StringComparison.Ordinal) => true, - _ => false, - }; - } + => IsDefined(name); #endif /// @@ -247,8 +246,9 @@ namespace Something.Blah /// If the parse operation fails, contains the default value of the underlying type /// of . This parameter is passed uninitialized. /// to read value in case insensitive mode; to read value in case sensitive mode. - /// If , considers the value included in metadata attributes such as - /// [Display] attribute when parsing, otherwise only considers the member names. + /// If , + /// considers the value included in attribute + /// when parsing, otherwise only considers the member names. /// if the value parameter was converted successfully; otherwise, . public static bool TryParse( #if NETCOREAPP3_0_OR_GREATER @@ -270,7 +270,6 @@ namespace Something.Blah out global::Something.Blah.ShortName value, bool allowMatchingMetadataAttribute) { - switch (name) { case string s when s.Equals(nameof(global::Something.Blah.ShortName.First), global::System.StringComparison.OrdinalIgnoreCase): @@ -296,7 +295,6 @@ namespace Something.Blah out global::Something.Blah.ShortName value, bool allowMatchingMetadataAttribute) { - switch (name) { case nameof(global::Something.Blah.ShortName.First): @@ -351,8 +349,8 @@ namespace Something.Blah /// /// The case-sensitive string representation of the enumeration name or underlying value to convert /// to read value in case insensitive mode; to read value in case sensitive mode. - /// If , considers the value included in metadata attributes such as - /// [Display] attribute when parsing, otherwise only considers the member names. + /// If , considers the value included in + /// attribute when parsing, otherwise only considers the member names. /// An object of type whose /// value is represented by public static global::Something.Blah.ShortName Parse( @@ -418,8 +416,8 @@ namespace Something.Blah /// If the parse operation fails, contains the default value of the underlying type /// of . This parameter is passed uninitialized. /// to read value in case insensitive mode; to read value in case sensitive mode. - /// If , considers the value included in metadata attributes such as - /// [Display] attribute when parsing, otherwise only considers the member names. + /// If , considers the value included in + /// attribute when parsing, otherwise only considers the member names. /// if the value parameter was converted successfully; otherwise, . public static bool TryParse( #if NETCOREAPP3_0_OR_GREATER diff --git a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesEnumCorrectly_csharp14IsSupported=True_defaultSource=None.verified.txt b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesEnumCorrectly_csharp14IsSupported=True_defaultSource=None.verified.txt new file mode 100644 index 0000000..e939832 --- /dev/null +++ b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesEnumCorrectly_csharp14IsSupported=True_defaultSource=None.verified.txt @@ -0,0 +1,424 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the NetEscapades.EnumGenerators source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#nullable enable + +namespace Something.Blah +{ +#pragma warning disable CS0612 // Ignore usages of obsolete members or enums +#pragma warning disable CS0618 // Ignore usages of obsolete members or enums + /// + /// Extension methods for + /// + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("NetEscapades.EnumGenerators", "FIXED_VERSION")] + public static partial class ShortName + { + /// + /// The number of members in the enum. + /// This is a non-distinct count of defined names. + /// + public const int Length = 2; + + /// + /// Returns the string representation of the value. + /// Directly equivalent to calling ToString() on . + /// + /// The value to retrieve the string value for + /// The string representation of the value, the same as that returned by ToString() + public static string ToStringFast(this global::Something.Blah.ShortName value) + => value switch + { + global::Something.Blah.ShortName.First => nameof(global::Something.Blah.ShortName.First), + global::Something.Blah.ShortName.Second => nameof(global::Something.Blah.ShortName.Second), + _ => value.AsUnderlyingType().ToString(), + }; + + /// + /// Cast a value of to the underlying type (int). + /// This is mainly a convenience method. + /// + /// The value of cast to the underlying type. + public static int AsUnderlyingType(this global::Something.Blah.ShortName value) + { + return (int) value; + } + + // C#14 Extension member syntax + extension(global::Something.Blah.ShortName) + { + + /// + /// Returns a boolean telling whether the given enum value exists in the enumeration. + /// + /// The value to check if it's defined + /// if the value exists in the enumeration, otherwise + public static bool IsDefined(global::Something.Blah.ShortName value) + => value switch + { + global::Something.Blah.ShortName.First => true, + global::Something.Blah.ShortName.Second => true, + _ => false, + }; + + /// + /// Returns a boolean telling whether an enum with the given name exists in the enumeration. + /// + /// The name to check if it's defined + /// if a member with the name exists in the enumeration, otherwise + public static bool IsDefined(string name) + => name switch + { + nameof(global::Something.Blah.ShortName.First) => true, + nameof(global::Something.Blah.ShortName.Second) => true, + _ => false, + }; + +#if NETCOREAPP && !NETCOREAPP2_0 && !NETCOREAPP1_1 && !NETCOREAPP1_0 + /// + /// Returns a boolean telling whether an enum with the given name exists in the enumeration + /// + /// The name to check if it's defined + /// if a member with the name exists in the enumeration, otherwise + public static bool IsDefined(in global::System.ReadOnlySpan name) + => name switch + { + global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::Something.Blah.ShortName.First), global::System.StringComparison.Ordinal) => true, + global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::Something.Blah.ShortName.Second), global::System.StringComparison.Ordinal) => true, + _ => false, + }; +#endif + + /// + /// Converts the string representation of the name or numeric value of + /// an to the equivalent instance. + /// + /// The case-sensitive string representation of the enumeration name or underlying value to convert + /// An object of type whose + /// value is represented by + public static global::Something.Blah.ShortName Parse( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + string? name) + => TryParse(name, out var value, false, false) ? value : ThrowValueNotFound(name); + + /// + /// Converts the string representation of the name or numeric value of + /// an to the equivalent instance. + /// + /// The case-sensitive string representation of the enumeration name or underlying value to convert + /// to read value in case insensitive mode; to read value in case sensitive mode. + /// An object of type whose + /// value is represented by + public static global::Something.Blah.ShortName Parse( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + string? name, + bool ignoreCase) + => TryParse(name, out var value, ignoreCase, false) ? value : ThrowValueNotFound(name); + +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.DoesNotReturn] +#endif + private static global::Something.Blah.ShortName ThrowValueNotFound(string? name) + => throw new global::System.ArgumentException($"Requested value '{name}' was not found."); + + /// + /// Converts the string representation of the name or numeric value of + /// an to the equivalent instance. + /// The return value indicates whether the conversion succeeded. + /// + /// The case-sensitive string representation of the enumeration name or underlying value to convert + /// When this method returns, contains an object of type + /// whose + /// value is represented by if the parse operation succeeds. + /// If the parse operation fails, contains the default value of the underlying type + /// of . This parameter is passed uninitialized. + /// if the value parameter was converted successfully; otherwise, . + public static bool TryParse( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + string? name, + out global::Something.Blah.ShortName value) + => TryParse(name, out value, false, false); + + /// + /// Converts the string representation of the name or numeric value of + /// an to the equivalent instance. + /// The return value indicates whether the conversion succeeded. + /// + /// The string representation of the enumeration name or underlying value to convert + /// When this method returns, contains an object of type + /// whose + /// value is represented by if the parse operation succeeds. + /// If the parse operation fails, contains the default value of the underlying type + /// of . This parameter is passed uninitialized. + /// to read value in case insensitive mode; to read value in case sensitive mode. + /// if the value parameter was converted successfully; otherwise, . + public static bool TryParse( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + string? name, + out global::Something.Blah.ShortName value, + bool ignoreCase) + => TryParse(name, out value, ignoreCase, false); + + private static bool TryParse( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + string? name, + out global::Something.Blah.ShortName value, + bool ignoreCase, + bool allowMatchingMetadataAttribute) + => ignoreCase + ? TryParseIgnoreCase(name, out value, allowMatchingMetadataAttribute) + : TryParseWithCase(name, out value, allowMatchingMetadataAttribute); + + private static bool TryParseIgnoreCase( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + string? name, + out global::Something.Blah.ShortName value, + bool allowMatchingMetadataAttribute) + { + switch (name) + { + case string s when s.Equals(nameof(global::Something.Blah.ShortName.First), global::System.StringComparison.OrdinalIgnoreCase): + value = global::Something.Blah.ShortName.First; + return true; + case string s when s.Equals(nameof(global::Something.Blah.ShortName.Second), global::System.StringComparison.OrdinalIgnoreCase): + value = global::Something.Blah.ShortName.Second; + return true; + case string s when int.TryParse(name, out var val): + value = (global::Something.Blah.ShortName)val; + return true; + default: + value = default; + return false; + } + } + + private static bool TryParseWithCase( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + string? name, + out global::Something.Blah.ShortName value, + bool allowMatchingMetadataAttribute) + { + switch (name) + { + case nameof(global::Something.Blah.ShortName.First): + value = global::Something.Blah.ShortName.First; + return true; + case nameof(global::Something.Blah.ShortName.Second): + value = global::Something.Blah.ShortName.Second; + return true; + case string s when int.TryParse(name, out var val): + value = (global::Something.Blah.ShortName)val; + return true; + default: + value = default; + return false; + } + } + +#if NETCOREAPP && !NETCOREAPP2_0 && !NETCOREAPP1_1 && !NETCOREAPP1_0 + /// + /// Converts the string representation of the name or numeric value of + /// an to the equivalent instance. + /// + /// The case-sensitive string representation of the enumeration name or underlying value to convert + /// An object of type whose + /// value is represented by + public static global::Something.Blah.ShortName Parse( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + in global::System.ReadOnlySpan name) + => TryParse(name, out var value, false, false) ? value : ThrowValueNotFound(name.ToString()); + + /// + /// Converts the string representation of the name or numeric value of + /// an to the equivalent instance. + /// + /// The case-sensitive string representation of the enumeration name or underlying value to convert + /// to read value in case insensitive mode; to read value in case sensitive mode. + /// An object of type whose + /// value is represented by + public static global::Something.Blah.ShortName Parse( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + in global::System.ReadOnlySpan name, + bool ignoreCase) + => TryParse(name, out var value, ignoreCase, false) ? value : ThrowValueNotFound(name.ToString()); + + /// + /// Converts the span representation of the name or numeric value of + /// an to the equivalent instance. + /// The return value indicates whether the conversion succeeded. + /// + /// The span representation of the enumeration name or underlying value to convert + /// When this method returns, contains an object of type + /// whose + /// value is represented by if the parse operation succeeds. + /// If the parse operation fails, contains the default value of the underlying type + /// of . This parameter is passed uninitialized. + /// if the value parameter was converted successfully; otherwise, . + public static bool TryParse( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + in global::System.ReadOnlySpan name, + out global::Something.Blah.ShortName value) + => TryParse(name, out value, false, false); + + /// + /// Converts the span representation of the name or numeric value of + /// an to the equivalent instance. + /// The return value indicates whether the conversion succeeded. + /// + /// The span representation of the enumeration name or underlying value to convert + /// When this method returns, contains an object of type + /// whose + /// value is represented by if the parse operation succeeds. + /// If the parse operation fails, contains the default value of the underlying type + /// of . This parameter is passed uninitialized. + /// to read value in case insensitive mode; to read value in case sensitive mode. + /// if the value parameter was converted successfully; otherwise, . + public static bool TryParse( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + in global::System.ReadOnlySpan name, + out global::Something.Blah.ShortName value, + bool ignoreCase) + => TryParse(name, out value, ignoreCase, false); + + private static bool TryParse( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + in global::System.ReadOnlySpan name, + out global::Something.Blah.ShortName value, + bool ignoreCase, + bool allowMatchingMetadataAttribute) + => ignoreCase + ? TryParseIgnoreCase(in name, out value, allowMatchingMetadataAttribute) + : TryParseWithCase(in name, out value, allowMatchingMetadataAttribute); + + private static bool TryParseIgnoreCase( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + in global::System.ReadOnlySpan name, + out global::Something.Blah.ShortName result, + bool allowMatchingMetadataAttribute) + { + switch (name) + { + case global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::Something.Blah.ShortName.First), global::System.StringComparison.OrdinalIgnoreCase): + result = global::Something.Blah.ShortName.First; + return true; + case global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::Something.Blah.ShortName.Second), global::System.StringComparison.OrdinalIgnoreCase): + result = global::Something.Blah.ShortName.Second; + return true; + case global::System.ReadOnlySpan current when int.TryParse(name, out var numericResult): + result = (global::Something.Blah.ShortName)numericResult; + return true; + default: + result = default; + return false; + } + } + + private static bool TryParseWithCase( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + in global::System.ReadOnlySpan name, + out global::Something.Blah.ShortName result, + bool allowMatchingMetadataAttribute) + { + switch (name) + { + case global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::Something.Blah.ShortName.First), global::System.StringComparison.Ordinal): + result = global::Something.Blah.ShortName.First; + return true; + case global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::Something.Blah.ShortName.Second), global::System.StringComparison.Ordinal): + result = global::Something.Blah.ShortName.Second; + return true; + case global::System.ReadOnlySpan current when int.TryParse(name, out var numericResult): + result = (global::Something.Blah.ShortName)numericResult; + return true; + default: + result = default; + return false; + } + } +#endif + + /// + /// Retrieves an array of the values of the members defined in + /// . + /// Note that this returns a new array with every invocation, so + /// should be cached if appropriate. + /// + /// An array of the values defined in + public static global::Something.Blah.ShortName[] GetValues() + { + return new[] + { + global::Something.Blah.ShortName.First, + global::Something.Blah.ShortName.Second, + }; + } + + /// + /// Retrieves an array of the underlying-values of the members defined in + /// . + /// Note that this returns a new array with every invocation, so + /// should be cached if appropriate. + /// + /// An array of the underlying-values defined in + public static int[] GetValuesAsUnderlyingType() + { + return new[] + { + (int) global::Something.Blah.ShortName.First, + (int) global::Something.Blah.ShortName.Second, + }; + } + + /// + /// Retrieves an array of the names of the members defined in + /// . + /// Note that this returns a new array with every invocation, so + /// should be cached if appropriate. + /// + /// An array of the names of the members defined in + public static string[] GetNames() + { + return new[] + { + nameof(global::Something.Blah.ShortName.First), + nameof(global::Something.Blah.ShortName.Second), + }; + } + } + } +#pragma warning restore CS0612 // Ignore usages of obsolete members or enums +#pragma warning restore CS0618 // Ignore usages of obsolete members or enums +} \ No newline at end of file diff --git a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesEnumWithRepeatedValuesCorrectly_csharp14IsSupported=False.verified.txt b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesEnumWithRepeatedValuesCorrectly_csharp14IsSupported=False.verified.txt index 1e59f08..a06f187 100644 --- a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesEnumWithRepeatedValuesCorrectly_csharp14IsSupported=False.verified.txt +++ b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesEnumWithRepeatedValuesCorrectly_csharp14IsSupported=False.verified.txt @@ -25,20 +25,6 @@ namespace Something.Blah /// public const int Length = 3; - /// - /// Returns the string representation of the value. - /// If the attribute is decorated with a [Display] or [Description]attribute, then - /// uses the provided value. Otherwise uses the name of the member, equivalent to - /// calling ToString() on . - /// - /// The value to retrieve the string value for - /// If uses the value provided in the - /// [Display] or [Description]attribute as the string representation of the member. - /// If , always uses the name of the member, the same as if ToString() was called. - /// The string representation of the value - public static string ToStringFast(this global::Something.Blah.ShortName value, bool useMetadataAttributes) - => useMetadataAttributes ? value.ToStringFastWithMetadata() : value.ToStringFast(); - /// /// Returns the string representation of the value. /// Directly equivalent to calling ToString() on . @@ -53,6 +39,20 @@ namespace Something.Blah _ => value.AsUnderlyingType().ToString(), }; + /// + /// Returns the string representation of the value. + /// If the member is decorated with the attribute + /// then that value is returned. Otherwise returns uses the name of the member, + /// equivalent to calling ToString() on . + /// + /// The value to retrieve the string value for + /// If uses the value provided in the + /// attribute as the string representation of the member. + /// If , always uses the name of the member, the same as if ToString() was called. + /// The string representation of the value + public static string ToStringFast(this global::Something.Blah.ShortName value, bool useMetadataAttributes) + => useMetadataAttributes ? value.ToStringFastWithMetadata() : value.ToStringFast(); + private static string ToStringFastWithMetadata(this global::Something.Blah.ShortName value) => value.ToStringFast(); @@ -84,27 +84,28 @@ namespace Something.Blah /// /// The name to check if it's defined /// if a member with the name exists in the enumeration, otherwise - public static bool IsDefined(string name) => IsDefined(name, allowMatchingMetadataAttribute: false); + public static bool IsDefined(string name) + => name switch + { + nameof(global::Something.Blah.ShortName.First) => true, + nameof(global::Something.Blah.ShortName.Second) => true, + nameof(global::Something.Blah.ShortName.Third) => true, + _ => false, + }; /// /// Returns a boolean telling whether an enum with the given name exists in the enumeration, - /// or if a member decorated with a [Display] attribute + /// or if a member decorated with /// with the required name exists. /// /// The name to check if it's defined - /// If , considers the value of metadata attributes,otherwise ignores them + /// If , + /// considers the value of + /// instead of the member name, otherwise ignores them /// if a member with the name exists in the enumeration, or a member is decorated /// with a [Display] attribute with the name, otherwise public static bool IsDefined(string name, bool allowMatchingMetadataAttribute) - { - return name switch - { - nameof(global::Something.Blah.ShortName.First) => true, - nameof(global::Something.Blah.ShortName.Second) => true, - nameof(global::Something.Blah.ShortName.Third) => true, - _ => false, - }; - } + => IsDefined(name); #if NETCOREAPP && !NETCOREAPP2_0 && !NETCOREAPP1_1 && !NETCOREAPP1_0 /// @@ -112,28 +113,26 @@ namespace Something.Blah /// /// The name to check if it's defined /// if a member with the name exists in the enumeration, otherwise - public static bool IsDefined(in global::System.ReadOnlySpan name) => IsDefined(name, allowMatchingMetadataAttribute: false); + public static bool IsDefined(in global::System.ReadOnlySpan name) + => name switch + { + global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::Something.Blah.ShortName.First), global::System.StringComparison.Ordinal) => true, + global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::Something.Blah.ShortName.Second), global::System.StringComparison.Ordinal) => true, + global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::Something.Blah.ShortName.Third), global::System.StringComparison.Ordinal) => true, + _ => false, + }; /// /// Returns a boolean telling whether an enum with the given name exists in the enumeration, - /// or optionally if a member decorated with a [Display] attribute - /// with the required name exists. - /// Slower then the overload, but doesn't allocate memory./> + /// or optionally if a member decorated with + /// exists. Slower then the overload, but doesn't allocate memory./> /// /// The name to check if it's defined /// If , considers the value of metadata attributes,otherwise ignores them /// if a member with the name exists in the enumeration, or a member is decorated /// with a [Display] attribute with the name, otherwise public static bool IsDefined(in global::System.ReadOnlySpan name, bool allowMatchingMetadataAttribute) - { - return name switch - { - global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::Something.Blah.ShortName.First), global::System.StringComparison.Ordinal) => true, - global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::Something.Blah.ShortName.Second), global::System.StringComparison.Ordinal) => true, - global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::Something.Blah.ShortName.Third), global::System.StringComparison.Ordinal) => true, - _ => false, - }; - } + => IsDefined(name); #endif /// @@ -245,8 +244,9 @@ namespace Something.Blah /// If the parse operation fails, contains the default value of the underlying type /// of . This parameter is passed uninitialized. /// to read value in case insensitive mode; to read value in case sensitive mode. - /// If , considers the value included in metadata attributes such as - /// [Display] attribute when parsing, otherwise only considers the member names. + /// If , + /// considers the value included in attribute + /// when parsing, otherwise only considers the member names. /// if the value parameter was converted successfully; otherwise, . public static bool TryParse( #if NETCOREAPP3_0_OR_GREATER @@ -268,7 +268,6 @@ namespace Something.Blah out global::Something.Blah.ShortName value, bool allowMatchingMetadataAttribute) { - switch (name) { case string s when s.Equals(nameof(global::Something.Blah.ShortName.First), global::System.StringComparison.OrdinalIgnoreCase): @@ -297,7 +296,6 @@ namespace Something.Blah out global::Something.Blah.ShortName value, bool allowMatchingMetadataAttribute) { - switch (name) { case nameof(global::Something.Blah.ShortName.First): @@ -355,8 +353,8 @@ namespace Something.Blah /// /// The case-sensitive string representation of the enumeration name or underlying value to convert /// to read value in case insensitive mode; to read value in case sensitive mode. - /// If , considers the value included in metadata attributes such as - /// [Display] attribute when parsing, otherwise only considers the member names. + /// If , considers the value included in + /// attribute when parsing, otherwise only considers the member names. /// An object of type whose /// value is represented by public static global::Something.Blah.ShortName Parse( @@ -422,8 +420,8 @@ namespace Something.Blah /// If the parse operation fails, contains the default value of the underlying type /// of . This parameter is passed uninitialized. /// to read value in case insensitive mode; to read value in case sensitive mode. - /// If , considers the value included in metadata attributes such as - /// [Display] attribute when parsing, otherwise only considers the member names. + /// If , considers the value included in + /// attribute when parsing, otherwise only considers the member names. /// if the value parameter was converted successfully; otherwise, . public static bool TryParse( #if NETCOREAPP3_0_OR_GREATER diff --git a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesEnumWithRepeatedValuesCorrectly_csharp14IsSupported=True.verified.txt b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesEnumWithRepeatedValuesCorrectly_csharp14IsSupported=True.verified.txt index d052668..95487b7 100644 --- a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesEnumWithRepeatedValuesCorrectly_csharp14IsSupported=True.verified.txt +++ b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesEnumWithRepeatedValuesCorrectly_csharp14IsSupported=True.verified.txt @@ -25,20 +25,6 @@ namespace Something.Blah /// public const int Length = 3; - /// - /// Returns the string representation of the value. - /// If the attribute is decorated with a [Display] or [Description]attribute, then - /// uses the provided value. Otherwise uses the name of the member, equivalent to - /// calling ToString() on . - /// - /// The value to retrieve the string value for - /// If uses the value provided in the - /// [Display] or [Description]attribute as the string representation of the member. - /// If , always uses the name of the member, the same as if ToString() was called. - /// The string representation of the value - public static string ToStringFast(this global::Something.Blah.ShortName value, bool useMetadataAttributes) - => useMetadataAttributes ? value.ToStringFastWithMetadata() : value.ToStringFast(); - /// /// Returns the string representation of the value. /// Directly equivalent to calling ToString() on . @@ -53,6 +39,20 @@ namespace Something.Blah _ => value.AsUnderlyingType().ToString(), }; + /// + /// Returns the string representation of the value. + /// If the member is decorated with the attribute + /// then that value is returned. Otherwise returns uses the name of the member, + /// equivalent to calling ToString() on . + /// + /// The value to retrieve the string value for + /// If uses the value provided in the + /// attribute as the string representation of the member. + /// If , always uses the name of the member, the same as if ToString() was called. + /// The string representation of the value + public static string ToStringFast(this global::Something.Blah.ShortName value, bool useMetadataAttributes) + => useMetadataAttributes ? value.ToStringFastWithMetadata() : value.ToStringFast(); + private static string ToStringFastWithMetadata(this global::Something.Blah.ShortName value) => value.ToStringFast(); @@ -88,27 +88,28 @@ namespace Something.Blah /// /// The name to check if it's defined /// if a member with the name exists in the enumeration, otherwise - public static bool IsDefined(string name) => IsDefined(name, allowMatchingMetadataAttribute: false); + public static bool IsDefined(string name) + => name switch + { + nameof(global::Something.Blah.ShortName.First) => true, + nameof(global::Something.Blah.ShortName.Second) => true, + nameof(global::Something.Blah.ShortName.Third) => true, + _ => false, + }; /// /// Returns a boolean telling whether an enum with the given name exists in the enumeration, - /// or if a member decorated with a [Display] attribute + /// or if a member decorated with /// with the required name exists. /// /// The name to check if it's defined - /// If , considers the value of metadata attributes,otherwise ignores them + /// If , + /// considers the value of + /// instead of the member name, otherwise ignores them /// if a member with the name exists in the enumeration, or a member is decorated /// with a [Display] attribute with the name, otherwise public static bool IsDefined(string name, bool allowMatchingMetadataAttribute) - { - return name switch - { - nameof(global::Something.Blah.ShortName.First) => true, - nameof(global::Something.Blah.ShortName.Second) => true, - nameof(global::Something.Blah.ShortName.Third) => true, - _ => false, - }; - } + => IsDefined(name); #if NETCOREAPP && !NETCOREAPP2_0 && !NETCOREAPP1_1 && !NETCOREAPP1_0 /// @@ -116,28 +117,26 @@ namespace Something.Blah /// /// The name to check if it's defined /// if a member with the name exists in the enumeration, otherwise - public static bool IsDefined(in global::System.ReadOnlySpan name) => IsDefined(name, allowMatchingMetadataAttribute: false); + public static bool IsDefined(in global::System.ReadOnlySpan name) + => name switch + { + global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::Something.Blah.ShortName.First), global::System.StringComparison.Ordinal) => true, + global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::Something.Blah.ShortName.Second), global::System.StringComparison.Ordinal) => true, + global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::Something.Blah.ShortName.Third), global::System.StringComparison.Ordinal) => true, + _ => false, + }; /// /// Returns a boolean telling whether an enum with the given name exists in the enumeration, - /// or optionally if a member decorated with a [Display] attribute - /// with the required name exists. - /// Slower then the overload, but doesn't allocate memory./> + /// or optionally if a member decorated with + /// exists. Slower then the overload, but doesn't allocate memory./> /// /// The name to check if it's defined /// If , considers the value of metadata attributes,otherwise ignores them /// if a member with the name exists in the enumeration, or a member is decorated /// with a [Display] attribute with the name, otherwise public static bool IsDefined(in global::System.ReadOnlySpan name, bool allowMatchingMetadataAttribute) - { - return name switch - { - global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::Something.Blah.ShortName.First), global::System.StringComparison.Ordinal) => true, - global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::Something.Blah.ShortName.Second), global::System.StringComparison.Ordinal) => true, - global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::Something.Blah.ShortName.Third), global::System.StringComparison.Ordinal) => true, - _ => false, - }; - } + => IsDefined(name); #endif /// @@ -249,8 +248,9 @@ namespace Something.Blah /// If the parse operation fails, contains the default value of the underlying type /// of . This parameter is passed uninitialized. /// to read value in case insensitive mode; to read value in case sensitive mode. - /// If , considers the value included in metadata attributes such as - /// [Display] attribute when parsing, otherwise only considers the member names. + /// If , + /// considers the value included in attribute + /// when parsing, otherwise only considers the member names. /// if the value parameter was converted successfully; otherwise, . public static bool TryParse( #if NETCOREAPP3_0_OR_GREATER @@ -272,7 +272,6 @@ namespace Something.Blah out global::Something.Blah.ShortName value, bool allowMatchingMetadataAttribute) { - switch (name) { case string s when s.Equals(nameof(global::Something.Blah.ShortName.First), global::System.StringComparison.OrdinalIgnoreCase): @@ -301,7 +300,6 @@ namespace Something.Blah out global::Something.Blah.ShortName value, bool allowMatchingMetadataAttribute) { - switch (name) { case nameof(global::Something.Blah.ShortName.First): @@ -359,8 +357,8 @@ namespace Something.Blah /// /// The case-sensitive string representation of the enumeration name or underlying value to convert /// to read value in case insensitive mode; to read value in case sensitive mode. - /// If , considers the value included in metadata attributes such as - /// [Display] attribute when parsing, otherwise only considers the member names. + /// If , considers the value included in + /// attribute when parsing, otherwise only considers the member names. /// An object of type whose /// value is represented by public static global::Something.Blah.ShortName Parse( @@ -426,8 +424,8 @@ namespace Something.Blah /// If the parse operation fails, contains the default value of the underlying type /// of . This parameter is passed uninitialized. /// to read value in case insensitive mode; to read value in case sensitive mode. - /// If , considers the value included in metadata attributes such as - /// [Display] attribute when parsing, otherwise only considers the member names. + /// If , considers the value included in + /// attribute when parsing, otherwise only considers the member names. /// if the value parameter was converted successfully; otherwise, . public static bool TryParse( #if NETCOREAPP3_0_OR_GREATER diff --git a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFlagsEnumCorrectly_csharp14IsSupported=False.verified.txt b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFlagsEnumCorrectly_csharp14IsSupported=False.verified.txt index 477d5bf..57eedea 100644 --- a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFlagsEnumCorrectly_csharp14IsSupported=False.verified.txt +++ b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFlagsEnumCorrectly_csharp14IsSupported=False.verified.txt @@ -25,20 +25,6 @@ namespace Something.Blah /// public const int Length = 2; - /// - /// Returns the string representation of the value. - /// If the attribute is decorated with a [Display] or [Description]attribute, then - /// uses the provided value. Otherwise uses the name of the member, equivalent to - /// calling ToString() on . - /// - /// The value to retrieve the string value for - /// If uses the value provided in the - /// [Display] or [Description]attribute as the string representation of the member. - /// If , always uses the name of the member, the same as if ToString() was called. - /// The string representation of the value - public static string ToStringFast(this global::Something.Blah.ShortName value, bool useMetadataAttributes) - => useMetadataAttributes ? value.ToStringFastWithMetadata() : value.ToStringFast(); - /// /// Returns the string representation of the value. /// Directly equivalent to calling ToString() on . @@ -53,6 +39,20 @@ namespace Something.Blah _ => value.ToString(), }; + /// + /// Returns the string representation of the value. + /// If the member is decorated with the attribute + /// then that value is returned. Otherwise returns uses the name of the member, + /// equivalent to calling ToString() on . + /// + /// The value to retrieve the string value for + /// If uses the value provided in the + /// attribute as the string representation of the member. + /// If , always uses the name of the member, the same as if ToString() was called. + /// The string representation of the value + public static string ToStringFast(this global::Something.Blah.ShortName value, bool useMetadataAttributes) + => useMetadataAttributes ? value.ToStringFastWithMetadata() : value.ToStringFast(); + private static string ToStringFastWithMetadata(this global::Something.Blah.ShortName value) => value.ToStringFast(); @@ -96,26 +96,27 @@ namespace Something.Blah /// /// The name to check if it's defined /// if a member with the name exists in the enumeration, otherwise - public static bool IsDefined(string name) => IsDefined(name, allowMatchingMetadataAttribute: false); + public static bool IsDefined(string name) + => name switch + { + nameof(global::Something.Blah.ShortName.First) => true, + nameof(global::Something.Blah.ShortName.Second) => true, + _ => false, + }; /// /// Returns a boolean telling whether an enum with the given name exists in the enumeration, - /// or if a member decorated with a [Display] attribute + /// or if a member decorated with /// with the required name exists. /// /// The name to check if it's defined - /// If , considers the value of metadata attributes,otherwise ignores them + /// If , + /// considers the value of + /// instead of the member name, otherwise ignores them /// if a member with the name exists in the enumeration, or a member is decorated /// with a [Display] attribute with the name, otherwise public static bool IsDefined(string name, bool allowMatchingMetadataAttribute) - { - return name switch - { - nameof(global::Something.Blah.ShortName.First) => true, - nameof(global::Something.Blah.ShortName.Second) => true, - _ => false, - }; - } + => IsDefined(name); #if NETCOREAPP && !NETCOREAPP2_0 && !NETCOREAPP1_1 && !NETCOREAPP1_0 /// @@ -123,27 +124,25 @@ namespace Something.Blah /// /// The name to check if it's defined /// if a member with the name exists in the enumeration, otherwise - public static bool IsDefined(in global::System.ReadOnlySpan name) => IsDefined(name, allowMatchingMetadataAttribute: false); + public static bool IsDefined(in global::System.ReadOnlySpan name) + => name switch + { + global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::Something.Blah.ShortName.First), global::System.StringComparison.Ordinal) => true, + global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::Something.Blah.ShortName.Second), global::System.StringComparison.Ordinal) => true, + _ => false, + }; /// /// Returns a boolean telling whether an enum with the given name exists in the enumeration, - /// or optionally if a member decorated with a [Display] attribute - /// with the required name exists. - /// Slower then the overload, but doesn't allocate memory./> + /// or optionally if a member decorated with + /// exists. Slower then the overload, but doesn't allocate memory./> /// /// The name to check if it's defined /// If , considers the value of metadata attributes,otherwise ignores them /// if a member with the name exists in the enumeration, or a member is decorated /// with a [Display] attribute with the name, otherwise public static bool IsDefined(in global::System.ReadOnlySpan name, bool allowMatchingMetadataAttribute) - { - return name switch - { - global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::Something.Blah.ShortName.First), global::System.StringComparison.Ordinal) => true, - global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::Something.Blah.ShortName.Second), global::System.StringComparison.Ordinal) => true, - _ => false, - }; - } + => IsDefined(name); #endif /// @@ -255,8 +254,9 @@ namespace Something.Blah /// If the parse operation fails, contains the default value of the underlying type /// of . This parameter is passed uninitialized. /// to read value in case insensitive mode; to read value in case sensitive mode. - /// If , considers the value included in metadata attributes such as - /// [Display] attribute when parsing, otherwise only considers the member names. + /// If , + /// considers the value included in attribute + /// when parsing, otherwise only considers the member names. /// if the value parameter was converted successfully; otherwise, . public static bool TryParse( #if NETCOREAPP3_0_OR_GREATER @@ -278,7 +278,6 @@ namespace Something.Blah out global::Something.Blah.ShortName value, bool allowMatchingMetadataAttribute) { - switch (name) { case string s when s.Equals(nameof(global::Something.Blah.ShortName.First), global::System.StringComparison.OrdinalIgnoreCase): @@ -304,7 +303,6 @@ namespace Something.Blah out global::Something.Blah.ShortName value, bool allowMatchingMetadataAttribute) { - switch (name) { case nameof(global::Something.Blah.ShortName.First): @@ -359,8 +357,8 @@ namespace Something.Blah /// /// The case-sensitive string representation of the enumeration name or underlying value to convert /// to read value in case insensitive mode; to read value in case sensitive mode. - /// If , considers the value included in metadata attributes such as - /// [Display] attribute when parsing, otherwise only considers the member names. + /// If , considers the value included in + /// attribute when parsing, otherwise only considers the member names. /// An object of type whose /// value is represented by public static global::Something.Blah.ShortName Parse( @@ -426,8 +424,8 @@ namespace Something.Blah /// If the parse operation fails, contains the default value of the underlying type /// of . This parameter is passed uninitialized. /// to read value in case insensitive mode; to read value in case sensitive mode. - /// If , considers the value included in metadata attributes such as - /// [Display] attribute when parsing, otherwise only considers the member names. + /// If , considers the value included in + /// attribute when parsing, otherwise only considers the member names. /// if the value parameter was converted successfully; otherwise, . public static bool TryParse( #if NETCOREAPP3_0_OR_GREATER diff --git a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFlagsEnumCorrectly_csharp14IsSupported=True.verified.txt b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFlagsEnumCorrectly_csharp14IsSupported=True.verified.txt index 26bc037..3d08304 100644 --- a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFlagsEnumCorrectly_csharp14IsSupported=True.verified.txt +++ b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFlagsEnumCorrectly_csharp14IsSupported=True.verified.txt @@ -25,20 +25,6 @@ namespace Something.Blah /// public const int Length = 2; - /// - /// Returns the string representation of the value. - /// If the attribute is decorated with a [Display] or [Description]attribute, then - /// uses the provided value. Otherwise uses the name of the member, equivalent to - /// calling ToString() on . - /// - /// The value to retrieve the string value for - /// If uses the value provided in the - /// [Display] or [Description]attribute as the string representation of the member. - /// If , always uses the name of the member, the same as if ToString() was called. - /// The string representation of the value - public static string ToStringFast(this global::Something.Blah.ShortName value, bool useMetadataAttributes) - => useMetadataAttributes ? value.ToStringFastWithMetadata() : value.ToStringFast(); - /// /// Returns the string representation of the value. /// Directly equivalent to calling ToString() on . @@ -53,6 +39,20 @@ namespace Something.Blah _ => value.ToString(), }; + /// + /// Returns the string representation of the value. + /// If the member is decorated with the attribute + /// then that value is returned. Otherwise returns uses the name of the member, + /// equivalent to calling ToString() on . + /// + /// The value to retrieve the string value for + /// If uses the value provided in the + /// attribute as the string representation of the member. + /// If , always uses the name of the member, the same as if ToString() was called. + /// The string representation of the value + public static string ToStringFast(this global::Something.Blah.ShortName value, bool useMetadataAttributes) + => useMetadataAttributes ? value.ToStringFastWithMetadata() : value.ToStringFast(); + private static string ToStringFastWithMetadata(this global::Something.Blah.ShortName value) => value.ToStringFast(); @@ -100,26 +100,27 @@ namespace Something.Blah /// /// The name to check if it's defined /// if a member with the name exists in the enumeration, otherwise - public static bool IsDefined(string name) => IsDefined(name, allowMatchingMetadataAttribute: false); + public static bool IsDefined(string name) + => name switch + { + nameof(global::Something.Blah.ShortName.First) => true, + nameof(global::Something.Blah.ShortName.Second) => true, + _ => false, + }; /// /// Returns a boolean telling whether an enum with the given name exists in the enumeration, - /// or if a member decorated with a [Display] attribute + /// or if a member decorated with /// with the required name exists. /// /// The name to check if it's defined - /// If , considers the value of metadata attributes,otherwise ignores them + /// If , + /// considers the value of + /// instead of the member name, otherwise ignores them /// if a member with the name exists in the enumeration, or a member is decorated /// with a [Display] attribute with the name, otherwise public static bool IsDefined(string name, bool allowMatchingMetadataAttribute) - { - return name switch - { - nameof(global::Something.Blah.ShortName.First) => true, - nameof(global::Something.Blah.ShortName.Second) => true, - _ => false, - }; - } + => IsDefined(name); #if NETCOREAPP && !NETCOREAPP2_0 && !NETCOREAPP1_1 && !NETCOREAPP1_0 /// @@ -127,27 +128,25 @@ namespace Something.Blah /// /// The name to check if it's defined /// if a member with the name exists in the enumeration, otherwise - public static bool IsDefined(in global::System.ReadOnlySpan name) => IsDefined(name, allowMatchingMetadataAttribute: false); + public static bool IsDefined(in global::System.ReadOnlySpan name) + => name switch + { + global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::Something.Blah.ShortName.First), global::System.StringComparison.Ordinal) => true, + global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::Something.Blah.ShortName.Second), global::System.StringComparison.Ordinal) => true, + _ => false, + }; /// /// Returns a boolean telling whether an enum with the given name exists in the enumeration, - /// or optionally if a member decorated with a [Display] attribute - /// with the required name exists. - /// Slower then the overload, but doesn't allocate memory./> + /// or optionally if a member decorated with + /// exists. Slower then the overload, but doesn't allocate memory./> /// /// The name to check if it's defined /// If , considers the value of metadata attributes,otherwise ignores them /// if a member with the name exists in the enumeration, or a member is decorated /// with a [Display] attribute with the name, otherwise public static bool IsDefined(in global::System.ReadOnlySpan name, bool allowMatchingMetadataAttribute) - { - return name switch - { - global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::Something.Blah.ShortName.First), global::System.StringComparison.Ordinal) => true, - global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::Something.Blah.ShortName.Second), global::System.StringComparison.Ordinal) => true, - _ => false, - }; - } + => IsDefined(name); #endif /// @@ -259,8 +258,9 @@ namespace Something.Blah /// If the parse operation fails, contains the default value of the underlying type /// of . This parameter is passed uninitialized. /// to read value in case insensitive mode; to read value in case sensitive mode. - /// If , considers the value included in metadata attributes such as - /// [Display] attribute when parsing, otherwise only considers the member names. + /// If , + /// considers the value included in attribute + /// when parsing, otherwise only considers the member names. /// if the value parameter was converted successfully; otherwise, . public static bool TryParse( #if NETCOREAPP3_0_OR_GREATER @@ -282,7 +282,6 @@ namespace Something.Blah out global::Something.Blah.ShortName value, bool allowMatchingMetadataAttribute) { - switch (name) { case string s when s.Equals(nameof(global::Something.Blah.ShortName.First), global::System.StringComparison.OrdinalIgnoreCase): @@ -308,7 +307,6 @@ namespace Something.Blah out global::Something.Blah.ShortName value, bool allowMatchingMetadataAttribute) { - switch (name) { case nameof(global::Something.Blah.ShortName.First): @@ -363,8 +361,8 @@ namespace Something.Blah /// /// The case-sensitive string representation of the enumeration name or underlying value to convert /// to read value in case insensitive mode; to read value in case sensitive mode. - /// If , considers the value included in metadata attributes such as - /// [Display] attribute when parsing, otherwise only considers the member names. + /// If , considers the value included in + /// attribute when parsing, otherwise only considers the member names. /// An object of type whose /// value is represented by public static global::Something.Blah.ShortName Parse( @@ -430,8 +428,8 @@ namespace Something.Blah /// If the parse operation fails, contains the default value of the underlying type /// of . This parameter is passed uninitialized. /// to read value in case insensitive mode; to read value in case sensitive mode. - /// If , considers the value included in metadata attributes such as - /// [Display] attribute when parsing, otherwise only considers the member names. + /// If , considers the value included in + /// attribute when parsing, otherwise only considers the member names. /// if the value parameter was converted successfully; otherwise, . public static bool TryParse( #if NETCOREAPP3_0_OR_GREATER diff --git a/tests/NetEscapades.EnumGenerators.Tests/SourceGenerationHelperSnapshotTests.cs b/tests/NetEscapades.EnumGenerators.Tests/SourceGenerationHelperSnapshotTests.cs index 93fe7f9..cb23a64 100644 --- a/tests/NetEscapades.EnumGenerators.Tests/SourceGenerationHelperSnapshotTests.cs +++ b/tests/NetEscapades.EnumGenerators.Tests/SourceGenerationHelperSnapshotTests.cs @@ -9,10 +9,14 @@ namespace NetEscapades.EnumGenerators.Tests; [UsesVerify] public class SourceGenerationHelperSnapshotTests { + private const MetadataSource DefaultMetadataSource = MetadataSource.EnumMemberAttribute; + [Theory] - [InlineData(true)] - [InlineData(false)] - public Task GeneratesEnumCorrectly(bool csharp14IsSupported) + [InlineData(true, MetadataSource.EnumMemberAttribute)] + [InlineData(false, MetadataSource.EnumMemberAttribute)] + [InlineData(true, MetadataSource.None)] + [InlineData(false, MetadataSource.None)] + public Task GeneratesEnumCorrectly(bool csharp14IsSupported, MetadataSource defaultSource) { var value = new EnumToGenerate( "ShortName", @@ -22,18 +26,18 @@ public Task GeneratesEnumCorrectly(bool csharp14IsSupported) isPublic: true, new List<(string Key, EnumValueOption Value)> { - ("First", new EnumValueOption(null, false, 0)), - ("Second", new EnumValueOption(null, false, 1)), + ("First", EnumValueOption.CreateWithoutAttributes(0)), + ("Second", EnumValueOption.CreateWithoutAttributes(1)), }, hasFlags: false, - isDisplayAttributeUsed: false); + metadataSource: null); - var result = SourceGenerationHelper.GenerateExtensionClass(value, csharp14IsSupported).Content; + var result = SourceGenerationHelper.GenerateExtensionClass(value, csharp14IsSupported, defaultSource).Content; return Verifier.Verify(result) .ScrubExpectedChanges() .UseDirectory("Snapshots") - .UseParameters(csharp14IsSupported); + .UseParameters(csharp14IsSupported, defaultSource); } [Theory] @@ -49,14 +53,14 @@ public Task GeneratesEnumWithRepeatedValuesCorrectly(bool csharp14IsSupported) isPublic: true, new List<(string Key, EnumValueOption Value)> { - ("First", new EnumValueOption(null, false, 0)), - ("Second", new EnumValueOption(null, false, 1)), - ("Third", new EnumValueOption(null, false, 0)), + ("First", EnumValueOption.CreateWithoutAttributes(0)), + ("Second", EnumValueOption.CreateWithoutAttributes(1)), + ("Third", EnumValueOption.CreateWithoutAttributes(0)), }, hasFlags: false, - isDisplayAttributeUsed: false); + null); - var result = SourceGenerationHelper.GenerateExtensionClass(value, csharp14IsSupported).Content; + var result = SourceGenerationHelper.GenerateExtensionClass(value, csharp14IsSupported, DefaultMetadataSource).Content; return Verifier.Verify(result) .ScrubExpectedChanges() @@ -77,13 +81,13 @@ public Task GeneratesFlagsEnumCorrectly(bool csharp14IsSupported) isPublic: true, new List<(string, EnumValueOption)> { - ("First", new EnumValueOption(null, false, 0)), - ("Second", new EnumValueOption(null, false, 1)), + ("First", EnumValueOption.CreateWithoutAttributes(0)), + ("Second", EnumValueOption.CreateWithoutAttributes(1)), }, hasFlags: true, - isDisplayAttributeUsed: false); + metadataSource: null); - var result = SourceGenerationHelper.GenerateExtensionClass(value, csharp14IsSupported).Content; + var result = SourceGenerationHelper.GenerateExtensionClass(value, csharp14IsSupported, DefaultMetadataSource).Content; return Verifier.Verify(result) .ScrubExpectedChanges() diff --git a/tests/NetEscapades.EnumGenerators.Tests/TestHelpers.cs b/tests/NetEscapades.EnumGenerators.Tests/TestHelpers.cs index 143f825..8ae3bee 100644 --- a/tests/NetEscapades.EnumGenerators.Tests/TestHelpers.cs +++ b/tests/NetEscapades.EnumGenerators.Tests/TestHelpers.cs @@ -33,6 +33,9 @@ private static IIncrementalGenerator[] GetSourceGenerators() => public static string LoadEmbeddedAttribute() => LoadEmbeddedResource("NetEscapades.EnumGenerators.Tests.EnumExtensionsAttribute.cs"); + public static string LoadEmbeddedMetadataSource() + => LoadEmbeddedResource("NetEscapades.EnumGenerators.Tests.MetadataSource.cs"); + private static string LoadEmbeddedResource(string resourceName) { var assembly = typeof(TestHelpers).Assembly; @@ -118,7 +121,9 @@ private static (ImmutableArray Diagnostics, ImmutableArray).Assembly.Location), + MetadataReference.CreateFromFile(typeof(System.ComponentModel.DescriptionAttribute).Assembly.Location), MetadataReference.CreateFromFile(typeof(System.ComponentModel.DataAnnotations.DisplayAttribute).Assembly.Location), + MetadataReference.CreateFromFile(typeof(System.Runtime.Serialization.EnumMemberAttribute).Assembly.Location), MetadataReference.CreateFromFile(typeof(System.CodeDom.Compiler.GeneratedCodeAttribute).Assembly.Location) ]); From fb571caee9aacfdf710e0863602a257a96f1eecc Mon Sep 17 00:00:00 2001 From: Andrew Lock Date: Wed, 13 Aug 2025 21:53:52 +0100 Subject: [PATCH 2/5] Add integration tests --- ...ithEnumMemberInNamespaceExtensionsTests.cs | 68 +++++++++++++++ ...tadataSourcesInNamespaceExtensionsTests.cs | 83 +++++++++++++++++++ .../Enums.cs | 41 +++++++-- .../ExtensionTests.cs | 6 +- ...des.EnumGenerators.IntegrationTests.csproj | 4 + 5 files changed, 191 insertions(+), 11 deletions(-) create mode 100644 tests/NetEscapades.EnumGenerators.IntegrationTests/EnumWithEnumMemberInNamespaceExtensionsTests.cs create mode 100644 tests/NetEscapades.EnumGenerators.IntegrationTests/EnumWithNoMetadataSourcesInNamespaceExtensionsTests.cs diff --git a/tests/NetEscapades.EnumGenerators.IntegrationTests/EnumWithEnumMemberInNamespaceExtensionsTests.cs b/tests/NetEscapades.EnumGenerators.IntegrationTests/EnumWithEnumMemberInNamespaceExtensionsTests.cs new file mode 100644 index 0000000..257392f --- /dev/null +++ b/tests/NetEscapades.EnumGenerators.IntegrationTests/EnumWithEnumMemberInNamespaceExtensionsTests.cs @@ -0,0 +1,68 @@ +using System; +using Xunit; + +#if INTEGRATION_TESTS +namespace NetEscapades.EnumGenerators.IntegrationTests; +#elif NETSTANDARD_INTEGRATION_TESTS +namespace NetEscapades.EnumGenerators.NetStandard.IntegrationTests; +#elif INTERCEPTOR_TESTS +namespace NetEscapades.EnumGenerators.Interceptors.IntegrationTests; +#elif NUGET_INTEGRATION_TESTS +namespace NetEscapades.EnumGenerators.Nuget.IntegrationTests; +#elif NUGET_INTERCEPTOR_TESTS +namespace NetEscapades.EnumGenerators.Nuget.Interceptors.IntegrationTests; +#else +#error Unknown integration tests +#endif + +public class EnumWithEnumMemberInNamespaceExtensionsTests : ExtensionTests, ITestData +{ + public TheoryData ValidEnumValues() => new() + { + EnumWithEnumMemberInNamespace.First, + EnumWithEnumMemberInNamespace.Second, + (EnumWithEnumMemberInNamespace)3, + }; + + public TheoryData ValuesToParse() => new() + { + "First", + "Second", + "2nd", + "2ND", + "first", + "SECOND", + "3", + "267", + "-267", + "2147483647", + "3000000000", + "Fourth", + "Fifth", + }; + + protected override string[] GetNames() => EnumWithEnumMemberInNamespaceExtensions.GetNames(); + protected override EnumWithEnumMemberInNamespace[] GetValues() => EnumWithEnumMemberInNamespaceExtensions.GetValues(); + protected override int[] GetValuesAsUnderlyingType() => EnumWithEnumMemberInNamespaceExtensions.GetValuesAsUnderlyingType(); + protected override int AsUnderlyingValue(EnumWithEnumMemberInNamespace value) => value.AsUnderlyingType(); + + protected override string ToStringFast(EnumWithEnumMemberInNamespace value) => value.ToStringFast(); + protected override string ToStringFast(EnumWithEnumMemberInNamespace value, bool withMetadata) => value.ToStringFast(withMetadata); + protected override bool IsDefined(EnumWithEnumMemberInNamespace value) => EnumWithEnumMemberInNamespaceExtensions.IsDefined(value); + protected override bool IsDefined(string name, bool allowMatchingMetadataAttribute) => EnumWithEnumMemberInNamespaceExtensions.IsDefined(name, allowMatchingMetadataAttribute); +#if READONLYSPAN + protected override bool IsDefined(in ReadOnlySpan name, bool allowMatchingMetadataAttribute = false) => EnumWithEnumMemberInNamespaceExtensions.IsDefined(name, allowMatchingMetadataAttribute); +#endif + protected override bool TryParse(string name, out EnumWithEnumMemberInNamespace parsed, bool ignoreCase, bool allowMatchingMetadataAttribute) + => EnumWithEnumMemberInNamespaceExtensions.TryParse(name, out parsed, ignoreCase, allowMatchingMetadataAttribute); +#if READONLYSPAN + protected override bool TryParse(in ReadOnlySpan name, out EnumWithEnumMemberInNamespace parsed, bool ignoreCase, bool allowMatchingMetadataAttribute) + => EnumWithEnumMemberInNamespaceExtensions.TryParse(name, out parsed, ignoreCase, allowMatchingMetadataAttribute); +#endif + protected override EnumWithEnumMemberInNamespace Parse(string name, bool ignoreCase, bool allowMatchingMetadataAttribute) + => EnumWithEnumMemberInNamespaceExtensions.Parse(name, ignoreCase); +#if READONLYSPAN + protected override EnumWithEnumMemberInNamespace Parse(in ReadOnlySpan name, bool ignoreCase, bool allowMatchingMetadataAttribute) + => EnumWithEnumMemberInNamespaceExtensions.Parse(name, ignoreCase); +#endif +} \ No newline at end of file diff --git a/tests/NetEscapades.EnumGenerators.IntegrationTests/EnumWithNoMetadataSourcesInNamespaceExtensionsTests.cs b/tests/NetEscapades.EnumGenerators.IntegrationTests/EnumWithNoMetadataSourcesInNamespaceExtensionsTests.cs new file mode 100644 index 0000000..48f75c5 --- /dev/null +++ b/tests/NetEscapades.EnumGenerators.IntegrationTests/EnumWithNoMetadataSourcesInNamespaceExtensionsTests.cs @@ -0,0 +1,83 @@ +using System; +using Xunit; + +#if INTEGRATION_TESTS +namespace NetEscapades.EnumGenerators.IntegrationTests; +#elif NETSTANDARD_INTEGRATION_TESTS +namespace NetEscapades.EnumGenerators.NetStandard.IntegrationTests; +#elif INTERCEPTOR_TESTS +namespace NetEscapades.EnumGenerators.Interceptors.IntegrationTests; +#elif NUGET_INTEGRATION_TESTS +namespace NetEscapades.EnumGenerators.Nuget.IntegrationTests; +#elif NUGET_INTERCEPTOR_TESTS +namespace NetEscapades.EnumGenerators.Nuget.Interceptors.IntegrationTests; +#else +#error Unknown integration tests +#endif + +#nullable enable +public class EnumWithNoMetadataSourcesInNamespaceExtensionsTests : ExtensionTests, ITestData +{ + public TheoryData ValidEnumValues() => new() + { + EnumWithNoMetadataSources.First, + EnumWithNoMetadataSources.Second, + (EnumWithNoMetadataSources)3, + }; + + public TheoryData ValuesToParse() => new() + { + "First", + "Second", + "2nd", + "2ND", + "first", + "SECOND", + "3", + "267", + "-267", + "2147483647", + "3000000000", + "Fourth", + "Fifth", + }; + + protected override string[] GetNames() => EnumWithNoMetadataSourcesExtensions.GetNames(); + protected override EnumWithNoMetadataSources[] GetValues() => EnumWithNoMetadataSourcesExtensions.GetValues(); + protected override int[] GetValuesAsUnderlyingType() => EnumWithNoMetadataSourcesExtensions.GetValuesAsUnderlyingType(); + protected override int AsUnderlyingValue(EnumWithNoMetadataSources value) => value.AsUnderlyingType(); + + // Can't call the "withMetadata" versions of all these + protected override string ToStringFast(EnumWithNoMetadataSources value) => value.ToStringFast(); + protected override string ToStringFast(EnumWithNoMetadataSources value, bool withMetadata) => value.ToStringFast(); + protected override bool IsDefined(EnumWithNoMetadataSources value) => EnumWithNoMetadataSourcesExtensions.IsDefined(value); + protected override bool IsDefined(string name, bool allowMatchingMetadataAttribute) => EnumWithNoMetadataSourcesExtensions.IsDefined(name); +#if READONLYSPAN + protected override bool IsDefined(in ReadOnlySpan name, bool allowMatchingMetadataAttribute = false) => EnumWithNoMetadataSourcesExtensions.IsDefined(name); +#endif + protected override bool TryParse(string name, out EnumWithNoMetadataSources parsed, bool ignoreCase, bool allowMatchingMetadataAttribute) + => EnumWithNoMetadataSourcesExtensions.TryParse(name, out parsed, ignoreCase); +#if READONLYSPAN + protected override bool TryParse(in ReadOnlySpan name, out EnumWithNoMetadataSources parsed, bool ignoreCase, bool allowMatchingMetadataAttribute) + => EnumWithNoMetadataSourcesExtensions.TryParse(name, out parsed, ignoreCase); +#endif + protected override EnumWithNoMetadataSources Parse(string name, bool ignoreCase, bool allowMatchingMetadataAttribute) + => EnumWithNoMetadataSourcesExtensions.Parse(name, ignoreCase); +#if READONLYSPAN + protected override EnumWithNoMetadataSources Parse(in ReadOnlySpan name, bool ignoreCase, bool allowMatchingMetadataAttribute) + => EnumWithNoMetadataSourcesExtensions.Parse(name, ignoreCase); +#endif + + protected override bool TryGetDisplayNameOrDescription( + string? value, +#if NETCOREAPP3_0_OR_GREATER + [System.Diagnostics.CodeAnalysis.NotNullWhen(true)] out string? displayName) +#else + out string? displayName) +#endif + { + // no metadata + displayName = null; + return false; + } +} \ No newline at end of file diff --git a/tests/NetEscapades.EnumGenerators.IntegrationTests/Enums.cs b/tests/NetEscapades.EnumGenerators.IntegrationTests/Enums.cs index da600e3..66abc99 100644 --- a/tests/NetEscapades.EnumGenerators.IntegrationTests/Enums.cs +++ b/tests/NetEscapades.EnumGenerators.IntegrationTests/Enums.cs @@ -1,6 +1,7 @@ using System; using System.ComponentModel; using System.ComponentModel.DataAnnotations; +using System.Runtime.Serialization; [assembly:NetEscapades.EnumGenerators.EnumExtensions()] [assembly:NetEscapades.EnumGenerators.EnumExtensions()] @@ -21,7 +22,7 @@ public enum EnumInSystem public enum NonInterceptableEnum { First = 0, - [Display(Name = "2nd")] Second = 1, + [EnumMember(Value = "2nd")] Second = 1, Third = 2, } } @@ -41,7 +42,7 @@ public class Foo public enum EnumInFoo { First = 0, - [Display(Name = "2nd")] + [EnumMember(Value = "2nd")] Second = 1, Third = 2, } @@ -72,7 +73,7 @@ public enum EnumInNamespace Third = 2, } - [EnumExtensions] + [EnumExtensions(MetadataSource = MetadataSource.DisplayAttribute)] public enum EnumWithDisplayNameInNamespace { First = 0, @@ -83,7 +84,7 @@ public enum EnumWithDisplayNameInNamespace Third = 2, } - [EnumExtensions] + [EnumExtensions(MetadataSource = MetadataSource.DescriptionAttribute)] public enum EnumWithDescriptionInNamespace { First = 0, @@ -94,7 +95,29 @@ public enum EnumWithDescriptionInNamespace Third = 2, } - [EnumExtensions] + [EnumExtensions(MetadataSource = MetadataSource.EnumMemberAttribute)] + public enum EnumWithEnumMemberInNamespace + { + First = 0, + + [EnumMember(Value = "2nd")] + Second = 1, + + Third = 2, + } + + [EnumExtensions(MetadataSource = MetadataSource.None)] + public enum EnumWithNoMetadataSources + { + First = 0, + + [EnumMember(Value = "2nd")] + Second = 1, + + Third = 2, + } + + [EnumExtensions(MetadataSource = MetadataSource.DisplayAttribute)] public enum EnumWithSameDisplayName { First = 0, @@ -126,7 +149,7 @@ public enum FlagsEnum ThirdAndFourth = Third | Fourth, } - [EnumExtensions] + [EnumExtensions(MetadataSource = MetadataSource.DescriptionAttribute)] public enum StringTesting { [System.ComponentModel.Description("Quotes \"")] Quotes, @@ -142,7 +165,7 @@ public enum EnumWithExtensionInOtherNamespace { First = 0, - [Display(Name = "2nd")] Second = 1, + [EnumMember(Value = "2nd")] Second = 1, Third = 2, } @@ -160,8 +183,8 @@ public enum EnumWithRepeatedValues [EnumExtensions] public enum EnumWithRepeatedValuesWithDisplayNames { - [Display(Name = "Main")] First = 0, + [EnumMember(Value = "Main")] First = 0, Second = 1, - [Display(Name = "Repeated")] Third = 0, // Repeated value with display name + [EnumMember(Value = "Repeated")] Third = 0, // Repeated value with display name } } \ No newline at end of file diff --git a/tests/NetEscapades.EnumGenerators.IntegrationTests/ExtensionTests.cs b/tests/NetEscapades.EnumGenerators.IntegrationTests/ExtensionTests.cs index 75fb5c6..77f760d 100644 --- a/tests/NetEscapades.EnumGenerators.IntegrationTests/ExtensionTests.cs +++ b/tests/NetEscapades.EnumGenerators.IntegrationTests/ExtensionTests.cs @@ -3,6 +3,7 @@ using System.ComponentModel.DataAnnotations; using System.Linq; using System.Reflection; +using System.Runtime.Serialization; using FluentAssertions; using FluentAssertions.Execution; using Xunit; @@ -375,7 +376,7 @@ private bool TryGetEnumByDisplayNameOrDescription(string name, bool ignoreCase, return false; } - private bool TryGetDisplayNameOrDescription( + protected virtual bool TryGetDisplayNameOrDescription( string? value, #if NETCOREAPP3_0_OR_GREATER [System.Diagnostics.CodeAnalysis.NotNullWhen(true)] out string? displayName) @@ -395,7 +396,8 @@ private bool TryGetDisplayNameOrDescription( { // Doesn't take order into account, but we don't test with both currently displayName = memberInfo[0].GetCustomAttribute()?.GetName() - ?? memberInfo[0].GetCustomAttribute()?.Description; + ?? memberInfo[0].GetCustomAttribute()?.Description + ?? memberInfo[0].GetCustomAttribute()?.Value; if (displayName is null) { return false; diff --git a/tests/NetEscapades.EnumGenerators.IntegrationTests/NetEscapades.EnumGenerators.IntegrationTests.csproj b/tests/NetEscapades.EnumGenerators.IntegrationTests/NetEscapades.EnumGenerators.IntegrationTests.csproj index c058c76..90cd573 100644 --- a/tests/NetEscapades.EnumGenerators.IntegrationTests/NetEscapades.EnumGenerators.IntegrationTests.csproj +++ b/tests/NetEscapades.EnumGenerators.IntegrationTests/NetEscapades.EnumGenerators.IntegrationTests.csproj @@ -5,7 +5,11 @@ $(DefineConstants);INTEGRATION_TESTS $(DefineConstants);READONLYSPAN; true + EnumMemberAttribute + + + From 0fc6b918f18ff5157036f748cf813967e9184012 Mon Sep 17 00:00:00 2001 From: Andrew Lock Date: Thu, 14 Aug 2025 20:42:12 +0100 Subject: [PATCH 3/5] Fix typo --- .../EnumExtensionsAttribute.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/NetEscapades.EnumGenerators.Attributes/EnumExtensionsAttribute.cs b/src/NetEscapades.EnumGenerators.Attributes/EnumExtensionsAttribute.cs index f32f33d..7182773 100644 --- a/src/NetEscapades.EnumGenerators.Attributes/EnumExtensionsAttribute.cs +++ b/src/NetEscapades.EnumGenerators.Attributes/EnumExtensionsAttribute.cs @@ -23,7 +23,7 @@ public class EnumExtensionsAttribute : System.Attribute /// /// The metadata source to use when serializing and deserializing using - /// ToStringFast() and TryParse(). If not provided, the + /// ToStringFast() and TryParse(). If not provided /// will be /// used to provide the values. Alternatively, you can disable this feature /// entirely by using . From f430771b28c1485198b9ccc86e46636816a8c2b8 Mon Sep 17 00:00:00 2001 From: Andrew Lock Date: Thu, 14 Aug 2025 22:49:25 +0100 Subject: [PATCH 4/5] Fix build --- .github/workflows/BuildAndPack.yml | 2 -- build/Build.cs | 1 - .../NetEscapades.EnumGenerators.csproj | 2 +- 3 files changed, 1 insertion(+), 4 deletions(-) diff --git a/.github/workflows/BuildAndPack.yml b/.github/workflows/BuildAndPack.yml index 63f51e4..57e50ee 100644 --- a/.github/workflows/BuildAndPack.yml +++ b/.github/workflows/BuildAndPack.yml @@ -64,9 +64,7 @@ jobs: .nuke/temp ~/.nuget/packages !~/.nuget/packages/netescapades.enumgenerators - !~/.nuget/packages/netescapades.enumgenerators.attributes !~/.nuget/packages/netescapades.enumgenerators.interceptors - !~/.nuget/packages/netescapades.enumgenerators.interceptors.attributes key: ${{ runner.os }}-${{ hashFiles('**/global.json', '**/*.csproj') }} - name: Run './build.cmd Clean Test TestPackage PushToNuGet diff --git a/build/Build.cs b/build/Build.cs index 5b6efa6..b744ad4 100644 --- a/build/Build.cs +++ b/build/Build.cs @@ -101,7 +101,6 @@ class Build : NukeBuild Target TestPackage => _ => _ .DependsOn(Pack) .After(Test) - .Produces(ArtifactsDirectory) .Executes(() => { var projectFiles = new[] diff --git a/src/NetEscapades.EnumGenerators/NetEscapades.EnumGenerators.csproj b/src/NetEscapades.EnumGenerators/NetEscapades.EnumGenerators.csproj index 14c08a7..4092213 100644 --- a/src/NetEscapades.EnumGenerators/NetEscapades.EnumGenerators.csproj +++ b/src/NetEscapades.EnumGenerators/NetEscapades.EnumGenerators.csproj @@ -22,7 +22,7 @@ all runtime; build; native; contentfiles; analyzers; buildtransitive - + From 57bd75b961a587d3ab2c62096b1deef8c7f9010b Mon Sep 17 00:00:00 2001 From: Andrew Lock Date: Fri, 12 Sep 2025 21:21:49 +0100 Subject: [PATCH 5/5] Make sure we don't overwrite the value if it's set --- .../NetEscapades.EnumGenerators.props | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/NetEscapades.EnumGenerators/NetEscapades.EnumGenerators.props b/src/NetEscapades.EnumGenerators/NetEscapades.EnumGenerators.props index cb4d3b0..a30e539 100644 --- a/src/NetEscapades.EnumGenerators/NetEscapades.EnumGenerators.props +++ b/src/NetEscapades.EnumGenerators/NetEscapades.EnumGenerators.props @@ -1,6 +1,6 @@ - EnumMemberAttribute + EnumMemberAttribute