Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update to simple using statements #73632

Merged
merged 2 commits into from
May 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1506,17 +1506,15 @@ private async Task VerifyWorkerAsync(string markup, bool isBuilder)
{
MarkupTestFile.GetPosition(markup, out var code, out int position);

using (var workspaceFixture = new CSharpTestWorkspaceFixture())
{
workspaceFixture.GetWorkspace(GetComposition());
var document1 = workspaceFixture.UpdateDocument(code, SourceCodeKind.Regular);
await CheckResultsAsync(document1, position, isBuilder);
using var workspaceFixture = new CSharpTestWorkspaceFixture();
workspaceFixture.GetWorkspace(GetComposition());
var document1 = workspaceFixture.UpdateDocument(code, SourceCodeKind.Regular);
await CheckResultsAsync(document1, position, isBuilder);

if (await CanUseSpeculativeSemanticModelAsync(document1, position))
{
var document2 = workspaceFixture.UpdateDocument(code, SourceCodeKind.Regular, cleanBeforeUpdate: false);
await CheckResultsAsync(document2, position, isBuilder);
}
if (await CanUseSpeculativeSemanticModelAsync(document1, position))
{
var document2 = workspaceFixture.UpdateDocument(code, SourceCodeKind.Regular, cleanBeforeUpdate: false);
await CheckResultsAsync(document2, position, isBuilder);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,12 @@ protected static async Task ExpectExtractMethodToFailAsync(

protected static async Task NotSupported_ExtractMethodAsync(string codeWithMarker)
{
using (var workspace = EditorTestWorkspace.CreateCSharp(codeWithMarker))
using var workspace = EditorTestWorkspace.CreateCSharp(codeWithMarker);
Assert.NotNull(await Record.ExceptionAsync(async () =>
{
Assert.NotNull(await Record.ExceptionAsync(async () =>
{
var testDocument = workspace.Documents.Single();
var tree = await ExtractMethodAsync(workspace, testDocument);
}));
}
var testDocument = workspace.Documents.Single();
var tree = await ExtractMethodAsync(workspace, testDocument);
}));
}

protected static async Task TestExtractMethodAsync(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -312,12 +312,10 @@ protected static void CompileTestSource(string dllFilePath, string[] sourceCodeP
emitOptions = emitOptions.WithFallbackSourceFileEncoding(fallbackEncoding);
}

using (var dllStream = FileUtilities.CreateFileStreamChecked(File.Create, dllFilePath, nameof(dllFilePath)))
using (var pdbStream = (pdbFilePath == null ? null : FileUtilities.CreateFileStreamChecked(File.Create, pdbFilePath, nameof(pdbFilePath))))
{
var result = compilation.Emit(dllStream, pdbStream, options: emitOptions, embeddedTexts: embeddedTexts);
Assert.Empty(result.Diagnostics.Where(d => d.Severity == DiagnosticSeverity.Error));
}
using var dllStream = FileUtilities.CreateFileStreamChecked(File.Create, dllFilePath, nameof(dllFilePath));
using var pdbStream = (pdbFilePath == null ? null : FileUtilities.CreateFileStreamChecked(File.Create, pdbFilePath, nameof(pdbFilePath)));
var result = compilation.Emit(dllStream, pdbStream, options: emitOptions, embeddedTexts: embeddedTexts);
Assert.Empty(result.Diagnostics.Where(d => d.Severity == DiagnosticSeverity.Error));
}

protected static string GetDllPath(string path)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,17 +177,15 @@ public bool ExecuteCommand(ReturnKeyCommandArgs args, CommandExecutionContext co
// According to JasonMal, the text undo history is associated with the surface buffer
// in projection buffer scenarios, so the following line's usage of the surface buffer
// is correct.
using (var transaction = _undoHistoryRegistry.GetHistory(args.TextView.TextBuffer).CreateTransaction(EditorFeaturesResources.Insert_new_line))
{
var editorOperations = _editorOperationsFactoryService.GetEditorOperations(args.TextView);
editorOperations.InsertNewLine();
using var transaction = _undoHistoryRegistry.GetHistory(args.TextView.TextBuffer).CreateTransaction(EditorFeaturesResources.Insert_new_line);
var editorOperations = _editorOperationsFactoryService.GetEditorOperations(args.TextView);
editorOperations.InsertNewLine();

CompleteComment(args.SubjectBuffer, args.TextView, InsertOnEnterTyped, CancellationToken.None);
CompleteComment(args.SubjectBuffer, args.TextView, InsertOnEnterTyped, CancellationToken.None);

// Since we're wrapping the ENTER key undo transaction, we always complete
// the transaction -- even if we didn't generate anything.
transaction.Complete();
}
// Since we're wrapping the ENTER key undo transaction, we always complete
// the transaction -- even if we didn't generate anything.
transaction.Complete();

return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,49 +103,47 @@ internal static async Task TestChangeSignatureViaCommandAsync(
OptionsCollection options = null,
int expectedSelectedIndex = -1)
{
using (var testState = ChangeSignatureTestState.Create(markup, languageName, parseOptions, options))
using var testState = ChangeSignatureTestState.Create(markup, languageName, parseOptions, options);
testState.TestChangeSignatureOptionsService.UpdatedSignature = updatedSignature;
var result = await testState.ChangeSignatureAsync().ConfigureAwait(false);

if (expectedSuccess)
{
Assert.True(result.Succeeded);
Assert.Null(result.ChangeSignatureFailureKind);
}
else
{
testState.TestChangeSignatureOptionsService.UpdatedSignature = updatedSignature;
var result = await testState.ChangeSignatureAsync().ConfigureAwait(false);
Assert.False(result.Succeeded);

if (expectedSuccess)
if (expectedFailureReason != null)
{
Assert.True(result.Succeeded);
Assert.Null(result.ChangeSignatureFailureKind);
Assert.Equal(expectedFailureReason, result.ChangeSignatureFailureKind);
}
else
{
Assert.False(result.Succeeded);
}

if (expectedFailureReason != null)
{
Assert.Equal(expectedFailureReason, result.ChangeSignatureFailureKind);
}
}
// Allow testing of invocation document regardless of success/failure
if (expectedUpdatedInvocationDocumentCode != null)
{
var updatedInvocationDocument = result.UpdatedSolution.GetDocument(testState.InvocationDocument.Id);
var updatedCode = (await updatedInvocationDocument.GetTextAsync()).ToString();
Assert.Equal(expectedUpdatedInvocationDocumentCode, updatedCode);
}

// Allow testing of invocation document regardless of success/failure
if (expectedUpdatedInvocationDocumentCode != null)
{
var updatedInvocationDocument = result.UpdatedSolution.GetDocument(testState.InvocationDocument.Id);
var updatedCode = (await updatedInvocationDocument.GetTextAsync()).ToString();
Assert.Equal(expectedUpdatedInvocationDocumentCode, updatedCode);
}
if (verifyNoDiagnostics)
{
var diagnostics = (await testState.InvocationDocument.GetSemanticModelAsync()).GetDiagnostics();

if (verifyNoDiagnostics)
if (diagnostics.Length > 0)
{
var diagnostics = (await testState.InvocationDocument.GetSemanticModelAsync()).GetDiagnostics();

if (diagnostics.Length > 0)
{
Assert.True(false, CreateDiagnosticsString(diagnostics, updatedSignature, testState.InvocationDocument, totalParameters, (await testState.InvocationDocument.GetTextAsync()).ToString()));
}
Assert.True(false, CreateDiagnosticsString(diagnostics, updatedSignature, testState.InvocationDocument, totalParameters, (await testState.InvocationDocument.GetTextAsync()).ToString()));
}
}

if (expectedSelectedIndex != -1)
{
var parameterConfiguration = await testState.GetParameterConfigurationAsync();
Assert.Equal(expectedSelectedIndex, parameterConfiguration.SelectedIndex);
}
if (expectedSelectedIndex != -1)
{
var parameterConfiguration = await testState.GetParameterConfigurationAsync();
Assert.Equal(expectedSelectedIndex, parameterConfiguration.SelectedIndex);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -395,12 +395,10 @@ protected async Task TestActionCountAsync(
TestParameters parameters = null)
{
var ps = parameters ?? TestParameters.Default;
using (var workspace = CreateWorkspaceFromOptions(initialMarkup, ps))
{
var (actions, _) = await GetCodeActionsAsync(workspace, ps);
using var workspace = CreateWorkspaceFromOptions(initialMarkup, ps);
var (actions, _) = await GetCodeActionsAsync(workspace, ps);

Assert.Equal(count, actions.Length);
}
Assert.Equal(count, actions.Length);
}

internal Task TestInRegularAndScriptAsync(
Expand Down Expand Up @@ -485,25 +483,23 @@ private async Task TestAsync(
var warningSpans = expectedSpanMap.GetOrAdd("Warning", _ => ImmutableArray<TextSpan>.Empty);
var navigationSpans = expectedSpanMap.GetOrAdd("Navigation", _ => ImmutableArray<TextSpan>.Empty);

using (var workspace = CreateWorkspaceFromOptions(initialMarkup, parameters))
using var workspace = CreateWorkspaceFromOptions(initialMarkup, parameters);
// Ideally this check would always run, but there are several hundred tests that would need to be
// updated with {|Unnecessary:|} spans.
if (unnecessarySpans.Any())
{
// Ideally this check would always run, but there are several hundred tests that would need to be
// updated with {|Unnecessary:|} spans.
if (unnecessarySpans.Any())
{
var allDiagnostics = await GetDiagnosticsWorkerAsync(workspace, parameters
.WithRetainNonFixableDiagnostics(true)
.WithIncludeDiagnosticsOutsideSelection(true));
var allDiagnostics = await GetDiagnosticsWorkerAsync(workspace, parameters
.WithRetainNonFixableDiagnostics(true)
.WithIncludeDiagnosticsOutsideSelection(true));

TestUnnecessarySpans(allDiagnostics, unnecessarySpans, UnnecessaryMarkupKey, initialMarkupWithoutSpans);
}

var (_, action) = await GetCodeActionsAsync(workspace, parameters);
await TestActionAsync(
workspace, expected, action,
conflictSpans, renameSpans, warningSpans, navigationSpans,
parameters);
TestUnnecessarySpans(allDiagnostics, unnecessarySpans, UnnecessaryMarkupKey, initialMarkupWithoutSpans);
}

var (_, action) = await GetCodeActionsAsync(workspace, parameters);
await TestActionAsync(
workspace, expected, action,
conflictSpans, renameSpans, warningSpans, navigationSpans,
parameters);
}

private static void TestUnnecessarySpans(
Expand Down Expand Up @@ -697,58 +693,56 @@ protected static Document GetDocumentToVerify(DocumentId expectedChangedDocument

private static async Task VerifyAgainstWorkspaceDefinitionAsync(string expectedText, Solution newSolution, TestComposition composition)
{
using (var expectedWorkspace = TestWorkspace.Create(expectedText, composition: composition))
using var expectedWorkspace = TestWorkspace.Create(expectedText, composition: composition);
var expectedSolution = expectedWorkspace.CurrentSolution;
Assert.Equal(expectedSolution.Projects.Count(), newSolution.Projects.Count());
foreach (var project in newSolution.Projects)
{
var expectedSolution = expectedWorkspace.CurrentSolution;
Assert.Equal(expectedSolution.Projects.Count(), newSolution.Projects.Count());
foreach (var project in newSolution.Projects)
var expectedProject = expectedSolution.GetProjectsByName(project.Name).Single();
Assert.Equal(expectedProject.Documents.Count(), project.Documents.Count());

foreach (var doc in project.Documents)
{
var expectedProject = expectedSolution.GetProjectsByName(project.Name).Single();
Assert.Equal(expectedProject.Documents.Count(), project.Documents.Count());
var root = await doc.GetSyntaxRootAsync();
var expectedDocuments = expectedProject.Documents.Where(d => d.Name == doc.Name);

foreach (var doc in project.Documents)
if (expectedDocuments.Any())
{
var root = await doc.GetSyntaxRootAsync();
var expectedDocuments = expectedProject.Documents.Where(d => d.Name == doc.Name);

if (expectedDocuments.Any())
{
Assert.Single(expectedDocuments);
}
else
{
AssertEx.Fail($"Could not find document with name '{doc.Name}'");
}

var expectedDocument = expectedDocuments.Single();

var expectedRoot = await expectedDocument.GetSyntaxRootAsync();
VerifyExpectedDocumentText(expectedRoot.ToFullString(), root.ToFullString());
Assert.Single(expectedDocuments);
}

foreach (var additionalDoc in project.AdditionalDocuments)
else
{
var root = await additionalDoc.GetTextAsync();
var expectedDocument = expectedProject.AdditionalDocuments.Single(d => d.Name == additionalDoc.Name);
var expectedRoot = await expectedDocument.GetTextAsync();
VerifyExpectedDocumentText(expectedRoot.ToString(), root.ToString());
AssertEx.Fail($"Could not find document with name '{doc.Name}'");
}

foreach (var analyzerConfigDoc in project.AnalyzerConfigDocuments)
var expectedDocument = expectedDocuments.Single();

var expectedRoot = await expectedDocument.GetSyntaxRootAsync();
VerifyExpectedDocumentText(expectedRoot.ToFullString(), root.ToFullString());
}

foreach (var additionalDoc in project.AdditionalDocuments)
{
var root = await additionalDoc.GetTextAsync();
var expectedDocument = expectedProject.AdditionalDocuments.Single(d => d.Name == additionalDoc.Name);
var expectedRoot = await expectedDocument.GetTextAsync();
VerifyExpectedDocumentText(expectedRoot.ToString(), root.ToString());
}

foreach (var analyzerConfigDoc in project.AnalyzerConfigDocuments)
{
var root = await analyzerConfigDoc.GetTextAsync();
var actualString = root.ToString();
if (actualString.StartsWith(AutoGeneratedAnalyzerConfigHeader))
{
var root = await analyzerConfigDoc.GetTextAsync();
var actualString = root.ToString();
if (actualString.StartsWith(AutoGeneratedAnalyzerConfigHeader))
{
// Skip validation for analyzer config file that is auto-generated by test framework
// for applying code style options.
continue;
}

var expectedDocument = expectedProject.AnalyzerConfigDocuments.Single(d => d.FilePath == analyzerConfigDoc.FilePath);
var expectedRoot = await expectedDocument.GetTextAsync();
VerifyExpectedDocumentText(expectedRoot.ToString(), actualString);
// Skip validation for analyzer config file that is auto-generated by test framework
// for applying code style options.
continue;
}

var expectedDocument = expectedProject.AnalyzerConfigDocuments.Single(d => d.FilePath == analyzerConfigDoc.FilePath);
var expectedRoot = await expectedDocument.GetTextAsync();
VerifyExpectedDocumentText(expectedRoot.ToString(), actualString);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,8 @@ protected static async Task<Tuple<Solution, Solution>> TestAddDocument(
// If there is just one document change then we expect the preview to be a WpfTextView
var previews = await editHandler.GetPreviewsAsync(workspace, operations, CancellationToken.None);
var content = (await previews.GetPreviewsAsync())[0];
using (var diffView = content as DifferenceViewerPreview)
{
Assert.NotNull(diffView.Viewer);
}
using var diffView = content as DifferenceViewerPreview;
Assert.NotNull(diffView.Viewer);
}
else
{
Expand Down
Loading
Loading