< Summary

Information
Class: Amusoft.DotnetNew.Tests.Scopes.LoggingScope
Assembly: Amusoft.DotnetNew.Tests
File(s): /home/runner/work/Amusoft.DotnetNew.Tests/Amusoft.DotnetNew.Tests/src/Amusoft.DotnetNew.Tests/Scopes/LoggingScope.cs
Tag: 127_14865883074
Line coverage
100%
Covered lines: 37
Uncovered lines: 0
Coverable lines: 37
Total lines: 121
Line coverage: 100%
Branch coverage
93%
Covered branches: 30
Total branches: 32
Branch coverage: 93.7%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%88100%
get_Connected()100%11100%
AddRewriter(...)100%44100%
AddResult(...)100%44100%
AddInvocation(...)100%44100%
ToFullString(...)100%11100%
Print(...)100%22100%
ApplyRewriters(...)100%22100%
TryAddRewriter(...)100%22100%
TryAddInvocation(...)100%11100%
TryAddInvocation(...)50%22100%
TryAddResult(...)100%22100%
ToFullString()50%22100%

File(s)

/home/runner/work/Amusoft.DotnetNew.Tests/Amusoft.DotnetNew.Tests/src/Amusoft.DotnetNew.Tests/Scopes/LoggingScope.cs

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3using System.Diagnostics.CodeAnalysis;
 4using System.Linq;
 5using System.Text;
 6using Amusoft.DotnetNew.Tests.Diagnostics;
 7using Amusoft.DotnetNew.Tests.Interfaces;
 8using Amusoft.Toolkit.Threading;
 9
 10namespace Amusoft.DotnetNew.Tests.Scopes;
 11
 12/// <summary>
 13/// Logging scope to capture any logs
 14/// </summary>
 15public class LoggingScope : AmbientScope<LoggingScope>
 16{
 17  /// <summary>
 18  /// Constructor
 19  /// </summary>
 20  /// <param name="connected">whether to connect the current logging scope to its parent</param>
 3921  public LoggingScope(bool connected = true)
 22  {
 3923    Connected = connected;
 3924    if (ParentScope?._rewriters is { Count: > 0 } rewriters)
 25    {
 11226      foreach (var rewriter in rewriters)
 27      {
 4328        AddRewriter(rewriter);
 29      }
 30    }
 3931  }
 32
 33  /// <summary>
 34  /// Whether to forward rewriters, invocations and results to parent scopes
 35  /// </summary>
 11936  public bool Connected { get; }
 37
 3938  private readonly List<ICommandData> _commandData = new();
 3939  private readonly HashSet<ICommandRewriter> _rewriters = new();
 40
 41  internal void AddRewriter(ICommandRewriter rewriter)
 42  {
 6543    _rewriters.Add(rewriter);
 6544    if(Connected && ParentScope is { } parentScope)
 345      parentScope.AddRewriter(rewriter);
 6546  }
 47
 48  internal void AddResult(ICommandResult result)
 49  {
 3450    _commandData.Add(result);
 3451    if(Connected && ParentScope is { } parentScope)
 352      parentScope.AddResult(result);
 3453  }
 54
 55  internal void AddInvocation(ICommandInvocation command)
 56  {
 2057    _commandData.Add(command);
 2058    if(Connected && ParentScope is { } parentScope)
 359      parentScope.AddInvocation(command);
 2060  }
 61
 62  /// <summary>
 63  /// prints the output as string
 64  /// </summary>
 65  /// <param name="kind"></param>
 66  /// <returns></returns>
 67  public string ToFullString(PrintKind kind)
 68  {
 1469    var sb = new StringBuilder();
 1470    Print(sb, kind);
 1471    return sb.ToString();
 72  }
 73
 74  /// <summary>
 75  /// Prints the collected data
 76  /// </summary>
 77  /// <param name="stringBuilder"></param>
 78  /// <param name="kind"></param>
 79  internal void Print(StringBuilder stringBuilder, PrintKind kind)
 80  {
 1781    var data = GetPrintables();
 82
 7683    foreach (var printable in data)
 84    {
 2185      printable.Print(stringBuilder);
 86    }
 87
 1788    ApplyRewriters(stringBuilder);
 89
 90    [ExcludeFromCodeCoverage]
 91    IEnumerable<IPrintable> GetPrintables()
 92    {
 93      IEnumerable<IPrintable> printables = kind switch
 94      {
 95        PrintKind.All => _commandData,
 96        PrintKind.Invocations => _commandData.OfType<ICommandInvocation>(),
 97        PrintKind.Responses => _commandData.OfType<ICommandResult>(),
 98        _ => throw new ArgumentOutOfRangeException(nameof(kind), kind, null)
 99      };
 100      return printables;
 101    }
 17102  }
 103
 104  private void ApplyRewriters(StringBuilder stringBuilder)
 105  {
 97106    foreach (var commandRewriter in _rewriters.OrderBy(d => d.ExecutionOrder))
 107    {
 21108      commandRewriter.Rewrite(stringBuilder);
 109    }
 17110  }
 111
 27112  internal static void TryAddRewriter(ICommandRewriter item) => Current?.AddRewriter(item);
 113
 13114  internal static void TryAddInvocation(string item) => TryAddInvocation(new DotnetCommand(item));
 115
 13116  internal static void TryAddInvocation(ICommandInvocation item) => Current?.AddInvocation(item);
 117
 26118  internal static void TryAddResult(ICommandResult item) => Current?.AddResult(item);
 119
 1120  internal static string? ToFullString() => Current?.ToFullString(PrintKind.All);
 121}