< Summary - SonghayCore

Line coverage
27%
Covered lines: 50
Uncovered lines: 133
Coverable lines: 183
Total lines: 495
Line coverage: 27.3%
Branch coverage
25%
Covered branches: 18
Total branches: 70
Branch coverage: 25.7%
Method coverage

Method coverage is only available for sponsors.

Upgrade to PRO version

Metrics

File(s)

/home/rasx/sourceRoot/SonghayCore/SonghayCore/Extensions/IActivityExtensions._.cs

#LineLine coverage
 1namespace Songhay.Extensions;
 2
 3/// <summary>
 4/// Extensions of <see cref="IActivity"/>
 5/// </summary>
 6// ReSharper disable once InconsistentNaming
 7public static partial class IActivityExtensions
 8{
 9    /// <summary>
 10    /// Starts the <see cref="IActivity"/>.
 11    /// </summary>
 12    /// <param name="activity">The activity.</param>
 13    /// <param name="args">The arguments.</param>
 14    /// <param name="traceSource">The trace source.</param>
 15    /// <returns>The <see cref="TraceSource"/> log.</returns>
 16    public static string? StartActivity(this IActivity? activity, ProgramArgs? args, TraceSource? traceSource) =>
 117        activity.StartActivity(args, traceSource, traceWriterGetter: null, flushLog: true);
 18
 19    /// <summary>
 20    /// Starts the <see cref="IActivity"/>.
 21    /// </summary>
 22    /// <param name="activity">The activity.</param>
 23    /// <param name="args">The arguments.</param>
 24    /// <param name="traceSource">The trace source.</param>
 25    /// <param name="traceWriterGetter">gets the <see cref="TextWriter"/> for the <see cref="TraceSource"/>.</param>
 26    /// <param name="flushLog">when <c>true</c> return <see cref="TraceSource"/> log</param>
 27    /// <returns>The <see cref="TraceSource"/> log when <c>flushLog</c> is <c>true</c>.</returns>
 28    public static string? StartActivity(this IActivity? activity, ProgramArgs? args, TraceSource? traceSource,
 29        Func<TextWriter>? traceWriterGetter, bool flushLog)
 230    {
 231        ArgumentNullException.ThrowIfNull(activity);
 232        ArgumentNullException.ThrowIfNull(args);
 33
 234        using var writer = traceWriterGetter?.Invoke() ?? new StringWriter();
 235        using var listener = new TextWriterTraceListener(writer);
 36
 237        traceSource?.Listeners.Add(listener);
 38
 239        string? log = null;
 40
 41        try
 242        {
 243            activity.Start(args);
 244        }
 45        finally
 246        {
 247            if (flushLog)
 148            {
 149                listener.Flush();
 150                log = writer.ToString();
 151            }
 252        }
 53
 254        return log;
 255    }
 56
 57    /// <summary>
 58    /// Starts the <see cref="IActivity"/>
 59    /// for the specified output (<typeparamref name="TOutput"/>).
 60    /// </summary>
 61    /// <param name="activity">The activity.</param>
 62    /// <param name="input">The input.</param>
 63    /// <param name="traceSource">The trace source.</param>
 64    /// <returns><see cref="ActivityOutput{TOutput}"/></returns>
 65    public static ActivityOutput<TOutput> StartActivityForOutput<TInput, TOutput>(this IActivity? activity,
 66        TInput input, TraceSource? traceSource) =>
 167        activity.StartActivityForOutput<TInput, TOutput>(input, traceSource, traceWriterGetter: null,
 168            flushLog: true);
 69
 70    /// <summary>
 71    /// Starts the <see cref="IActivity"/>
 72    /// for the specified output (<typeparamref name="TOutput"/>).
 73    /// </summary>
 74    /// <param name="activity">The activity.</param>
 75    /// <param name="input">The input.</param>
 76    /// <param name="traceSource">The trace source.</param>
 77    /// <param name="traceWriterGetter">gets the <see cref="TextWriter"/> for the <see cref="TraceSource"/>.</param>
 78    /// <param name="flushLog">when <c>true</c> return <see cref="TraceSource"/> log</param>
 79    /// <returns>The <see cref="TraceSource"/> log when <c>flushLog</c> is <c>true</c>.</returns>
 80    public static ActivityOutput<TOutput> StartActivityForOutput<TInput, TOutput>(this IActivity? activity,
 81        TInput input, TraceSource? traceSource, Func<TextWriter>? traceWriterGetter, bool flushLog)
 182    {
 183        using var writer = traceWriterGetter?.Invoke() ?? new StringWriter();
 184        using var listener = new TextWriterTraceListener(writer);
 85
 186        traceSource?.Listeners.Add(listener);
 87
 188        var activityOutput = new ActivityOutput<TOutput>();
 89
 90        try
 191        {
 192            var activityWithOutput = activity.ToActivityWithOutput<TInput, TOutput>();
 93
 194            activityOutput.Output = activityWithOutput!.StartForOutput(input);
 195        }
 96        finally
 197        {
 198            if (flushLog)
 199            {
 1100                listener.Flush();
 1101                var log = writer.ToString();
 1102                activityOutput.Log = log;
 1103            }
 1104        }
 105
 1106        return activityOutput;
 1107    }
 108
 109    /// <summary>
 110    /// Starts the <see cref="IActivity"/>, asynchronously.
 111    /// </summary>
 112    /// <typeparam name="TInput">The type of the input.</typeparam>
 113    /// <typeparam name="TOutput">The type of the output.</typeparam>
 114    /// <param name="activity">The activity.</param>
 115    /// <param name="input">The input.</param>
 116    /// <param name="traceSource">The <see cref="TraceSource"/>.</param>
 117    /// <returns><see cref="ActivityOutput{TOutput}"/></returns>
 118    public static async Task<ActivityOutput<TOutput>> StartActivityAsync<TInput, TOutput>(this IActivity? activity,
 119        TInput input, TraceSource? traceSource) =>
 0120        await activity
 0121            .StartActivityAsync<TInput, TOutput>(input, traceSource, traceWriterGetter: null)
 0122            .ConfigureAwait(continueOnCapturedContext: false);
 123
 124    /// <summary>
 125    /// Starts the <see cref="IActivity"/>, asynchronously.
 126    /// </summary>
 127    /// <typeparam name="TInput">The type of the input.</typeparam>
 128    /// <typeparam name="TOutput">The type of the output.</typeparam>
 129    /// <param name="activity">The activity.</param>
 130    /// <param name="input">The input.</param>
 131    /// <param name="traceSource">The <see cref="TraceSource"/>.</param>
 132    /// <param name="traceWriterGetter">gets the <see cref="TextWriter"/> for the <see cref="TraceSource"/>.</param>
 133    /// <returns><see cref="ActivityOutput{TOutput}"/></returns>
 134    public static async Task<ActivityOutput<TOutput>> StartActivityAsync<TInput, TOutput>(this IActivity? activity,
 135        TInput input, TraceSource? traceSource, Func<TextWriter>? traceWriterGetter)
 0136    {
 0137        ArgumentNullException.ThrowIfNull(activity);
 138
 0139        await using var writer = traceWriterGetter?.Invoke() ?? new StringWriter();
 0140        using var listener = new TextWriterTraceListener(writer);
 141
 0142        traceSource?.Listeners.Add(listener);
 143
 0144        var activityOutput = new ActivityOutput<TOutput>();
 145
 146        try
 0147        {
 0148            var activityWithOutput = activity.ToActivityWithTask<TInput, TOutput>();
 0149            activityOutput.Output = await activityWithOutput!
 0150                .StartAsync(input)
 0151                .ConfigureAwait(continueOnCapturedContext: false);
 0152        }
 153        finally
 0154        {
 0155            listener.Flush();
 0156            activityOutput.Log = writer.ToString();
 0157        }
 158
 0159        return activityOutput;
 0160    }
 161
 162    /// <summary>
 163    /// Starts the <see cref="IActivity"/>, asynchronously.
 164    /// </summary>
 165    /// <typeparam name="TInput">The type of the input.</typeparam>
 166    /// <param name="activity">The activity.</param>
 167    /// <param name="input">The input.</param>
 168    /// <param name="traceSource">The <see cref="TraceSource"/>.</param>
 169    /// <returns>The <see cref="TraceSource"/> log.</returns>
 170    public static async Task<string?> StartActivityAsync<TInput>(this IActivity? activity, TInput input,
 171        TraceSource? traceSource) =>
 0172        await activity
 0173            .StartActivityAsync(input, traceSource, traceWriterGetter: null, flushLog: true)
 0174            .ConfigureAwait(continueOnCapturedContext: false);
 175
 176    /// <summary>
 177    /// Starts the <see cref="IActivity"/>, asynchronously.
 178    /// </summary>
 179    /// <typeparam name="TInput">The type of the input.</typeparam>
 180    /// <param name="activity">The activity.</param>
 181    /// <param name="input">The input.</param>
 182    /// <param name="traceSource">The <see cref="TraceSource"/>.</param>
 183    /// <param name="traceWriterGetter">gets the <see cref="TextWriter"/> for the <see cref="TraceSource"/>.</param>
 184    /// <param name="flushLog">when <c>true</c> return <see cref="TraceSource"/> log</param>
 185    /// <returns>The <see cref="TraceSource"/> log when <c>flushLog</c> is <c>true</c>.</returns>
 186    public static async Task<string?> StartActivityAsync<TInput>(this IActivity? activity, TInput input,
 187        TraceSource? traceSource, Func<TextWriter>? traceWriterGetter, bool flushLog)
 0188    {
 0189        ArgumentNullException.ThrowIfNull(activity);
 190
 0191        await using var writer = traceWriterGetter?.Invoke() ?? new StringWriter();
 0192        using var listener = new TextWriterTraceListener(writer);
 193
 0194        traceSource?.Listeners.Add(listener);
 195
 0196        string? log = null;
 197
 198        try
 0199        {
 0200            var activityWithTask = activity.ToActivityWithTask<TInput>();
 0201            await activityWithTask!
 0202                .StartAsync(input)
 0203                .ConfigureAwait(continueOnCapturedContext: false);
 0204        }
 205        finally
 0206        {
 0207            if (flushLog)
 0208            {
 0209                listener.Flush();
 0210                log = writer.ToString();
 0211            }
 0212        }
 213
 0214        return log;
 0215    }
 216
 217    /// <summary>
 218    /// Starts the <see cref="IActivity"/>, asynchronously.
 219    /// </summary>
 220    /// <param name="activity">The activity.</param>
 221    /// <param name="traceSource">The <see cref="TraceSource"/>.</param>
 222    /// <returns>The <see cref="TraceSource"/> log.</returns>
 223    public static async Task<string?> StartActivityAsync(this IActivity? activity, TraceSource? traceSource) =>
 0224        await activity
 0225            .StartActivityAsync(traceSource, traceWriterGetter: null, flushLog: true)
 0226            .ConfigureAwait(continueOnCapturedContext: false);
 227
 228    /// <summary>
 229    /// Starts the <see cref="IActivity"/>, asynchronously.
 230    /// </summary>
 231    /// <param name="activity">The activity.</param>
 232    /// <param name="traceSource">The <see cref="TraceSource"/>.</param>
 233    /// <param name="traceWriterGetter">gets the <see cref="TextWriter"/> for the <see cref="TraceSource"/>.</param>
 234    /// <param name="flushLog">when <c>true</c> return <see cref="TraceSource"/> log</param>
 235    /// <returns>The <see cref="TraceSource"/> log when <c>flushLog</c> is <c>true</c>; otherwise, <c>null</c>.</returns
 236    public static async Task<string?> StartActivityAsync(this IActivity? activity, TraceSource? traceSource,
 237        Func<TextWriter>? traceWriterGetter, bool flushLog)
 0238    {
 0239        ArgumentNullException.ThrowIfNull(activity);
 240
 0241        await using var writer = traceWriterGetter?.Invoke() ?? new StringWriter();
 0242        using var listener = new TextWriterTraceListener(writer);
 243
 0244        traceSource?.Listeners.Add(listener);
 245
 0246        string? log = null;
 247
 248        try
 0249        {
 0250            var activityWithTask = activity.ToActivityWithTask();
 0251            await activityWithTask!
 0252                .StartAsync()
 0253                .ConfigureAwait(continueOnCapturedContext: false);
 0254        }
 255        finally
 0256        {
 0257            if (flushLog)
 0258            {
 0259                listener.Flush();
 0260                log = writer.ToString();
 0261            }
 0262        }
 263
 0264        return log;
 0265    }
 266}

