/
NUnitDiscoveryTestClasses.cs
403 lines (329 loc) · 15.2 KB
/
NUnitDiscoveryTestClasses.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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
// ***********************************************************************
// Copyright (c) 2020-2021 Charlie Poole, Terje Sandstrom
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// ***********************************************************************
using System.Collections.Generic;
using System.Linq;
// ReSharper disable InconsistentNaming
namespace NUnit.VisualStudio.TestAdapter.NUnitEngine
{
public interface INUnitDiscoverySuiteBase : INUnitTestCasePropertyInfo
{
string Id { get; }
string Name { get; }
string FullName { get; }
int TestCaseCount { get; }
INUnitDiscoverySuiteBase Parent { get; }
NUnitDiscoveryProperties NUnitDiscoveryProperties { get; }
bool IsExplicit { get; }
bool IsExplicitReverse { get; }
bool IsParameterizedMethod { get; }
void AddToAllTestCases(NUnitDiscoveryTestCase tc);
}
public abstract class NUnitDiscoverySuiteBase : INUnitDiscoverySuiteBase
{
public string Id { get; }
public string Name { get; }
public string FullName { get; }
public int TestCaseCount { get; }
public RunStateEnum RunState { get; }
public INUnitDiscoverySuiteBase Parent { get; }
private NUnitDiscoverySuiteBase(string id, string name, string fullname, int count)
{
Id = id;
Name = name;
FullName = fullname;
TestCaseCount = count;
}
protected NUnitDiscoverySuiteBase(BaseProperties other)
: this(other.Id, other.Name, other.Fullname, other.TestCaseCount)
{
RunState = other.RunState;
foreach (var prop in other.Properties)
{
NUnitDiscoveryProperties.Add(prop);
}
}
protected NUnitDiscoverySuiteBase(BaseProperties other, INUnitDiscoverySuiteBase parent) : this(other)
{
Parent = parent;
}
public NUnitDiscoveryProperties NUnitDiscoveryProperties { get; } = new NUnitDiscoveryProperties();
public abstract bool IsExplicit { get; }
public virtual bool IsExplicitReverse => RunState == RunStateEnum.Explicit || (Parent?.IsExplicitReverse ?? RunState == RunStateEnum.Explicit);
public virtual bool IsParameterizedMethod => false;
public IEnumerable<NUnitProperty> Properties => NUnitDiscoveryProperties.Properties;
public virtual void AddToAllTestCases(NUnitDiscoveryTestCase tc)
{
Parent.AddToAllTestCases(tc);
}
}
public class NUnitDiscoveryTestRun : NUnitDiscoverySuiteBase
{
public NUnitDiscoveryTestRun(BaseProperties baseProps) : base(baseProps)
{
}
public NUnitDiscoveryTestAssembly TestAssembly { get; set; }
public override bool IsExplicit =>
RunState == RunStateEnum.Explicit || TestAssembly.IsExplicit;
public void AddTestAssembly(NUnitDiscoveryTestAssembly testAssembly)
{
TestAssembly = testAssembly;
}
}
public class NUnitDiscoveryProperties
{
private List<NUnitProperty> TheProperties { get; } = new List<NUnitProperty>();
public IEnumerable<NUnitProperty> Properties => TheProperties;
public void Add(NUnitProperty p) => TheProperties.Add(p);
public bool AllInternal => TheProperties.All(o => o.IsInternal);
}
public class NUnitDiscoveryTestSuite : NUnitDiscoveryCanHaveTestFixture
{
public NUnitDiscoveryTestSuite(BaseProperties theBase, INUnitDiscoverySuiteBase parent) : base(theBase, parent)
{
}
public NUnitDiscoveryTestAssembly ParentAssembly { get; set; }
}
public sealed class NUnitDiscoveryTestAssembly : NUnitDiscoveryTestSuite
{
public NUnitDiscoveryTestAssembly(BaseProperties theBase, NUnitDiscoveryTestRun parent) : base(theBase, parent)
{ }
private readonly List<NUnitDiscoveryTestCase> allTestCases = new List<NUnitDiscoveryTestCase>();
/// <summary>
/// If all testcases are Explicit, we can run this one.
/// </summary>
public IEnumerable<NUnitDiscoveryTestCase> AllTestCases => allTestCases;
/// <summary>
/// If there are a mixture of explicit and non-explicit, this one will filter out the explicit ones.
/// </summary>
public IEnumerable<NUnitDiscoveryTestCase> RunnableTestCases => allTestCases.Where(c => !c.IsExplicitReverse);
public void AddTestSuiteToAssembly(NUnitDiscoveryTestSuite ts)
{
AddTestSuite(ts);
}
public override void AddToAllTestCases(NUnitDiscoveryTestCase tc)
{
allTestCases.Add(tc);
}
}
public sealed class NUnitDiscoveryTestFixture : NUnitDiscoveryCanHaveTestCases
{
private readonly List<NUnitDiscoveryParameterizedMethod> parameterizedMethods = new List<NUnitDiscoveryParameterizedMethod>();
public IEnumerable<NUnitDiscoveryParameterizedMethod> ParameterizedMethods => parameterizedMethods;
private readonly List<NUnitDiscoveryTheory> theories = new List<NUnitDiscoveryTheory>();
private readonly List<NUnitDiscoveryGenericMethod> genericMethods = new List<NUnitDiscoveryGenericMethod>();
public IEnumerable<NUnitDiscoveryTheory> Theories => theories;
public override int NoOfActualTestCases =>
base.NoOfActualTestCases
+ parameterizedMethods.Sum(o => o.NoOfActualTestCases)
+ theories.Sum(o => o.NoOfActualTestCases)
+ genericMethods.Sum(o => o.NoOfActualTestCases);
public override bool IsExplicit
{
get
{
if (RunState == RunStateEnum.Explicit)
return true;
var all = new List<NUnitDiscoverySuiteBase>();
all.AddRange(TestCases.Cast<NUnitDiscoverySuiteBase>());
all.AddRange(parameterizedMethods.Cast<NUnitDiscoverySuiteBase>());
all.AddRange(theories.Cast<NUnitDiscoverySuiteBase>());
all.AddRange(genericMethods.Cast<NUnitDiscoverySuiteBase>());
return all.All(o => o.IsExplicit);
}
}
public IEnumerable<NUnitDiscoveryGenericMethod> GenericMethods => genericMethods;
public NUnitDiscoveryTestFixture(BaseProperties theBase, string classname, INUnitDiscoverySuiteBase parent) : base(theBase, parent, classname)
{
}
public void AddParameterizedMethod(NUnitDiscoveryParameterizedMethod ts)
{
parameterizedMethods.Add(ts);
}
public void AddTheory(NUnitDiscoveryTheory tc)
{
theories.Add(tc);
}
public void AddGenericMethod(NUnitDiscoveryGenericMethod tc)
{
genericMethods.Add(tc);
}
}
public interface INUnitDiscoveryTestCase : INUnitDiscoverySuiteBase
{
string ClassName { get; }
string MethodName { get; }
long Seed { get; set; }
}
/// <summary>
/// Interface for common properties between event testcase and discoverytestcase.
/// </summary>
public interface INUnitCommonTestCase
{
string Id { get; }
string Name { get; }
string FullName { get; }
string ClassName { get; }
string MethodName { get; }
long Seed { get; }
}
public sealed class NUnitDiscoveryTestCase : NUnitDiscoverySuiteBase, INUnitDiscoveryTestCase, INUnitCommonTestCase
{
public string ClassName { get; }
public string MethodName { get; set; }
public long Seed { get; set; }
public NUnitDiscoveryTestCase(BaseProperties theBase, INUnitDiscoveryCanHaveTestCases parent,
string className,
string methodname, long seed) : base(theBase, parent)
{
ClassName = className;
MethodName = methodname;
Seed = seed;
}
public override bool IsExplicit => RunState == RunStateEnum.Explicit;
}
public sealed class NUnitDiscoveryParameterizedMethod : NUnitDiscoveryCanHaveTestCases
{
public NUnitDiscoveryParameterizedMethod(BaseProperties theBase, string classname, INUnitDiscoverySuiteBase parent) : base(theBase, parent, classname)
{
}
public override bool IsParameterizedMethod => true;
}
public interface INUnitDiscoveryCanHaveTestCases : INUnitDiscoverySuiteBase
{
IEnumerable<NUnitDiscoveryTestCase> TestCases { get; }
int NoOfActualTestCases { get; }
void AddTestCase(NUnitDiscoveryTestCase tc);
}
public abstract class NUnitDiscoveryCanHaveTestCases : NUnitDiscoverySuiteBase, INUnitDiscoveryCanHaveTestCases
{
private readonly List<NUnitDiscoveryTestCase> testCases = new List<NUnitDiscoveryTestCase>();
public IEnumerable<NUnitDiscoveryTestCase> TestCases => testCases;
public virtual int NoOfActualTestCases => testCases.Count;
public override bool IsExplicit =>
RunState == RunStateEnum.Explicit || testCases.AllWithEmptyFalse(o => o.IsExplicit);
public string ClassName { get; }
public void AddTestCase(NUnitDiscoveryTestCase tc)
{
testCases.Add(tc);
Parent.AddToAllTestCases(tc);
}
protected NUnitDiscoveryCanHaveTestCases(BaseProperties theBase, INUnitDiscoverySuiteBase parent, string classname) : base(theBase, parent)
{
ClassName = classname;
}
}
public interface INUnitDiscoveryCanHaveTestFixture : INUnitDiscoverySuiteBase
{
IEnumerable<NUnitDiscoveryTestFixture> TestFixtures { get; }
int NoOfActualTestCases { get; }
void AddTestFixture(NUnitDiscoveryTestFixture tf);
}
public abstract class NUnitDiscoveryCanHaveTestFixture : NUnitDiscoverySuiteBase, INUnitDiscoveryCanHaveTestFixture
{
private readonly List<NUnitDiscoveryTestFixture> testFixtures = new List<NUnitDiscoveryTestFixture>();
public IEnumerable<NUnitDiscoveryTestFixture> TestFixtures => testFixtures;
public void AddTestFixture(NUnitDiscoveryTestFixture tf)
{
testFixtures.Add(tf);
}
protected NUnitDiscoveryCanHaveTestFixture(BaseProperties theBase, INUnitDiscoverySuiteBase parent) : base(theBase, parent)
{
}
private readonly List<NUnitDiscoveryTestSuite> testSuites = new List<NUnitDiscoveryTestSuite>();
private readonly List<NUnitDiscoveryGenericFixture> genericFixtures = new List<NUnitDiscoveryGenericFixture>();
private readonly List<NUnitDiscoverySetUpFixture> setUpFixtures = new List<NUnitDiscoverySetUpFixture>();
private readonly List<NUnitDiscoveryParameterizedTestFixture> parameterizedFixtures = new List<NUnitDiscoveryParameterizedTestFixture>();
public IEnumerable<NUnitDiscoveryTestSuite> TestSuites => testSuites;
public IEnumerable<NUnitDiscoverySetUpFixture> SetUpFixtures => setUpFixtures;
public IEnumerable<NUnitDiscoveryParameterizedTestFixture> ParameterizedFixtures => parameterizedFixtures;
public IEnumerable<NUnitDiscoveryGenericFixture> GenericFixtures => genericFixtures;
public override bool IsExplicit
{
get
{
if (RunState == RunStateEnum.Explicit)
return true;
var fullList = new List<NUnitDiscoverySuiteBase>();
fullList.AddRange(TestFixtures.Cast<NUnitDiscoverySuiteBase>());
fullList.AddRange(TestSuites.Cast<NUnitDiscoverySuiteBase>());
fullList.AddRange(GenericFixtures.Cast<NUnitDiscoverySuiteBase>());
fullList.AddRange(SetUpFixtures.Cast<NUnitDiscoverySuiteBase>());
fullList.AddRange(ParameterizedFixtures.Cast<NUnitDiscoverySuiteBase>());
return fullList.All(o => o.IsExplicit);
}
}
public int NoOfActualTestCases => testFixtures.Sum(o => o.NoOfActualTestCases)
+ testSuites.Sum(o => o.NoOfActualTestCases)
+ genericFixtures.Sum(o => o.NoOfActualTestCases)
+ setUpFixtures.Sum(o => o.NoOfActualTestCases)
+ parameterizedFixtures.Sum(o => o.NoOfActualTestCases);
public void AddTestSuite(NUnitDiscoveryTestSuite ts)
{
testSuites.Add(ts);
}
public void AddTestGenericFixture(NUnitDiscoveryGenericFixture ts)
{
genericFixtures.Add(ts);
}
public void AddSetUpFixture(NUnitDiscoverySetUpFixture ts)
{
setUpFixtures.Add(ts);
}
public void AddParameterizedFixture(NUnitDiscoveryParameterizedTestFixture ts)
{
parameterizedFixtures.Add(ts);
}
}
public sealed class NUnitDiscoveryGenericFixture : NUnitDiscoveryCanHaveTestFixture
{
public NUnitDiscoveryGenericFixture(BaseProperties theBase, INUnitDiscoverySuiteBase parent) : base(theBase, parent)
{
}
}
public sealed class NUnitDiscoveryParameterizedTestFixture : NUnitDiscoveryCanHaveTestFixture
{
public NUnitDiscoveryParameterizedTestFixture(BaseProperties theBase, NUnitDiscoveryCanHaveTestFixture parent) : base(theBase, parent)
{
}
}
public sealed class NUnitDiscoverySetUpFixture : NUnitDiscoveryCanHaveTestFixture
{
public string ClassName { get; }
public NUnitDiscoverySetUpFixture(BaseProperties theBase, string classname, NUnitDiscoveryCanHaveTestFixture parent) : base(theBase, parent)
{
ClassName = classname;
}
}
public sealed class NUnitDiscoveryTheory : NUnitDiscoveryCanHaveTestCases
{
public NUnitDiscoveryTheory(BaseProperties theBase, string classname, INUnitDiscoverySuiteBase parent) : base(theBase, parent, classname)
{
}
}
public sealed class NUnitDiscoveryGenericMethod : NUnitDiscoveryCanHaveTestCases
{
public NUnitDiscoveryGenericMethod(BaseProperties theBase, string classname, INUnitDiscoverySuiteBase parent) : base(theBase, parent, classname)
{
}
}
}