< Summary - CoreWCF Coverage — PR #1766

Information
Class: CoreWCF.BuildTools.MethodSymbolExtensions
Assembly: CoreWCF.BuildTools.Roslyn3.11
File(s): /home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.BuildTools/src/Extensions.cs
Line coverage
91%
Covered lines: 11
Uncovered lines: 1
Coverable lines: 12
Total lines: 149
Line coverage: 91.6%
Branch coverage
75%
Covered branches: 9
Total branches: 12
Branch coverage: 75%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
IsGeneratedCode(...)50%44100%
IsMatchingUserProvidedMethod(...)87.5%8890.9%

File(s)

/home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.BuildTools/src/Extensions.cs

#LineLine coverage
 1// Licensed to the .NET Foundation under one or more agreements.
 2// The .NET Foundation licenses this file to you under the MIT license.
 3
 4using System.Linq;
 5using Microsoft.CodeAnalysis;
 6using Microsoft.CodeAnalysis.CSharp;
 7using Microsoft.CodeAnalysis.CSharp.Syntax;
 8
 9namespace CoreWCF.BuildTools;
 10
 11internal static class MethodSymbolExtensions
 12{
 13    public static bool? IsGeneratedCode(this IMethodSymbol methodSymbol)
 14014        => methodSymbol.Locations.FirstOrDefault()?.SourceTree?.FilePath.EndsWith(".g.cs");
 15
 16    public static bool IsMatchingUserProvidedMethod(this IMethodSymbol methodSymbol, IMethodSymbol userProvidedMethodSym
 17    {
 4818            int parameterFound = 0;
 4819            if (methodSymbol.Name != userProvidedMethodSymbol.Name)
 20            {
 021                return false;
 22            }
 23
 4824            var parameters = methodSymbol.Parameters;
 25
 24026            for (int i = 0,j = 0; i < userProvidedMethodSymbol.Parameters.Length; i++)
 27            {
 4828                IParameterSymbol parameterSymbol = userProvidedMethodSymbol.Parameters[i];
 4829                if (parameterSymbol.GetOneAttributeOf(coreWCFInjectedAttribute, fromServicesAttribute) is not null)
 30                {
 31                    continue;
 32                }
 33
 4834                if (parameterSymbol.IsMatchingParameter(parameters[j]))
 35                {
 3636                    j++;
 3637                    parameterFound++;
 38                }
 39            }
 40
 4841            return parameterFound == parameters.Length;
 42        }
 43}
 44
 45internal static class ParameterSymbolExtensions
 46{
 47    public static bool IsMatchingParameter(this IParameterSymbol symbol, IParameterSymbol parameterSymbol)
 48        => SymbolEqualityComparer.Default.Equals(symbol.Type, parameterSymbol.Type);
 49}
 50
 51internal static class SymbolExtensions
 52{
 53    public static AttributeData? GetOneAttributeOf(this ISymbol symbol, params INamedTypeSymbol?[] attributeTypeSymbols)
 54    {
 55            if (attributeTypeSymbols.Length == 0)
 56            {
 57                return null;
 58            }
 59
 60            foreach (var attribute in symbol.GetAttributes())
 61            {
 62                foreach (var namedTypeSymbol in attributeTypeSymbols.Where(static s => s is not null))
 63                {
 64                    if (SymbolEqualityComparer.Default.Equals(attribute.AttributeClass, namedTypeSymbol))
 65                    {
 66                        return attribute;
 67                    }
 68                }
 69            }
 70
 71            return null;
 72        }
 73
 74    public static bool HasOneAttributeInheritFrom(this ISymbol symbol, params INamedTypeSymbol?[] attributeTypeSymbols)
 75    {
 76            if (attributeTypeSymbols.Length == 0)
 77            {
 78                return false;
 79            }
 80
 81            foreach (var attribute in symbol.GetAttributes())
 82            {
 83                foreach (var @interface in attribute.AttributeClass!.AllInterfaces)
 84                {
 85                    foreach (var namedTypeSymbol in attributeTypeSymbols.Where(static s => s is not null))
 86                    {
 87                        if (SymbolEqualityComparer.Default.Equals(@interface, namedTypeSymbol))
 88                        {
 89                            return true;
 90                        }
 91                    }
 92                }
 93            }
 94
 95            return false;
 96        }
 97
 98    public static bool IsPrivate(this ISymbol symbol)
 99    {
 100            bool result = symbol.DeclaredAccessibility == Accessibility.Private;
 101            if (!result && symbol.ContainingType != null)
 102            {
 103                return symbol.ContainingType.IsPrivate();
 104            }
 105            return result;
 106        }
 107}
 108
 109internal static class NamedTypeSymbolExtensions
 110{
 111    public static bool IsPartial(this INamedTypeSymbol namedTypeSymbol, out INamedTypeSymbol parentType)
 112    {
 113            bool result = namedTypeSymbol.DeclaringSyntaxReferences.Select(static s => s.GetSyntax()).OfType<ClassDeclar
 114            if (result && namedTypeSymbol.ContainingType != null)
 115            {
 116                return namedTypeSymbol.ContainingType.IsPartial(out parentType);
 117            }
 118            parentType = namedTypeSymbol;
 119            return result;
 120        }
 121}
 122
 123internal static class TypedConstantExtensions
 124{
 125    public static string ToSafeCSharpString(this TypedConstant typedConstant)
 126    {
 127            if (typedConstant.Kind == TypedConstantKind.Array)
 128            {
 129                return $"new [] {typedConstant.ToCSharpString()}";
 130            }
 131
 132            return typedConstant.ToCSharpString();
 133
 134        }
 135}
 136
 137internal static class StringExtensions
 138{
 139    public static string ToMessagePropertyVariableName(this string propertyName)
 140    {
 141        string[] parts = propertyName.Split('.');
 142        return parts.Last().ToCamelCase();
 143    }
 144
 145    private static string ToCamelCase(this string propertyName)
 146    {
 147            return $"{char.ToLowerInvariant(propertyName[0])}{propertyName.Substring(1)}";
 148        }
 149}