Skip to content

Commit

Permalink
Merge pull request #1128 from OmniSharp/features/shared-test-host
Browse files Browse the repository at this point in the history
Share a test host instance between tests that are part of the same collection in OmniSharp.Roslyn.CSharp tests
  • Loading branch information
DustinCampbell authored Apr 5, 2018
2 parents 1f2eec6 + f6771aa commit fdfa303
Show file tree
Hide file tree
Showing 25 changed files with 364 additions and 363 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ namespace OmniSharp.Roslyn.CSharp.Tests
{
public class AbstractAutoCompleteTestFixture : AbstractSingleRequestHandlerTestFixture<IntellisenseService>
{
protected AbstractAutoCompleteTestFixture(ITestOutputHelper output)
: base(output)
protected AbstractAutoCompleteTestFixture(ITestOutputHelper output, SharedOmniSharpHostFixture sharedOmniSharpHostFixture)
: base(output, sharedOmniSharpHostFixture)
{
}

Expand All @@ -19,27 +19,25 @@ protected AbstractAutoCompleteTestFixture(ITestOutputHelper output)
protected async Task<IEnumerable<AutoCompleteResponse>> FindCompletionsAsync(string filename, string source, bool wantSnippet = false, string triggerChar = null)
{
var testFile = new TestFile(filename, source);
using (var host = CreateOmniSharpHost(testFile))
{
var point = testFile.Content.GetPointFromPosition();
SharedOmniSharpTestHost.AddFilesToWorkspace(testFile);
var point = testFile.Content.GetPointFromPosition();

var request = new AutoCompleteRequest
{
Line = point.Line,
Column = point.Offset,
FileName = testFile.FileName,
Buffer = testFile.Content.Code,
WordToComplete = GetPartialWord(testFile.Content),
WantMethodHeader = true,
WantSnippet = wantSnippet,
WantReturnType = true,
TriggerCharacter = triggerChar
};
var request = new AutoCompleteRequest
{
Line = point.Line,
Column = point.Offset,
FileName = testFile.FileName,
Buffer = testFile.Content.Code,
WordToComplete = GetPartialWord(testFile.Content),
WantMethodHeader = true,
WantSnippet = wantSnippet,
WantReturnType = true,
TriggerCharacter = triggerChar
};

var requestHandler = GetRequestHandler(host);
var requestHandler = GetRequestHandler(SharedOmniSharpTestHost);

return await requestHandler.Handle(request);
}
return await requestHandler.Handle(request);
}

private static string GetPartialWord(TestContent testConnect)
Expand Down
30 changes: 13 additions & 17 deletions tests/OmniSharp.Roslyn.CSharp.Tests/DiagnosticsFacts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ namespace OmniSharp.Roslyn.CSharp.Tests
{
public class DiagnosticsFacts : AbstractSingleRequestHandlerTestFixture<CodeCheckService>
{
public DiagnosticsFacts(ITestOutputHelper output)
: base(output)
public DiagnosticsFacts(ITestOutputHelper output, SharedOmniSharpHostFixture sharedOmniSharpHostFixture)
: base(output, sharedOmniSharpHostFixture)
{
}

Expand All @@ -21,29 +21,25 @@ public DiagnosticsFacts(ITestOutputHelper output)
[Fact]
public async Task CodeCheckSpecifiedFileOnly()
{
using (var host = CreateOmniSharpHost(
new TestFile("a.cs", "class C { int n = true; }")))
{
var requestHandler = GetRequestHandler(host);
var quickFixes = await requestHandler.Handle(new CodeCheckRequest() { FileName = "a.cs" });
SharedOmniSharpTestHost.AddFilesToWorkspace(new TestFile("a.cs", "class C { int n = true; }"));
var requestHandler = GetRequestHandler(SharedOmniSharpTestHost);
var quickFixes = await requestHandler.Handle(new CodeCheckRequest() { FileName = "a.cs" });

Assert.Single(quickFixes.QuickFixes);
Assert.Equal("a.cs", quickFixes.QuickFixes.First().FileName);
}
Assert.Single(quickFixes.QuickFixes);
Assert.Equal("a.cs", quickFixes.QuickFixes.First().FileName);
}

[Fact]
public async Task CheckAllFiles()
{
using (var host = CreateOmniSharpHost(
SharedOmniSharpTestHost.AddFilesToWorkspace(
new TestFile("a.cs", "class C1 { int n = true; }"),
new TestFile("b.cs", "class C2 { int n = true; }")))
{
var handler = GetRequestHandler(host);
var quickFixes = await handler.Handle(new CodeCheckRequest());
new TestFile("b.cs", "class C2 { int n = true; }"));

Assert.Equal(2, quickFixes.QuickFixes.Count());
}
var handler = GetRequestHandler(SharedOmniSharpTestHost);
var quickFixes = await handler.Handle(new CodeCheckRequest());

Assert.Equal(2, quickFixes.QuickFixes.Count());
}
}
}
97 changes: 45 additions & 52 deletions tests/OmniSharp.Roslyn.CSharp.Tests/DiagnosticsV2Facts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ namespace OmniSharp.Roslyn.CSharp.Tests
{
public partial class DiagnosticsV2Facts : AbstractTestFixture
{
public DiagnosticsV2Facts(ITestOutputHelper output)
: base(output)
public DiagnosticsV2Facts(ITestOutputHelper output, SharedOmniSharpHostFixture sharedOmniSharpHostFixture)
: base(output, sharedOmniSharpHostFixture)
{
}

Expand All @@ -24,27 +24,25 @@ public async Task CodeCheckSpecifiedFileOnly(string filename)
{
var testFile = new TestFile(filename, "class C { int n = true; }");

using (var host = CreateOmniSharpHost(testFile))
SharedOmniSharpTestHost.AddFilesToWorkspace(testFile);
var messages = new List<DiagnosticMessage>();
var emitter = new DiagnosticTestEmitter(messages);
var forwarder = new DiagnosticEventForwarder(emitter)
{
var messages = new List<DiagnosticMessage>();
var emitter = new DiagnosticTestEmitter(messages);
var forwarder = new DiagnosticEventForwarder(emitter)
{
IsEnabled = true
};

var service = new CSharpDiagnosticService(host.Workspace, forwarder, this.LoggerFactory);
service.QueueDiagnostics(filename);

await emitter.Emitted;

Assert.Single(messages);
var message = messages.First();
Assert.Single(message.Results);
var result = message.Results.First();
Assert.Single(result.QuickFixes);
Assert.Equal(filename, result.FileName);
}
IsEnabled = true
};

var service = new CSharpDiagnosticService(SharedOmniSharpTestHost.Workspace, forwarder, this.LoggerFactory);
service.QueueDiagnostics(filename);

await emitter.Emitted;

Assert.Single(messages);
var message = messages.First();
Assert.Single(message.Results);
var result = message.Results.First();
Assert.Single(result.QuickFixes);
Assert.Equal(filename, result.FileName);
}

[Theory]
Expand All @@ -55,30 +53,28 @@ public async Task CheckAllFiles(string filename1, string filename2)
var testFile1 = new TestFile(filename1, "class C1 { int n = true; }");
var testFile2 = new TestFile(filename2, "class C2 { int n = true; }");

using (var host = CreateOmniSharpHost(testFile1, testFile2))
{
var messages = new List<DiagnosticMessage>();
var emitter = new DiagnosticTestEmitter(messages);
var forwarder = new DiagnosticEventForwarder(emitter);
var service = new CSharpDiagnosticService(host.Workspace, forwarder, this.LoggerFactory);
SharedOmniSharpTestHost.AddFilesToWorkspace(testFile1, testFile2);
var messages = new List<DiagnosticMessage>();
var emitter = new DiagnosticTestEmitter(messages);
var forwarder = new DiagnosticEventForwarder(emitter);
var service = new CSharpDiagnosticService(SharedOmniSharpTestHost.Workspace, forwarder, this.LoggerFactory);

var controller = new DiagnosticsService(host.Workspace, forwarder, service);
var response = await controller.Handle(new DiagnosticsRequest());
var controller = new DiagnosticsService(SharedOmniSharpTestHost.Workspace, forwarder, service);
var response = await controller.Handle(new DiagnosticsRequest());

await emitter.Emitted;
await emitter.Emitted;

Assert.Single(messages);
var message = messages.First();
Assert.Equal(2, message.Results.Count());
Assert.Single(messages);
var message = messages.First();
Assert.Equal(2, message.Results.Count());

var a = message.Results.First(x => x.FileName == filename1);
Assert.Single(a.QuickFixes);
Assert.Equal(filename1, a.FileName);
var a = message.Results.First(x => x.FileName == filename1);
Assert.Single(a.QuickFixes);
Assert.Equal(filename1, a.FileName);

var b = message.Results.First(x => x.FileName == filename2);
Assert.Single(b.QuickFixes);
Assert.Equal(filename2, b.FileName);
}
var b = message.Results.First(x => x.FileName == filename2);
Assert.Single(b.QuickFixes);
Assert.Equal(filename2, b.FileName);
}

[Theory]
Expand All @@ -88,19 +84,16 @@ public async Task EnablesWhenEndPointIsHit(string filename1, string filename2)
{
var testFile1 = new TestFile(filename1, "class C1 { int n = true; }");
var testFile2 = new TestFile(filename2, "class C2 { int n = true; }");
SharedOmniSharpTestHost.AddFilesToWorkspace(testFile1, testFile2);
var messages = new List<DiagnosticMessage>();
var emitter = new DiagnosticTestEmitter(messages);
var forwarder = new DiagnosticEventForwarder(emitter);
var service = new CSharpDiagnosticService(SharedOmniSharpTestHost.Workspace, forwarder, this.LoggerFactory);

using (var host = CreateOmniSharpHost(testFile1, testFile2))
{
var messages = new List<DiagnosticMessage>();
var emitter = new DiagnosticTestEmitter(messages);
var forwarder = new DiagnosticEventForwarder(emitter);
var service = new CSharpDiagnosticService(host.Workspace, forwarder, this.LoggerFactory);

var controller = new DiagnosticsService(host.Workspace, forwarder, service);
var response = await controller.Handle(new DiagnosticsRequest());
var controller = new DiagnosticsService(SharedOmniSharpTestHost.Workspace, forwarder, service);
var response = await controller.Handle(new DiagnosticsRequest());

Assert.True(forwarder.IsEnabled);
}
Assert.True(forwarder.IsEnabled);
}
}
}
30 changes: 14 additions & 16 deletions tests/OmniSharp.Roslyn.CSharp.Tests/FindImplementationFacts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ namespace OmniSharp.Roslyn.CSharp.Tests
{
public class FindImplementationFacts : AbstractSingleRequestHandlerTestFixture<FindImplementationsService>
{
public FindImplementationFacts(ITestOutputHelper output)
: base(output)
public FindImplementationFacts(ITestOutputHelper output, SharedOmniSharpHostFixture sharedOmniSharpHostFixture)
: base(output, sharedOmniSharpHostFixture)
{
}

Expand Down Expand Up @@ -134,23 +134,21 @@ public void Foo() {}
private async Task<IEnumerable<ISymbol>> FindImplementationsAsync(string code, string filename)
{
var testFile = new TestFile(filename, code);
using (var host = CreateOmniSharpHost(testFile))
{
var point = testFile.Content.GetPointFromPosition();
var requestHandler = GetRequestHandler(host);
SharedOmniSharpTestHost.AddFilesToWorkspace(testFile);
var point = testFile.Content.GetPointFromPosition();
var requestHandler = GetRequestHandler(SharedOmniSharpTestHost);

var request = new FindImplementationsRequest
{
Line = point.Line,
Column = point.Offset,
FileName = testFile.FileName,
Buffer = testFile.Content.Code
};
var request = new FindImplementationsRequest
{
Line = point.Line,
Column = point.Offset,
FileName = testFile.FileName,
Buffer = testFile.Content.Code
};

var implementations = await requestHandler.Handle(request);
var implementations = await requestHandler.Handle(request);

return await SymbolsFromQuickFixesAsync(host.Workspace, implementations.QuickFixes);
}
return await SymbolsFromQuickFixesAsync(SharedOmniSharpTestHost.Workspace, implementations.QuickFixes);
}

private async Task<IEnumerable<ISymbol>> SymbolsFromQuickFixesAsync(OmniSharpWorkspace workspace, IEnumerable<QuickFix> quickFixes)
Expand Down
36 changes: 17 additions & 19 deletions tests/OmniSharp.Roslyn.CSharp.Tests/FindReferencesFacts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ namespace OmniSharp.Roslyn.CSharp.Tests
{
public class FindReferencesFacts : AbstractSingleRequestHandlerTestFixture<FindUsagesService>
{
public FindReferencesFacts(ITestOutputHelper output)
: base(output)
public FindReferencesFacts(ITestOutputHelper output, SharedOmniSharpHostFixture sharedOmniSharpHostFixture)
: base(output, sharedOmniSharpHostFixture)
{
}

Expand Down Expand Up @@ -235,25 +235,23 @@ private Task<QuickFixResponse> FindUsagesAsync(string code, bool excludeDefiniti

private async Task<QuickFixResponse> FindUsagesAsync(TestFile[] testFiles, bool onlyThisFile, bool excludeDefinition = false)
{
using (var host = CreateOmniSharpHost(testFiles))
{
var file = testFiles.Single(tf => tf.Content.HasPosition);
var point = file.Content.GetPointFromPosition();
SharedOmniSharpTestHost.AddFilesToWorkspace(testFiles);
var file = testFiles.Single(tf => tf.Content.HasPosition);
var point = file.Content.GetPointFromPosition();

var requestHandler = GetRequestHandler(host);
var requestHandler = GetRequestHandler(SharedOmniSharpTestHost);

var request = new FindUsagesRequest
{
Line = point.Line,
Column = point.Offset,
FileName = file.FileName,
Buffer = file.Content.Code,
OnlyThisFile = onlyThisFile,
ExcludeDefinition = excludeDefinition
};

return await requestHandler.Handle(request);
}
var request = new FindUsagesRequest
{
Line = point.Line,
Column = point.Offset,
FileName = file.FileName,
Buffer = file.Content.Code,
OnlyThisFile = onlyThisFile,
ExcludeDefinition = excludeDefinition
};

return await requestHandler.Handle(request);
}
}
}
20 changes: 8 additions & 12 deletions tests/OmniSharp.Roslyn.CSharp.Tests/FindSymbolsFacts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ namespace OmniSharp.Roslyn.CSharp.Tests
{
public class FindSymbolsFacts : AbstractSingleRequestHandlerTestFixture<FindSymbolsService>
{
public FindSymbolsFacts(ITestOutputHelper output)
: base(output)
public FindSymbolsFacts(ITestOutputHelper output, SharedOmniSharpHostFixture sharedOmniSharpHostFixture)
: base(output, sharedOmniSharpHostFixture)
{
}

Expand Down Expand Up @@ -257,23 +257,19 @@ public class ConfigurationOptions : IConfigurationOptions { }
private async Task<QuickFixResponse> FindSymbolsAsync(string code)
{
var testFile = new TestFile("dummy.cs", code);
using (var host = CreateOmniSharpHost(testFile))
{
var requestHandler = GetRequestHandler(host);
SharedOmniSharpTestHost.AddFilesToWorkspace(testFile);
var requestHandler = GetRequestHandler(SharedOmniSharpTestHost);

return await requestHandler.Handle(null);
}
return await requestHandler.Handle(null);
}

private async Task<QuickFixResponse> FindSymbolsWithFilterAsync(string code, string filter)
{
var testFile = new TestFile("dummy.cs", code);
using (var host = CreateOmniSharpHost(testFile))
{
var requestHandler = GetRequestHandler(host);
SharedOmniSharpTestHost.AddFilesToWorkspace(testFile);
var requestHandler = GetRequestHandler(SharedOmniSharpTestHost);

return await requestHandler.Handle(new FindSymbolsRequest { Filter = filter });
}
return await requestHandler.Handle(new FindSymbolsRequest { Filter = filter });
}
}
}
Loading

0 comments on commit fdfa303

Please sign in to comment.