-
Notifications
You must be signed in to change notification settings - Fork 325
/
Copy pathTestCase.cs
390 lines (338 loc) · 13.5 KB
/
TestCase.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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using Microsoft.VisualStudio.TestPlatform.CoreUtilities;
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Utilities;
namespace Microsoft.VisualStudio.TestPlatform.ObjectModel;
/// <summary>
/// Stores information about a test case.
/// </summary>
[DataContract]
public sealed class TestCase : TestObject
{
private Guid _defaultId = Guid.Empty;
private Guid _id;
private string? _displayName;
private string _fullyQualifiedName;
private string _source;
/// <summary>
/// Initializes a new instance of the <see cref="TestCase"/> class.
/// </summary>
/// <remarks>This constructor doesn't perform any parameter validation, it is meant to be used for serialization."/></remarks>
#pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
public TestCase()
#pragma warning restore CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
{
// TODO: Make private
// Default constructor for Serialization.
}
/// <summary>
/// Initializes a new instance of the <see cref="TestCase"/> class.
/// </summary>
/// <param name="fullyQualifiedName">
/// Fully qualified name of the test case.
/// </param>
/// <param name="executorUri">
/// The Uri of the executor to use for running this test.
/// </param>
/// <param name="source">
/// Test container source from which the test is discovered.
/// </param>
public TestCase(string fullyQualifiedName, Uri executorUri, string source)
{
ValidateArg.NotNullOrEmpty(fullyQualifiedName, nameof(fullyQualifiedName));
ValidateArg.NotNullOrEmpty(source, nameof(source));
_fullyQualifiedName = fullyQualifiedName;
ExecutorUri = executorUri ?? throw new ArgumentNullException(nameof(executorUri));
_source = source;
LineNumber = -1;
_defaultId = Guid.Empty;
}
/// <summary>
/// LocalExtensionData which can be used by Adapter developers for local transfer of extended properties.
/// Note that this data is available only for in-Proc execution, and may not be available for OutProc executors
/// </summary>
public object? LocalExtensionData { get; set; }
/// <summary>
/// Gets or sets the id of the test case.
/// </summary>
[DataMember]
public Guid Id
{
get
{
if (_id == Guid.Empty)
{
if (_defaultId == Guid.Empty)
{
_defaultId = GetTestId();
}
return _defaultId;
}
return _id;
}
set => _id = value;
}
/// <summary>
/// Gets or sets the fully qualified name of the test case.
/// </summary>
[DataMember]
public string FullyQualifiedName
{
get => _fullyQualifiedName;
// defaultId should be reset as it is based on FullyQualifiedName and Source.
set => SetVariableAndResetId(ref _fullyQualifiedName, value);
}
/// <summary>
/// Gets or sets the display name of the test case.
/// </summary>
[DataMember]
public string DisplayName
{
get => _displayName.IsNullOrEmpty() ? GetFullyQualifiedName() : _displayName;
set => _displayName = value;
}
/// <summary>
/// Gets or sets the Uri of the Executor to use for running this test.
/// </summary>
[DataMember]
public Uri ExecutorUri
{
get; set;
}
/// <summary>
/// Gets the test container source from which the test is discovered.
/// </summary>
[DataMember]
public string Source
{
get => _source;
set
{
_source = value;
// defaultId should be reset as it is based on FullyQualifiedName and Source.
_defaultId = Guid.Empty;
}
}
/// <summary>
/// Gets or sets the source code file path of the test.
/// </summary>
[DataMember]
public string? CodeFilePath
{
get; set;
}
/// <summary>
/// Gets or sets the line number of the test.
/// </summary>
[DataMember]
public int LineNumber
{
get; set;
}
/// <summary>
/// Returns the TestProperties currently specified in this TestObject.
/// </summary>
public override IEnumerable<TestProperty> Properties
{
get
{
return TestCaseProperties.Properties.Concat(base.Properties);
}
}
/// <summary>
/// Creates a Id of TestCase
/// </summary>
/// <returns>Guid test id</returns>
private Guid GetTestId()
{
// To generate id hash "ExecutorUri + source + Name";
// If source is a file name then just use the filename for the identifier since the
// file might have moved between discovery and execution (in appx mode for example)
// This is not elegant because the Source contents should be a black box to the framework.
// For example in the database adapter case this is not a file path.
string source = Source;
// As discussed with team, we found no scenario for netcore, & fullclr where the Source is not present where ID is generated,
// which means we would always use FileName to generate ID. In cases where somehow Source Path contained garbage character the API Path.GetFileName()
// we are simply returning original input.
// For UWP where source during discovery, & during execution can be on different machine, in such case we should always use Path.GetFileName()
try
{
// If source name is malformed, GetFileName API will throw exception, so use same input malformed string to generate ID
source = Path.GetFileName(source);
}
catch
{
// do nothing
}
// We still need to handle parameters in the case of a Theory or TestGroup of test cases that are only
// distinguished by parameters.
var testcaseFullName = ExecutorUri + source;
// If ManagedType and ManagedMethod properties are filled than TestId should be based on those.
testcaseFullName += GetFullyQualifiedName();
return EqtHash.GuidFromString(testcaseFullName);
}
private void SetVariableAndResetId<T>(ref T variable, T value)
{
variable = value;
_defaultId = Guid.Empty;
}
private void SetPropertyAndResetId<T>(TestProperty property, T value)
{
SetPropertyValue(property, value);
_defaultId = Guid.Empty;
}
/// <summary>
/// Return TestProperty's value
/// </summary>
/// <returns></returns>
protected override object? ProtectedGetPropertyValue(TestProperty property, object? defaultValue)
{
ValidateArg.NotNull(property, nameof(property));
return property.Id switch
{
"TestCase.CodeFilePath" => CodeFilePath,
"TestCase.DisplayName" => DisplayName,
"TestCase.ExecutorUri" => ExecutorUri,
"TestCase.FullyQualifiedName" => FullyQualifiedName,
"TestCase.Id" => Id,
"TestCase.LineNumber" => LineNumber,
"TestCase.Source" => Source,
// It is a custom test case property. Should be retrieved from the TestObject store.
_ => base.ProtectedGetPropertyValue(property, defaultValue),
};
}
/// <summary>
/// Set TestProperty's value
/// </summary>
protected override void ProtectedSetPropertyValue(TestProperty property, object? value)
{
ValidateArg.NotNull(property, nameof(property));
switch (property.Id)
{
case "TestCase.CodeFilePath":
CodeFilePath = value as string;
return;
case "TestCase.DisplayName":
DisplayName = (value as string)!;
return;
case "TestCase.ExecutorUri":
ExecutorUri = value as Uri ?? new Uri((value as string)!);
return;
case "TestCase.FullyQualifiedName":
FullyQualifiedName = (value as string)!;
return;
case "TestCase.Id":
if (value is Guid guid)
{
Id = guid;
}
else if (value is string guidString)
{
Id = GuidPolyfill.Parse(guidString, CultureInfo.InvariantCulture);
}
else
{
Id = Guid.Empty;
}
return;
case "TestCase.LineNumber":
LineNumber = (int)value!;
return;
case "TestCase.Source":
Source = (value as string)!;
return;
}
// It is a custom test case property. Should be set in the TestObject store.
base.ProtectedSetPropertyValue(property, value);
}
private static readonly TestProperty ManagedTypeProperty = TestProperty.Register("TestCase.ManagedType", "ManagedType", string.Empty, string.Empty, typeof(string), o => !StringUtils.IsNullOrWhiteSpace(o as string), TestPropertyAttributes.Hidden, typeof(TestCase));
private static readonly TestProperty ManagedMethodProperty = TestProperty.Register("TestCase.ManagedMethod", "ManagedMethod", string.Empty, string.Empty, typeof(string), o => !StringUtils.IsNullOrWhiteSpace(o as string), TestPropertyAttributes.Hidden, typeof(TestCase));
private bool ContainsManagedMethodAndType => !StringUtils.IsNullOrWhiteSpace(ManagedMethod) && !StringUtils.IsNullOrWhiteSpace(ManagedType);
private string? ManagedType
{
get => GetPropertyValue<string>(ManagedTypeProperty, null);
set => SetPropertyAndResetId(ManagedTypeProperty, value);
}
private string? ManagedMethod
{
get => GetPropertyValue<string>(ManagedMethodProperty, null);
set => SetPropertyAndResetId(ManagedMethodProperty, value);
}
private string GetFullyQualifiedName() => ContainsManagedMethodAndType ? $"{ManagedType}.{ManagedMethod}" : FullyQualifiedName;
/// <inheritdoc/>
public override string ToString() => GetFullyQualifiedName();
}
/// <summary>
/// Well-known TestCase properties
/// </summary>
public static class TestCaseProperties
{
/// <summary>
/// These are the core Test properties and may be available in commandline/TeamBuild to filter tests.
/// These Property names should not be localized.
/// </summary>
private const string IdLabel = "Id";
private const string FullyQualifiedNameLabel = "FullyQualifiedName";
private const string NameLabel = "Name";
private const string ExecutorUriLabel = "Executor Uri";
private const string SourceLabel = "Source";
private const string FilePathLabel = "File Path";
private const string LineNumberLabel = "Line Number";
public static readonly TestProperty Id = TestProperty.Register("TestCase.Id", IdLabel, string.Empty, string.Empty, typeof(Guid), ValidateGuid, TestPropertyAttributes.Hidden, typeof(TestCase));
public static readonly TestProperty FullyQualifiedName = TestProperty.Register("TestCase.FullyQualifiedName", FullyQualifiedNameLabel, string.Empty, string.Empty, typeof(string), ValidateName, TestPropertyAttributes.Hidden, typeof(TestCase));
public static readonly TestProperty DisplayName = TestProperty.Register("TestCase.DisplayName", NameLabel, string.Empty, string.Empty, typeof(string), ValidateDisplay, TestPropertyAttributes.None, typeof(TestCase));
public static readonly TestProperty ExecutorUri = TestProperty.Register("TestCase.ExecutorUri", ExecutorUriLabel, string.Empty, string.Empty, typeof(Uri), ValidateExecutorUri, TestPropertyAttributes.Hidden, typeof(TestCase));
public static readonly TestProperty Source = TestProperty.Register("TestCase.Source", SourceLabel, typeof(string), typeof(TestCase));
public static readonly TestProperty CodeFilePath = TestProperty.Register("TestCase.CodeFilePath", FilePathLabel, typeof(string), typeof(TestCase));
public static readonly TestProperty LineNumber = TestProperty.Register("TestCase.LineNumber", LineNumberLabel, typeof(int), TestPropertyAttributes.Hidden, typeof(TestCase));
internal static TestProperty[] Properties { get; } =
[
CodeFilePath,
DisplayName,
ExecutorUri,
FullyQualifiedName,
Id,
LineNumber,
Source
];
private static bool ValidateName(object? value)
{
return !StringUtils.IsNullOrWhiteSpace((string?)value);
}
private static bool ValidateDisplay(object? value)
{
// only check for null and pass the rest up to UI for validation
return value != null;
}
private static bool ValidateExecutorUri(object? value)
{
return value != null;
}
private static bool ValidateGuid(object? value)
{
if (value?.ToString() is not string sValue)
{
return false;
}
// TODO: Replace with TryParse?
try
{
_ = new Guid(sValue);
return true;
}
catch (FormatException)
{
return false;
}
catch (OverflowException)
{
return false;
}
}
}