/home/rasx/sourceRoot/SonghayCore/SonghayCore/Extensions/IActivityExtensions.Configuration.cs

#LineLine coverage
 1namespace Songhay.Extensions;
 2
 3// ReSharper disable once InconsistentNaming
 4public static partial class IActivityExtensions
 5{
 6    /// <summary>
 7    /// Returns <see cref="IActivity"/> with <see cref="IConfigurationRoot"/> added when available.
 8    /// </summary>
 9    /// <param name="activity">The activity.</param>
 10    /// <param name="configuration">The configuration.</param>
 11    public static IActivity WithConfiguration(this IActivity? activity, IConfigurationRoot? configuration)
 012    {
 013        ArgumentNullException.ThrowIfNull(activity);
 014        if (configuration == null) return activity;
 15
 16        // ReSharper disable once SuspiciousTypeConversion.Global
 017        if (activity is not IActivityConfigurationSupport activityWithConfiguration) return activity;
 18
 019        activityWithConfiguration.AddConfiguration(configuration);
 20
 021        return activity;
 022    }
 23}

/home/rasx/sourceRoot/SonghayCore/SonghayCore/Extensions/IActivityExtensions.ConsoleTraceListener.cs

#LineLine coverage
 1namespace Songhay.Extensions;
 2
 3// ReSharper disable once InconsistentNaming
 4public static partial class IActivityExtensions
 5{
 6    /// <summary>
 7    /// Starts the <see cref="IActivity"/>
 8    /// with <see cref="ConsoleTraceListener"/>.
 9    /// </summary>
 10    /// <param name="activity">The activity.</param>
 11    /// <param name="args">The arguments.</param>
 12    /// <param name="traceSource">The <see cref="TraceSource"/>.</param>
 13    /// <returns>The <see cref="TraceSource"/> log.</returns>
 14    public static void StartConsoleActivity(this IActivity? activity, ProgramArgs args, TraceSource? traceSource)
 015    {
 016        using var listener = new ConsoleTraceListener();
 17
 018        traceSource?.Listeners.Add(listener);
 19
 20        try
 021        {
 022            activity?.Start(args);
 023        }
 24        finally
 025        {
 026            listener.Flush();
 027        }
 028    }
 29
 30    /// <summary>
 31    /// Starts the <see cref="IActivity"/>, asynchronously
 32    /// with <see cref="ConsoleTraceListener"/>.
 33    /// </summary>
 34    /// <typeparam name="TInput">The type of the input.</typeparam>
 35    /// <param name="activity">The activity.</param>
 36    /// <param name="input">The input.</param>
 37    /// <param name="traceSource">The <see cref="TraceSource"/>.</param>
 38    /// <returns>The <see cref="TraceSource"/> log.</returns>
 39    public static async Task StartConsoleActivityAsync<TInput>(this IActivity? activity, TInput? input,
 40        TraceSource? traceSource)
 041    {
 042        ArgumentNullException.ThrowIfNull(activity);
 43
 044        using var listener = new ConsoleTraceListener();
 45
 046        traceSource?.Listeners.Add(listener);
 47
 048        var activityWithTask = activity.ToActivityWithTask<TInput>();
 49        try
 050        {
 051            await activityWithTask!
 052                .StartAsync(input)
 053                .ConfigureAwait(continueOnCapturedContext: false);
 054        }
 55        finally
 056        {
 057            listener.Flush();
 058        }
 059    }
 60
 61    /// <summary>
 62    /// Starts the <see cref="IActivity"/>, asynchronously
 63    /// with the specified <see cref="ConsoleTraceListener"/>.
 64    /// </summary>
 65    /// <typeparam name="TInput">The type of the input.</typeparam>
 66    /// <typeparam name="TOutput">The type of the output.</typeparam>
 67    /// <param name="activity">The activity.</param>
 68    /// <param name="input">The input.</param>
 69    /// <param name="traceSource">The <see cref="TraceSource"/>.</param>
 70    /// <returns>The <see cref="TraceSource"/> log.</returns>
 71    public static async Task<TOutput?> StartConsoleActivityAsync<TInput, TOutput>(this IActivity? activity,
 72        TInput? input, TraceSource? traceSource)
 073    {
 074        ArgumentNullException.ThrowIfNull(activity);
 75
 076        using var listener = new ConsoleTraceListener();
 77
 078        traceSource?.Listeners.Add(listener);
 79
 080        var activityWithTask = activity.ToActivityWithTask<TInput, TOutput>();
 81        TOutput? output;
 82        try
 083        {
 84
 085            output = await activityWithTask!
 086                .StartAsync(input)
 087                .ConfigureAwait(continueOnCapturedContext: false);
 088        }
 89        finally
 090        {
 091            listener.Flush();
 092        }
 93
 094        return output;
 095    }
 96
 97    /// <summary>
 98    /// Starts the <see cref="IActivity"/>, synchronously
 99    /// with the specified <see cref="ConsoleTraceListener"/>.
 100    /// </summary>
 101    /// <typeparam name="TInput">The type of the input.</typeparam>
 102    /// <typeparam name="TOutput">The type of the output.</typeparam>
 103    /// <param name="activity">The activity.</param>
 104    /// <param name="input">The input.</param>
 105    /// <param name="traceSource">The <see cref="TraceSource"/>.</param>
 106    /// <returns>The <see cref="TraceSource"/> log.</returns>
 107    public static TOutput? StartConsoleActivityForOutput<TInput, TOutput>(this IActivity? activity, TInput? input,
 108        TraceSource? traceSource)
 0109    {
 0110        ArgumentNullException.ThrowIfNull(activity);
 111
 0112        using var listener = new ConsoleTraceListener();
 113
 0114        traceSource?.Listeners.Add(listener);
 115
 0116        var activityWithOutput = activity.ToActivityWithOutput<TInput, TOutput>();
 117        TOutput? output;
 118        try
 0119        {
 120
 0121            output = activityWithOutput!.StartForOutput(input);
 0122        }
 123        finally
 0124        {
 0125            listener.Flush();
 0126        }
 127
 0128        return output;
 0129    }
 130}

