-
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for emiting warning / errors from cecilifier ( #292)
- Loading branch information
Showing
13 changed files
with
200 additions
and
66 deletions.
There are no files selected for viewing
68 changes: 68 additions & 0 deletions
68
Cecilifier.Core.Tests/Tests/Unit/CecilifierContextTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
using System.Linq; | ||
using Cecilifier.Core.Misc; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CSharp; | ||
using NUnit.Framework; | ||
|
||
namespace Cecilifier.Core.Tests.Tests.Unit; | ||
|
||
[TestFixture] | ||
public class CecilifierContextTests | ||
{ | ||
[OneTimeSetUp] | ||
public void SetUpFixture() | ||
{ | ||
var syntaxTree = CSharpSyntaxTree.ParseText("class C {}"); | ||
var comp = CSharpCompilation.Create("", new[] { syntaxTree }); | ||
_semanticModel = comp.GetSemanticModel(syntaxTree); | ||
} | ||
|
||
|
||
[Test] | ||
public void WarningDiagnosticsAreEmitted() | ||
{ | ||
var cecilifierContext = new CecilifierContext(_semanticModel, new CecilifierOptions(), 0); | ||
cecilifierContext.EmitWarning("Simple Warning"); | ||
|
||
var found = cecilifierContext.Diagnostics.Where(d => d.Message.Contains("Simple Warning")).ToList(); | ||
Assert.That(found, Is.Not.Null); | ||
Assert.That(found.Count, Is.EqualTo(1)); | ||
Assert.That(found[0].Kind, Is.EqualTo(DiagnosticKind.Warning)); | ||
} | ||
|
||
[Test] | ||
public void ErrorDiagnosticsAreEmitted() | ||
{ | ||
var cecilifierContext = new CecilifierContext(_semanticModel, new CecilifierOptions(), 0); | ||
cecilifierContext.EmitError("Simple Error"); | ||
|
||
var found = cecilifierContext.Diagnostics.Where(d => d.Message.Contains("Simple Error")).ToList(); | ||
Assert.That(found, Is.Not.Null); | ||
Assert.That(found.Count, Is.EqualTo(1)); | ||
Assert.That(found[0].Kind, Is.EqualTo(DiagnosticKind.Error)); | ||
} | ||
|
||
[Test] | ||
public void DiagnosticsEmitsEquivalentPreprocessorDirectives() | ||
{ | ||
var cecilifierContext = new CecilifierContext(_semanticModel, new CecilifierOptions(), 0); | ||
cecilifierContext.EmitError("Simple Error"); | ||
cecilifierContext.EmitWarning("Simple Warning"); | ||
|
||
Assert.That(cecilifierContext.Output, Contains.Substring("#error Simple Error")); | ||
Assert.That(cecilifierContext.Output, Contains.Substring("#warning Simple Warning")); | ||
} | ||
|
||
[Test] | ||
public void NewLinesInDiagnosticsEmitsMultiplePreprocessorDirectives() | ||
{ | ||
var cecilifierContext = new CecilifierContext(_semanticModel, new CecilifierOptions(), 0); | ||
cecilifierContext.EmitWarning("Warning with\nmultiple\nlines"); | ||
|
||
Assert.That(cecilifierContext.Output, Contains.Substring("#warning Warning with")); | ||
Assert.That(cecilifierContext.Output, Contains.Substring("#warning multiple")); | ||
Assert.That(cecilifierContext.Output, Contains.Substring("#warning lines")); | ||
} | ||
|
||
private SemanticModel _semanticModel; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
using System; | ||
using Microsoft.CodeAnalysis; | ||
|
||
namespace Cecilifier.Core.Misc; | ||
|
||
public enum DiagnosticKind | ||
{ | ||
Information, | ||
Warning, | ||
Error | ||
} | ||
|
||
public record struct SourceLineInformation(int StartLineNumber, int StartColumn, int EndLineNumber, int EndColumn); | ||
|
||
public record struct CecilifierDiagnostic(DiagnosticKind Kind, string Message, SourceLineInformation LineInformation) | ||
{ | ||
public static CecilifierDiagnostic FromCompiler(Diagnostic diagnostic) | ||
{ | ||
var lineSpan = diagnostic.Location.GetLineSpan(); | ||
return new CecilifierDiagnostic( | ||
diagnostic.Severity switch | ||
{ | ||
DiagnosticSeverity.Error => DiagnosticKind.Error, | ||
DiagnosticSeverity.Hidden => DiagnosticKind.Information, | ||
DiagnosticSeverity.Info => DiagnosticKind.Information, | ||
DiagnosticSeverity.Warning => DiagnosticKind.Warning, | ||
_ => throw new ArgumentOutOfRangeException() | ||
}, | ||
diagnostic.GetMessage(), | ||
new SourceLineInformation(lineSpan.Span.Start.Line + 1, lineSpan.Span.Start.Character + 1, lineSpan.Span.End.Line + 1,lineSpan.Span.End.Character + 1)); | ||
} | ||
|
||
public static CecilifierDiagnostic FromAstNode(SyntaxNode node, DiagnosticKind diagnosticKind, string message) | ||
{ | ||
var lineSpan = node != null ? node.GetLocation().GetLineSpan() : new FileLinePositionSpan(); | ||
return new CecilifierDiagnostic( | ||
diagnosticKind, | ||
message, | ||
new SourceLineInformation(lineSpan.Span.Start.Line + 1, lineSpan.Span.Start.Character + 1, lineSpan.Span.End.Line + 1,lineSpan.Span.End.Character + 1)); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
using System.Collections.Generic; | ||
using System.Text.Json.Serialization; | ||
using Cecilifier.Core.Mappings; | ||
using Cecilifier.Core.Misc; | ||
|
||
namespace Cecilifier.Web; | ||
|
||
public class CecilifiedWebResult | ||
{ | ||
[JsonPropertyName("status")] public int Status { get; set; } | ||
[JsonPropertyName("data")] public string Data { get; set; } | ||
[JsonPropertyName("counter")] public int Counter { get; set; } | ||
[JsonPropertyName("clientsCounter")] public uint Clients { get; set; } | ||
[JsonPropertyName("maximumUnique")] public uint MaximumUnique { get; set; } | ||
[JsonPropertyName("kind")] public char Kind { get; set; } | ||
[JsonPropertyName("mappings")] public IList<Mapping> Mappings { get; set; } | ||
[JsonPropertyName("mainTypeName")] public string MainTypeName { get; set; } | ||
[JsonPropertyName("diagnostics")] public IList<CecilifierDiagnostic> Diagnostics { get; set; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.