| Name | Message | Date |
|---|---|---|
| 📄 FindCalleesShould.cs | 7 days ago |
📄
core/tests/CalleeBuilderTests/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
using System.Collections.Immutable; using System.Linq; using System.Threading; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp; using Shouldly; using Xunit; namespace Cfa.Tests.CalleeBuilderTests; public sealed class FindCalleesShould { [Theory] [InlineData("Value")] [InlineData("this.Value")] public void FindExpressionBodiedProperties(string access) { // Arrange var source = $$""" class Example { int Value => 1; void Start() { _ = {{access}}; } } """; // Act var callees = FindCallees(source); // Assert callees.ShouldHaveSingleItem().Target.Name.ShouldBe("Value"); } [Fact] public void FollowMethodCallsInsideProperties() { // Arrange const string source = """ class Example { int Value => GetValue(); int GetValue() => 1; void Start() { _ = Value; } } """; // Act var callees = FindCallees(source); // Assert var property = callees.ShouldHaveSingleItem(); property.Target.Name.ShouldBe("Value"); property.Callees.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 void FindPropertiesWithAccessorBodies(string declaration, string access) { // Arrange var source = $$""" class Example { {{declaration}} void Start() { {{access}} } } """; // Act var callees = FindCallees(source); // Assert callees.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 void FollowAssignedPropertiesAndMethodCallsInsideTheirSetters(string setter, string assignment) { // Arrange var source = $$""" class Example { int Value { {{setter}} } void Save(int value) { } void Start() { {{assignment}} } } """; // Act var callees = FindCallees(source); // Assert var property = callees.ShouldHaveSingleItem(); property.Target.Name.ShouldBe("Value"); property.Callees.ShouldHaveSingleItem().Target.Name.ShouldBe("Save"); } [Theory] [InlineData("_ = this.Value;")] [InlineData("this.Value = 2;")] public void IgnoreAutoPropertiesEvenWhenTheyHaveInitializers(string access) { // Arrange var source = $$""" class Example { int Value { get; set; } = 1; void Start() { {{access}} } } """; // Act var callees = FindCallees(source); // Assert callees.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 void 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 callees = FindCallees(source); // Assert var indexer = callees.ShouldHaveSingleItem(); indexer.Target.ShouldBeAssignableTo<IPropertySymbol>().IsIndexer.ShouldBeTrue(); indexer.Callees.ShouldHaveSingleItem().Target.Name.ShouldBe(method); } [Fact] public void 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 callees = FindCallees(source); // Assert callees.ShouldBeEmpty(); } private static IImmutableList<Callee> FindCallees(string source) { var compilation = CSharpCompilation.Create( "Example", [CSharpSyntaxTree.ParseText(source)], [MetadataReference.CreateFromFile(typeof(object).Assembly.Location)], new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary) ); var start = compilation.GetTypeByMetadataName("Example")!.GetMembers("Start").Single(); return new CalleeBuilder([compilation]).FindCallees(start, CancellationToken.None); } }