Skip to content

Commit

Permalink
Add C#12 reproducers for rules T (#8142)
Browse files Browse the repository at this point in the history
  • Loading branch information
cristian-ambrosini-sonarsource authored Oct 5, 2023
1 parent 40b2e83 commit 0a11018
Show file tree
Hide file tree
Showing 11 changed files with 104 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,15 @@ public void TestClassShouldHaveTestMethod_CSharp11() =>
.AddReferences(NuGetMetadataReference.NUnit(Constants.NuGetLatestVersion))
.Verify();

[DataTestMethod]
public void TestClassShouldHaveTestMethod_CSharp12() =>
builder
.WithOptions(ParseOptionsHelper.FromCSharp12)
.AddPaths("TestClassShouldHaveTestMethod.CSharp12.cs")
.AddReferences(NuGetMetadataReference.MSTestTestFramework(Constants.NuGetLatestVersion))
.AddReferences(NuGetMetadataReference.NUnit(Constants.NuGetLatestVersion))
.Verify();

#endif

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ public void ThreadStaticWithInitializer_CSharp11() =>
.WithOptions(ParseOptionsHelper.FromCSharp11)
.Verify();

[TestMethod]
public void ThreadStaticWithInitializer_CSharp12() =>
builder.AddPaths("ThreadStaticWithInitializer.CSharp12.cs")
.WithOptions(ParseOptionsHelper.FromCSharp12)
.Verify();

#endif

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ public void TooManyGenericParameters_CSharp10() =>
public void TooManyGenericParameters_CSharp11() =>
builder.AddPaths("TooManyGenericParameters.CSharp11.cs").WithOptions(ParseOptionsHelper.FromCSharp11).Verify();

[TestMethod]
public void TooManyGenericParameters_CSharp12() =>
builder.AddPaths("TooManyGenericParameters.CSharp12.cs").WithOptions(ParseOptionsHelper.FromCSharp12).Verify();

#endif

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ public void TooManyParameters_CS_CustomValues_CSharp11() =>
.WithOptions(ParseOptionsHelper.FromCSharp11)
.Verify();

[TestMethod]
public void TooManyParameters_CS_CustomValues_CSharp12() =>
builderCSMax3.AddPaths("TooManyParameters_CustomValues.CSharp12.cs")
.WithOptions(ParseOptionsHelper.FromCSharp12)
.Verify();

#endif

[TestMethod]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,22 @@ namespace SonarAnalyzer.UnitTest.Rules
[TestClass]
public class TypeExaminationOnSystemTypeTest
{
private readonly VerifierBuilder builder = new VerifierBuilder<TypeExaminationOnSystemType>();

[TestMethod]
public void TypeExaminationOnSystemType() =>
new VerifierBuilder<TypeExaminationOnSystemType>().AddPaths("TypeExaminationOnSystemType.cs").Verify();
builder.AddPaths("TypeExaminationOnSystemType.cs").Verify();

#if NET

[TestMethod]
public void TypeExaminationOnSystemType_CSharp12() =>
builder.AddPaths("TypeExaminationOnSystemType.CSharp12.cs")
.WithTopLevelStatements()
.WithOptions(ParseOptionsHelper.FromCSharp12)
.Verify();

#endif

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using NUnit.Framework;

[TestClass]
class ClassTest1(int primaryConstructor) // Noncompliant
// ^^^^^^^^^^
{
}

[TestFixture]
class ClassTest2(int primaryConstructor) // Noncompliant
// ^^^^^^^^^^
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;
using Point2D = (int, int);

public class ThreadStaticWithInitializer
{
public class Foo
{
[ThreadStatic]
public static int[] PerThreadArray = [1, 2, 3]; // Noncompliant

[ThreadStatic]
public static Point2D PerThreadPoint = new Point2D(); // Noncompliant
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class Bar<T1, T2, T3>(string classParam) // Noncompliant
{
void Method()
{
bool GenericLambda<T1, T2, T3, T4>(T1 lambdaParam = default(T1)) => true; // Noncompliant
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,10 @@ namespace MyLib
{
public abstract class FrameworkBaseClass<T1, T2, T3> // Noncompliant
{

void Method()
{
bool GenericLambda<T1, T2, T3, T4>() => true; // Noncompliant
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System;

public class MyWrongClass(int p1, int p2, int p3, int p4) { } // FN {{Constructor has 4 parameters, which is greater than the 3 authorized.}}

public class SubClass(int p1, int p2, int p3, int p4) : MyWrongClass(p1, p2, p3, p4) { } // Compliant: base class requires them

public class SubClass2() : MyWrongClass(1, 2, 3, 4) // Compliant
{
public SubClass2(int p1, int p2, int p3, int p4, int p5) : this() { } // Noncompliant

void Method()
{
var a = (int p1 = 1, int p2 = 2, int p3 = 3, int p4 = 4) => true; // Noncompliant {{Lambda has 4 parameters, which is greater than the 3 authorized.}}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System;
using Point2D = (int, int);

Point2D p = new(1, 2);

if (p.GetType().IsInstanceOfType(typeof(Point2D))) // Noncompliant
{ /* ... */ }

typeof(Point2D).IsInstanceOfType("42"); // Compliant
typeof(Point2D).IsInstanceOfType(typeof(Point2D)); // Noncompliant

0 comments on commit 0a11018

Please sign in to comment.