< Summary

Information
Class: MyNet.Humanizer.EnumExtensions
Assembly: MyNet.Humanizer
File(s): https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.Humanizer/Extensions/EnumExtensions.cs
Tag: 323_28699572109
Line coverage
25%
Covered lines: 1
Uncovered lines: 3
Coverable lines: 4
Total lines: 54
Line coverage: 25%
Branch coverage
50%
Covered branches: 1
Total branches: 2
Branch coverage: 50%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
HumanizeWith(...)100%210%
HumanizeWith(...)100%210%
GetDescription(...)50%22100%

File(s)

https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.Humanizer/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.ComponentModel;
 9using System.Globalization;
 10using MyNet.Humanizer.Display;
 11using MyNet.Primitives;
 12using MyNet.Reflection;
 13
 14#pragma warning disable IDE0130 // Namespace does not match folder structure
 15namespace MyNet.Humanizer;
 16#pragma warning restore IDE0130 // Namespace does not match folder structure
 17
 18/// <summary>
 19/// Contains extension methods for humanizing Enums.
 20/// </summary>
 21public static class EnumExtensions
 22{
 23    extension(Enum value)
 24    {
 25        /// <summary>
 26        /// Gets the display name using an explicit provider resolved from DI.
 27        /// </summary>
 28        /// <param name="provider">The display name provider.</param>
 29        /// <param name="culture">The culture to use when retrieving the display name.</param>
 30        /// <returns>The display name of the enum value.</returns>
 31        public string HumanizeWith(IDisplayTextStrategy<Enum> provider, CultureInfo? culture = null)
 032            => value.HumanizeWith(provider, DisplayTextOptions.Default, culture);
 33
 34        /// <summary>
 35        /// Gets the display name using an explicit provider resolved from DI.
 36        /// </summary>
 37        /// <param name="provider">The display name provider.</param>
 38        /// <param name="options">The options to use when retrieving the display name.</param>
 39        /// <param name="culture">The culture to use when retrieving the display name.</param>
 40        /// <returns>The display name of the enum value.</returns>
 41        public string HumanizeWith(IDisplayTextStrategy<Enum> provider, DisplayTextOptions options, CultureInfo? culture
 42        {
 043            ArgumentNullException.ThrowIfNull(provider);
 044            return provider.GetDisplayText(value, options, culture.OrCurrent());
 45        }
 46
 47        /// <summary>
 48        /// Gets the description of the enum value, if it has a <see cref="DescriptionAttribute"/>. Otherwise, returns n
 49        /// </summary>
 50        /// <returns>The description of the enum value, or null if not defined.</returns>
 651        public string? GetDescription() => value.GetAttribute<DescriptionAttribute>()?.Description;
 52    }
 53}
 54