Repository navigation
Expand file tree
/
Copy pathDynamicDataAttributeTests.cs
More file actions
789 lines (674 loc) · 32.9 KB
/
Copy pathDynamicDataAttributeTests.cs
File metadata and controls
789 lines (674 loc) · 32.9 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
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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using AwesomeAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting.Resources;
using TestFramework.ForTestingMSTest;
namespace Microsoft.VisualStudio.TestPlatform.TestFramework.UnitTests.Attributes;
public class DynamicDataAttributeTests : TestContainer
{
private readonly DummyTestClass _dummyTestClass;
private readonly MethodInfo _testMethodInfo;
private DynamicDataAttribute _dynamicDataAttribute;
public DynamicDataAttributeTests()
{
_dummyTestClass = new DummyTestClass();
_testMethodInfo = _dummyTestClass.GetType().GetTypeInfo().GetDeclaredMethod("TestMethod1")!;
_dynamicDataAttribute = new DynamicDataAttribute("ReusableTestDataProperty");
}
public void ReflectionArgumentsArePassedInSourceOrderWithoutFlatteningReturnedRows()
{
var attribute = new DynamicDataAttribute(nameof(ReflectionPartitionSources.Arguments), typeof(ReflectionPartitionSources), 7, "label");
object[][] actual = attribute.GetData(_testMethodInfo).ToArray();
actual.Should().HaveCount(2);
actual[0].Should().Equal(7, "label");
actual[1].Should().Equal("label", 8);
}
public void ReflectionScalarEnumerablesWrapEachElementAndPreserveNullAndEmptyPartitions()
{
foreach (string member in new[] { nameof(ReflectionPartitionSources.Scalars), nameof(ReflectionPartitionSources.ScalarMethod) })
{
object[][] actual = new DynamicDataAttribute(member, typeof(ReflectionPartitionSources)).GetData(_testMethodInfo).ToArray();
actual.Should().HaveCount(3);
actual[0].Should().Equal(17);
actual[1].Should().HaveCount(1);
actual[1][0].Should().BeNull();
actual[2].Should().Equal("text");
}
new DynamicDataAttribute(nameof(ReflectionPartitionSources.EmptyScalars), typeof(ReflectionPartitionSources)).GetData(_testMethodInfo).Should().BeEmpty();
}
public void ReflectionExplicitMissingMembersAndInvalidKindReportExactSource()
{
foreach (DynamicDataSourceType kind in new[] { DynamicDataSourceType.Property, DynamicDataSourceType.Method, DynamicDataSourceType.Field })
{
var attribute = new DynamicDataAttribute("absent", typeof(ReflectionPartitionSources), kind);
Action action = () => attribute.GetData(_testMethodInfo);
action.Should().ThrowExactly<ArgumentNullException>().Which.ParamName.Should().Be(kind + " absent");
}
var invalid = new DynamicDataAttribute("invalid-kind", typeof(ReflectionPartitionSources), (DynamicDataSourceType)999);
Action unsupported = () => invalid.GetData(_testMethodInfo);
unsupported.Should().ThrowExactly<ArgumentNullException>().Which.ParamName.Should().Be(
string.Format(CultureInfo.InvariantCulture, FrameworkMessages.DynamicDataValueNull, "invalid-kind", typeof(ReflectionPartitionSources).FullName));
}
public void ReflectionMethodsRejectInstanceOpenGenericAndParamsLayoutsBeforeInvocation()
{
foreach (string member in new[]
{
nameof(ReflectionPartitionSources.InstanceMethod),
nameof(ReflectionPartitionSources.GenericMethod),
nameof(ReflectionPartitionSources.ParamsMethod),
#if NET9_0_OR_GREATER
nameof(ReflectionPartitionSources.ParamsCollection),
#endif
})
{
var attribute = new DynamicDataAttribute(member, typeof(ReflectionPartitionSources), DynamicDataSourceType.Method);
Action action = () => attribute.GetData(_testMethodInfo);
action.Should().ThrowExactly<NotSupportedException>().Which.Message
.Should().Contain(nameof(ReflectionPartitionSources) + "." + member);
}
}
public void ReflectionPropertiesRejectInstanceOrSetterOnlyLayoutsAndPreserveInvokedExceptions()
{
foreach (string member in new[] { nameof(ReflectionPartitionSources.InstanceProperty), nameof(ReflectionPartitionSources.SetterOnly) })
{
var attribute = new DynamicDataAttribute(member, typeof(ReflectionPartitionSources), DynamicDataSourceType.Property);
Action action = () => attribute.GetData(_testMethodInfo);
action.Should().ThrowExactly<NotSupportedException>().Which.Message.Should().Contain(member);
}
foreach (string member in new[] { nameof(ReflectionPartitionSources.ThrowingProperty), nameof(ReflectionPartitionSources.ThrowingMethod) })
{
var attribute = new DynamicDataAttribute(member, typeof(ReflectionPartitionSources));
Action action = () => attribute.GetData(_testMethodInfo);
action.Should().ThrowExactly<TargetInvocationException>().Which.InnerException!
.Should().BeOfType<InvalidOperationException>().Which.Message.Should().Be("source sentinel");
}
}
public void ReflectionPlainNonEnumerableSourceCannotBeInventedAsOneScalarRow()
{
var attribute = new DynamicDataAttribute(nameof(ReflectionPartitionSources.NonEnumerable), typeof(ReflectionPartitionSources));
Action action = () => attribute.GetData(_testMethodInfo);
ArgumentNullException error = action.Should().ThrowExactly<ArgumentNullException>().Which;
error.ParamName.Should().Be(string.Format(
CultureInfo.InvariantCulture,
FrameworkMessages.DynamicDataIEnumerableNull,
nameof(ReflectionPartitionSources.NonEnumerable),
typeof(ReflectionPartitionSources).FullName));
}
private sealed class ReflectionPartitionSources
{
public static IEnumerable<object[]> Arguments(int number, string label) => [[number, label], [label, number + 1]];
public static IEnumerable Scalars => new object?[] { 17, null, "text" };
public static IEnumerable ScalarMethod() => new object?[] { 17, null, "text" };
public static IEnumerable EmptyScalars => Array.Empty<int>();
public static object NonEnumerable => new();
public IEnumerable<object[]> InstanceMethod() => throw new InvalidOperationException("Must be rejected before invocation.");
public static IEnumerable<object[]> GenericMethod<T>() => throw new InvalidOperationException("Must be rejected before invocation.");
public static IEnumerable<object[]> ParamsMethod(params int[] values) => throw new InvalidOperationException("Must be rejected before invocation.");
#if NET9_0_OR_GREATER
public static IEnumerable<object[]> ParamsCollection(params IEnumerable<int> values) => throw new InvalidOperationException("Must be rejected before invocation.");
#endif
public IEnumerable<object[]> InstanceProperty => throw new InvalidOperationException("Must be rejected before invocation.");
public static IEnumerable<object[]> SetterOnly
{
set
{
_ = value;
throw new InvalidOperationException("Must be rejected before invocation.");
}
}
public static IEnumerable<object[]> ThrowingProperty => throw new InvalidOperationException("source sentinel");
public static IEnumerable<object[]> ThrowingMethod() => throw new InvalidOperationException("source sentinel");
}
public void GetDataShouldThrowExceptionIfInvalidPropertyNameIsSpecifiedOrPropertyDoesNotExist()
{
_dynamicDataAttribute = new DynamicDataAttribute("ABC");
Action action = () => _dynamicDataAttribute.GetData(_testMethodInfo);
action.Should().Throw<InvalidOperationException>()
.WithMessage(string.Format(CultureInfo.InvariantCulture, FrameworkMessages.DynamicDataSourceShouldExistAndBeValid, "ABC", _testMethodInfo.DeclaringType!.FullName));
}
public void GetDataShouldReadDataFromProperty()
{
MethodInfo methodInfo = _dummyTestClass.GetType().GetTypeInfo().GetDeclaredMethod("TestMethod1")!;
_dynamicDataAttribute = new DynamicDataAttribute("ReusableTestDataProperty");
IEnumerable<object[]> data = _dynamicDataAttribute.GetData(methodInfo);
data.Should().NotBeNull();
data.ToList().Should().HaveCount(2);
}
public void GetDataShouldReadDataFromPropertyInDifferentClass()
{
MethodInfo methodInfo = _dummyTestClass.GetType().GetTypeInfo().GetDeclaredMethod("TestMethod1")!;
_dynamicDataAttribute = new DynamicDataAttribute("ReusableTestDataProperty2", typeof(DummyTestClass2));
IEnumerable<object[]> data = _dynamicDataAttribute.GetData(methodInfo);
data.Should().NotBeNull();
data.ToList().Should().HaveCount(2);
}
public void GetDataShouldReadDataFromMethod()
{
MethodInfo methodInfo = _dummyTestClass.GetType().GetTypeInfo().GetDeclaredMethod("TestMethod2")!;
_dynamicDataAttribute = new DynamicDataAttribute("ReusableTestDataMethod", DynamicDataSourceType.Method);
IEnumerable<object[]> data = _dynamicDataAttribute.GetData(methodInfo);
data.Should().NotBeNull();
data.ToList().Should().HaveCount(2);
}
public void GetDataShouldReadDataFromMethodInDifferentClass()
{
MethodInfo methodInfo = _dummyTestClass.GetType().GetTypeInfo().GetDeclaredMethod("TestMethod2")!;
_dynamicDataAttribute = new DynamicDataAttribute("ReusableTestDataMethod2", typeof(DummyTestClass2), DynamicDataSourceType.Method);
IEnumerable<object[]> data = _dynamicDataAttribute.GetData(methodInfo);
data.Should().NotBeNull();
data.ToList().Should().HaveCount(2);
}
public void GetDataShouldThrowExceptionIfPropertyReturnsNull()
{
Action action = () =>
{
MethodInfo methodInfo = _dummyTestClass.GetType().GetTypeInfo().GetDeclaredMethod("TestMethod4")!;
_dynamicDataAttribute = new DynamicDataAttribute("NullProperty", typeof(DummyTestClass));
_dynamicDataAttribute.GetData(methodInfo);
};
action.Should().Throw<ArgumentNullException>();
}
public void GetDataShouldNotThrowExceptionIfPropertyReturnsEmpty()
{
MethodInfo methodInfo = _dummyTestClass.GetType().GetTypeInfo().GetDeclaredMethod("TestMethod5")!;
_dynamicDataAttribute = new DynamicDataAttribute("EmptyProperty", typeof(DummyTestClass));
IEnumerable<object[]> data = _dynamicDataAttribute.GetData(methodInfo);
// The callers in AssemblyEnumerator and TestMethodRunner are responsible
// for throwing an exception if data is empty and ConsiderEmptyDataSourceAsInconclusive is false.
data.Should().BeEmpty();
}
public void GetDataShouldThrowExceptionIfPropertyDoesNotReturnCorrectType()
{
Action action = () =>
{
MethodInfo methodInfo = _dummyTestClass.GetType().GetTypeInfo().GetDeclaredMethod("TestMethod3")!;
_dynamicDataAttribute = new DynamicDataAttribute("WrongDataTypeProperty", typeof(DummyTestClass));
_dynamicDataAttribute.GetData(methodInfo);
};
action.Should().Throw<ArgumentNullException>();
}
public void GetDisplayNameShouldReturnDisplayName()
{
object[] data = [1, 2, 3];
string? displayName = _dynamicDataAttribute.GetDisplayName(_testMethodInfo, data);
displayName.Should().Be("TestMethod1 (1,2,3)");
}
public void GetDisplayNameShouldReturnDisplayNameWithDynamicDataDisplayName()
{
object[] data = [1, 2, 3];
_dynamicDataAttribute.DynamicDataDisplayName = "GetCustomDynamicDataDisplayName";
string? displayName = _dynamicDataAttribute.GetDisplayName(_testMethodInfo, data);
displayName.Should().Be("DynamicDataTestWithDisplayName TestMethod1 with 3 parameters");
}
public void GetDisplayNameShouldReturnDisplayNameWithDynamicDataDisplayNameInDifferentClass()
{
object[] data = [1, 2, 3];
_dynamicDataAttribute.DynamicDataDisplayName = "GetCustomDynamicDataDisplayName2";
_dynamicDataAttribute.DynamicDataDisplayNameDeclaringType = typeof(DummyTestClass2);
string? displayName = _dynamicDataAttribute.GetDisplayName(_testMethodInfo, data);
displayName.Should().Be("DynamicDataTestWithDisplayName TestMethod1 with 3 parameters");
}
public void GetDisplayNameShouldThrowExceptionWithDynamicDataDisplayNameMethodMissingParameters()
{
Action action = () =>
{
object[] data = [1, 2, 3];
_dynamicDataAttribute.DynamicDataDisplayName = "GetDynamicDataDisplayNameWithMissingParameters";
_dynamicDataAttribute.GetDisplayName(_testMethodInfo, data);
};
action.Should().Throw<ArgumentNullException>();
}
public void GetDisplayNameShouldThrowExceptionWithDynamicDataDisplayNameMethodInvalidReturnType()
{
Action action = () =>
{
object[] data = [1, 2, 3];
_dynamicDataAttribute.DynamicDataDisplayName = "GetDynamicDataDisplayNameWithInvalidReturnType";
_dynamicDataAttribute.GetDisplayName(_testMethodInfo, data);
};
action.Should().Throw<ArgumentNullException>();
}
public void GetDisplayNameShouldThrowExceptionWithDynamicDataDisplayNameMethodInvalidFirstParameterType()
{
Action action = () =>
{
object[] data = [1, 2, 3];
_dynamicDataAttribute.DynamicDataDisplayName = "GetDynamicDataDisplayNameWithInvalidFirstParameterType";
_dynamicDataAttribute.GetDisplayName(_testMethodInfo, data);
};
action.Should().Throw<ArgumentNullException>();
}
public void GetDisplayNameShouldThrowExceptionWithDynamicDataDisplayNameMethodInvalidSecondParameterType()
{
Action action = () =>
{
object[] data = [1, 2, 3];
_dynamicDataAttribute.DynamicDataDisplayName = "GetDynamicDataDisplayNameWithInvalidSecondParameterType";
_dynamicDataAttribute.GetDisplayName(_testMethodInfo, data);
};
action.Should().Throw<ArgumentNullException>();
}
public void GetDisplayNameShouldThrowExceptionWithDynamicDataDisplayNameMethodNonStatic()
{
Action action = () =>
{
object[] data = [1, 2, 3];
_dynamicDataAttribute.DynamicDataDisplayName = "GetDynamicDataDisplayNameNonStatic";
_dynamicDataAttribute.GetDisplayName(_testMethodInfo, data);
};
action.Should().Throw<ArgumentNullException>();
}
public void GetDisplayNameShouldThrowExceptionWithDynamicDataDisplayNameMethodPrivate()
{
Action action = () =>
{
object[] data = [1, 2, 3];
_dynamicDataAttribute.DynamicDataDisplayName = "GetDynamicDataDisplayNamePrivate";
_dynamicDataAttribute.GetDisplayName(_testMethodInfo, data);
};
action.Should().Throw<ArgumentNullException>();
}
public void GetDisplayNameShouldThrowExceptionWithMissingDynamicDataDisplayNameMethod()
{
Action action = () =>
{
object[] data = [1, 2, 3];
_dynamicDataAttribute.DynamicDataDisplayName = "MissingCustomDynamicDataDisplayName";
_dynamicDataAttribute.GetDisplayName(_testMethodInfo, data);
};
action.Should().Throw<ArgumentNullException>();
}
public void GetDisplayNameShouldReturnEmptyStringIfDataIsNull()
{
string? displayName = _dynamicDataAttribute.GetDisplayName(_testMethodInfo, null);
displayName.Should().BeNull();
}
public void GetDisplayNameHandlesNullValues()
{
string?[] data = ["value1", "value2", null];
string?[] data1 = [null, "value1", "value2"];
string?[] data2 = ["value1", null, "value2"];
string? displayName = _dynamicDataAttribute.GetDisplayName(_testMethodInfo, data);
displayName.Should().Be("TestMethod1 (\"value1\",\"value2\",null)");
displayName = _dynamicDataAttribute.GetDisplayName(_testMethodInfo, data1);
displayName.Should().Be("TestMethod1 (null,\"value1\",\"value2\")");
displayName = _dynamicDataAttribute.GetDisplayName(_testMethodInfo, data2);
displayName.Should().Be("TestMethod1 (\"value1\",null,\"value2\")");
}
public void GetDisplayNameForArrayOfMultipleItems()
{
string? displayName = _dynamicDataAttribute.GetDisplayName(_testMethodInfo, [new[] { "a", "b", "c" }]);
displayName.Should().Be("TestMethod1 ([\"a\",\"b\",\"c\"])");
}
public void GetDisplayNameForMultipleArraysOfOneItem()
{
string? displayName = _dynamicDataAttribute.GetDisplayName(_testMethodInfo, [new[] { "a" }, new[] { "1" }]);
displayName.Should().Be("TestMethod1 ([\"a\"],[\"1\"])");
}
public void GetDisplayNameForMultipleArraysOfMultipleItems()
{
string? displayName = _dynamicDataAttribute.GetDisplayName(_testMethodInfo, [new[] { "a", "b", "c" }, new[] { "1", "2", "3" }]);
displayName.Should().Be("TestMethod1 ([\"a\",\"b\",\"c\"],[\"1\",\"2\",\"3\"])");
}
public void GetDisplayNameForMultipleArraysOfMultipleItemsValueTypes()
{
string? displayName = _dynamicDataAttribute.GetDisplayName(_testMethodInfo, [new[] { 1, 2, 3 }, new[] { 4, 5, 6 }]);
displayName.Should().Be("TestMethod1 ([1,2,3],[4,5,6])");
}
public void GetDisplayNameForMultipleArraysOfArraysOfMultipleItems()
{
string? displayName = _dynamicDataAttribute.GetDisplayName(_testMethodInfo, [new[] { ["a", "b", "c"], ["d", "e", "f"], new[] { "gh", "ij", "kl" } }, new[] { 'm', 'n', 'o' }, new[] { ["1", "2", "3"], ["4", "5", "6"], new[] { "7", "8", "9" } }]);
displayName.Should().Be("TestMethod1 ([[\"a\",\"b\",\"c\"],[\"d\",\"e\",\"f\"],[\"gh\",\"ij\",\"kl\"]],['m','n','o'],[[\"1\",\"2\",\"3\"],[\"4\",\"5\",\"6\"],[\"7\",\"8\",\"9\"]])");
}
public void DynamicDataSource_WithTuple_Works()
{
MethodInfo testMethodInfo = new TestClassTupleData().GetType().GetTypeInfo().GetDeclaredMethod(nameof(TestClassTupleData.DynamicDataTestWithTuple))!;
var dynamicDataAttribute = new DynamicDataAttribute(nameof(TestClassTupleData.DataWithTuple), typeof(TestClassTupleData));
dynamicDataAttribute.GetData(testMethodInfo);
dynamicDataAttribute = new DynamicDataAttribute(nameof(TestClassTupleData.GetDataWithTuple), typeof(TestClassTupleData), DynamicDataSourceType.Method);
dynamicDataAttribute.GetData(testMethodInfo);
}
public void DynamicDataSource_WithValueTuple_Works()
{
MethodInfo testMethodInfo = new TestClassTupleData().GetType().GetTypeInfo().GetDeclaredMethod(nameof(TestClassTupleData.DynamicDataTestWithTuple))!;
var dynamicDataAttribute = new DynamicDataAttribute(nameof(TestClassTupleData.DataWithValueTuple), typeof(TestClassTupleData));
dynamicDataAttribute.GetData(testMethodInfo);
dynamicDataAttribute = new DynamicDataAttribute(nameof(TestClassTupleData.GetDataWithValueTuple), typeof(TestClassTupleData), DynamicDataSourceType.Method);
dynamicDataAttribute.GetData(testMethodInfo);
}
public void DynamicDataSource_WithValueTupleWithTupleSyntax_Works()
{
MethodInfo testMethodInfo = new TestClassTupleData().GetType().GetTypeInfo().GetDeclaredMethod(nameof(TestClassTupleData.DynamicDataTestWithTuple))!;
var dynamicDataAttribute = new DynamicDataAttribute(nameof(TestClassTupleData.DataWithValueTupleWithTupleSyntax), typeof(TestClassTupleData));
dynamicDataAttribute.GetData(testMethodInfo);
dynamicDataAttribute = new DynamicDataAttribute(nameof(TestClassTupleData.GetDataWithValueTupleWithTupleSyntax), typeof(TestClassTupleData), DynamicDataSourceType.Method);
dynamicDataAttribute.GetData(testMethodInfo);
}
public void GetDataShouldReadDataFromField()
{
MethodInfo methodInfo = _dummyTestClass.GetType().GetTypeInfo().GetDeclaredMethod("TestMethod1")!;
_dynamicDataAttribute = new DynamicDataAttribute("ReusableTestDataField", DynamicDataSourceType.Field);
IEnumerable<object[]> data = _dynamicDataAttribute.GetData(methodInfo);
data.Should().NotBeNull();
data.ToList().Should().HaveCount(2);
}
public void GetDataShouldReadDataFromFieldInDifferentClass()
{
MethodInfo methodInfo = _dummyTestClass.GetType().GetTypeInfo().GetDeclaredMethod("TestMethod1")!;
_dynamicDataAttribute = new DynamicDataAttribute("ReusableTestDataField2", typeof(DummyTestClass2), DynamicDataSourceType.Field);
IEnumerable<object[]> data = _dynamicDataAttribute.GetData(methodInfo);
data.Should().NotBeNull();
data.ToList().Should().HaveCount(2);
}
public void GetDataShouldReadDataFromFieldInAutoDetectMode()
{
MethodInfo methodInfo = _dummyTestClass.GetType().GetTypeInfo().GetDeclaredMethod("TestMethod1")!;
_dynamicDataAttribute = new DynamicDataAttribute("ReusableTestDataField");
IEnumerable<object[]> data = _dynamicDataAttribute.GetData(methodInfo);
data.Should().NotBeNull();
data.ToList().Should().HaveCount(2);
}
public void GetDataShouldThrowExceptionIfFieldReturnsNull()
{
Action action = () =>
{
MethodInfo methodInfo = _dummyTestClass.GetType().GetTypeInfo().GetDeclaredMethod("TestMethod1")!;
_dynamicDataAttribute = new DynamicDataAttribute("NullField", typeof(DummyTestClass), DynamicDataSourceType.Field);
_dynamicDataAttribute.GetData(methodInfo);
};
action.Should().Throw<ArgumentNullException>();
}
public void GetDataShouldNotThrowExceptionIfFieldReturnsEmpty()
{
MethodInfo methodInfo = _dummyTestClass.GetType().GetTypeInfo().GetDeclaredMethod("TestMethod1")!;
_dynamicDataAttribute = new DynamicDataAttribute("EmptyField", typeof(DummyTestClass), DynamicDataSourceType.Field);
IEnumerable<object[]> data = _dynamicDataAttribute.GetData(methodInfo);
// The callers in AssemblyEnumerator and TestMethodRunner are responsible
// for throwing an exception if data is empty and ConsiderEmptyDataSourceAsInconclusive is false.
data.Should().BeEmpty();
}
public void GetDataShouldThrowExceptionIfFieldDoesNotReturnCorrectType()
{
Action action = () =>
{
MethodInfo methodInfo = _dummyTestClass.GetType().GetTypeInfo().GetDeclaredMethod("TestMethod1")!;
_dynamicDataAttribute = new DynamicDataAttribute("WrongDataTypeField", typeof(DummyTestClass), DynamicDataSourceType.Field);
_dynamicDataAttribute.GetData(methodInfo);
};
action.Should().Throw<ArgumentNullException>();
}
public void GetDataShouldThrowExceptionIfFieldIsNotStatic()
{
Action action = () =>
{
MethodInfo methodInfo = _dummyTestClass.GetType().GetTypeInfo().GetDeclaredMethod("TestMethod1")!;
_dynamicDataAttribute = new DynamicDataAttribute("NonStaticField", typeof(DummyTestClass), DynamicDataSourceType.Field);
_dynamicDataAttribute.GetData(methodInfo);
};
action.Should().Throw<NotSupportedException>();
}
/// <summary>
/// The dummy test class.
/// </summary>
[TestClass]
[SuppressMessage("StyleCop.CSharp.MaintainabilityRules", "SA1401:Fields should be private", Justification = "Test use case")]
internal class DummyTestClass
{
/// <summary>
/// Gets the reusable test data property.
/// </summary>
public static IEnumerable<object[]> ReusableTestDataProperty => [[1, 2, 3], [4, 5, 6]];
/// <summary>
/// Gets the null test data property.
/// </summary>
public static IEnumerable<object[]> NullProperty => null!;
/// <summary>
/// Gets the empty test data property.
/// </summary>
public static IEnumerable<object[]> EmptyProperty => [];
/// <summary>
/// Gets the wrong test data property i.e. Property returning something other than
/// expected data type of <see cref="IEnumerable{T}"/>.
/// </summary>
public static string WrongDataTypeProperty => "Dummy";
/// <summary>
/// The reusable test data field.
/// </summary>
public static IEnumerable<object[]> ReusableTestDataField = [[1, 2, 3], [4, 5, 6]];
/// <summary>
/// The null test data field.
/// </summary>
public static IEnumerable<object[]> NullField = null!;
/// <summary>
/// The empty test data field.
/// </summary>
public static IEnumerable<object[]> EmptyField = [];
/// <summary>
/// The wrong test data field i.e. Field returning something other than
/// expected data type of <see cref="IEnumerable{T}"/>.
/// </summary>
public static string WrongDataTypeField = "Dummy";
/// <summary>
/// Non-static field that should cause an error.
/// </summary>
public IEnumerable<object[]> NonStaticField = [[1, 2, 3]];
/// <summary>
/// The reusable test data method.
/// </summary>
/// <returns>
/// The <see cref="IEnumerable{T}"/>.
/// </returns>
public static IEnumerable<object[]> ReusableTestDataMethod() => [[1, 2, 3], [4, 5, 6]];
/// <summary>
/// The custom display name method.
/// </summary>
/// <param name="methodInfo">
/// The method info of test method.
/// </param>
/// <param name="data">
/// The test data which is passed to test method.
/// </param>
/// <returns>
/// The <see cref="string"/>.
/// </returns>
public static string GetCustomDynamicDataDisplayName(MethodInfo methodInfo, object[] data)
=> $"DynamicDataTestWithDisplayName {methodInfo.Name} with {data.Length} parameters";
/// <summary>
/// Custom display name method with missing parameters.
/// </summary>
/// <returns>
/// The <see cref="string"/>.
/// </returns>
public static string GetDynamicDataDisplayNameWithMissingParameters() => throw new InvalidOperationException();
/// <summary>
/// Custom display name method with invalid return type.
/// </summary>
public static void GetDynamicDataDisplayNameWithInvalidReturnType() => throw new InvalidOperationException();
/// <summary>
/// Custom display name method with invalid first parameter type.
/// </summary>
/// <param name="methodInfo">
/// The method info of test method.
/// </param>
/// <param name="data">
/// The test data which is passed to test method.
/// </param>
/// <returns>
/// The <see cref="string"/>.
/// </returns>
public static string GetDynamicDataDisplayNameWithInvalidFirstParameterType(string methodInfo, object[] data) => throw new InvalidOperationException();
/// <summary>
/// Custom display name method with invalid second parameter.
/// </summary>
/// <param name="methodInfo">
/// The method info of test method.
/// </param>
/// <param name="data">
/// The test data which is passed to test method.
/// </param>
/// <returns>
/// The <see cref="string"/>.
/// </returns>
public static string GetDynamicDataDisplayNameWithInvalidSecondParameterType(MethodInfo methodInfo, string data) => throw new InvalidOperationException();
/// <summary>
/// Custom display name method that is not static.
/// </summary>
/// <param name="methodInfo">
/// The method info of test method.
/// </param>
/// <param name="data">
/// The test data which is passed to test method.
/// </param>
/// <returns>
/// The <see cref="string"/>.
/// </returns>
public string GetDynamicDataDisplayNameNonStatic(MethodInfo methodInfo, object[] data) => throw new InvalidOperationException();
/// <summary>
/// The test method 1.
/// </summary>
[TestMethod]
[DynamicData("ReusableTestDataProperty")]
public void TestMethod1()
{
}
/// <summary>
/// The test method 2.
/// </summary>
[TestMethod]
[DynamicData("ReusableTestDataMethod")]
public void TestMethod2()
{
}
/// <summary>
/// The test method 3.
/// </summary>
[TestMethod]
[DynamicData("WrongDataTypeProperty")]
public void TestMethod3()
{
}
/// <summary>
/// The test method 4.
/// </summary>
[TestMethod]
[DynamicData("NullProperty")]
public void TestMethod4()
{
}
/// <summary>
/// The test method 5.
/// </summary>
[TestMethod]
[DynamicData("EmptyProperty")]
public void TestMethod5()
{
}
/// <summary>
/// DataRow test method 1.
/// </summary>
[DataRow("First", "Second", null)]
[DataRow(null, "First", "Second")]
[DataRow("First", null, "Second")]
[TestMethod]
public void DataRowTestMethod()
{
}
/// <summary>
/// Custom display name method that is private.
/// </summary>
/// <param name="methodInfo">
/// The method info of test method.
/// </param>
/// <param name="data">
/// The test data which is passed to test method.
/// </param>
/// <returns>
/// The <see cref="string"/>.
/// </returns>
#pragma warning disable IDE0051 // Remove unused private members
private static string GetDynamicDataDisplayNamePrivate(MethodInfo methodInfo, object[] data) => throw new InvalidOperationException();
}
[SuppressMessage("StyleCop.CSharp.MaintainabilityRules", "SA1401:Fields should be private", Justification = "For testing the use case")]
[SuppressMessage("Usage", "CA2211:Non-constant fields should not be visible", Justification = "For testing the use case")]
public class DummyTestClass2
{
/// <summary>
/// Gets the reusable test data property.
/// </summary>
public static IEnumerable<object[]> ReusableTestDataProperty2 => [[1, 2, 3], [4, 5, 6]];
/// <summary>
/// The reusable test data method.
/// </summary>
/// <returns>
/// The <see cref="IEnumerable"/>.
/// </returns>
public static IEnumerable<object[]> ReusableTestDataMethod2() => [[1, 2, 3], [4, 5, 6]];
/// <summary>
/// The reusable test data field.
/// </summary>
public static IEnumerable<object[]> ReusableTestDataField2 = [[1, 2, 3], [4, 5, 6]];
/// <summary>
/// The custom display name method.
/// </summary>
/// <param name="methodInfo">
/// The method info of test method.
/// </param>
/// <param name="data">
/// The test data which is passed to test method.
/// </param>
/// <returns>
/// The <see cref="string"/>.
/// </returns>
public static string GetCustomDynamicDataDisplayName2(MethodInfo methodInfo, object[] data)
=> $"DynamicDataTestWithDisplayName {methodInfo.Name} with {data.Length} parameters";
}
[TestClass]
internal class TestClassTupleData
{
public static IEnumerable<Tuple<int, string>> GetDataWithTuple()
{
yield return new(0, "0");
yield return new(1, "1");
}
public static IEnumerable<Tuple<int, string>> DataWithTuple
{
get
{
yield return new(0, "0");
yield return new(1, "1");
}
}
[SuppressMessage("StyleCop.CSharp.ReadabilityRules", "SA1141:Use tuple syntax", Justification = "We want to explicitly test this syntax")]
public static IEnumerable<ValueTuple<int, string>> GetDataWithValueTuple()
{
yield return new(0, "0");
yield return new(1, "1");
}
[SuppressMessage("StyleCop.CSharp.ReadabilityRules", "SA1141:Use tuple syntax", Justification = "We want to explicitly test this syntax")]
public static IEnumerable<ValueTuple<int, string>> DataWithValueTuple
{
get
{
yield return new(0, "0");
yield return new(1, "1");
}
}
public static IEnumerable<(int Integer, string AsString)> GetDataWithValueTupleWithTupleSyntax()
{
yield return (0, "0");
yield return (1, "1");
}
public static IEnumerable<(int Integer, string AsString)> DataWithValueTupleWithTupleSyntax
{
get
{
yield return (0, "0");
yield return (1, "1");
}
}
[DataTestMethod]
public void DynamicDataTestWithTuple(int value, string integerAsString)
{
}
}
}