< Summary

Information
Class: MyNet.Humanizer.Display.Strategies.DisplayTextStrategyBase<T>
Assembly: MyNet.Humanizer
File(s): https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.Humanizer/Display/Strategies/DisplayTextStrategyBase.cs
Tag: 323_28699572109
Line coverage
96%
Covered lines: 25
Uncovered lines: 1
Coverable lines: 26
Total lines: 140
Line coverage: 96.1%
Branch coverage
77%
Covered branches: 14
Total branches: 18
Branch coverage: 77.7%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
GetDisplayText(...)75%8881.25%
MyNet.Humanizer.Display.IDisplayTextStrategy.GetDisplayText(...)100%11100%
ProvideFallback(...)100%11100%
ResolveDisplayAttribute(...)70%1010100%
BuildResourceKey(...)100%11100%
BuildTranslationOptions(...)100%11100%

File(s)

https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.Humanizer/Display/Strategies/DisplayTextStrategyBase.cs

#LineLine coverage
 1// -----------------------------------------------------------------------
 2// <copyright file="DisplayTextStrategyBase.cs" company="Stéphane ANDRE">
 3// Copyright (c) Stéphane ANDRE. All rights reserved.
 4// </copyright>
 5// -----------------------------------------------------------------------
 6
 7using System;
 8using System.ComponentModel.DataAnnotations;
 9using System.Globalization;
 10using System.Resources;
 11using MyNet.Globalization.Localization.Translation;
 12using MyNet.Globalization.Localization.Translation.KeyGeneration;
 13using MyNet.Primitives;
 14
 15namespace MyNet.Humanizer.Display.Strategies;
 16
 17/// <summary>
 18/// Base class for display text providers, implementing the common logic for retrieving display texts from attributes, t
 19/// </summary>
 20/// <param name="translationService">The translator to use for translating resource keys.</param>
 21/// <param name="keyProvider">The key provider to use for generating translation keys.</param>
 22/// <typeparam name="T">The type of values for which to provide display texts.</typeparam>
 23public abstract class DisplayTextStrategyBase<T>(ITranslationService translationService, ITranslationKeyProvider keyProv
 24    where T : notnull
 25{
 26    /// <inheritdoc/>
 27    public virtual string GetDisplayText(T value, DisplayTextOptions options, CultureInfo culture)
 28    {
 11929        ArgumentNullException.ThrowIfNull(value);
 30
 31        // 1. Symbol
 11632        if (options.Style == DisplayStyle.Symbol)
 33        {
 234            var symbol = ProvideSymbol(value, culture);
 35
 236            if (!string.IsNullOrWhiteSpace(symbol))
 37            {
 038                return symbol;
 39            }
 40        }
 41
 42        // 2. Display name
 11643        var displayText = ProvideDisplayText(value, culture);
 44
 11345        if (!string.IsNullOrWhiteSpace(displayText))
 46        {
 2347            return displayText;
 48        }
 49
 50        // 3. Translation
 9051        var resourceKey = BuildResourceKey(value);
 9052        var translation = translationService.Translate(resourceKey, BuildTranslationOptions(options), culture);
 53
 9054        return !string.IsNullOrWhiteSpace(translation)
 9055            ? translation
 9056            :
 9057
 9058            // 4. Fallback
 9059            ProvideFallback(value, culture).OrEmpty();
 60    }
 61
 62    /// <summary>
 63    /// Explicit implementation of the non-generic IDisplayTextStrategy interface method, which casts the input value to
 64    /// </summary>
 65    /// <param name="value">The value for which to get the display text.</param>
 66    /// <param name="options">The options to use when generating the display text.</param>
 67    /// <param name="culture">The culture to use for determining the display text.</param>
 68    /// <returns>The display text for the given value.</returns>
 10469    string IDisplayTextStrategy.GetDisplayText(object value, DisplayTextOptions options, CultureInfo culture) => GetDisp
 70
 71    /// <summary>
 72    /// Provides the symbol for the specified value and culture, if available. This method should be implemented by deri
 73    /// </summary>
 74    /// <param name="value">The value for which to provide the symbol.</param>
 75    /// <param name="culture">The culture to use for symbol retrieval.</param>
 76    /// <returns>The symbol for the specified value and culture, or null/empty if not defined.</returns>
 77    protected abstract string? ProvideSymbol(T value, CultureInfo culture);
 78
 79    /// <summary>
 80    /// Provides the display name for the specified value and culture, if available. This method should be implemented b
 81    /// </summary>
 82    /// <param name="value">The value for which to provide the display name.</param>
 83    /// <param name="culture">The culture to use for display name retrieval.</param>
 84    /// <returns>The display name for the specified value and culture, or null/empty if not defined.</returns>
 85    protected abstract string? ProvideDisplayText(T value, CultureInfo culture);
 86
 87    /// <summary>
 88    /// Provides a fallback display name for the specified value and culture, if available. This method can be implement
 89    /// </summary>
 90    /// <param name="value">The value for which to provide the fallback display name.</param>
 91    /// <param name="culture">The culture to use for fallback display name retrieval.</param>
 92    /// <returns>The fallback display name for the specified value and culture, or null/empty if not defined.</returns>
 5193    protected virtual string? ProvideFallback(T value, CultureInfo culture) => value.ToString();
 94
 95    /// <summary>
 96    /// Resolves the display name from the specified DisplayAttribute, using the ResourceType if specified, or falling b
 97    /// </summary>
 98    /// <param name="displayAttribute">The DisplayAttribute to resolve.</param>
 99    /// <param name="culture">The culture to use for resource lookup.</param>
 100    /// <returns>The resolved display name.</returns>
 101    protected virtual string ResolveDisplayAttribute(DisplayAttribute displayAttribute, CultureInfo culture)
 102    {
 103        // 1.1 Display attribute with ResourceType specified
 18104        if (displayAttribute.ResourceType is not null)
 105        {
 106            // 1.1.1 Display attribute with ResourceType specified (Name)
 107            // 1.1.2 Display attribute with ResourceType specified (Description)
 3108            return new ResourceManager(displayAttribute.ResourceType).GetString(displayAttribute.Name ?? displayAttribut
 109        }
 110
 111        // 1.2 Display attribute with ResourceType specified (Name)
 15112        if (!string.IsNullOrWhiteSpace(displayAttribute.Name))
 113        {
 3114            return displayAttribute.Name;
 115        }
 116
 117        // 1.3 Display attribute without ResourceType specified (Description)
 12118        return !string.IsNullOrWhiteSpace(displayAttribute.Description) ? displayAttribute.Description : string.Empty;
 119    }
 120
 121    /// <summary>
 122    /// Builds the resource key for the specified enum value, following the convention of "{EnumType}{EnumStringValue}".
 123    /// </summary>
 124    /// <param name="value">The enum value for which to build the resource key.</param>
 125    /// <returns>The resource key for the specified enum value.</returns>
 126    protected virtual string BuildResourceKey(T value)
 127    {
 90128        ArgumentNullException.ThrowIfNull(value);
 129
 90130        return keyProvider.GetKey(value);
 131    }
 132
 133    /// <summary>
 134    /// Builds the translation options based on the specified display name options, including style and quantity informa
 135    /// </summary>
 136    /// <param name="options">The display name options to use for building the translation options.</param>
 137    /// <returns>The built translation options.</returns>
 90138    protected virtual TranslationOptions BuildTranslationOptions(DisplayTextOptions options) => new TranslationOptionsBu
 139}
 140