From 744f04612660590a7db50370fbfb1f16e59f07e9 Mon Sep 17 00:00:00 2001 From: Sergiusz Zalewski <38229504+KeterSCP@users.noreply.github.com> Date: Sun, 20 Aug 2023 22:41:06 +0200 Subject: [PATCH] Allow customizing document output type --- .../CompletionWorkspace.cs | 4 ++-- .../Tests/UnitTests.cs | 17 +++++++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/MonacoRoslynCompletionProvider/MonacoRoslynCompletionProvider/CompletionWorkspace.cs b/MonacoRoslynCompletionProvider/MonacoRoslynCompletionProvider/CompletionWorkspace.cs index 072a346..b24ba84 100644 --- a/MonacoRoslynCompletionProvider/MonacoRoslynCompletionProvider/CompletionWorkspace.cs +++ b/MonacoRoslynCompletionProvider/MonacoRoslynCompletionProvider/CompletionWorkspace.cs @@ -67,7 +67,7 @@ public static CompletionWorkspace Create(params string[] assemblies) return new CompletionWorkspace() { _workspace = workspace, _project = project, _metadataReferences = references }; } - public async Task CreateDocument(string code) + public async Task CreateDocument(string code, OutputKind outputKind = OutputKind.DynamicallyLinkedLibrary) { var document = _workspace.AddDocument(_project.Id, "MyFile2.cs", SourceText.From(code)); var st = await document.GetSyntaxTreeAsync(); @@ -75,7 +75,7 @@ public async Task CreateDocument(string code) CSharpCompilation .Create("Temp", new[] { st }, - options: new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary), + options: new CSharpCompilationOptions(outputKind), references: _metadataReferences ); diff --git a/MonacoRoslynCompletionProvider/Tests/UnitTests.cs b/MonacoRoslynCompletionProvider/Tests/UnitTests.cs index 5071591..b924027 100644 --- a/MonacoRoslynCompletionProvider/Tests/UnitTests.cs +++ b/MonacoRoslynCompletionProvider/Tests/UnitTests.cs @@ -1,7 +1,10 @@ +using System.Linq; using Microsoft.VisualStudio.TestTools.UnitTesting; using MonacoRoslynCompletionProvider; using System.Threading; using System.Threading.Tasks; +using Microsoft.CodeAnalysis; +using MonacoRoslynCompletionProvider.Api; namespace Tests { @@ -34,5 +37,19 @@ static void Main(string[] args) var info = await document.GetHoverInformation(258, CancellationToken.None); var info2 = await document.GetHoverInformation(267, CancellationToken.None); } + + [TestMethod] + public async Task DocumentShouldNotContainErrorsWhenUsingTopLevelStatements() + { + const string code = @"using System; +Console.WriteLine(""Hello, world!""); +"; + + var ws = CompletionWorkspace.Create(); + var document = await ws.CreateDocument(code, OutputKind.ConsoleApplication); + var codeCheckResults = await document.GetCodeCheckResults(CancellationToken.None); + + Assert.IsTrue(codeCheckResults.All(r => r.Severity != CodeCheckSeverity.Error)); + } } }