< Summary

Information
Class: IceRpc.TypeExtensions
Assembly: IceRpc
File(s): /home/runner/work/icerpc-csharp/icerpc-csharp/src/IceRpc/TypeExtensions.cs
Tag: 1856_27024993493
Line coverage
94%
Covered lines: 37
Uncovered lines: 2
Coverable lines: 39
Total lines: 77
Line coverage: 94.8%
Branch coverage
81%
Covered branches: 13
Total branches: 16
Branch coverage: 81.2%
Method coverage
100%
Covered methods: 2
Fully covered methods: 1
Total methods: 2
Method coverage: 100%
Full method coverage: 50%

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
GetDefaultServicePath(...)78.57%141493.1%
GetDefaultServicePathForInterface()100%22100%

File(s)

/home/runner/work/icerpc-csharp/icerpc-csharp/src/IceRpc/TypeExtensions.cs

#LineLine coverage
 1// Copyright (c) ZeroC, Inc.
 2
 3using System.Diagnostics;
 4
 5namespace IceRpc;
 6
 7/// <summary>Provides extension methods for <see cref="Type" />.</summary>
 8public static class TypeExtensions
 9{
 10    /// <summary>Retrieves the default service path, as set by the attribute <see cref="DefaultServicePathAttribute"/>.
 11    /// </summary>
 12    /// <param name="type">The interface with the <see cref="DefaultServicePathAttribute"/> attribute, or a class
 13    /// that implements such an interface.</param>
 14    /// <returns>The default service path.</returns>
 15    /// <exception cref="ArgumentException">Thrown if <paramref name="type" /> is neither a class nor an interface, or
 16    /// if it does not have a <see cref="DefaultServicePathAttribute" /> attribute, or if it is a class that implements
 17    /// multiple interfaces with a <see cref="DefaultServicePathAttribute" /> attribute.</exception>
 18    /// <remarks>When <paramref name="type" /> is an interface, this method only searches for the attribute on the
 19    /// interface itself.</remarks>
 20    /// <seealso cref="RouterExtensions.Map(Router, IDispatcher)" />
 21    /// <seealso cref="RouterExtensions.Map{TService}(Router, IDispatcher)" />
 22    public static string GetDefaultServicePath(this Type type)
 2323    {
 2324        if (type.IsClass)
 1325        {
 1326            string? defaultServicePath = null;
 27
 9328            foreach (Type interfaceType in type.GetInterfaces())
 3029            {
 3030                if (GetDefaultServicePathForInterface(interfaceType) is string path)
 1931                {
 1932                    if (defaultServicePath is null)
 1333                    {
 1334                        defaultServicePath = path;
 1335                    }
 36                    else
 637                    {
 38                        // GetInterfaces() does not return the same interface more than once.
 639                        throw new ArgumentException(
 640                            $"The class '{type}' implements multiple interfaces with a {nameof(DefaultServicePathAttribu
 641                            nameof(type));
 42                    }
 1343                }
 2444            }
 45
 746            return defaultServicePath ?? throw new ArgumentException(
 747                    $"The class '{type}' does not implement any interface with a {nameof(DefaultServicePathAttribute)} a
 748                    nameof(type));
 49        }
 1050        else if (type.IsInterface)
 1051        {
 1052            return GetDefaultServicePathForInterface(type) ?? throw new ArgumentException(
 1053                $"The interface '{type}' does not have a {nameof(DefaultServicePathAttribute)} attribute.",
 1054                nameof(type));
 55        }
 56        else
 057        {
 058            throw new ArgumentException($"The type '{type}' is neither a class nor an interface.", nameof(type));
 59        }
 60
 61        static string? GetDefaultServicePathForInterface(Type type)
 4062        {
 4063            Debug.Assert(type.IsInterface);
 4064            object[] attributes = type.GetCustomAttributes(typeof(DefaultServicePathAttribute), inherit: false);
 4065            if (attributes.Length == 1)
 2966            {
 2967                return ((DefaultServicePathAttribute)attributes[0]).Value;
 68            }
 69            else
 1170            {
 71                // The DefaultServicePathAttribute does not allow multiple instances on the same interface.
 1172                Debug.Assert(attributes.Length == 0);
 1173                return null;
 74            }
 4075        }
 1776    }
 77}