< Summary - CoreWCF Coverage — PR #1766

Information
Class: CoreWCF.BuildTools.SymbolExtensions
Assembly: CoreWCF.BuildTools.Roslyn4.0
File(s): /home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.BuildTools/src/Extensions.cs
Line coverage
90%
Covered lines: 19
Uncovered lines: 2
Coverable lines: 21
Total lines: 149
Line coverage: 90.4%
Branch coverage
86%
Covered branches: 19
Total branches: 22
Branch coverage: 86.3%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
GetOneAttributeOf(...)75%8887.5%
HasOneAttributeInheritFrom(...)90%101088.88%
IsPrivate(...)100%44100%

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)
 14        => methodSymbol.Locations.FirstOrDefault()?.SourceTree?.FilePath.EndsWith(".g.cs");
 15
 16    public static bool IsMatchingUserProvidedMethod(this IMethodSymbol methodSymbol, IMethodSymbol userProvidedMethodSym
 17    {
 18            int parameterFound = 0;
 19            if (methodSymbol.Name != userProvidedMethodSymbol.Name)
 20            {
 21                return false;
 22            }
 23
 24            var parameters = methodSymbol.Parameters;
 25
 26            for (int i = 0,j = 0; i < userProvidedMethodSymbol.Parameters.Length; i++)
 27            {
 28                IParameterSymbol parameterSymbol = userProvidedMethodSymbol.Parameters[i];
 29                if (parameterSymbol.GetOneAttributeOf(coreWCFInjectedAttribute, fromServicesAttribute) is not null)
 30                {
 31                    continue;
 32                }
 33
 34                if (parameterSymbol.IsMatchingParameter(parameters[j]))
 35                {
 36                    j++;
 37                    parameterFound++;
 38                }
 39            }
 40
 41            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    {
 311255            if (attributeTypeSymbols.Length == 0)
 56            {
 057                return null;
 58            }
 59
 849260            foreach (var attribute in symbol.GetAttributes())
 61            {
 1243862                foreach (var namedTypeSymbol in attributeTypeSymbols.Where(static s => s is not null))
 63                {
 339064                    if (SymbolEqualityComparer.Default.Equals(attribute.AttributeClass, namedTypeSymbol))
 65                    {
 226866                        return attribute;
 67                    }
 68                }
 69            }
 70
 84471            return null;
 226872        }
 73
 74    public static bool HasOneAttributeInheritFrom(this ISymbol symbol, params INamedTypeSymbol?[] attributeTypeSymbols)
 75    {
 16276            if (attributeTypeSymbols.Length == 0)
 77            {
 078                return false;
 79            }
 80
 56281            foreach (var attribute in symbol.GetAttributes())
 82            {
 35083                foreach (var @interface in attribute.AttributeClass!.AllInterfaces)
 84                {
 25085                    foreach (var namedTypeSymbol in attributeTypeSymbols.Where(static s => s is not null))
 86                    {
 5687                        if (SymbolEqualityComparer.Default.Equals(@interface, namedTypeSymbol))
 88                        {
 3089                            return true;
 90                        }
 91                    }
 92                }
 93            }
 94
 13295            return false;
 3096        }
 97
 98    public static bool IsPrivate(this ISymbol symbol)
 99    {
 216100            bool result = symbol.DeclaredAccessibility == Accessibility.Private;
 216101            if (!result && symbol.ContainingType != null)
 102            {
 68103                return symbol.ContainingType.IsPrivate();
 104            }
 148105            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}