public void TestRules()

in src/Analyzer.Core.BuiltInRuleTests/TestRunner.cs [33:66]


        public void TestRules(TestConfiguration ruleExpectations)
        {
            // Verify test config is valid (decided in GetTests function below).
            // Reason for failure is described in RuleId.
            if (ruleExpectations.RuleId.StartsWith("Invalid test"))
                Assert.Fail(ruleExpectations.RuleId);

            // Get template to analyze
            Assert.IsTrue(File.Exists(ruleExpectations.Template), message: $"Error: {ruleExpectations.Template} does not exist");
            var testTemplate = File.ReadAllText(ruleExpectations.Template);

            // Analyze template
            var templateFilePath = Path.Combine(Directory.GetCurrentDirectory(), ruleExpectations.Template);
            var evaluations = templateAnalyzer.AnalyzeTemplate(testTemplate, templateFilePath: templateFilePath);

            // Find any instances of the rule being tested
            var thisRuleEvaluations = evaluations.Where(e => e.RuleId.Equals(ruleExpectations.RuleId, StringComparison.OrdinalIgnoreCase)).ToList();

            // Get all lines reported as failed
            var failingLines = thisRuleEvaluations
                .Where(e => !e.Passed)
                .SelectMany(e => e.GetFailedResults().Select(r => r.SourceLocation.LineNumber))
                .ToList();

            failingLines.Sort();

            // Verify all expected lines are reported
            var expectedLines = ruleExpectations.ReportedFailures.Select(failure => failure.LineNumber).ToList();
            expectedLines.Sort();
            Assert.IsTrue(failingLines.SequenceEqual(expectedLines),
                "Expected failing lines do not match actual failed lines." + Environment.NewLine +
                $"Expected: [{string.Join(",", expectedLines)}]  Actual: [{string.Join(",", failingLines)}]" +
                (failingLines.Count > 0 ? "" : Environment.NewLine + "(Do the test directory and test config have the same name as the RuleId being tested?)"));
        }