< Summary - erichiller/mkmrk.Channels coverage

Information
Class: mkmrk.Channels.Extensions
Assembly: mkmrk.Channels
File(s): /home/runner/work/mkmrk.Channels/mkmrk.Channels/src/mkmrk.Channels/Common.cs
Tag: 161_8859726157
Line coverage
78%
Covered lines: 15
Uncovered lines: 4
Coverable lines: 19
Total lines: 82
Line coverage: 78.9%
Branch coverage
62%
Covered branches: 10
Total branches: 16
Branch coverage: 62.5%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Coverage history

Metrics

MethodBranch coverage Cyclomatic complexity Line coverage
GenericTypeShortDescriptor(...)75%480%
toXSeparatedString(...)58.33%1276.92%
ToCommaSeparatedString(...)100%1100%

File(s)

/home/runner/work/mkmrk.Channels/mkmrk.Channels/src/mkmrk.Channels/Common.cs

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3using System.Diagnostics.CodeAnalysis;
 4using System.Linq;
 5using System.Threading.Tasks;
 6
 7namespace mkmrk.Channels;
 8
 9/// <summary>
 10/// Extensions for <see cref="IEnumerable{T}"/> and similar, mostly for <see cref="System.Linq"/> type applications
 11/// </summary>
 12internal static class Extensions {
 13    /// <summary>
 14    /// Display a more concise Generic Type representation
 15    /// </summary>
 16    public static string GenericTypeShortDescriptor( this Type type, bool useShortGenericName = true ) =>
 217        type switch {
 018            null                      => throw new ArgumentNullException( nameof(type) ),
 219            _ when type.IsGenericType => $"{type.Name.Split( '`' ).First()}<{type.GenericTypeArguments.Select( t => Gene
 220            _                         => type.Name
 221        };
 22
 23
 24    /// <summary>
 25    /// Create <paramref name="joinString"/> separated string of elements
 26    /// </summary>
 27    [ return: NotNullIfNotNull( "list" ) ]
 228    private static string? toXSeparatedString( this System.Collections.IEnumerable? list, int? maxElementsToPrint, strin
 229        if ( list is null ) {
 030            return null;
 31        }
 232        List<string> strList = new ();
 233        int          i       = 0;
 234        foreach ( var x in list ) {
 235            strList.Add( x?.ToString() ?? "null" );
 236            if ( maxElementsToPrint is { } && ++i == maxElementsToPrint ) {
 037                strList.Add( "..." );
 038            }
 239        }
 240        return String.Join( joinString, strList );
 241    }
 42
 43    /// <summary>
 44    /// Create comma separated string of elements
 45    /// </summary>
 46    /// <returns>
 47    /// eg. <c>element1, element2, element3</c>
 48    /// </returns>
 49    [ return: NotNullIfNotNull( "list" ) ]
 50    internal static string? ToCommaSeparatedString( this System.Collections.IEnumerable? list, int? maxElementsToPrint =
 251        => toXSeparatedString( list, maxElementsToPrint, joinString );
 52}
 53
 54/// <summary>
 55/// Augmentations for <see cref="System.Threading.Tasks.ValueTask"/>
 56/// </summary>
 57internal static class ValueTaskExtensions {
 58    /// <summary>
 59    ///
 60    /// </summary>
 61    /// <remarks>This runs faster than using <c>Task.WhenAll( ...Select( ...AsTask ) )</c></remarks>
 62    [ SuppressMessage( "Design", "CA1031:Do not catch general exception types", Justification = "Required in order to co
 63    [ SuppressMessage( "Maintainability", "CA1508:Avoid dead conditional code" ) ]
 64    internal static async ValueTask WhenAll( this ValueTask[] tasks ) {
 65        // We don't allocate the list if no task throws
 66        List<Exception>? exceptions = null;
 67
 68        for ( var i = 0 ; i < tasks.Length ; i++ )
 69            try {
 70                await tasks[ i ].ConfigureAwait( false );
 71                // } catch ( TaskCanceledException ) {
 72                //     // TODO: is this correct?
 73                //     return;
 74            } catch ( Exception ex ) {
 75                exceptions ??= new List<Exception>( tasks.Length );
 76                exceptions.Add( ex );
 77            }
 78        if ( exceptions is not null ) {
 79            throw new AggregateException( exceptions );
 80        }
 81    }
 82}