< Summary - Kestrun — Combined Coverage

Information
Class: Kestrun.Scripting.CompilationErrorException
Assembly: Kestrun
File(s): /home/runner/work/Kestrun/Kestrun/src/CSharp/Kestrun/Scripting/CompilationErrorException.cs
Tag: Kestrun/Kestrun@9d3a582b2d63930269564a7591aa77ef297cadeb
Line coverage
96%
Covered lines: 25
Uncovered lines: 1
Coverable lines: 26
Total lines: 90
Line coverage: 96.1%
Branch coverage
100%
Covered branches: 10
Total branches: 10
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Coverage history

Coverage history 0 25 50 75 100

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
get_Diagnostics()100%11100%
.ctor(...)100%11100%
.ctor(...)100%210%
FormatMessage(...)100%88100%
FormatDiagnostic(...)100%22100%
GetDetailedErrorMessage()100%11100%
GetErrors()100%11100%
GetWarnings()100%11100%

File(s)

/home/runner/work/Kestrun/Kestrun/src/CSharp/Kestrun/Scripting/CompilationErrorException.cs

#LineLine coverage
 1using Microsoft.CodeAnalysis;
 2using System.Collections.Immutable;
 3using System.Text;
 4
 5namespace Kestrun.Scripting;
 6
 7/// <summary>
 8/// Exception thrown when C# script compilation fails.
 9/// Contains detailed diagnostic information about compilation errors.
 10/// </summary>
 11public class CompilationErrorException : Exception
 12{
 13    /// <summary>
 14    /// Gets the collection of diagnostics produced during compilation.
 15    /// </summary>
 216    public ImmutableArray<Diagnostic> Diagnostics { get; }
 17
 18    /// <summary>
 19    /// Initializes a new instance of the <see cref="CompilationErrorException"/> class with a specified error message a
 20    /// </summary>
 21    /// <param name="message">The error message that explains the reason for the exception.</param>
 22    /// <param name="diagnostics">The collection of diagnostics produced during compilation.</param>
 23    public CompilationErrorException(string message, ImmutableArray<Diagnostic> diagnostics)
 624        : base(FormatMessage(message, diagnostics)) => Diagnostics = diagnostics;
 25
 26    /// <summary>
 27    /// Initializes a new instance of the <see cref="CompilationErrorException"/> class with a specified error message, 
 28    /// </summary>
 29    /// <param name="message">The error message that explains the reason for the exception.</param>
 30    /// <param name="diagnostics">The collection of diagnostics produced during compilation.</param>
 31    /// <param name="innerException">The exception that is the cause of the current exception.</param>
 32    public CompilationErrorException(string message, ImmutableArray<Diagnostic> diagnostics, Exception innerException)
 033        : base(FormatMessage(message, diagnostics), innerException) => Diagnostics = diagnostics;
 34
 35    private static string FormatMessage(string baseMessage, ImmutableArray<Diagnostic> diagnostics)
 36    {
 337        var sb = new StringBuilder();
 338        _ = sb.AppendLine(baseMessage);
 339        _ = sb.AppendLine();
 40
 741        var errors = diagnostics.Where(d => d.Severity == DiagnosticSeverity.Error).ToArray();
 742        var warnings = diagnostics.Where(d => d.Severity == DiagnosticSeverity.Warning).ToArray();
 43
 344        if (errors.Any())
 45        {
 346            _ = sb.AppendLine($"Compilation Errors ({errors.Length}):");
 1247            for (var i = 0; i < errors.Length; i++)
 48            {
 349                var error = errors[i];
 350                _ = sb.AppendLine($"  {i + 1}. {FormatDiagnostic(error)}");
 51            }
 352            _ = sb.AppendLine();
 53        }
 54
 355        if (warnings.Any())
 56        {
 157            _ = sb.AppendLine($"Compilation Warnings ({warnings.Length}):");
 458            for (var i = 0; i < warnings.Length; i++)
 59            {
 160                var warning = warnings[i];
 161                _ = sb.AppendLine($"  {i + 1}. {FormatDiagnostic(warning)}");
 62            }
 63        }
 64
 365        return sb.ToString().TrimEnd();
 66    }
 67
 68    private static string FormatDiagnostic(Diagnostic diagnostic)
 69    {
 470        var location = diagnostic.Location;
 471        var position = location.IsInSource ? $" at line {location.GetLineSpan().StartLinePosition.Line + 1}, column {loc
 72
 473        return $"[{diagnostic.Id}] {diagnostic.GetMessage()}{position}";
 74    }
 75
 76    /// <summary>
 77    /// Gets a formatted string containing all error details.
 78    /// </summary>
 379    public string GetDetailedErrorMessage() => Message;
 80
 81    /// <summary>
 82    /// Gets only the error diagnostics (excluding warnings).
 83    /// </summary>
 384    public IEnumerable<Diagnostic> GetErrors() => Diagnostics.Where(d => d.Severity == DiagnosticSeverity.Error);
 85
 86    /// <summary>
 87    /// Gets only the warning diagnostics.
 88    /// </summary>
 389    public IEnumerable<Diagnostic> GetWarnings() => Diagnostics.Where(d => d.Severity == DiagnosticSeverity.Warning);
 90}