Skip to content

Commit

Permalink
Support TestWorkspace with source generated files without full XML sy…
Browse files Browse the repository at this point in the history
…ntax
  • Loading branch information
sharwell committed Dec 22, 2021
1 parent 79c8b92 commit 7812951
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ public async Task CrefCompletionSpeculatesOutsideTrivia()
class C
{
}";
using var workspace = TestWorkspace.Create(LanguageNames.CSharp, new CSharpCompilationOptions(OutputKind.ConsoleApplication), new CSharpParseOptions(), new[] { text }, ExportProvider);
using var workspace = TestWorkspace.Create(LanguageNames.CSharp, new CSharpCompilationOptions(OutputKind.ConsoleApplication), new CSharpParseOptions(), new[] { text }, exportProvider: ExportProvider);
var called = false;

var hostDocument = workspace.DocumentWithCursor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2777,7 +2777,7 @@ static void Main(string[] args)
override $$
}
}";
using var workspace = TestWorkspace.Create(LanguageNames.CSharp, new CSharpCompilationOptions(OutputKind.ConsoleApplication), new CSharpParseOptions(), new[] { text }, ExportProvider);
using var workspace = TestWorkspace.Create(LanguageNames.CSharp, new CSharpCompilationOptions(OutputKind.ConsoleApplication), new CSharpParseOptions(), new[] { text }, exportProvider: ExportProvider);
var provider = new OverrideCompletionProvider();
var testDocument = workspace.Documents.Single();
var document = workspace.CurrentSolution.GetRequiredDocument(testDocument.Id);
Expand Down
31 changes: 15 additions & 16 deletions src/EditorFeatures/CSharpTest/NavigateTo/NavigateToTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
Expand Down Expand Up @@ -1477,22 +1478,20 @@ public class C
[Fact]
public async Task DoIncludeSymbolsFromMultipleSourceGeneratedFiles()
{
using var workspace = TestWorkspace.Create(@"
<Workspace>
<Project Language=""C#"" CommonReferences=""true"">
<DocumentFromSourceGenerator>
public partial class C
{
}
</DocumentFromSourceGenerator>
<DocumentFromSourceGenerator>
public partial class C
{
}
</DocumentFromSourceGenerator>
</Project>
</Workspace>
", composition: EditorTestCompositions.EditorFeatures);
using var workspace = TestWorkspace.CreateCSharp(
files: Array.Empty<string>(),
sourceGeneratedFiles: new[]
{
@"
public partial class C
{
}",
@"
public partial class C
{
}",
},
composition: EditorTestCompositions.EditorFeatures);

_provider = new NavigateToItemProvider(workspace, AsynchronousOperationListenerProvider.NullListener, workspace.GetService<IThreadingContext>());
_aggregator = new NavigateToTestAggregator(_provider);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public async Task Test_FadingSpans()
{ LanguageNames.CSharp, ImmutableArray.Create<DiagnosticAnalyzer>(analyzer) }
};

using var workspace = TestWorkspace.CreateCSharp(new string[] { "class A { }", "class E { }" }, CSharpParseOptions.Default, composition: SquiggleUtilities.CompositionWithSolutionCrawler);
using var workspace = TestWorkspace.CreateCSharp(new string[] { "class A { }", "class E { }" }, parseOptions: CSharpParseOptions.Default, composition: SquiggleUtilities.CompositionWithSolutionCrawler);
using var wrapper = new DiagnosticTaggerWrapper<DiagnosticsClassificationTaggerProvider, ClassificationTag>(workspace, analyzerMap);
var tagger = wrapper.TaggerProvider.CreateTagger<ClassificationTag>(workspace.Documents.First().GetTextBuffer());
using var disposable = tagger as IDisposable;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public async Task Test_TagSourceDiffer()
{ LanguageNames.CSharp, ImmutableArray.Create<DiagnosticAnalyzer>(analyzer) }
};

