Inheritance: IAnalysisContext
 public override void Initialize(TestAnalysisContext context)
 {
     if (_exceptionCondition == ExceptionCondition.InvokingInitialize)
     {
         throw new InvalidOperationException(nameof(ExceptionCondition.InvokingInitialize));
     }
 }
        public override AnalysisApplicability CanAnalyze(TestAnalysisContext context, out string reasonIfNotApplicable)
        {
            reasonIfNotApplicable = null;
            if (_exceptionCondition == ExceptionCondition.InvokingCanAnalyze)
            {
                throw new InvalidOperationException(nameof(ExceptionCondition.InvokingCanAnalyze));
            }

            if (context.Options.RegardAnalysisTargetAsNotApplicable)
            {
                reasonIfNotApplicable = "testing NotApplicableToSpecifiedTarget";
                return(AnalysisApplicability.NotApplicableToSpecifiedTarget);
            }

            return(AnalysisApplicability.ApplicableToSpecifiedTarget);
        }
Ejemplo n.º 3
0
        public void BuildResult_BuildsExpectedResult()
        {
            // Arrange
            const string FormatId = "Default";
            const string RuleId = "TST0001";
            string[] Arguments = new string[] { "42", "54" };

            var context = new TestAnalysisContext
            {
                TargetUri = new System.Uri("file:///c:/src/file.c"),
                Rule = new Rule
                {
                    Id = RuleId,
                    MessageFormats = new Dictionary<string, string>
                    {
                        [FormatId] = "Expected {0} but got {1}."
                    }
                }
            };

            var region = new Region
            {
                StartLine = 42
            };

            // Act.
            Result result = RuleUtilities.BuildResult(
                ResultLevel.Error,
                context,
                region,
                FormatId,
                Arguments);

            // Assert.
            result.RuleId.Should().Be(RuleId);

            result.FormattedRuleMessage.FormatId.Should().Be(FormatId);

            result.FormattedRuleMessage.Arguments.Count.Should().Be(Arguments.Length);
            result.FormattedRuleMessage.Arguments[0].Should().Be(Arguments[0]);
            result.FormattedRuleMessage.Arguments[1].Should().Be(Arguments[1]);

            result.Locations.Count.Should().Be(1);
            result.Locations[0].AnalysisTarget.Region.ValueEquals(region).Should().BeTrue();
        }
        public override void Analyze(TestAnalysisContext context)
        {
            switch (_exceptionCondition)
            {
            case ExceptionCondition.InvokingAnalyze:
            {
                throw new InvalidOperationException(nameof(ExceptionCondition.InvokingAnalyze));
            }

            case ExceptionCondition.ParsingTarget:
            {
                Errors.LogTargetParseError(
                    context,
                    new Region
                    {
                        StartLine   = 42,
                        StartColumn = 54
                    },
                    "Could not parse target.");
                break;
            }

            case ExceptionCondition.LoadingPdb:
            {
                Errors.LogExceptionLoadingPdb(context, new InvalidOperationException("Test message"));
                break;
            }

            default:
            {
                context.Logger.Log(this,
                                   new Result()
                    {
                        RuleId  = Id,
                        Level   = FailureLevel.Warning,
                        Kind    = ResultKind.Fail,
                        Message = new Message {
                            Text = "Default location-free message from exception raising rule."
                        }
                    });

                break;
            }
            }
        }
Ejemplo n.º 5
0
        public void BuildResult_BuildsExpectedResult()
        {
            // Arrange
            const string RuleMessageId = "Default";
            const string RuleId        = "TST0001";

            string[] Arguments = new string[] { "42", "54" };

            var context = new TestAnalysisContext
            {
                TargetUri = new System.Uri("file:///c:/src/file.c"),
                Rule      = new ReportingDescriptor
                {
                    Id             = RuleId,
                    MessageStrings = new Dictionary <string, MultiformatMessageString>
                    {
                        [RuleMessageId] = new MultiformatMessageString {
                            Text = "Expected {0} but got {1}."
                        }
                    }
                }
            };

            var region = new Region
            {
                StartLine = 42
            };

            (context.RuntimeErrors & RuntimeConditions.OneOrMoreWarningsFired).Should().Be(RuntimeConditions.None);
            (context.RuntimeErrors & RuntimeConditions.OneOrMoreErrorsFired).Should().Be(RuntimeConditions.None);

            // Act.
            Result result = RuleUtilities.BuildResult(
                FailureLevel.Error,
                context,
                region,
                RuleMessageId,
                Arguments);

            // Assert.
            result.RuleId.Should().Be(RuleId);

            result.Message.Id.Should().Be(RuleMessageId);

            result.Message.Arguments.Count.Should().Be(Arguments.Length);
            result.Message.Arguments[0].Should().Be(Arguments[0]);
            result.Message.Arguments[1].Should().Be(Arguments[1]);

            result.Locations.Count.Should().Be(1);
            result.Locations[0].PhysicalLocation.Region.ValueEquals(region).Should().BeTrue();

            (context.RuntimeErrors & RuntimeConditions.OneOrMoreWarningsFired).Should().Be(RuntimeConditions.None);
            (context.RuntimeErrors & RuntimeConditions.OneOrMoreErrorsFired).Should().Be(RuntimeConditions.OneOrMoreErrorsFired);

            result = RuleUtilities.BuildResult(
                FailureLevel.Warning,
                context,
                region,
                RuleMessageId,
                Arguments);

            (context.RuntimeErrors & RuntimeConditions.OneOrMoreWarningsFired).Should().Be(RuntimeConditions.OneOrMoreWarningsFired);
        }