Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add C# 9 tests for S3877 , S1067 , S4226 #3750

Merged
merged 4 commits into from
Nov 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
pavel-mikula-sonarsource marked this conversation as resolved.
Show resolved Hide resolved

[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
pavel-mikula-sonarsource marked this conversation as resolved.
Show resolved Hide resolved

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
}
}