using var workspace = TestWorkspace.CreateCSharp(new string[] { "class A { }", "class E { }" }, CSharpParseOptions.Default);
using var workspace = TestWorkspace.CreateCSharp(new string[] { "class A { }", "class E { }" }, parseOptions: CSharpParseOptions.Default);
using var wrapper = new DiagnosticTaggerWrapper<DiagnosticsSquiggleTaggerProvider, IErrorTag>(workspace, analyzerMap);
var tagger = wrapper.TaggerProvider.CreateTagger<IErrorTag>(workspace.Documents.First().GetTextBuffer());
using var disposable = tagger as IDisposable;
Expand Down Expand Up @@ -69,7 +69,7 @@ public async Task Test_TagSourceDiffer()
[WpfFact, Trait(Traits.Feature, Traits.Features.Diagnostics)]
public async Task MultipleTaggersAndDispose()
{
using var workspace = TestWorkspace.CreateCSharp(new string[] { "class A {" }, CSharpParseOptions.Default);
using var workspace = TestWorkspace.CreateCSharp(new string[] { "class A {" }, parseOptions: CSharpParseOptions.Default);
using var wrapper = new DiagnosticTaggerWrapper<DiagnosticsSquiggleTaggerProvider, IErrorTag>(workspace);
// Make two taggers.
var tagger1 = wrapper.TaggerProvider.CreateTagger<IErrorTag>(workspace.Documents.First().GetTextBuffer());
Expand All @@ -89,7 +89,7 @@ public async Task MultipleTaggersAndDispose()
[WpfFact, Trait(Traits.Feature, Traits.Features.Diagnostics)]
public async Task TaggerProviderCreatedAfterInitialDiagnosticsReported()
{
using var workspace = TestWorkspace.CreateCSharp(new string[] { "class C {" }, CSharpParseOptions.Default);
using var workspace = TestWorkspace.CreateCSharp(new string[] { "class C {" }, parseOptions: CSharpParseOptions.Default);
using var wrapper = new DiagnosticTaggerWrapper<DiagnosticsSquiggleTaggerProvider, IErrorTag>(workspace, analyzerMap: null, createTaggerProvider: false);
// First, make sure all diagnostics have been reported.
await wrapper.WaitForTags();
Expand Down Expand Up @@ -119,7 +119,7 @@ public async Task TestWithMockDiagnosticService_TaggerProviderCreatedBeforeIniti

using var workspace = TestWorkspace.CreateCSharp(
new string[] { "class A { }" },
CSharpParseOptions.Default,
parseOptions: CSharpParseOptions.Default,
composition: s_compositionWithMockDiagnosticService);

var listenerProvider = workspace.ExportProvider.GetExportedValue<IAsynchronousOperationListenerProvider>();
Expand Down Expand Up @@ -156,7 +156,7 @@ public async Task TestWithMockDiagnosticService_TaggerProviderCreatedAfterInitia

using var workspace = TestWorkspace.CreateCSharp(
new string[] { "class A { }" },
CSharpParseOptions.Default,
parseOptions: CSharpParseOptions.Default,
composition: s_compositionWithMockDiagnosticService);

var listenerProvider = workspace.ExportProvider.GetExportedValue<IAsynchronousOperationListenerProvider>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#nullable disable

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
Expand Down Expand Up @@ -128,6 +129,7 @@ internal static TestWorkspace Create(
CompilationOptions compilationOptions,
ParseOptions parseOptions,
string[] files,
string[] sourceGeneratedFiles = null,
ExportProvider exportProvider = null,
TestComposition composition = null,
string[] metadataReferences = null,
Expand All @@ -137,7 +139,7 @@ internal static TestWorkspace Create(
bool openDocuments = false,
IDocumentServiceProvider documentServiceProvider = null)
{
var workspaceElement = CreateWorkspaceElement(language, compilationOptions, parseOptions, files, metadataReferences, extension, commonReferences);
var workspaceElement = CreateWorkspaceElement(language, compilationOptions, parseOptions, files, sourceGeneratedFiles, metadataReferences, extension, commonReferences);
return Create(workspaceElement, openDocuments, exportProvider, composition, workspaceKind, documentServiceProvider);
}

Expand Down Expand Up @@ -193,19 +195,20 @@ public static TestWorkspace CreateCSharp(
string[] metadataReferences = null,
bool openDocuments = false)
{
return CreateCSharp(new[] { file }, parseOptions, compilationOptions, exportProvider, composition, metadataReferences, openDocuments);
return CreateCSharp(new[] { file }, Array.Empty<string>(), parseOptions, compilationOptions, exportProvider, composition, metadataReferences, openDocuments);
}

public static TestWorkspace CreateCSharp(
string[] files,
string[] sourceGeneratedFiles = null,
ParseOptions parseOptions = null,
CompilationOptions compilationOptions = null,
ExportProvider exportProvider = null,
TestComposition composition = null,
string[] metadataReferences = null,
bool openDocuments = false)
{
return Create(LanguageNames.CSharp, compilationOptions, parseOptions, files, exportProvider, composition, metadataReferences, openDocuments: openDocuments);
return Create(LanguageNames.CSharp, compilationOptions, parseOptions, files, sourceGeneratedFiles, exportProvider, composition, metadataReferences, openDocuments: openDocuments);
}

public static TestWorkspace CreateCSharp2(
Expand All @@ -230,19 +233,20 @@ public static TestWorkspace CreateVisualBasic(
string[] metadataReferences = null,
bool openDocuments = false)
{
return CreateVisualBasic(new[] { file }, parseOptions, compilationOptions, exportProvider, composition, metadataReferences, openDocuments);
return CreateVisualBasic(new[] { file }, Array.Empty<string>(), parseOptions, compilationOptions, exportProvider, composition, metadataReferences, openDocuments);
}

public static TestWorkspace CreateVisualBasic(
string[] files,
string[] sourceGeneratedFiles = null,
ParseOptions parseOptions = null,
CompilationOptions compilationOptions = null,
ExportProvider exportProvider = null,
TestComposition composition = null,
string[] metadataReferences = null,
bool openDocuments = false)
{
return Create(LanguageNames.VisualBasic, compilationOptions, parseOptions, files, exportProvider, composition, metadataReferences, openDocuments: openDocuments);
return Create(LanguageNames.VisualBasic, compilationOptions, parseOptions, files, sourceGeneratedFiles, exportProvider, composition, metadataReferences, openDocuments: openDocuments);
}

/// <param name="files">Can pass in multiple file contents with individual source kind: files will be named test1.vb, test2.vbx, etc.</param>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ internal void InitializeDocuments(
compilationOptions,
parseOptions,
files,
sourceGeneratedFiles: Array.Empty<string>(),
metadataReferences,
extension,
commonReferences);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@

#nullable disable

using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.Text;
using Microsoft.CodeAnalysis.VisualBasic;
using Roslyn.Utilities;

Expand All @@ -22,23 +20,31 @@ internal static XElement CreateWorkspaceElement(
CompilationOptions compilationOptions = null,
ParseOptions parseOptions = null,
string[] files = null,
string[] sourceGeneratedFiles = null,
string[] metadataReferences = null,
string extension = null,
bool commonReferences = true)
{
var documentElements = new List<XElement>();

var index = 0;
extension ??= (language == LanguageNames.CSharp) ? CSharpExtension : VisualBasicExtension;
if (files != null)
{
var index = 0;
extension ??= (language == LanguageNames.CSharp) ? CSharpExtension : VisualBasicExtension;

foreach (var file in files)
{
documentElements.Add(CreateDocumentElement(file, GetDefaultTestSourceDocumentName(index++, extension), parseOptions));
}
}

if (sourceGeneratedFiles != null)
{
foreach (var file in sourceGeneratedFiles)
{
documentElements.Add(CreateDocumentFromSourceGeneratorElement(file, GetDefaultTestSourceDocumentName(index++, extension), parseOptions));
}
}

if (metadataReferences != null)
{
foreach (var reference in metadataReferences)
Expand Down Expand Up @@ -171,6 +177,14 @@ protected static XElement CreateDocumentElement(string code, string filePath, Pa
code.Replace("\r\n", "\n"));
}

protected static XElement CreateDocumentFromSourceGeneratorElement(string code, string hintName, ParseOptions parseOptions = null)
{
return new XElement(DocumentFromSourceGeneratorElementName,
new XAttribute(FilePathAttributeName, hintName),
CreateParseOptionsElement(parseOptions),
code.Replace("\r\n", "\n"));
}

private static XElement CreateParseOptionsElement(ParseOptions parseOptions)
{
return parseOptions == null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ Class C
End Sub
End Class]]></a>.Value.NormalizeLineEndings()

Using workspace = TestWorkspace.Create(LanguageNames.VisualBasic, New VisualBasicCompilationOptions(OutputKind.ConsoleApplication), New VisualBasicParseOptions(), {text}, ExportProvider)
Using workspace = TestWorkspace.Create(LanguageNames.VisualBasic, New VisualBasicCompilationOptions(OutputKind.ConsoleApplication), New VisualBasicParseOptions(), {text}, exportProvider:=ExportProvider)
Dim called = False

Dim hostDocument = workspace.DocumentWithCursor
Expand Down

0 comments on commit 7812951

Please sign in to comment.