| Name | Message | Date |
|---|---|---|
| 📄 FindCalleesShould.cs | 6 days ago | |
| 📄 FindCallersShould.cs | 6 days ago | |
| 📄 SourceToCallGraphHelper.cs | 6 days ago |
📄
core/tests/CallGraphBuilderTests/FindCalleesShould.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
using System.Threading.Tasks; using Microsoft.CodeAnalysis; using Shouldly; using Xunit; namespace Cfa.Tests.CallGraphBuilderTests; public sealed class FindCalleesShould { [Theory] [InlineData("Value")] [InlineData("this.Value")] public async Task FindExpressionBodiedProperties(string access) { // Arrange var source = $$""" class Example { int Value => 1; void Start() { _ = {{access}}; } } """; // Act var (start, calls) = await source.FindCallees(); // Assert calls[start].ShouldHaveSingleItem().Target.Name.ShouldBe("Value"); } [Fact] public async Task FollowMethodCallsInsideProperties() { // Arrange const string source = """ class Example { int Value => GetValue(); int GetValue() => 1; void Start() { _ = Value; } } """; // Act var (start, calls) = await source.FindCallees(); // Assert var property = calls[start].ShouldHaveSingleItem(); property.Target.Name.ShouldBe("Value"); calls[property.Target].ShouldHaveSingleItem().Target.Name.ShouldBe("GetValue"); } [Theory] [InlineData("int Value { get { return 1; } }", "_ = Value;")] [InlineData("int Value { get => 1; }", "_ = Value;")] [InlineData("int Value { set { } }", "Value = 1;")] public async Task FindPropertiesWithAccessorBodies(string declaration, string access) { // Arrange var source = $$""" class Example { {{declaration}} void Start() { {{access}} } } """; // Act var (start, calls) = await source.FindCallees(); // Assert calls[start].ShouldHaveSingleItem().Target.Name.ShouldBe("Value"); } [Theory] [InlineData("set { Save(value); }", "Value = 1;")] [InlineData("set { Save(value); }", "this.Value = 1;")] [InlineData("set => Save(value);", "Value = 1;")] [InlineData("set => Save(value);", "this.Value = 1;")] public async Task FollowAssignedPropertiesAndMethodCallsInsideTheirSetters(string setter, string assignment) { // Arrange var source = $$""" class Example { int Value { {{setter}} } void Save(int value) { } void Start() { {{assignment}} } } """; // Act var (start, calls) = await source.FindCallees(); // Assert var property = calls[start].ShouldHaveSingleItem(); property.Target.Name.ShouldBe("Value"); calls[property.Target].ShouldHaveSingleItem().Target.Name.ShouldBe("Save"); } [Theory] [InlineData("_ = this.Value;")] [InlineData("this.Value = 2;")] public async Task IgnoreAutoPropertiesEvenWhenTheyHaveInitializers(string access) { // Arrange var source = $$""" class Example { int Value { get; set; } = 1; void Start() { {{access}} } } """; // Act var (start, calls) = await source.FindCallees(); // Assert calls[start].ShouldBeEmpty(); } [Theory] [InlineData("int this[int index] => Read();", "_ = this[0];", "Read")] [InlineData("int this[int index] => Read();", "_ = this?[0];", "Read")] [InlineData("int this[int index] { get { return Read(); } }", "_ = this[0];", "Read")] [InlineData("int this[int index] { get => Read(); }", "_ = this[0];", "Read")] [InlineData("int this[int index] { set { Save(value); } }", "this[0] = 1;", "Save")] [InlineData("int this[int index] { set => Save(value); }", "this[0] = 1;", "Save")] public async Task FollowIndexersWithBodies(string declaration, string access, string method) { // Arrange var source = $$""" class Example { {{declaration}} int Read() => 1; void Save(int value) { } void Start() { {{access}} } } """; // Act var (start, calls) = await source.FindCallees(); // Assert var indexer = calls[start].ShouldHaveSingleItem(); indexer.Target.ShouldBeAssignableTo<IPropertySymbol>().IsIndexer.ShouldBeTrue(); calls[indexer.Target].ShouldHaveSingleItem().Target.Name.ShouldBe(method); } [Fact] public async Task IgnoreIndexersWithoutBodies() { // Arrange const string source = """ abstract class Example { public abstract int this[int index] { get; set; } void Start() { _ = this[0]; this[0] = 1; } } """; // Act var (start, calls) = await source.FindCallees(); // Assert calls[start].ShouldBeEmpty(); } [Theory] [InlineData("void Run();", "public void Run() => Helper();", "service.Run();")] [InlineData("void Run();", "void IService.Run() => Helper();", "service.Run();")] [InlineData("int Value { get; }", "public int Value => Helper();", "_ = service.Value;")] [InlineData("int Value { get; }", "int IService.Value => Helper();", "_ = service.Value;")] [InlineData("int this[int index] { get; }", "public int this[int index] => Helper();", "_ = service[0];")] public async Task FollowSourceImplementationsOfInterfaces(string contract, string implementation, string access) { // Arrange var source = $$""" interface IService { {{contract}} } class Example : IService { {{implementation}} int Helper() => 1; void Start(IService service) { {{access}} } } """; // Act var (start, calls) = await source.FindCallees(); // Assert var call = calls[start].ShouldHaveSingleItem(); call.Target.ContainingType.Name.ShouldBe("Example"); calls[call.Target].ShouldHaveSingleItem().Target.Name.ShouldBe("Helper"); } [Fact] public async Task NotListTheAbstractMemberOnlyTheImplementation() { // Arrange const string source = """ interface IService { void Run(); } class Example : IService { public void Run() { } void Start(IService service) { service.Run(); } } """; // Act var (start, calls) = await source.FindCallees(); // Assert var call = calls[start].ShouldHaveSingleItem(); call.Target.ContainingType.Name.ShouldBe("Example"); } [Fact] public async Task FollowImplementationsOfInterfacesFromMetadata() { // Arrange const string source = """ class Example : System.IDisposable { public void Dispose() => Cleanup(); void Cleanup() { } void Start(System.IDisposable service) { service.Dispose(); } } """; // Act var (start, calls) = await source.FindCallees(); // Assert var call = calls[start].ShouldHaveSingleItem(); call.Target.Name.ShouldBe("Dispose"); call.LinkKind.ShouldBe(LinkKind.Abstract); calls[call.Target].ShouldHaveSingleItem().Target.Name.ShouldBe("Cleanup"); } [Theory] [InlineData("service.Run(); Run();")] [InlineData("Run(); service.Run();")] public async Task PreferDirectCallsOverInterfaceLinks(string body) { // Arrange var source = $$""" interface IService { void Run(); } class Example : IService { public void Run() { } void Start(IService service) { {{body}} } } """; // Act var (start, calls) = await source.FindCallees(); // Assert var call = calls[start].ShouldHaveSingleItem(); call.LinkKind.ShouldBe(LinkKind.Direct); } [Fact] public async Task IgnoreInterfaceImplementationsWithoutPropertyBodies() { // Arrange const string source = """ interface IService { int Value { get; } } class Example : IService { public int Value { get; set; } void Start(IService service) { _ = service.Value; } } """; // Act var (start, calls) = await source.FindCallees(); // Assert calls[start].ShouldBeEmpty(); } [Theory] [InlineData("public abstract void Run();", "public override void Run() => Helper();", "service.Run();")] [InlineData("public abstract int Value { get; }", "public override int Value => Helper();", "_ = service.Value;")] [InlineData( "public abstract int this[int index] { get; }", "public override int this[int index] => Helper();", "_ = service[0];" )] public async Task FollowOverridesOfAbstractMembers(string contract, string implementation, string access) { // Arrange var source = $$""" abstract class Service { {{contract}} } class Example : Service { {{implementation}} int Helper() => 1; void Start(Service service) { {{access}} } } """; // Act var (start, calls) = await source.FindCallees(); // Assert var call = calls[start].ShouldHaveSingleItem(); call.Target.ContainingType.Name.ShouldBe("Example"); calls[call.Target].ShouldHaveSingleItem().Target.Name.ShouldBe("Helper"); } [Fact] public async Task FollowOverridesThroughIntermediateAbstractClasses() { // Arrange const string source = """ abstract class Service { public abstract void Run(); } abstract class Middle : Service { public abstract override void Run(); } class Example : Middle { public override void Run() { } void Start(Service service) { service.Run(); } } """; // Act var (start, calls) = await source.FindCallees(); // Assert var call = calls[start].ShouldHaveSingleItem(); call.Target.ContainingType.Name.ShouldBe("Example"); } [Theory] [InlineData("service.Run(); Run();")] [InlineData("Run(); service.Run();")] public async Task PreferDirectCallsOverAbstractDispatch(string body) { // Arrange var source = $$""" abstract class Service { public abstract void Run(); } class Example : Service { public override void Run() { } void Start(Service service) { {{body}} } } """; // Act var (start, calls) = await source.FindCallees(); // Assert var call = calls[start].ShouldHaveSingleItem(); call.LinkKind.ShouldBe(LinkKind.Direct); } [Fact] public async Task FollowVirtualMemberImplementations() { // Arrange const string source = """ class Service { public virtual void Run() { } } class Example : Service { public override void Run() { } void Start(Service service) { service.Run(); } } """; // Act var (start, calls) = await source.FindCallees(); // Assert calls[start] .ShouldContain(call => call.Target.ContainingType.Name == "Service" || call.LinkKind == LinkKind.Direct); calls[start] .ShouldContain(call => call.Target.ContainingType.Name == "Example" || call.LinkKind == LinkKind.Abstract); } [Fact] public async Task NotFollowVirtualMemberImplementation_WhenOverrideIsCalledDirectly() { // Arrange const string source = """ class Service { public virtual void Run() { } } class Example : Service { public override void Run() { } void Start() { Run(); } } """; // Act var (start, calls) = await source.FindCallees(); // Assert var call = calls[start].ShouldHaveSingleItem(); call.Target.ContainingType.Name.ShouldBe("Example"); call.LinkKind.ShouldBe(LinkKind.Direct); } [Theory] [InlineData("new Service()")] [InlineData("new()")] public async Task FollowConstructorsAndTheirMethodCalls(string creation) { // Arrange var source = $$""" class Service { public Service() { Initialize(); } void Initialize() { } } class Example { void Start() { Service service = {{creation}}; } } """; // Act var (start, calls) = await source.FindCallees(); // Assert var constructor = calls[start].ShouldHaveSingleItem(); constructor.Target.ShouldBeAssignableTo<IMethodSymbol>().MethodKind.ShouldBe(MethodKind.Constructor); constructor.LinkKind.ShouldBe(LinkKind.Direct); calls[constructor.Target].ShouldHaveSingleItem().Target.Name.ShouldBe("Initialize"); } [Theory] [InlineData("this")] [InlineData("base")] public async Task FollowChainedConstructors(string initializer) { // Arrange var source = $$""" class Parent { public Parent() { } public Parent(int value) { } } class Service : Parent { public Service() : {{initializer}}(1) { } public Service(int value) { } } class Example { void Start() { _ = new Service(); } } """; // Act var (start, calls) = await source.FindCallees(); // Assert var constructor = calls[start].ShouldHaveSingleItem(); var chained = calls[constructor.Target].ShouldHaveSingleItem(); var method = chained.Target.ShouldBeAssignableTo<IMethodSymbol>(); method.MethodKind.ShouldBe(MethodKind.Constructor); method.Parameters.ShouldHaveSingleItem().Type.SpecialType.ShouldBe(SpecialType.System_Int32); method.ContainingType.Name.ShouldBe(initializer is "this" ? "Service" : "Parent"); chained.LinkKind.ShouldBe(LinkKind.Direct); } [Fact] public async Task IgnoreConstructorsWithoutSourceDeclarations() { // Arrange const string source = """ class Service { } class Example { void Start() { _ = new Service(); _ = new object(); } } """; // Act var (start, calls) = await source.FindCallees(); // Assert calls[start].ShouldBeEmpty(); } }