-
Notifications
You must be signed in to change notification settings - Fork 222
/
SymbolHelperTest.cs
571 lines (484 loc) · 24.8 KB
/
SymbolHelperTest.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
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
/*
* SonarAnalyzer for .NET
* Copyright (C) 2015-2022 SonarSource SA
* mailto: contact AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Moq;
using SonarAnalyzer.Common;
using CodeAnalysisAccessibility = Microsoft.CodeAnalysis.Accessibility; // This is needed because there is an Accessibility namespace in the windows forms binaries.
namespace SonarAnalyzer.UnitTest.Helpers
{
[TestClass]
public class SymbolHelperTest
{
internal const string TestInput = @"
namespace NS
{
using System.Collections.Generic;
using PropertyBag = System.Collections.Generic.Dictionary<string, object>;
public class Base
{
public class Nested
{
public class NestedMore
{}
}
public virtual void Method1() { }
protected virtual void Method2() { }
public abstract int Property { get; set; }
public void Method4(){}
}
private class Derived1 : Base
{
public override int Property { get; set; }
}
public class Derived2 : Base, IInterface
{
public override int Property { get; set; }
public int Property2 { get; set; }
public void Method3(){}
public abstract void Method5();
public void EventHandler(object o, System.EventArgs args){}
}
public interface IInterface
{
int Property2 { get; set; }
void Method3();
void Method4<T, V>(List<T> param1, List<int> param2, List<V> param3, IList<int> param4);
}
}
";
private SnippetCompiler testCode;
[TestInitialize]
public void Compile()
{
testCode = new SnippetCompiler(TestInput, ignoreErrors: true, language: AnalyzerLanguage.CSharp);
}
[TestMethod]
public void Symbol_IsPublicApi()
{
ISymbol symbol = testCode.GetMethodSymbol("Base.Method1");
SymbolHelper.IsPubliclyAccessible(symbol).Should().BeTrue();
symbol = testCode.GetMethodSymbol("Base.Method2");
symbol.IsPubliclyAccessible().Should().BeTrue();
symbol = testCode.GetPropertySymbol("Base.Property");
symbol.IsPubliclyAccessible().Should().BeTrue();
symbol = testCode.GetPropertySymbol("IInterface.Property2");
symbol.IsPubliclyAccessible().Should().BeTrue();
symbol = testCode.GetPropertySymbol("Derived1.Property");
symbol.IsPubliclyAccessible().Should().BeFalse();
}
[TestMethod]
public void Symbol_IsInterfaceImplementationOrMemberOverride()
{
ISymbol symbol = testCode.GetMethodSymbol("Base.Method1");
symbol.GetInterfaceMember().Should().BeNull();
symbol.GetOverriddenMember().Should().BeNull();
symbol = testCode.GetPropertySymbol("Derived2.Property");
symbol.GetOverriddenMember().Should().NotBeNull();
symbol = testCode.GetPropertySymbol("Derived2.Property2");
symbol.GetInterfaceMember().Should().NotBeNull();
symbol = testCode.GetMethodSymbol("Derived2.Method3");
symbol.GetInterfaceMember().Should().NotBeNull();
}
[TestMethod]
public void Symbol_TryGetOverriddenOrInterfaceMember()
{
var methodSymbol = testCode.GetMethodSymbol("Base.Method1");
var actualOverriddenMethod = methodSymbol.GetOverriddenMember();
actualOverriddenMethod.Should().BeNull();
var expectedOverriddenProperty = testCode.GetPropertySymbol("Base.Property");
var propertySymbol = testCode.GetPropertySymbol("Derived2.Property");
var actualOverriddenProperty = propertySymbol.GetOverriddenMember();
actualOverriddenProperty.Should().NotBeNull();
actualOverriddenProperty.Should().Be(expectedOverriddenProperty);
var expectedOverriddenMethod = testCode.GetMethodSymbol("IInterface.Method3");
methodSymbol = testCode.GetMethodSymbol("Derived2.Method3");
actualOverriddenMethod = methodSymbol.GetInterfaceMember();
actualOverriddenMethod.Should().NotBeNull();
actualOverriddenMethod.Should().Be(expectedOverriddenMethod);
}
[TestMethod]
public void Symbol_IsChangeable()
{
var symbol = testCode.GetMethodSymbol("Base.Method1");
symbol.IsChangeable().Should().BeFalse();
symbol = testCode.GetMethodSymbol("Base.Method4");
symbol.IsChangeable().Should().BeTrue();
symbol = testCode.GetMethodSymbol("Derived2.Method5");
symbol.IsChangeable().Should().BeFalse();
symbol = testCode.GetMethodSymbol("Derived2.Method3");
symbol.IsChangeable().Should().BeFalse();
}
[TestMethod]
public void Symbol_IsProbablyEventHandler()
{
var symbol = testCode.GetMethodSymbol("Derived2.Method3");
symbol.IsEventHandler().Should().BeFalse();
symbol = testCode.GetMethodSymbol("Derived2.EventHandler");
symbol.IsEventHandler().Should().BeTrue();
}
[TestMethod]
public void Symbol_GetSelfAndBaseTypes()
{
var objectType = testCode.GetTypeByMetadataName("System.Object");
var baseTypes = objectType.GetSelfAndBaseTypes().ToList();
baseTypes.Should().ContainSingle();
baseTypes.First().Should().Be(objectType);
var derived1Type = testCode.GetTypeSymbol("Derived1") as INamedTypeSymbol;
baseTypes = derived1Type.GetSelfAndBaseTypes().ToList();
baseTypes.Should().HaveCount(3);
baseTypes.Should().HaveElementAt(0, derived1Type);
baseTypes.Should().HaveElementAt(1, testCode.GetTypeSymbol("Base"));
baseTypes.Should().HaveElementAt(2, objectType);
}
[TestMethod]
public void Symbol_GetAllNamedTypes_Namespace()
{
var nsSymbol = testCode.GetNamespaceSymbol("NS");
var typeSymbols = nsSymbol.GetAllNamedTypes();
typeSymbols.Should().HaveCount(6);
}
[TestMethod]
public void Symbol_GetAllNamedTypes_Type()
{
var typeSymbol = testCode.GetTypeSymbol("Base") as INamedTypeSymbol;
var typeSymbols = typeSymbol.GetAllNamedTypes();
typeSymbols.Should().HaveCount(3);
}
[TestMethod]
public void Symbol_IsKnownType()
{
var method4 = (MethodDeclarationSyntax)testCode.GetMethodDeclaration("IInterface.Method4");
method4.ParameterList
.Parameters[0]
.Type
.IsKnownType(KnownType.System_Collections_Generic_List_T, testCode.SemanticModel)
.Should().BeTrue();
method4.ParameterList
.Parameters[1]
.Type
.IsKnownType(KnownType.System_Collections_Generic_List_T, testCode.SemanticModel)
.Should().BeTrue();
method4.ParameterList
.Parameters[2]
.Type
.IsKnownType(KnownType.System_Collections_Generic_List_T, testCode.SemanticModel)
.Should().BeTrue();
method4.ParameterList
.Parameters[3]
.Type
.IsKnownType(KnownType.System_Collections_Generic_List_T, testCode.SemanticModel)
.Should().BeFalse();
}
[TestMethod]
public void IsAnyAttributeInOverridingChain_WhenMethodSymbolIsNull_ReturnsFalse() =>
SymbolHelper.IsAnyAttributeInOverridingChain((IMethodSymbol)null).Should().BeFalse();
[TestMethod]
public void IsAnyAttributeInOverridingChain_WhenPropertySymbolIsNull_ReturnsFalse() =>
SymbolHelper.IsAnyAttributeInOverridingChain((IPropertySymbol)null).Should().BeFalse();
[TestMethod]
public void AnyAttributeDerivesFrom_WhenSymbolIsNull_ReturnsFalse() =>
SymbolHelper.AnyAttributeDerivesFrom(null, KnownType.Void).Should().BeFalse();
[TestMethod]
public void AnyAttributeDerivesFromAny_WhenSymbolIsNull_ReturnsFalse() =>
SymbolHelper.AnyAttributeDerivesFromAny(null, ImmutableArray.Create(KnownType.Void)).Should().BeFalse();
[TestMethod]
public void GetAttributesForKnownType_WhenSymbolIsNull_ReturnsEmpty() =>
SymbolHelper.GetAttributes(null, KnownType.Void).Should().BeEmpty();
[TestMethod]
public void GetAttributesForKnownTypes_WhenSymbolIsNull_ReturnsEmpty() =>
SymbolHelper.GetAttributes(null, ImmutableArray.Create(KnownType.Void)).Should().BeEmpty();
[TestMethod]
public void GetParameters_WhenSymbolIsNotMethodOrProperty_ReturnsEmpty() =>
Mock.Of<ISymbol>(x => x.Kind == SymbolKind.Alias).GetParameters().Should().BeEmpty();
[TestMethod]
public void GetInterfaceMember_WhenSymbolIsNull_ReturnsEmpty() =>
((ISymbol)null).GetInterfaceMember().Should().BeNull();
[TestMethod]
public void GetOverriddenMember_WhenSymbolIsNull_ReturnsEmpty() =>
((ISymbol)null).GetOverriddenMember().Should().BeNull();
[TestMethod]
public void GetAllNamedTypesForNamespace_WhenSymbolIsNull_ReturnsEmpty() =>
((INamespaceSymbol)null).GetAllNamedTypes().Should().BeEmpty();
[TestMethod]
public void GetAllNamedTypesForNamedType_WhenSymbolIsNull_ReturnsEmpty() =>
((INamedTypeSymbol)null).GetAllNamedTypes().Should().BeEmpty();
[TestMethod]
public void GetSelfAndBaseTypes_WhenSymbolIsNull_ReturnsEmpty() =>
((ITypeSymbol)null).GetSelfAndBaseTypes().Should().BeEmpty();
[TestMethod]
public void GetEffectiveAccessibility_WhenSymbolIsNull_ReturnsNotApplicable() =>
((ISymbol)null).GetEffectiveAccessibility().Should().Be(CodeAnalysisAccessibility.NotApplicable);
[DataTestMethod]
[DataRow(SymbolKind.Alias, "alias")]
[DataRow(SymbolKind.ArrayType, "array")]
[DataRow(SymbolKind.Assembly, "assembly")]
[DataRow(SymbolKind.Discard, "discard")]
[DataRow(SymbolKind.DynamicType, "dynamic")]
[DataRow(SymbolKind.ErrorType, "error")]
[DataRow(SymbolKind.Event, "event")]
[DataRow(SymbolKind.Field, "field")]
[DataRow(SymbolKind.FunctionPointerType, "function pointer")]
[DataRow(SymbolKind.Label, "label")]
[DataRow(SymbolKind.Local, "local")]
[DataRow(SymbolKind.Namespace, "namespace")]
[DataRow(SymbolKind.NetModule, "netmodule")]
[DataRow(SymbolKind.Parameter, "parameter")]
[DataRow(SymbolKind.PointerType, "pointer")]
[DataRow(SymbolKind.Preprocessing, "preprocessing")]
[DataRow(SymbolKind.Property, "property")]
[DataRow(SymbolKind.RangeVariable, "range variable")]
[DataRow(SymbolKind.TypeParameter, "type parameter")]
public void GetClassification_SimpleKinds(SymbolKind symbolKind, string expected)
{
var fakeSymbol = new Mock<ISymbol>();
fakeSymbol.Setup(x => x.Kind).Returns(symbolKind);
fakeSymbol.Object.GetClassification().Should().Be(expected);
}
[TestMethod]
public void GetClassification_UnknowKind()
{
var fakeSymbol = new Mock<ISymbol>();
fakeSymbol.Setup(x => x.Kind).Returns((SymbolKind)999);
#if DEBUG
new Action(() => fakeSymbol.Object.GetClassification()).Should().Throw<NotSupportedException>();
#else
fakeSymbol.Object.GetClassification().Should().Be("symbol");
#endif
}
[DataTestMethod]
[DataRow(TypeKind.Array, "array")]
[DataRow(TypeKind.Class, "class")]
[DataRow(TypeKind.Delegate, "delegate")]
[DataRow(TypeKind.Dynamic, "dynamic")]
[DataRow(TypeKind.Enum, "enum")]
[DataRow(TypeKind.Error, "error")]
[DataRow(TypeKind.FunctionPointer, "function pointer")]
[DataRow(TypeKind.Interface, "interface")]
[DataRow(TypeKind.Module, "module")]
[DataRow(TypeKind.Pointer, "pointer")]
[DataRow(TypeKind.Struct, "struct")]
[DataRow(TypeKind.Structure, "struct")]
[DataRow(TypeKind.Submission, "submission")]
[DataRow(TypeKind.TypeParameter, "type parameter")]
[DataRow(TypeKind.Unknown, "unknown")]
public void GetClassification_NamedTypes(TypeKind typeKind, string expected)
{
var fakeSymbol = new Mock<INamedTypeSymbol>();
fakeSymbol.Setup(x => x.Kind).Returns(SymbolKind.NamedType);
fakeSymbol.Setup(x => x.TypeKind).Returns(typeKind);
fakeSymbol.Setup(x => x.IsRecord).Returns(false);
fakeSymbol.Object.GetClassification().Should().Be(expected);
}
[TestMethod]
public void GetClassification_NamedType_Unknown()
{
var fakeSymbol = new Mock<INamedTypeSymbol>();
fakeSymbol.Setup(x => x.Kind).Returns(SymbolKind.NamedType);
fakeSymbol.Setup(x => x.TypeKind).Returns((TypeKind)255);
#if DEBUG
new Action(() => fakeSymbol.Object.GetClassification()).Should().Throw<NotSupportedException>();
#else
fakeSymbol.Object.GetClassification().Should().Be("type");
#endif
}
[DataTestMethod]
[DataRow(TypeKind.Class, "record")]
[DataRow(TypeKind.Struct, "record struct")]
public void GetClassification_Record(TypeKind typeKind, string expected)
{
var fakeSymbol = new Mock<INamedTypeSymbol>();
fakeSymbol.Setup(x => x.Kind).Returns(SymbolKind.NamedType);
fakeSymbol.Setup(x => x.TypeKind).Returns(typeKind);
fakeSymbol.Setup(x => x.IsRecord).Returns(true);
fakeSymbol.Object.GetClassification().Should().Be(expected);
}
[DataTestMethod]
[DataRow(MethodKind.AnonymousFunction, "method")]
[DataRow(MethodKind.BuiltinOperator, "operator")]
[DataRow(MethodKind.Constructor, "constructor")]
[DataRow(MethodKind.Conversion, "operator")]
[DataRow(MethodKind.DeclareMethod, "method")]
[DataRow(MethodKind.DelegateInvoke, "method")]
[DataRow(MethodKind.Destructor, "destructor")]
[DataRow(MethodKind.EventAdd, "method")]
[DataRow(MethodKind.EventRaise, "method")]
[DataRow(MethodKind.EventRemove, "method")]
[DataRow(MethodKind.ExplicitInterfaceImplementation, "method")]
[DataRow(MethodKind.FunctionPointerSignature, "method")]
[DataRow(MethodKind.LambdaMethod, "method")]
[DataRow(MethodKind.LocalFunction, "method")]
[DataRow(MethodKind.Ordinary, "method")]
[DataRow(MethodKind.PropertyGet, "getter")]
[DataRow(MethodKind.PropertySet, "setter")]
[DataRow(MethodKind.ReducedExtension, "method")]
[DataRow(MethodKind.SharedConstructor, "constructor")]
[DataRow(MethodKind.StaticConstructor, "constructor")]
[DataRow(MethodKind.UserDefinedOperator, "operator")]
public void GetClassification_Method(MethodKind methodKind, string expected)
{
var fakeSymbol = new Mock<IMethodSymbol>();
fakeSymbol.Setup(x => x.Kind).Returns(SymbolKind.Method);
fakeSymbol.Setup(x => x.MethodKind).Returns(methodKind);
fakeSymbol.Object.GetClassification().Should().Be(expected);
}
[DataTestMethod]
[DataRow("BaseClass<int> ", "VirtualMethod ", "InheritedAttribute", "DerivedInheritedAttribute", "DerivedNotInheritedAttribute", "UnannotatedAttribute", "NotInheritedAttribute")]
[DataRow("DerivedOpenGeneric<int>", "VirtualMethod ", "InheritedAttribute", "DerivedInheritedAttribute", "DerivedNotInheritedAttribute", "UnannotatedAttribute")]
[DataRow("DerivedClosedGeneric ", "VirtualMethod ", "InheritedAttribute", "DerivedInheritedAttribute", "DerivedNotInheritedAttribute", "UnannotatedAttribute")]
[DataRow("DerivedNoOverrides<int>", "VirtualMethod ", "InheritedAttribute", "DerivedInheritedAttribute", "DerivedNotInheritedAttribute", "UnannotatedAttribute", "NotInheritedAttribute")]
[DataRow("DerivedOpenGeneric<int>", "GenericVirtualMethod<int> ", "InheritedAttribute", "DerivedInheritedAttribute", "DerivedNotInheritedAttribute", "UnannotatedAttribute")]
[DataRow("DerivedClosedGeneric ", "GenericVirtualMethod<int> ", "InheritedAttribute", "DerivedInheritedAttribute", "DerivedNotInheritedAttribute", "UnannotatedAttribute")]
[DataRow("DerivedNoOverrides<int>", "GenericVirtualMethod<int> ", "InheritedAttribute", "DerivedInheritedAttribute", "DerivedNotInheritedAttribute", "UnannotatedAttribute", "NotInheritedAttribute")]
[DataRow("DerivedOpenGeneric<int>", "NonVirtualMethod ")]
[DataRow("DerivedClosedGeneric ", "NonVirtualMethod ")]
[DataRow("DerivedNoOverrides<int>", "NonVirtualMethod ", "InheritedAttribute", "DerivedInheritedAttribute", "DerivedNotInheritedAttribute", "UnannotatedAttribute", "NotInheritedAttribute")]
[DataRow("DerivedOpenGeneric<int>", "GenericNonVirtualMethod<int>")]
[DataRow("DerivedClosedGeneric ", "GenericNonVirtualMethod<int>")]
[DataRow("DerivedNoOverrides<int>", "GenericNonVirtualMethod<int>", "InheritedAttribute", "DerivedInheritedAttribute", "DerivedNotInheritedAttribute", "UnannotatedAttribute", "NotInheritedAttribute")]
public void GetAttributesWithInherited_MethodSymbol(string className, string methodName, params string[] expectedAttributes)
{
className = className.TrimEnd();
methodName = methodName.TrimEnd();
var code = $$"""
using System;
[AttributeUsage(AttributeTargets.All, Inherited = true)]
public class InheritedAttribute : Attribute { }
[AttributeUsage(AttributeTargets.All, Inherited = false)]
public class NotInheritedAttribute : Attribute { }
public class DerivedInheritedAttribute: InheritedAttribute { }
public class DerivedNotInheritedAttribute: NotInheritedAttribute { }
public class UnannotatedAttribute : Attribute { }
public class BaseClass<T1>
{
[Inherited]
[DerivedInherited]
[NotInherited]
[DerivedNotInherited]
[Unannotated]
public virtual void VirtualMethod() { }
[Inherited]
[DerivedInherited]
[NotInherited]
[DerivedNotInherited]
[Unannotated]
public void NonVirtualMethod() { }
[Inherited]
[DerivedInherited]
[NotInherited]
[DerivedNotInherited]
[Unannotated]
public void GenericNonVirtualMethod<T2>() { }
[Inherited]
[DerivedInherited]
[NotInherited]
[DerivedNotInherited]
[Unannotated]
public virtual void GenericVirtualMethod<T2>() { }
}
public class DerivedOpenGeneric<T1>: BaseClass<T1>
{
public override void VirtualMethod() { }
public new void NonVirtualMethod() { }
public new void GenericNonVirtualMethod<T2>() { }
public override void GenericVirtualMethod<T2>() { }
}
public class DerivedClosedGeneric: BaseClass<int>
{
public override void VirtualMethod() { }
public new void NonVirtualMethod() { }
public new void GenericNonVirtualMethod<T2>() { }
public override void GenericVirtualMethod<T2>() { }
}
public class DerivedNoOverrides<T>: BaseClass<T> { }
public class Program
{
public static void Main()
{
new {{className}}().{{methodName}}();
}
}
""";
var compiler = new SnippetCompiler(code);
var invocationExpression = compiler.GetNodes<InvocationExpressionSyntax>().Should().ContainSingle().Subject;
var method = compiler.GetSymbol<IMethodSymbol>(invocationExpression);
var actual = method.GetAttributesWithInherited().Select(x => x.AttributeClass.Name).ToList();
actual.Should().BeEquivalentTo(expectedAttributes);
// GetAttributesWithInherited should behave like MemberInfo.GetCustomAttributes from runtime reflection:
var type = compiler.EmitAssembly().GetType(className.Replace("<int>", "`1"), throwOnError: true);
var methodInfo = type.GetMethod(methodName.Replace("<int>", string.Empty));
methodInfo.GetCustomAttributes(inherit: true).Select(x => x.GetType().Name).Should().BeEquivalentTo(expectedAttributes);
}
[DataTestMethod]
[DataRow("BaseClass<int> ", "InheritedAttribute", "DerivedInheritedAttribute", "DerivedNotInheritedAttribute", "UnannotatedAttribute", "NotInheritedAttribute")]
[DataRow("DerivedOpenGeneric<int>", "InheritedAttribute", "DerivedInheritedAttribute", "DerivedNotInheritedAttribute", "UnannotatedAttribute")]
[DataRow("DerivedClosedGeneric ", "InheritedAttribute", "DerivedInheritedAttribute", "DerivedNotInheritedAttribute", "UnannotatedAttribute")]
[DataRow("Implement ")]
public void GetAttributesWithInherited_TypeSymbol(string className, params string[] expectedAttributes)
{
className = className.TrimEnd();
var code = $$"""
using System;
[AttributeUsage(AttributeTargets.All, Inherited = true)]
public class InheritedAttribute : Attribute { }
[AttributeUsage(AttributeTargets.All, Inherited = false)]
public class NotInheritedAttribute : Attribute { }
public class DerivedInheritedAttribute: InheritedAttribute { }
public class DerivedNotInheritedAttribute: NotInheritedAttribute { }
public class UnannotatedAttribute : Attribute { }
[Inherited]
[DerivedInherited]
[NotInherited]
[DerivedNotInherited]
[Unannotated]
public class BaseClass<T1> { }
[Inherited]
[DerivedInherited]
[NotInherited]
[DerivedNotInherited]
[Unannotated]
public interface IInterface { }
public class DerivedOpenGeneric<T1>: BaseClass<T1> { }
public class DerivedClosedGeneric: BaseClass<int> { }
public class Implement: IInterface { }
public class Program
{
public static void Main()
{
new {{className}}();
}
}
""";
var compiler = new SnippetCompiler(code);
var objectCreation = compiler.GetNodes<ObjectCreationExpressionSyntax>().Should().ContainSingle().Subject;
if (compiler.GetSymbol<IMethodSymbol>(objectCreation) is { MethodKind: MethodKind.Constructor, ReceiverType: { } receiver })
{
var actual = receiver.GetAttributesWithInherited().Select(x => x.AttributeClass.Name).ToList();
actual.Should().BeEquivalentTo(expectedAttributes);
}
else
{
Assert.Fail("Constructor could not be found.");
}
// GetAttributesWithInherited should behave like MemberInfo.GetCustomAttributes from runtime reflection:
var type = compiler.EmitAssembly().GetType(className.Replace("<int>", "`1"), throwOnError: true);
type.GetCustomAttributes(inherit: true).Select(x => x.GetType().Name).Should().BeEquivalentTo(expectedAttributes);
}
}
}