/home/rasx/sourceRoot/SonghayCore/SonghayCore/Extensions/IActivityExtensions.Conversion.cs

#LineLine coverage
 1namespace Songhay.Extensions;
 2
 3// ReSharper disable once InconsistentNaming
 4public static partial class IActivityExtensions
 5{
 6    /// <summary>
 7    /// Converts the specified <see cref="IActivity" />
 8    /// to <see cref="IActivityWithOutput{TInput,TOutput}" />.
 9    /// </summary>
 10    /// <typeparam name="TInput">The type of the input.</typeparam>
 11    /// <typeparam name="TOutput">The type of the output.</typeparam>
 12    /// <param name="activity">The activity.</param>
 13    public static IActivityWithOutput<TInput?, TOutput?>?
 14        ToActivityWithOutput<TInput, TOutput>(this IActivity? activity) =>
 115        activity as IActivityWithOutput<TInput?, TOutput?>;
 16
 17    /// <summary>
 18    /// Converts the specified <see cref="IActivity" />
 19    /// to <see cref="IActivityWithTask{TInput,TOutput}" />.
 20    /// </summary>
 21    /// <typeparam name="TInput">The type of the input.</typeparam>
 22    /// <typeparam name="TOutput">The type of the output.</typeparam>
 23    /// <param name="activity">The activity.</param>
 24    public static IActivityWithTask<TInput?, TOutput?>? ToActivityWithTask<TInput, TOutput>(this IActivity? activity) =>
 025        activity as IActivityWithTask<TInput?, TOutput?>;
 26
 27    /// <summary>
 28    /// Converts the specified <see cref="IActivity" />
 29    /// to <see cref="IActivityWithTask{TInput}" />.
 30    /// </summary>
 31    /// <typeparam name="TInput">The type of the input.</typeparam>
 32    /// <param name="activity">The activity.</param>
 33    public static IActivityWithTask<TInput?>? ToActivityWithTask<TInput>(this IActivity? activity) =>
 034        activity as IActivityWithTask<TInput?>;
 35
 36    /// <summary>
 37    /// Converts the specified <see cref="IActivity" />
 38    /// to <see cref="IActivityWithTask" />.
 39    /// </summary>
 40    /// <param name="activity">The activity.</param>
 041    public static IActivityWithTask? ToActivityWithTask(this IActivity? activity) => activity as IActivityWithTask;
 42
 43    /// <summary>
 44    /// Converts the specified <see cref="IActivity" />
 45    /// to <see cref="IActivityWithTaskOutput{TOutput}" />.
 46    /// </summary>
 47    /// <typeparam name="TOutput">The type of the output.</typeparam>
 48    /// <param name="activity">The activity.</param>
 49    public static IActivityWithTaskOutput<TOutput?>? ToActivityWithTaskOutput<TOutput>(this IActivity? activity) =>
 050        activity as IActivityWithTaskOutput<TOutput?>;
 51}

/home/rasx/sourceRoot/SonghayCore/SonghayCore/Extensions/IActivityExtensions.Lazy.cs

#LineLine coverage
 1namespace Songhay.Extensions;
 2
 3// ReSharper disable once InconsistentNaming
 4public static partial class IActivityExtensions
 5{
 6    /// <summary>
 7    /// Gets the <see cref="IActivity"/>.
 8    /// </summary>
 9    /// <param name="activities">The activities.</param>
 10    /// <param name="activityName">Name of the activity.</param>
 11    public static IActivity? GetActivity(this Dictionary<string, Lazy<IActivity?>>? activities, string? activityName)
 512    {
 513        if (activities == null) return null;
 14
 515        activityName.ThrowWhenNullOrWhiteSpace();
 16
 517        if (!activities.ContainsKey(activityName))
 018            throw new FormatException($"The expected {nameof(IActivity)} name, {activityName}, is not here.");
 19
 520        var activity = activities[activityName].Value;
 521        if (activity == null) throw new NullReferenceException("The expected Activity is not here.");
 22
 523        return activity;
 524    }
 25}