Skip to content

Commit 1e62f60

Browse files
Add test cases and fix for INTL0003 triggering on test methods with underscores
Co-authored-by: BenjaminMichaelis <[email protected]>
1 parent 12d24c9 commit 1e62f60

2 files changed

Lines changed: 123 additions & 0 deletions

File tree

IntelliTect.Analyzer/IntelliTect.Analyzer.Test/NamingMethodPascalTests.cs

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,99 @@ void IInterface.foo() { }
442442
VerifyCSharpDiagnostic(test, expected1, expected2);
443443
}
444444

445+
[TestMethod]
446+
[Description("Test method with underscores should not trigger INTL0003")]
447+
public void TestMethodWithUnderscores_MSTest_NoDiagnosticInformationReturned()
448+
{
449+
string test = @"
450+
using System;
451+
using Microsoft.VisualStudio.TestTools.UnitTesting;
452+
453+
namespace ConsoleApplication1
454+
{
455+
[TestClass]
456+
public class TypeName
457+
{
458+
[TestMethod]
459+
public void FooThing_IsFooThing_HasFooThing()
460+
{
461+
Assert.IsTrue(true);
462+
}
463+
}
464+
}";
465+
466+
VerifyCSharpDiagnostic(test);
467+
}
468+
469+
[TestMethod]
470+
[Description("Test method with underscores should not trigger INTL0003 - xUnit Fact")]
471+
public void TestMethodWithUnderscores_XunitFact_NoDiagnosticInformationReturned()
472+
{
473+
string test = @"
474+
using System;
475+
using Xunit;
476+
477+
namespace ConsoleApplication1
478+
{
479+
public class TypeName
480+
{
481+
[Fact]
482+
public void FooThing_IsFooThing_HasFooThing()
483+
{
484+
Assert.True(true);
485+
}
486+
}
487+
}";
488+
489+
VerifyCSharpDiagnostic(test);
490+
}
491+
492+
[TestMethod]
493+
[Description("Test method with underscores should not trigger INTL0003 - xUnit Theory")]
494+
public void TestMethodWithUnderscores_XunitTheory_NoDiagnosticInformationReturned()
495+
{
496+
string test = @"
497+
using System;
498+
using Xunit;
499+
500+
namespace ConsoleApplication1
501+
{
502+
public class TypeName
503+
{
504+
[Theory]
505+
public void FooThing_IsFooThing_HasFooThing()
506+
{
507+
Assert.True(true);
508+
}
509+
}
510+
}";
511+
512+
VerifyCSharpDiagnostic(test);
513+
}
514+
515+
[TestMethod]
516+
[Description("Test method with underscores should not trigger INTL0003 - NUnit Test")]
517+
public void TestMethodWithUnderscores_NUnitTest_NoDiagnosticInformationReturned()
518+
{
519+
string test = @"
520+
using System;
521+
using NUnit.Framework;
522+
523+
namespace ConsoleApplication1
524+
{
525+
public class TypeName
526+
{
527+
[Test]
528+
public void FooThing_IsFooThing_HasFooThing()
529+
{
530+
Assert.That(true, Is.True);
531+
}
532+
}
533+
}";
534+
535+
VerifyCSharpDiagnostic(test);
536+
}
537+
445538

446539
protected override CodeFixProvider GetCSharpCodeFixProvider()
447540
{

IntelliTect.Analyzer/IntelliTect.Analyzer/Analyzers/NamingMethodPascal.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,39 @@ private static void AnalyzeSymbol(SymbolAnalysisContext context)
9494
return;
9595
}
9696

97+
// Skip test methods - they commonly use underscores for readability (e.g., "Method_Scenario_ExpectedResult")
98+
if (IsTestMethod(namedTypeSymbol))
99+
{
100+
return;
101+
}
102+
97103
Diagnostic diagnostic = Diagnostic.Create(_Rule, namedTypeSymbol.Locations[0], name);
98104

99105
context.ReportDiagnostic(diagnostic);
100106
}
107+
108+
private static bool IsTestMethod(IMethodSymbol methodSymbol)
109+
{
110+
// Common test framework attributes
111+
string[] testAttributeNames =
112+
[
113+
"TestMethod", // MSTest
114+
"TestMethodAttribute",
115+
"Fact", // xUnit
116+
"FactAttribute",
117+
"Theory", // xUnit
118+
"TheoryAttribute",
119+
"Test", // NUnit
120+
"TestAttribute",
121+
"TestCase", // NUnit
122+
"TestCaseAttribute",
123+
"TestCaseSource", // NUnit
124+
"TestCaseSourceAttribute"
125+
];
126+
127+
ImmutableArray<AttributeData> attributes = methodSymbol.GetAttributes();
128+
return attributes.Any(attribute =>
129+
testAttributeNames.Contains(attribute.AttributeClass?.Name));
130+
}
101131
}
102132
}

0 commit comments

Comments
 (0)