< Summary

Information
Class: MyNet.UI.Locators.Conventions.NamingConventionHelpers
Assembly: MyNet.UI
File(s): https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.UI/Locators/Conventions/NamingConventionHelpers.cs
Tag: 323_28699572109
Line coverage
96%
Covered lines: 29
Uncovered lines: 1
Coverable lines: 30
Total lines: 120
Line coverage: 96.6%
Branch coverage
87%
Covered branches: 14
Total branches: 16
Branch coverage: 87.5%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.cctor()100%11100%
RemoveSuffix(...)100%44100%
GetBaseNameFromViewModel(...)100%11100%
GetBaseNameFromView(...)100%11100%
IsViewType(...)100%11100%
IsViewModelType(...)100%11100%
GetAssemblyType(...)100%11100%
ReplaceNamespaceSegment(...)100%88100%
GetParentNamespace(...)50%4475%

File(s)

https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.UI/Locators/Conventions/NamingConventionHelpers.cs

#LineLine coverage
 1// -----------------------------------------------------------------------
 2// <copyright file="NamingConventionHelpers.cs" company="Stéphane ANDRE">
 3// Copyright (c) Stéphane ANDRE. All rights reserved.
 4// </copyright>
 5// -----------------------------------------------------------------------
 6
 7using System;
 8
 9namespace MyNet.UI.Locators.Conventions;
 10
 11/// <summary>
 12/// Provides helper methods for naming conventions used in view and view model resolution, such as removing common suffi
 13/// </summary>
 14internal static class NamingConventionHelpers
 15{
 16    /// <summary>
 17    /// Gets the list of recognized view suffixes used for type resolution.
 18    /// </summary>
 319    internal static readonly string[] ViewSuffixes =
 320    [
 321        "View", "Control", "UserControl", "Page", "Activity", "Window", "Fragment"
 322    ];
 23
 324    private static readonly string[] ViewModelSuffixes =
 325    [
 326        "ViewModel"
 327    ];
 28
 29    /// <summary>
 30    /// Removes any of the specified suffixes from the end of the given name, if present. Comparison is case-insensitive
 31    /// </summary>
 32    /// <param name="name">The name from which to remove the suffix.</param>
 33    /// <param name="suffixes">An array of suffixes to remove.</param>
 34    /// <returns>The name without the suffix if a match was found; otherwise, the original name.</returns>
 35    public static string RemoveSuffix(string name, string[] suffixes)
 36    {
 31837        foreach (var suffix in suffixes)
 38        {
 12339            if (name.EndsWith(suffix, StringComparison.OrdinalIgnoreCase))
 5440                return name[..^suffix.Length];
 41        }
 42
 943        return name;
 44    }
 45
 46    /// <summary>
 47    /// Removes common view model suffixes from the given type name to derive the base name for view resolution. This me
 48    /// </summary>
 49    /// <param name="type">The type from which to derive the base name.</param>
 50    /// <returns>The base name without the view model suffix.</returns>
 2451    public static string GetBaseNameFromViewModel(Type type) => RemoveSuffix(type.Name, ViewModelSuffixes);
 52
 53    /// <summary>
 54    /// Removes common view suffixes from the given type name to derive the base name for view model resolution. This me
 55    /// </summary>
 56    /// <param name="type">The type from which to derive the base name.</param>
 57    /// <returns>The base name without the view suffix.</returns>
 3958    public static string GetBaseNameFromView(Type type) => RemoveSuffix(type.Name, ViewSuffixes);
 59
 60    /// <summary>
 61    /// Determines whether the specified type is a view type based on its name ending with one of the recognized view su
 62    /// </summary>
 63    /// <param name="type">The type to check.</param>
 64    /// <returns><see langword="true"/> if the type is a view type; otherwise, <see langword="false"/>.</returns>
 2465    public static bool IsViewType(Type type) => GetBaseNameFromView(type) != type.Name;
 66
 67    /// <summary>
 68    /// Determines whether the specified type is a view model type (name ends with <c>ViewModel</c>).
 69    /// </summary>
 70    public static bool IsViewModelType(Type type) =>
 4871        type.Name.EndsWith("ViewModel", StringComparison.Ordinal);
 72
 73    /// <summary>
 74    /// Loads a type from the same assembly as <paramref name="source"/> using its fully qualified name.
 75    /// </summary>
 76    public static Type? GetAssemblyType(Type source, string fullTypeName) =>
 4277        source.Assembly.GetType(fullTypeName);
 78
 79    /// <summary>
 80    /// Replaces exact namespace segments (dot-separated) with a new value. Only whole segments are matched.
 81    /// </summary>
 82    /// <param name="ns">The original namespace.</param>
 83    /// <param name="from">The segment to replace.</param>
 84    /// <param name="to">The new segment.</param>
 85    /// <returns>The modified namespace, or the original when no segment matches.</returns>
 86    public static string? ReplaceNamespaceSegment(string? ns, string from, string to)
 87    {
 3988        if (string.IsNullOrEmpty(ns))
 389            return ns;
 90
 3691        var segments = ns.Split('.');
 3692        var replaced = false;
 93
 34894        for (var i = 0; i < segments.Length; i++)
 95        {
 13896            if (!segments[i].Equals(from, StringComparison.Ordinal))
 97                continue;
 98
 3699            segments[i] = to;
 36100            replaced = true;
 101        }
 102
 36103        return replaced ? string.Join('.', segments) : ns;
 104    }
 105
 106    /// <summary>
 107    /// Gets the parent namespace of the given namespace.
 108    /// </summary>
 109    /// <param name="ns">The namespace.</param>
 110    /// <returns>The parent namespace if it exists; otherwise, the original namespace.</returns>
 111    public static string? GetParentNamespace(string? ns)
 112    {
 6113        if (string.IsNullOrWhiteSpace(ns))
 0114            return ns;
 115
 6116        var lastDot = ns.LastIndexOf('.');
 6117        return lastDot > 0 ? ns[..lastDot] : ns;
 118    }
 119}
 120