-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathCSharpHighlightingWithinSolutionTestBase.cs
More file actions
246 lines (207 loc) · 11.1 KB
/
CSharpHighlightingWithinSolutionTestBase.cs
File metadata and controls
246 lines (207 loc) · 11.1 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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
// --
// -- TestCop http://github.com/testcop
// -- License http://github.com/testcop/license
// -- Copyright 2013
// --
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using JetBrains.Annotations;
using JetBrains.Application.Components;
using JetBrains.Application.DataContext;
using JetBrains.Application.Settings;
using JetBrains.Application.Threading;
using JetBrains.Application.UI.Actions;
using JetBrains.Application.UI.ActionsRevised.Menu;
using JetBrains.Application.UI.Controls.JetPopupMenu;
using JetBrains.DataFlow;
using JetBrains.IDE;
using JetBrains.Lifetimes;
using JetBrains.ProjectModel;
using JetBrains.ProjectModel.Properties;
using JetBrains.ReSharper.Daemon.Impl;
using JetBrains.ReSharper.Feature.Services.Daemon;
using JetBrains.ReSharper.FeaturesTestFramework.Daemon;
using JetBrains.ReSharper.Psi;
using JetBrains.ReSharper.Psi.CSharp;
using JetBrains.ReSharper.TestFramework;
using JetBrains.TestFramework;
using JetBrains.TestFramework.Projects;
using JetBrains.TextControl;
using JetBrains.TextControl.DataContext;
using JetBrains.Util;
using NUnit.Framework;
using TestCop.Plugin.Extensions;
namespace TestCop.Plugin.Tests
{
[System.ComponentModel.Category("CSharp")]
[TestFileExtension(".cs")]
[TestFixture]
public abstract class CSharpHighlightingWithinSolutionTestBase : BaseTest
{
private ISolution _loadedTestSolution;
protected ISolution LoadedTestSolution
{
get { return _loadedTestSolution; }
}
protected abstract string SolutionName { get; }
[CanBeNull]
protected PsiLanguageType CompilerIdsLanguage
{
get { return CSharpLanguage.Instance; }
}
private TestSolutionManager SolutionManager
{
get
{
return ShellInstance.GetComponent<TestSolutionManager>();
}
}
public override void TestFixtureSetUp()
{
base.TestFixtureSetUp();
RunGuarded(() =>
{
using (Locks.UsingWriteLock())
{
VirtualFileSystemPath solutionFilePath = GetVirtualTestDataFilePath(SolutionName);
if (!solutionFilePath.ExistsFile)
{
Assert.Fail("Solution file doesn't exist: " + solutionFilePath);
}
_loadedTestSolution =
SolutionManager.OpenExistingSolution(solutionFilePath);
Assert.IsNotNull(_loadedTestSolution, "Failed to load solution " + solutionFilePath.FullPath);
}
});
}
public override void TestFixtureTearDown()
{
Assert.IsNotNull(_loadedTestSolution, "_loadedTestSolution == null");
RunGuarded(() =>
{
ShellInstance.GetComponent<TestSolutionManager>().CloseSolution(_loadedTestSolution);
_loadedTestSolution = null;
});
base.TestFixtureTearDown();
}
protected static void ClearRegExSettingsPriorToRun(IContextBoundSettingsStore settingsStore)
{
settingsStore.SetValue<TestFileAnalysisSettings, string>(
s => s.TestProjectToCodeProjectNameSpaceRegEx, "NOT SET BY TEST");
settingsStore.SetValue<TestFileAnalysisSettings, string>(
s => s.TestProjectToCodeProjectNameSpaceRegExReplace, "NOT SET BY TEST");
settingsStore.SetValue<TestFileAnalysisSettings, string>(
s => s.TestProjectNameToCodeProjectNameRegEx, "NOT SET BY TEST");
settingsStore.SetValue<TestFileAnalysisSettings, string>(
s => s.SingleTestRegexTestToAssembly, "NOT SET BY TEST");
}
protected virtual TestHighlightingDumper CreateHighlightDumper(IPsiSourceFile sourceFile, TextWriter writer)
{
return new TestHighlightingDumper(sourceFile, writer, HighlightingPredicate, CompilerIdsLanguage);
}
protected virtual bool HighlightingPredicate(IHighlighting highlighting, IPsiSourceFile sourceFile, IContextBoundSettingsStore settingsStore)
{
return true;
}
public void DoTestFiles(string fullProjectPathToTestFile)
{
Assert.IsNotNull(_loadedTestSolution, "Expected solution to be loaded");
bool processedFile = false;
var listOfProjectPaths = new List<string>();
RunGuarded(() =>
{
foreach (var project in _loadedTestSolution.GetAllProjects())
{
List<IProjectFile> projectFiles = project.GetAllProjectFiles().ToList();
foreach (IProjectFile projectFile in
projectFiles.Where(p => p.LanguageType.Is<CSharpProjectFileType>()))
{
listOfProjectPaths.Add(projectFile.GetPresentableProjectPath());
if (fullProjectPathToTestFile != projectFile.GetPresentableProjectPath())
continue;
IPsiSourceFile sourceFile = projectFile.ToSourceFile();
Assert.IsNotNull(sourceFile);
if (!sourceFile.Properties.IsNonUserFile)
{
processedFile = true;
Assert.IsTrue(projectFile.Kind == ProjectItemKind.PHYSICAL_FILE);
IProjectFile file = projectFile;
ExecuteWithGold(projectFile.Location.FullPath
, (writer =>
{
var highlightDumper =
CreateHighlightDumper(sourceFile, writer);
highlightDumper.DoHighlighting(
DaemonProcessKind.VISIBLE_DOCUMENT);
highlightDumper.Dump();
DumperShortCutAction(file, writer);
}));
return;
}
}
}
});
if (!processedFile)
{
listOfProjectPaths.ForEach(f => Trace.WriteLine("Located Item:" + f));
Assert.Fail("Failed to project file by project path " + fullProjectPathToTestFile);
}
}
protected virtual void DumperShortCutAction(IProjectFile projectFile, TextWriter textwriter)
{
Lifetime.Using((lifetime =>
{
var textControl = OpenTextControl(projectFile);
try
{
var jumpToTestFileAction = GetShortcutAction(textwriter);
if(jumpToTestFileAction==null) return;
IDataContext context = DataContextOfTestTextControl.Create(lifetime,textControl, _loadedTestSolution);
Func<Lifetime, DataContexts, IDataContext> dataContext = textControl.ToDataContext();
if ((jumpToTestFileAction).Update(context, new ActionPresentation(), null))
{
(jumpToTestFileAction).Execute(context, null);
}
}
finally
{
CloseTextControl(textControl);
}
}));
}
private void CloseTextControl(ITextControl textControl)
{
if(textControl != null)
_loadedTestSolution.GetComponent<IEditorManager>().CloseTextControl(textControl, CloseTextControlSaveOptions.SaveIfDirty);
}
protected virtual IExecutableAction GetShortcutAction(TextWriter textwriter)
{
return null;
}
protected ITextControl OpenTextControl(IProjectFile projectFile, int? caretOffset = null)
{
IEditorManager editorManager = projectFile.GetSolution().GetComponent<IEditorManager>();
Task<ITextControl> openProjectFileAsync = editorManager.OpenProjectFileAsync(projectFile, new OpenFileOptions(FireAndForget: true));
openProjectFileAsync.Wait();
return openProjectFileAsync.Result;
}
public Action<JetPopupMenus, JetPopupMenu, JetPopupMenu.ShowWhen> CreateJetPopMenuShowToWriterAction(TextWriter textWriter)
{
Action<JetPopupMenus, JetPopupMenu, JetPopupMenu.ShowWhen> menuDisplayer = (menus, menu, when) =>
{
foreach (var itm in Enumerable.ToList(menu.ItemKeys).Cast<SimpleMenuItem>())
{
var s = itm.Text+ itm.ShortcutText ?? "";
textWriter.WriteLine("[{0}] {1}",
((JetBrains.Application.UI.Controls.RichTextAutomation)menu.Caption.Value).RichTextBlock.Value.Text
, s);
}
};
return menuDisplayer;
}
}
}