Skip to content

Commit c6baeef

Browse files
authored
fix: add clarifying comment and tests for PublicMethodMissingTestAttributeAnalyzer (#4863) (#4912)
The MethodKind.Ordinary filter already correctly excludes property accessors (PropertyGet/PropertySet), event accessors (EventAdd/EventRemove), constructors, destructors, and operators. Added a clarifying comment to document this behavior and new test cases for properties, events, overrides, and static methods to prevent future regressions.
1 parent ce025a4 commit c6baeef

2 files changed

Lines changed: 104 additions & 1 deletion

File tree

TUnit.Analyzers.Tests/PublicMethodMissingTestAttributeAnalyzerTests.cs

Lines changed: 101 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ public class MyClass : IAsyncDisposable
143143
public void MyTest()
144144
{
145145
}
146-
146+
147147
public ValueTask DisposeAsync()
148148
{
149149
return ValueTask.CompletedTask;
@@ -152,4 +152,104 @@ public ValueTask DisposeAsync()
152152
"""
153153
);
154154
}
155+
156+
[Test]
157+
public async Task Property_Getter_Setter_No_Error()
158+
{
159+
await Verifier
160+
.VerifyAnalyzerAsync(
161+
"""
162+
using TUnit.Core;
163+
164+
public class MyClass
165+
{
166+
[Test]
167+
public void MyTest()
168+
{
169+
}
170+
171+
public string? Name { get; set; }
172+
173+
public int Age
174+
{
175+
get { return 0; }
176+
set { }
177+
}
178+
}
179+
"""
180+
);
181+
}
182+
183+
[Test]
184+
public async Task Event_Accessors_No_Error()
185+
{
186+
await Verifier
187+
.VerifyAnalyzerAsync(
188+
"""
189+
using System;
190+
using TUnit.Core;
191+
192+
public class MyClass
193+
{
194+
[Test]
195+
public void MyTest()
196+
{
197+
}
198+
199+
public event EventHandler MyEvent
200+
{
201+
add { }
202+
remove { }
203+
}
204+
}
205+
"""
206+
);
207+
}
208+
209+
[Test]
210+
public async Task Override_Method_No_Error()
211+
{
212+
await Verifier
213+
.VerifyAnalyzerAsync(
214+
"""
215+
using TUnit.Core;
216+
217+
public class MyClass
218+
{
219+
[Test]
220+
public void MyTest()
221+
{
222+
}
223+
224+
public override string ToString()
225+
{
226+
return "test";
227+
}
228+
}
229+
"""
230+
);
231+
}
232+
233+
[Test]
234+
public async Task Static_Method_No_Error()
235+
{
236+
await Verifier
237+
.VerifyAnalyzerAsync(
238+
"""
239+
using TUnit.Core;
240+
241+
public class MyClass
242+
{
243+
[Test]
244+
public void MyTest()
245+
{
246+
}
247+
248+
public static void StaticHelper()
249+
{
250+
}
251+
}
252+
"""
253+
);
254+
}
155255
}

TUnit.Analyzers/PublicMethodMissingTestAttributeAnalyzer.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ private void AnalyzeSymbol(SymbolAnalysisContext context)
3232
return;
3333
}
3434

35+
// MethodKind.Ordinary excludes property accessors (PropertyGet/PropertySet),
36+
// event accessors (EventAdd/EventRemove), constructors, destructors, operators,
37+
// and other compiler-generated method kinds.
3538
foreach (var method in methods
3639
.Where(x => x.MethodKind == MethodKind.Ordinary)
3740
.Where(x => !x.IsAbstract)

0 commit comments

Comments
 (0)