< Summary

Information
Class: MyNet.Humanizer.Facade.EnumExtensions
Assembly: MyNet.Humanizer
File(s): https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.Humanizer/Facade/Extensions/EnumExtensions.cs
Tag: 323_28699572109
Line coverage
100%
Covered lines: 22
Uncovered lines: 0
Coverable lines: 22
Total lines: 131
Line coverage: 100%
Branch coverage
100%
Covered branches: 12
Total branches: 12
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
Humanize(...)100%11100%
Humanize(...)100%11100%
DehumanizeTo(...)100%11100%
DehumanizeTo(...)100%11100%
TryDehumanizeTo(...)100%11100%
DehumanizeCore(...)100%1212100%

File(s)

https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.Humanizer/Facade/Extensions/EnumExtensions.cs

#LineLine coverage
 1// -----------------------------------------------------------------------
 2// <copyright file="EnumExtensions.cs" company="Stéphane ANDRE">
 3// Copyright (c) Stéphane ANDRE. All rights reserved.
 4// </copyright>
 5// -----------------------------------------------------------------------
 6
 7using System;
 8using System.Collections.Generic;
 9using System.ComponentModel;
 10using System.Globalization;
 11using MyNet.Humanizer.Display;
 12
 13#pragma warning disable IDE0130 // Namespace does not match folder structure
 14namespace MyNet.Humanizer.Facade;
 15#pragma warning restore IDE0130 // Namespace does not match folder structure
 16
 17/// <summary>
 18/// Contains extension methods for humanizing Enums.
 19/// </summary>
 20public static class EnumExtensions
 21{
 22    extension(Enum value)
 23    {
 24        /// <summary>
 25        /// Gets the display name of the enum value, if it has a <see cref="DisplayNameAttribute"/>. Otherwise, returns 
 26        /// </summary>
 27        /// <param name="culture">The culture to use when retrieving the display name.</param>
 28        /// <returns>The display name of the enum value.</returns>
 6629        public string Humanize(CultureInfo? culture = null) => value.Humanize(DisplayTextOptions.Default, culture);
 30
 31        /// <summary>
 32        /// Gets the display name of the enum value, if it has a <see cref="DisplayNameAttribute"/>. Otherwise, returns 
 33        /// Uses the DI-registered <see cref="IDisplayTextStrategy{T}"/>.
 34        /// </summary>
 35        /// <param name="options">The options to use when retrieving the display name.</param>
 36        /// <param name="culture">The culture to use when retrieving the display name.</param>
 37        /// <returns>The display name of the enum value.</returns>
 38        public string Humanize(DisplayTextOptions options, CultureInfo? culture = null)
 9039            => TextHumanizer.Humanize(value, options, culture);
 40    }
 41
 42    extension(string? input)
 43    {
 44        /// <summary>
 45        /// Dehumanizes the input string to the specified target enum type. The method first attempts to parse the input
 46        /// </summary>
 47        /// <param name="culture">The culture to use for humanization.</param>
 48        /// <typeparam name="TEnum">The type of the target enum.</typeparam>
 49        /// <returns>The matched enum value.</returns>
 50        /// <exception cref="KeyNotFoundException">Thrown if no matching enum value is found.</exception>
 51        public TEnum DehumanizeTo<TEnum>(CultureInfo? culture = null)
 52            where TEnum : struct, Enum
 2153            => (TEnum)DehumanizeCore(input, typeof(TEnum), culture);
 54
 55        /// <summary>
 56        /// Dehumanizes the input string to the specified target enum type. The method first attempts to parse the input
 57        /// </summary>
 58        /// <param name="targetEnum">The target enum type to dehumanize to.</param>
 59        /// <param name="culture">The culture to use for humanization.</param>
 60        /// <returns>The matched enum value.</returns>
 61        /// <exception cref="KeyNotFoundException">Thrown if no matching enum value is found.</exception>
 362        public Enum DehumanizeTo(Type targetEnum, CultureInfo? culture = null) => DehumanizeCore(input, targetEnum, cult
 63
 64        /// <summary>
 65        /// Attempts to dehumanize the input string to the specified target enum type. The method first attempts to pars
 66        /// </summary>
 67        /// <param name="result">The resulting enum value if a match is found; otherwise, null.</param>
 68        /// <param name="culture">The culture to use for humanization.</param>
 69        /// <typeparam name="TEnum">The type of the target enum.</typeparam>
 70        /// <returns>True if a match is found; otherwise, false.</returns>
 71        public bool TryDehumanizeTo<TEnum>(out TEnum? result, CultureInfo? culture = null)
 72            where TEnum : struct, Enum
 73        {
 74            try
 75            {
 676                result = input.DehumanizeTo<TEnum>(culture);
 377                return true;
 78            }
 379            catch (Exception)
 80            {
 381                result = null;
 382                return false;
 83            }
 84        }
 85    }
 86
 87    /// <summary>
 88    /// Core method that performs the dehumanization logic for both generic and non-generic versions of DehumanizeTo.
 89    /// </summary>
 90    /// <param name="input">The input string that was being dehumanized.</param>
 91    /// <param name="targetEnum">The target enum type to dehumanize to.</param>
 92    /// <param name="culture">The culture to use for humanization.</param>
 93    /// <returns>The matched enum value or null if no match is found and the behavior is set to return default.</returns
 94    /// <exception cref="ArgumentNullException">Thrown when the targetEnum is null.</exception>
 95    /// <exception cref="ArgumentException">Thrown when the targetEnum is not an enum.</exception>
 96    private static Enum DehumanizeCore(string? input, Type targetEnum, CultureInfo? culture)
 97    {
 2498        ArgumentNullException.ThrowIfNull(targetEnum);
 99
 24100        if (!targetEnum.IsEnum)
 101        {
 3102            throw new ArgumentException($"Type '{targetEnum}' is not an enum.", nameof(targetEnum));
 103        }
 104
 21105        if (string.IsNullOrWhiteSpace(input))
 106        {
 3107            throw new ArgumentException($"Input '{input}' cannot be null or empty.", nameof(input));
 108        }
 109
 110        // 1. Current parsing
 18111        if (Enum.TryParse(targetEnum, input, true, out var parsed) && parsed is Enum parsedEnum)
 112        {
 9113            return parsedEnum;
 114        }
 115
 116        // 2. Humanized parsing
 114117        foreach (var value in Enum.GetValues(targetEnum))
 118        {
 51119            var enumValue = (Enum)value;
 120
 51121            if (string.Equals(enumValue.Humanize(culture: culture), input, StringComparison.OrdinalIgnoreCase))
 122            {
 3123                return enumValue;
 124            }
 125        }
 126
 127        // 3. Not found
 3128        throw new KeyNotFoundException($"No matching enum value found for input '{input}' in enum type '{targetEnum}'.")
 3129    }
 130}
 131