forked from dotnet/roslyn
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExtractMethodMiscellaneousTests.cs
More file actions
174 lines (134 loc) · 5.67 KB
/
ExtractMethodMiscellaneousTests.cs
File metadata and controls
174 lines (134 loc) · 5.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
// Licensed to the .NET Foundation under one or more agreements.
// 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.Linq;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.ExtractMethod;
using Microsoft.CodeAnalysis.Editor.UnitTests;
using Microsoft.CodeAnalysis.Editor.UnitTests.Extensions;
using Microsoft.CodeAnalysis.Editor.UnitTests.Utilities;
using Microsoft.CodeAnalysis.ExtractMethod;
using Microsoft.CodeAnalysis.Notification;
using Microsoft.CodeAnalysis.Shared.TestHooks;
using Microsoft.CodeAnalysis.Test.Utilities;
using Microsoft.VisualStudio.Text;
using Microsoft.VisualStudio.Text.Editor.Commanding.Commands;
using Roslyn.Test.Utilities;
using Xunit;
namespace Microsoft.CodeAnalysis.Editor.CSharp.UnitTests.ExtractMethod;
using static CSharpSyntaxTokens;
[UseExportProvider]
[Trait(Traits.Feature, Traits.Features.ExtractMethod)]
public sealed class ExtractMethodMiscellaneousTests
{
[Fact]
public void ServiceTest1()
{
var markupCode = """
class A
{
/* test */ [|public|] void Test(int i, int b, int c)
{
}
}
""";
MarkupTestFile.GetSpan(markupCode, out var code, out var span);
var root = SyntaxFactory.ParseCompilationUnit(code);
var result = CSharpSyntaxTriviaService.Instance.SaveTriviaAroundSelection(root, span);
var rootWithAnnotation = result.Root;
// find token to replace
var publicToken = rootWithAnnotation.DescendantTokens().First(t => t.Kind() == SyntaxKind.PublicKeyword);
// replace the token with new one
var newRoot = rootWithAnnotation.ReplaceToken(publicToken, PrivateKeyword);
// restore trivia around it
var rootWithTriviaRestored = result.RestoreTrivia(newRoot);
var expected = """
class A
{
/* test */ private void Test(int i, int b, int c)
{
}
}
""";
Assert.Equal(expected, rootWithTriviaRestored.ToFullString());
}
[Fact]
public void ServiceTest2()
{
var markupCode = """
class A
{
#if true
[|/* test */ public|] void Test(int i, int b, int c)
{
}
#endif
}
""";
MarkupTestFile.GetSpan(markupCode, out var code, out var span);
var root = SyntaxFactory.ParseCompilationUnit(code);
var result = CSharpSyntaxTriviaService.Instance.SaveTriviaAroundSelection(root, span);
var rootWithAnnotation = result.Root;
// find token to replace
var publicToken = rootWithAnnotation.DescendantTokens().First(t => t.Kind() == SyntaxKind.PublicKeyword);
// replace the token with new one
var newRoot = rootWithAnnotation.ReplaceToken(publicToken, PrivateKeyword);
// restore trivia around it
var rootWithTriviaRestored = result.RestoreTrivia(newRoot);
var expected = """
class A
{
#if true
private void Test(int i, int b, int c)
{
}
#endif
}
""";
Assert.Equal(expected, rootWithTriviaRestored.ToFullString());
}
[WpfFact]
public async Task TestExtractMethodCommandHandlerErrorMessage()
{
var markupCode = """
class A
{
[|void Method() {}|]
}
""";
await TestCommandHandler(markupCode, result: null, expectNotification: true);
}
private static async Task TestCommandHandler(string markupCode, string? result, bool expectNotification)
{
using var workspace = EditorTestWorkspace.CreateCSharp(markupCode, composition: EditorTestCompositions.EditorFeaturesWpf);
var testDocument = workspace.Documents.Single();
var view = testDocument.GetTextView();
view.Selection.Select(new SnapshotSpan(
view.TextBuffer.CurrentSnapshot, testDocument.SelectedSpans[0].Start, testDocument.SelectedSpans[0].Length), isReversed: false);
result ??= view.TextBuffer.CurrentSnapshot.GetText();
var callBackService = (INotificationServiceCallback)workspace.Services.GetRequiredService<INotificationService>();
var gotNotification = false;
callBackService.NotificationCallback = (_, _, _) => gotNotification = true;
var handler = workspace.ExportProvider.GetCommandHandler<ExtractMethodCommandHandler>(PredefinedCommandHandlerNames.ExtractMethod, ContentTypeNames.CSharpContentType);
handler.ExecuteCommand(new ExtractMethodCommandArgs(view, view.TextBuffer), TestCommandExecutionContext.Create());
var waiter = workspace.ExportProvider.GetExportedValue<IAsynchronousOperationListenerProvider>().GetWaiter(FeatureAttribute.ExtractMethod);
await waiter.ExpeditedWaitAsync();
Assert.Equal(expectNotification, gotNotification);
Assert.Equal(result, view.TextBuffer.CurrentSnapshot.GetText());
}
[WpfFact, WorkItem("https://github.com/dotnet/roslyn/issues/65465")]
public async Task TestExtractLocalFunctionInTopLevelFromCommandHandler()
{
var markupCode = """
System.Console.WriteLine([|"string"|]);
""";
await TestCommandHandler(markupCode, """
System.Console.WriteLine(NewMethod());
static string NewMethod()
{
return "string";
}
""", expectNotification: false);
}
}