Skip to content

Commit

Permalink
Add C# 9 tests for S3877 , S1067 , S4226 (#3750)
Browse files Browse the repository at this point in the history
  • Loading branch information
andrei-epure-sonarsource authored Nov 16, 2020
1 parent 3a9f062 commit 204f23b
Show file tree
Hide file tree
Showing 7 changed files with 123 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*
/*
* SonarAnalyzer for .NET
* Copyright (C) 2015-2020 SonarSource SA
* mailto: contact AT sonarsource DOT com
Expand Down Expand Up @@ -30,11 +30,17 @@ public class ExceptionShouldNotBeThrownFromUnexpectedMethodsTest
{
[TestMethod]
[TestCategory("Rule")]
public void ExceptionShouldNotBeThrownFromUnexpectedMethods()
{
public void ExceptionShouldNotBeThrownFromUnexpectedMethods() =>
Verifier.VerifyAnalyzer(@"TestCases\ExceptionShouldNotBeThrownFromUnexpectedMethods.cs",
new ExceptionShouldNotBeThrownFromUnexpectedMethods(),
ParseOptionsHelper.FromCSharp8);
}
#if NET5_0
[TestMethod]
[TestCategory("Rule")]
public void ExceptionShouldNotBeThrownFromUnexpectedMethods_CSharp9() =>
Verifier.VerifyAnalyzerFromCSharp9Library(@"TestCases\ExceptionShouldNotBeThrownFromUnexpectedMethods.CSharp9.cs",
new ExceptionShouldNotBeThrownFromUnexpectedMethods());
#endif

}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*
/*
* SonarAnalyzer for .NET
* Copyright (C) 2015-2020 SonarSource SA
* mailto: contact AT sonarsource DOT com
Expand Down Expand Up @@ -28,20 +28,21 @@ public class ExpressionComplexityTest
{
[TestMethod]
[TestCategory("Rule")]
public void ExpressionComplexity_CSharp()
{
var diagnostic = new SonarAnalyzer.Rules.CSharp.ExpressionComplexity { Maximum = 3};
public void ExpressionComplexity_CSharp() =>
Verifier.VerifyAnalyzer(@"TestCases\ExpressionComplexity.cs",
diagnostic,
new SonarAnalyzer.Rules.CSharp.ExpressionComplexity { Maximum = 3},
options: ParseOptionsHelper.FromCSharp8);
}

[TestMethod]
[TestCategory("Rule")]
public void ExpressionComplexity_VisualBasic()
{
var diagnostic = new SonarAnalyzer.Rules.VisualBasic.ExpressionComplexity { Maximum = 3 };
Verifier.VerifyAnalyzer(@"TestCases\ExpressionComplexity.vb", diagnostic);
}
public void ExpressionComplexity_CSharp9() =>
Verifier.VerifyAnalyzerFromCSharp9Console(@"TestCases\ExpressionComplexity.CSharp9.cs",
new SonarAnalyzer.Rules.CSharp.ExpressionComplexity { Maximum = 3});

[TestMethod]
[TestCategory("Rule")]
public void ExpressionComplexity_VisualBasic() =>
Verifier.VerifyAnalyzer(@"TestCases\ExpressionComplexity.vb",
new SonarAnalyzer.Rules.VisualBasic.ExpressionComplexity { Maximum = 3 });
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,15 @@ public class ExtensionMethodShouldBeInSeparateNamespaceTest
{
[TestMethod]
[TestCategory("Rule")]
public void ExtensionMethodShouldBeInSeparateNamespace()
{
public void ExtensionMethodShouldBeInSeparateNamespace() =>
Verifier.VerifyAnalyzer(@"TestCases\ExtensionMethodShouldBeInSeparateNamespace.cs",
new ExtensionMethodShouldBeInSeparateNamespace());
}

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

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using System;
using System.Runtime.CompilerServices;

namespace Tests.Diagnostics
{
record Record1 : IDisposable
{
static Record1() => throw new NotImplementedException();
public void Dispose()
{
throw new NotImplementedException();
}
}

record Record2 : IDisposable
{
static Record2() => throw new Exception(); // FN, needs support for throw expressions

[ModuleInitializer]
internal static void M1()
{
throw new Exception(); // FN
}

event EventHandler OnSomething
{
add
{
throw new Exception(); // Noncompliant
}
remove
{
throw new InvalidOperationException(); // Compliant
}
}

public override string ToString()
{
throw new Exception(); // Noncompliant
}

public void Dispose()
{
throw new Exception(); // Noncompliant
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -262,4 +262,10 @@ public void Dispose()
throw new Exception(); // Noncompliant
}
}

class ArrowMethods : IDisposable
{
static ArrowMethods() => throw new Exception(); // FN, needs support for throw expressions
public void Dispose() => throw new Exception(); // FN
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
var d1 = true && false && true && false && true && true; // Noncompliant

object x = null;

if (x is true or true or true or true or true) { } // FN

if (x is true and true and true and true and true) { } // FN

if (x is 10 or 20 or 40 or 50 or 60 or 70) { } // FN

if (x is < 10 or < 20 or < 30 and (40 or 50 or 60)) { } // FN
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
var isTopLevelFile = true;

public record GlobalRecord { }

public static class GlobalExtensions
{
public static void Bar(this GlobalRecord r) { } // Noncompliant
}

namespace MyLibrary
{
public record Foo { }
}
namespace Helpers
{
public static class MyExtensions
{
public static void Bar(this MyLibrary.Foo foo) { }
public static void Bar(this GlobalRecord foo) { }
}
}
namespace Same
{
public record R { }
public static class Extensions
{
public static void Bar(this R r) { } // Noncompliant
}
}

0 comments on commit 204f23b

Please sign in to comment.