Skip to content

Commit

Permalink
Add C#9 tests for S1186, S1116, S2291 , S2197, S4035, S4220 (#3749)
Browse files Browse the repository at this point in the history
  • Loading branch information
andrei-epure-sonarsource authored Nov 16, 2020
1 parent 2850f21 commit 530c8f4
Show file tree
Hide file tree
Showing 16 changed files with 305 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
using csharp::SonarAnalyzer.Rules.CSharp;
using SonarAnalyzer.UnitTest.MetadataReferences;
using SonarAnalyzer.UnitTest.TestFramework;
using Microsoft.CodeAnalysis;

namespace SonarAnalyzer.UnitTest.Rules
{
Expand All @@ -41,6 +42,15 @@ public void EmptyMethod()
options: ParseOptionsHelper.FromCSharp8);
}

#if NET5_0
[TestMethod]
[TestCategory("Rule")]
public void EmptyMethod_CSharp9()
{
Verifier.VerifyAnalyzerFromCSharp9Console(@"TestCases\EmptyMethod.CSharp9.cs", new EmptyMethod());
}
#endif

[TestMethod]
[TestCategory("CodeFix")]
public void EmptyMethod_CodeFix_Throw()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using csharp::SonarAnalyzer.Rules.CSharp;
using SonarAnalyzer.UnitTest.TestFramework;
using Microsoft.CodeAnalysis;

