/
Element.cs
294 lines (247 loc) · 8.08 KB
/
Element.cs
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
using System;
using System.Collections.Generic;
using System.Linq;
using JetBrains.Application;
using JetBrains.ProjectModel;
using JetBrains.ReSharper.Psi;
using JetBrains.ReSharper.Psi.Caches;
using JetBrains.ReSharper.Psi.Tree;
using JetBrains.ReSharper.UnitTestFramework;
using JetBrains.Util;
using Machine.Specifications.ReSharperRunner.Factories;
namespace Machine.Specifications.ReSharperRunner.Presentation
{
public abstract class Element : IUnitTestElement
{
readonly IClrTypeName _declaringTypeName;
readonly ProjectModelElementEnvoy _projectEnvoy;
readonly MSpecUnitTestProvider _provider;
readonly UnitTestTaskFactory _taskFactory;
Element _parent;
readonly PsiModuleManager _psiModuleManager;
readonly CacheManager _cacheManager;
protected Element(MSpecUnitTestProvider provider,
PsiModuleManager psiModuleManager,
CacheManager cacheManager,
Element parent,
ProjectModelElementEnvoy projectEnvoy,
IClrTypeName declaringTypeName,
bool isIgnored)
{
if (projectEnvoy == null && !Shell.Instance.IsTestShell)
{
throw new ArgumentNullException("project");
}
if (declaringTypeName == null)
{
throw new ArgumentNullException("declaringTypeName");
}
if (projectEnvoy != null)
{
_projectEnvoy = projectEnvoy;
}
_provider = provider;
_declaringTypeName = declaringTypeName;
_psiModuleManager = psiModuleManager;
_cacheManager = cacheManager;
if (isIgnored)
{
ExplicitReason = "Ignored";
}
TypeName = declaringTypeName;
Parent = parent;
Children = new List<IUnitTestElement>();
State = UnitTestElementState.Valid;
_taskFactory = new UnitTestTaskFactory(_provider.ID);
}
public IClrTypeName TypeName { get; protected set; }
public abstract string Kind { get; }
public abstract IEnumerable<UnitTestElementCategory> Categories { get; }
public string ExplicitReason { get; private set; }
public abstract string Id
{
get;
}
public IUnitTestProvider Provider
{
get { return _provider; }
}
public IUnitTestElement Parent
{
get { return _parent; }
set
{
if (_parent == value)
{
return;
}
if (_parent != null)
{
_parent.RemoveChild(this);
}
_parent = (Element)value;
if (_parent != null)
{
_parent.AddChild(this);
}
}
}
public ICollection<IUnitTestElement> Children { get; private set; }
public abstract string ShortName { get; }
public bool Explicit
{
get { return false; }
}
public UnitTestElementState State { get; set; }
public IProject GetProject()
{
return _projectEnvoy.GetValidProjectElement() as IProject;
}
public UnitTestNamespace GetNamespace()
{
return new UnitTestNamespace(_declaringTypeName.GetNamespaceName());
}
public UnitTestElementDisposition GetDisposition()
{
IDeclaredElement element = GetDeclaredElement();
if (element == null || !element.IsValid())
{
return UnitTestElementDisposition.InvalidDisposition;
}
var locations = new List<UnitTestElementLocation>();
element.GetDeclarations().ForEach(declaration =>
{
IFile file = declaration.GetContainingFile();
if (file != null)
{
locations.Add(new UnitTestElementLocation(file.GetSourceFile().ToProjectFile(),
declaration.GetNameDocumentRange().TextRange,
declaration.GetDocumentRange().TextRange));
}
});
return new UnitTestElementDisposition(locations, this);
}
public bool Equals(IUnitTestElement other)
{
if (ReferenceEquals(this, other))
{
return true;
}
if (other.GetType() == GetType())
{
var element = (Element) other;
return other.ShortName == ShortName
&& other.Provider == Provider
&& Equals(element._projectEnvoy, _projectEnvoy)
&& element._declaringTypeName == _declaringTypeName;
}
return false;
}
public abstract string GetPresentation();
public string GetPresentation(IUnitTestElement unitTestElement)
{
return GetPresentation();
}
public abstract IDeclaredElement GetDeclaredElement();
public IEnumerable<IProjectFile> GetProjectFiles()
{
ITypeElement declaredType = GetDeclaredType();
if (declaredType == null)
{
return EmptyArray<IProjectFile>.Instance;
}
return declaredType.GetSourceFiles().Select(x => x.ToProjectFile());
}
#if RESHARPER_7
public IList<UnitTestTask> GetTaskSequence(ICollection<IUnitTestElement> explicitElements, IUnitTestLaunch unitTestLaunch)
#else
public IList<UnitTestTask> GetTaskSequence(IList<IUnitTestElement> explicitElements)
#endif
{
if (this is ContextSpecificationElement)
{
var contextSpecification = this as ContextSpecificationElement;
var context = contextSpecification.Context;
return new List<UnitTestTask>
{
_taskFactory.CreateAssemblyLoadTask(context),
_taskFactory.CreateRunAssemblyTask(context),
_taskFactory.CreateContextTask(context),
_taskFactory.CreateContextSpecificationTask(context, contextSpecification)
};
}
if (this is BehaviorSpecificationElement)
{
var behaviorSpecification = this as BehaviorSpecificationElement;
var behavior = behaviorSpecification.Behavior;
var context = behavior.Context;
return new List<UnitTestTask>
{
_taskFactory.CreateAssemblyLoadTask(context),
_taskFactory.CreateRunAssemblyTask(context),
_taskFactory.CreateContextTask(context),
_taskFactory.CreateBehaviorSpecificationTask(context, behaviorSpecification)
};
}
if (this is ContextElement || this is BehaviorElement)
{
return EmptyArray<UnitTestTask>.Instance;
}
throw new ArgumentException(String.Format("Element is not a Machine.Specifications element: '{0}'", this));
}
public virtual string GetTitlePrefix()
{
return String.Empty;
}
protected ITypeElement GetDeclaredType()
{
IProject project = GetProject();
if (project == null)
{
return null;
}
var psiModule = _psiModuleManager.GetPrimaryPsiModule(project);
if (psiModule == null)
{
return null;
}
var declarationsCache = _cacheManager.GetDeclarationsCache(psiModule, true, true);
try
{
return declarationsCache.GetTypeElementByCLRName(_declaringTypeName);
}
catch (NullReferenceException)
{
return null;
}
}
public IClrTypeName GetTypeClrName()
{
return _declaringTypeName;
}
public override bool Equals(object obj)
{
var other = obj as Element;
if (other == null)
{
return false;
}
return Equals(other._projectEnvoy, _projectEnvoy) && other.Id == Id;
}
public override int GetHashCode()
{
var result = 0;
result = 29 * result + _projectEnvoy.GetHashCode();
result = 29 * result + Id.GetHashCode();
return result;
}
void AddChild(Element behaviorElement)
{
Children.Add(behaviorElement);
}
void RemoveChild(Element behaviorElement)
{
Children.Remove(behaviorElement);
}
}
}