public void ClassPropertiesWithSetters_YieldsDiagnostics()
        {
            var commonExpected =
                new DiagnosticResultCommonProperties(new AA1101ClassPropertiesShouldBePubliclyRead_OnlyAnalyzer());
            var expected1 =
                new DiagnosticResult(commonExpected,
                                     Format(Resources.AA1101ClassPropertiesShouldBePubliclyReadOnlyMessageFormat,
                                            "Property2",
                                            "ContainsSetters"),
                                     Option <DiagnosticLocation> .Some(new DiagnosticLocation(10, 38, 41)));

            var expected2 =
                new DiagnosticResult(commonExpected,
                                     Format(Resources.AA1101ClassPropertiesShouldBePubliclyReadOnlyMessageFormat,
                                            "Property7",
                                            "ContainsSetters"),
                                     Option <DiagnosticLocation> .Some(new DiagnosticLocation(20, 13, 16)));

            DiagnosticVerifier.VerifyDiagnostics <AA1101ClassPropertiesShouldBePubliclyRead_OnlyAnalyzer>(
                @"..\..\..\CodeUnderTest\CodeToTestClassPropertySetters.cs",
                expected1,
                expected2);
        }
        public void MethodsWithAnd_YieldsDiagnostics()
        {
            var commonExpected =
                new DiagnosticResultCommonProperties(new AA2103MethodShouldNotContainAndAnalyzer());
            var expected1 =
                new DiagnosticResult(commonExpected,
                                     Format(Resources.AA2103MethodShouldNotContainAndMessageFormat, "DoSomethingAndSomethingElse"),
                                     Option <DiagnosticLocation> .Some(new DiagnosticLocation(7, 28, 55)));

            var expected2 =
                new DiagnosticResult(commonExpected,
                                     Format(Resources.AA2103MethodShouldNotContainAndMessageFormat, "DoSomethingAndSomethingElse"),
                                     Option <DiagnosticLocation> .Some(new DiagnosticLocation(14, 28, 55)));

            var expected3 =
                new DiagnosticResult(commonExpected,
                                     Format(Resources.AA2103MethodShouldNotContainAndMessageFormat, "AThingAndAnotherThing"),
                                     Option <DiagnosticLocation> .Some(new DiagnosticLocation(16, 21, 42)));

            var expected4 =
                new DiagnosticResult(commonExpected,
                                     Format(Resources.AA2103MethodShouldNotContainAndMessageFormat, "AndAndAnd"),
                                     Option <DiagnosticLocation> .Some(new DiagnosticLocation(20, 25, 34)));

            var expected5 =
                new DiagnosticResult(commonExpected,
                                     Format(Resources.AA2103MethodShouldNotContainAndMessageFormat, "AThingAndAnotherThing"),
                                     Option <DiagnosticLocation> .Some(new DiagnosticLocation(27, 14, 35)));

            DiagnosticVerifier.VerifyDiagnostics <AA2103MethodShouldNotContainAndAnalyzer>(
                TestFiles.CodeToTestMethodsWithAnd,
                expected1,
                expected2,
                expected3,
                expected4,
                expected5);
        }
        public void UsingCommandLineArguments_CS_Partial()
        {
            var compilation = SolutionBuilder.Create()
                              .AddProject(AnalyzerLanguage.CSharp)
                              .AddSnippet(@"
partial class Program1
{
    static partial void Main(params string[] args) // Noncompliant
    {
        System.Console.WriteLine(args);
    }
}")
                              .AddSnippet(@"
partial class Program1
{
    static partial void Main(params string[] args); // Compliant, we raise only on methods with implementation
}")
                              .GetCompilation();

            DiagnosticVerifier.Verify(
                compilation,
                new CS.UsingCommandLineArguments(AnalyzerConfiguration.AlwaysEnabled),
                CompilationErrorBehavior.Default);
        }
        public void UsingCommandLineArguments_VB_Partial()
        {
            var compilation = SolutionBuilder.Create()
                              .AddProject(AnalyzerLanguage.VisualBasic)
                              .AddSnippet(@"
Partial Class Program1
    Private Shared Sub Main(ParamArray args As String()) ' Noncompliant
        System.Console.WriteLine(args)
    End Sub
End Class
")
                              .AddSnippet(@"
Partial Class Program1
    Private Shared Partial Sub Main(ParamArray args As String()) ' Compliant, we raise only on methods with implementation
    End Sub
End Class
")
                              .GetCompilation();

            DiagnosticVerifier.Verify(
                compilation,
                new VB.UsingCommandLineArguments(AnalyzerConfiguration.AlwaysEnabled),
                CompilationErrorBehavior.Default);
        }
示例#5
0
        public static void VerifyNoIssueReported(string path, SonarDiagnosticAnalyzer diagnosticAnalyzer,
                                                 IEnumerable <ParseOptions> options = null, CompilationErrorBehavior checkMode = CompilationErrorBehavior.Default,
                                                 IEnumerable <MetadataReference> additionalReferences = null)
        {
            var projectBuilder = SolutionBuilder.Create()
                                 .AddProject(AnalyzerLanguage.FromPath(path))
                                 .AddReferences(additionalReferences)
                                 .AddDocument(path);


            if (options == null)
            {
                var compilation = projectBuilder.GetCompilation(null);
                DiagnosticVerifier.VerifyNoIssueReported(compilation, diagnosticAnalyzer, checkMode);
            }
            else
            {
                foreach (var option in options)
                {
                    var compilation = projectBuilder.GetCompilation(option);
                    DiagnosticVerifier.VerifyNoIssueReported(compilation, diagnosticAnalyzer, checkMode);
                }
            }
        }
        public void MethodsWithRefOrOutParams_YieldsDiagnostics()
        {
            var commonExpected =
                new DiagnosticResultCommonProperties(new AA2100MethodParametersMustNotBeRefOrOutAnalyzer());

            var expected1 = new DiagnosticResult(commonExpected,
                                                 String.Format(Resources.AA2100MethodParametersMustNotBeRefOrOutMessageFormat,
                                                               "p",
                                                               "UsesRefParameter",
                                                               "ref"),
                                                 Option <DiagnosticLocation> .Some(new DiagnosticLocation(11, 45, 54)));

            var expected2 = new DiagnosticResult(commonExpected,
                                                 String.Format(Resources.AA2100MethodParametersMustNotBeRefOrOutMessageFormat,
                                                               "p",
                                                               "UsesOutParameter",
                                                               "out"),
                                                 Option <DiagnosticLocation> .Some(new DiagnosticLocation(17, 45, 54)));

            DiagnosticVerifier.VerifyDiagnostics <AA2100MethodParametersMustNotBeRefOrOutAnalyzer>(
                TestFiles.CodeToTestDetectingOutAndRefParameters,
                expected1,
                expected2);
        }
 public void IgnoredCode_ShouldYieldNoDiagnostics() =>
 DiagnosticVerifier.VerifyDiagnostics <AA1301VariablesShouldBeAssignedOnceOnlyAnalyzer>(
     @"..\..\CodeUnderTest\AutogeneratedVariableReassignment.cs");
 public void NoCode_ShouldYieldNoDiagnostics() =>
 DiagnosticVerifier.VerifyDiagnostics <AA1301VariablesShouldBeAssignedOnceOnlyAnalyzer>(
     @"..\..\CodeUnderTest\EmptyFile.cs");
示例#9
0
 public void NoCode_ShouldYieldNoDiagnostics() =>
 DiagnosticVerifier.VerifyDiagnostics <AA2000DoNotUseNotImplementedExceptionAnalyzer>(
     @"..\..\..\CodeUnderTest\EmptyFile.cs");
示例#10
0
 public void IgnoredCode_ShouldYieldNoDiagnostics() =>
 DiagnosticVerifier.VerifyDiagnostics <AA1300ParametersShouldNotBeModifiedAnalyzer>(
     @"..\..\CodeUnderTest\AutogeneratedParameterReassignment.cs");
 public void NoCode_ShouldYieldNoDiagnostics() =>
 DiagnosticVerifier.VerifyDiagnostics <AA1100InterfacePropertiesShouldBeRead_OnlyAnalyzer>(
     @"..\..\CodeUnderTest\EmptyFile.cs");
 public void StaticMethodWithNoParamsInGeneratedCode_YieldsNoDiagnostics()
 {
     DiagnosticVerifier.VerifyDiagnostics <AA1001StaticMethodsShouldHaveAtLeastOneParameterAnalyzer>(
         TestFiles.AutogeneratedNoParameterCode);
 }
 public void CodeWithSetters_YieldsNoDiagnostics() =>
 DiagnosticVerifier.VerifyDiagnostics <AA1000StaticMethodsShouldNotBeVoidAnalyzer>(
     TestFiles.CodeToTestStaticVoidAnalyzerIgnoresSetters);
 public void CodeInIgnoredFile_YieldsNoDiagnostics() =>
 DiagnosticVerifier.VerifyDiagnostics <AA1000StaticMethodsShouldNotBeVoidAnalyzer>(
     TestFiles.FileThatShouldBeIgnored);
 public void AutogeneratedCode_YieldsNoDiagnostics() =>
 DiagnosticVerifier.VerifyDiagnostics <AA1000StaticMethodsShouldNotBeVoidAnalyzer>(
     TestFiles.AutogeneratedStaticVoidCode);
 public void NoCode_ShouldYieldNoDiagnostics()
 => DiagnosticVerifier.VerifyDiagnostics <AA1000StaticMethodsShouldNotBeVoidAnalyzer>(TestFiles.EmptyFile);
 public void NoCode_ShouldYieldNoDiagnostics() =>
 DiagnosticVerifier.VerifyDiagnostics <AA2100MethodParametersMustNotBeRefOrOutAnalyzer>(TestFiles.EmptyFile);
 public void CodeInIgnoredFile_YieldsNoDiagnostics() =>
 DiagnosticVerifier.VerifyDiagnostics <AA1101ClassPropertiesShouldBePubliclyRead_OnlyAnalyzer>(
     TestFiles.FileThatShouldBeIgnored);
        public void NonSimpleIntReassignments_YieldsDiagnostics()
        {
            var analyzer       = new AA1301VariablesShouldBeAssignedOnceOnlyAnalyzer();
            var commonExpected = new DiagnosticResultCommonProperties(analyzer);

            var expected1 =
                new DiagnosticResult(commonExpected,
                                     Format(Resources.AA1301VariablesShouldBeAssignedOnceOnlyMessageFormat,
                                            "a"),
                                     Option <DiagnosticLocation> .Some(new DiagnosticLocation(12, 13, 19)));

            var expected2 =
                new DiagnosticResult(commonExpected,
                                     Format(Resources.AA1301VariablesShouldBeAssignedOnceOnlyMessageFormat,
                                            "a"),
                                     Option <DiagnosticLocation> .Some(new DiagnosticLocation(13, 13, 19)));

            var expected3 =
                new DiagnosticResult(commonExpected,
                                     Format(Resources.AA1301VariablesShouldBeAssignedOnceOnlyMessageFormat,
                                            "a"),
                                     Option <DiagnosticLocation> .Some(new DiagnosticLocation(14, 13, 19)));

            var expected4 =
                new DiagnosticResult(commonExpected,
                                     Format(Resources.AA1301VariablesShouldBeAssignedOnceOnlyMessageFormat,
                                            "a"),
                                     Option <DiagnosticLocation> .Some(new DiagnosticLocation(15, 13, 19)));

            var expected5 =
                new DiagnosticResult(commonExpected,
                                     Format(Resources.AA1301VariablesShouldBeAssignedOnceOnlyMessageFormat,
                                            "a"),
                                     Option <DiagnosticLocation> .Some(new DiagnosticLocation(16, 13, 19)));

            var expected6 =
                new DiagnosticResult(commonExpected,
                                     Format(Resources.AA1301VariablesShouldBeAssignedOnceOnlyMessageFormat,
                                            "a"),
                                     Option <DiagnosticLocation> .Some(new DiagnosticLocation(17, 13, 20)));

            var expected7 =
                new DiagnosticResult(commonExpected,
                                     Format(Resources.AA1301VariablesShouldBeAssignedOnceOnlyMessageFormat,
                                            "a"),
                                     Option <DiagnosticLocation> .Some(new DiagnosticLocation(18, 13, 20)));

            var expected8 =
                new DiagnosticResult(commonExpected,
                                     Format(Resources.AA1301VariablesShouldBeAssignedOnceOnlyMessageFormat,
                                            "a"),
                                     Option <DiagnosticLocation> .Some(new DiagnosticLocation(19, 13, 16)));

            var expected9 =
                new DiagnosticResult(commonExpected,
                                     Format(Resources.AA1301VariablesShouldBeAssignedOnceOnlyMessageFormat,
                                            "a"),
                                     Option <DiagnosticLocation> .Some(new DiagnosticLocation(20, 13, 16)));

            var expected10 =
                new DiagnosticResult(commonExpected,
                                     Format(Resources.AA1301VariablesShouldBeAssignedOnceOnlyMessageFormat,
                                            "a"),
                                     Option <DiagnosticLocation> .Some(new DiagnosticLocation(21, 13, 16)));

            var expected11 =
                new DiagnosticResult(commonExpected,
                                     Format(Resources.AA1301VariablesShouldBeAssignedOnceOnlyMessageFormat,
                                            "a"),
                                     Option <DiagnosticLocation> .Some(new DiagnosticLocation(22, 13, 16)));

            DiagnosticVerifier.VerifyDiagnostics <AA1301VariablesShouldBeAssignedOnceOnlyAnalyzer>(
                @"..\..\CodeUnderTest\CodeToTestNonSimpleIntVariableReassignments.cs",
                expected1,
                expected2,
                expected3,
                expected4,
                expected5,
                expected6,
                expected7,
                expected8,
                expected9,
                expected10,
                expected11);
        }
示例#20
0
 public void IgnoredCode_ShouldYieldNoDiagnostics() =>
 DiagnosticVerifier.VerifyDiagnostics <AA1201AvoidUsingStaticPropertiesAnalyzer>(
     @"..\..\..\CodeUnderTest\AutogeneratedFieldsAndProperties.cs");
 public void NoCode_ShouldYieldNoDiagnostics() =>
 DiagnosticVerifier.VerifyDiagnostics <AA1001StaticMethodsShouldHaveAtLeastOneParameterAnalyzer>(TestFiles.EmptyFile);
示例#22
0
 public void NoCode_ShouldYieldNoDiagnostics() =>
 DiagnosticVerifier.VerifyDiagnostics <AA2104FileMustOnlyContainOneTypeDefinitionAnalyzer>(TestFiles.EmptyFile);
 public void CodeWithStaticGetters_YieldsNoDiagnostics()
 {
     DiagnosticVerifier.VerifyDiagnostics <AA1001StaticMethodsShouldHaveAtLeastOneParameterAnalyzer>(
         TestFiles.CodeToTestAtLeastOneParameterAnalyzerIgnoresGetters);
 }
 public void CodeInIgnoredFile_YieldsNoDiagnostics() =>
 DiagnosticVerifier.VerifyDiagnostics <AA2103MethodShouldNotContainAndAnalyzer>(
     TestFiles.FileThatShouldBeIgnored);
示例#25
0
 public void NoCode_ShouldYieldNoDiagnostics() =>
 DiagnosticVerifier.VerifyDiagnostics <AA1201AvoidUsingStaticPropertiesAnalyzer>(
     @"..\..\..\CodeUnderTest\EmptyFile.cs");
示例#26
0
 public void NoCode_ShouldYieldNoDiagnostics() =>
 DiagnosticVerifier.VerifyDiagnostics <AA1300ParametersShouldNotBeModifiedAnalyzer>(
     @"..\..\CodeUnderTest\EmptyFile.cs");
 public void DisablingRequestValidation_CS_WebConfig_Transformation(string configPath) =>
 DiagnosticVerifier.VerifyExternalFile(
     SolutionBuilder.Create().AddProject(AnalyzerLanguage.CSharp).GetCompilation(),
     new CS.DisablingRequestValidation(AnalyzerConfiguration.AlwaysEnabled),
     File.ReadAllText(configPath),
     TestHelper.CreateSonarProjectConfig(Path.GetDirectoryName(configPath), TestHelper.CreateFilesToAnalyze(Path.GetDirectoryName(configPath), configPath)));
示例#28
0
        public void CollectionShouldHaveElementAt_ShouldIgnoreDictionaryTypes()
        {
            string source = GenerateCode.DictionaryAssertion("actual[\"key\"].Should().Be(expectedValue);");

            DiagnosticVerifier.VerifyCSharpDiagnostic <CollectionShouldHaveElementAtAnalyzer>(source);
        }
 public void NoCode_ShouldYieldNoDiagnostics() =>
 DiagnosticVerifier.VerifyDiagnostics <AA1102InnerTypesMustBePrivateAnalyzer>(@"..\..\..\CodeUnderTest\EmptyFile.cs");
 public void NoCode_ShouldYieldNoDiagnostics() =>
 DiagnosticVerifier.VerifyDiagnostics <AA2103MethodShouldNotContainAndAnalyzer>(TestFiles.EmptyFile);