namespace SonarAnalyzer.UnitTest.Rules
{
Expand All @@ -30,20 +31,32 @@ public class EmptyStatementTest
{
[TestMethod]
[TestCategory("Rule")]
public void EmptyStatement()
{
public void EmptyStatement() =>
Verifier.VerifyAnalyzer(@"TestCases\EmptyStatement.cs", new EmptyStatement());
}

[TestMethod]
[TestCategory("Rule")]
public void EmptyStatement_CSharp9() =>
Verifier.VerifyAnalyzerFromCSharp9Console(@"TestCases\EmptyStatement.CSharp9.cs", new EmptyStatement());

[TestMethod]
[TestCategory("CodeFix")]
public void EmptyStatement_CodeFix()
{
public void EmptyStatement_CodeFix() =>
Verifier.VerifyCodeFix(
@"TestCases\EmptyStatement.cs",
@"TestCases\EmptyStatement.Fixed.cs",
new EmptyStatement(),
new EmptyStatementCodeFixProvider());
}

[TestMethod]
[TestCategory("CodeFix")]
public void EmptyStatement_CodeFix_CSharp9() =>
Verifier.VerifyCodeFix(
@"TestCases\EmptyStatement.CSharp9.cs",
@"TestCases\EmptyStatement.CSharp9.Fixed.cs",
new EmptyStatement(),
new EmptyStatementCodeFixProvider(),
ParseOptionsHelper.FromCSharp9,
OutputKind.ConsoleApplication);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,12 @@ public class EnumerableSumInUncheckedTest
{
[TestMethod]
[TestCategory("Rule")]
public void EnumerableSumInUnchecked()
{
public void EnumerableSumInUnchecked() =>
Verifier.VerifyAnalyzer(@"TestCases\EnumerableSumInUnchecked.cs", new EnumerableSumInUnchecked());
}

[TestMethod]
[TestCategory("Rule")]
public void EnumerableSumInUnchecked_CSharp9() =>
Verifier.VerifyAnalyzerFromCSharp9Console(@"TestCases\EnumerableSumInUnchecked.CSharp9.cs", new EnumerableSumInUnchecked());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,13 @@ public class EqualityOnModulusTest
{
[TestMethod]
[TestCategory("Rule")]
public void EqualityOnModulus()
{
public void EqualityOnModulus() =>
Verifier.VerifyAnalyzer(@"TestCases\EqualityOnModulus.cs", new EqualityOnModulus());
}

[TestMethod]
[TestCategory("Rule")]
public void EqualityOnModulus_CSharp9() =>
Verifier.VerifyAnalyzerFromCSharp9Console(@"TestCases\EqualityOnModulus.CSharp9.cs", new EqualityOnModulus());

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,12 @@ public class EquatableClassShouldBeSealedTest
{
[TestMethod]
[TestCategory("Rule")]
public void EquatableClassShouldBeSealed()
{
Verifier.VerifyAnalyzer(@"TestCases\EquatableClassShouldBeSealed.cs",
new EquatableClassShouldBeSealed());
}
public void EquatableClassShouldBeSealed() =>
Verifier.VerifyAnalyzer(@"TestCases\EquatableClassShouldBeSealed.cs", new EquatableClassShouldBeSealed());

[TestMethod]
[TestCategory("Rule")]
public void EquatableClassShouldBeSealed_CSharp9() =>
Verifier.VerifyAnalyzerFromCSharp9Library(@"TestCases\EquatableClassShouldBeSealed.CSharp9.cs", new EquatableClassShouldBeSealed());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,14 @@ public class EventHandlerDelegateShouldHaveProperArgumentsTest
{
[TestMethod]
[TestCategory("Rule")]
public void EventHandlerDelegateShouldHaveProperArguments()
{
public void EventHandlerDelegateShouldHaveProperArguments() =>
Verifier.VerifyAnalyzer(@"TestCases\EventHandlerDelegateShouldHaveProperArguments.cs",
new EventHandlerDelegateShouldHaveProperArguments());
}

[TestMethod]
[TestCategory("Rule")]
public void EventHandlerDelegateShouldHaveProperArguments_CSharp9() =>
Verifier.VerifyAnalyzerFromCSharp9Library(@"TestCases\EventHandlerDelegateShouldHaveProperArguments.CSharp9.cs",
new EventHandlerDelegateShouldHaveProperArguments());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
using System;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

void Empty() { } // FN

void WithComment()
{
// because
}

void NotEmpty()
{
Console.WriteLine();
}

record EmptyMethod
{
void F2()
{
// Do nothing because of X and Y.
}

void F3()
{
Console.WriteLine();
}

[Conditional("DEBUG")]
void F4() // Noncompliant {{Add a nested comment explaining why this method is empty, throw a 'NotSupportedException' or complete the implementation.}}
{
}

protected virtual void F5()
{
}

extern void F6();

[DllImport("avifil32.dll")]
private static extern void F7();
}

abstract record MyR
{
void F1() { } // Noncompliant
public abstract void F2();
}

record MyR2 : MyR
{
public override void F2()
{
}
}

class WithProp
{
public string Prop
{
init { } // FN https://github.com/SonarSource/sonar-dotnet/issues/3753
}
}

class M
{
[ModuleInitializer]
internal static void M1() // Noncompliant
{
}

[ModuleInitializer]
internal static void M2()
{
// reason
}

[ModuleInitializer]
internal static void M3()
{
Console.WriteLine();
}
}

namespace D
{
partial class C
{
public partial void Foo();
public partial void Bar();
public partial void Qix();
}

partial class C
{
public partial void Foo() { } // Noncompliant

public partial void Bar()
{
// comment
}

public partial void Qix()
{
Console.WriteLine();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,12 @@ public virtual void F2() { }

public abstract void F3();
}

public class WithProp
{
public string Prop
{
set { } // FN https://github.com/SonarSource/sonar-dotnet/issues/3753
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,12 @@ public virtual void F2() { }

public abstract void F3();
}

public class WithProp
{
public string Prop
{
set { } // FN https://github.com/SonarSource/sonar-dotnet/issues/3753
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,12 @@ public virtual void F2() { }

public abstract void F3();
}

public class WithProp
{
public string Prop
{
set { } // FN https://github.com/SonarSource/sonar-dotnet/issues/3753
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;

; // Fixed
; // Fixed
Console.WriteLine();
while (true)
; // Fixed

record R
{
string P
{
init
{

}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;

; // Noncompliant {{Remove this empty statement.}}
; // Noncompliant
Console.WriteLine();
while (true)
; // Noncompliant

record R
{
string P
{
init
{
; // Noncompliant
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;

List<int> list = new();
int d = unchecked(list.Sum()); // Noncompliant {{Refactor this code to handle 'OverflowException'.}}
unchecked
{
int e = list.Sum(); // Noncompliant
e = Enumerable.Sum(list); // Noncompliant
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System;
using System.Collections.Generic;

nint x = 100;
var y = x % 2 == 1; // Noncompliant; if x is negative, x % 2 == -1
y = x % 2 != -1; // Noncompliant {{The result of this modulus operation may not be negative.}}
y = 1 == x % 2; // Noncompliant {{The result of this modulus operation may not be positive.}}

nuint unsignedX = 100;
var xx = unsignedX % 4 == 1; // Noncompliant FP
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System;

public record R
{
public bool Equals(R other) => true; // Error [CS8872]
}

public sealed record Q
{
public bool Equals(Q other) => true; // Compliant
}

public record RecordWithVirtualEquals : IEquatable<RecordWithVirtualEquals> // Compliant
{
public virtual bool Equals(RecordWithVirtualEquals other) => true;
}

public abstract record RecordWithAbstractEquals : IEquatable<RecordWithAbstractEquals> // Compliant
{
public abstract bool Equals(RecordWithAbstractEquals other);
}

public record R1(string x); // Compliant
public record R2(string x, string y) : R1(x); // Compliant
Loading

0 comments on commit 530c8f4

Please sign in to comment.