public static TestRuleBehaviors AccessibleWithinContextOnly(this TestRuleBehaviors behaviors) { // These are the only test behavior flags that *must* be retrieved via // a context object. For example, if a skimmer needs to raise an exception // when it is initialized, it should retrieve the test rule behavior that // specifies this from the context object that parameterizes the call. return(behaviors & ~behaviors.AccessibleOutsideOfContextOnly()); }
protected override TestAnalysisContext DetermineApplicabilityAndAnalyze(TestAnalysisContext context, IEnumerable <Skimmer <TestAnalysisContext> > skimmers, ISet <string> disabledSkimmers) { TestRuleBehaviors behaviors = context.Policy.GetProperty(TestRule.Behaviors); TestRule.s_testRuleBehaviors = behaviors.AccessibleOutsideOfContextOnly(); return(base.DetermineApplicabilityAndAnalyze(context, skimmers, disabledSkimmers)); }
public override void Analyze(TestAnalysisContext context) { // We do not access the static test rule behaviors here. We also want to // ensure this data is only set with flags (if any) that are legal for // this property. (s_testRuleBehaviors & s_testRuleBehaviors.AccessibleOutsideOfContextOnly()) .Should().Be(s_testRuleBehaviors); // Now we'll make sure the context test rule behaviors are restricted // to settings that are legal to pass in a context object. TestRuleBehaviors testRuleBehaviors = context.Policy.GetProperty(Behaviors); (testRuleBehaviors & testRuleBehaviors.AccessibleWithinContextOnly()) .Should().Be(testRuleBehaviors); switch (testRuleBehaviors) { case TestRuleBehaviors.RaiseExceptionInvokingAnalyze: { throw new InvalidOperationException(nameof(TestRuleBehaviors.RaiseExceptionInvokingAnalyze)); } case TestRuleBehaviors.RaiseTargetParseError: { Errors.LogTargetParseError( context, new Region { StartLine = 42, StartColumn = 54 }, "Could not parse target."); break; } case TestRuleBehaviors.LogError: { context.Logger.Log(this, new Result { RuleId = this.Id, Level = FailureLevel.Error, Message = new Message { Text = "Simple test rule message." } }); break; } default: { break; } } string fileName = Path.GetFileName(context.TargetUri.LocalPath); if (fileName.Contains(nameof(FailureLevel.Error))) { context.Logger.Log(this, RuleUtilities.BuildResult(FailureLevel.Error, context, null, nameof(SkimmerBaseTestResources.TEST1001_Failed), context.TargetUri.GetFileName())); } if (fileName.Contains(nameof(FailureLevel.Warning))) { context.Logger.Log(this, RuleUtilities.BuildResult(FailureLevel.Warning, context, null, nameof(SkimmerBaseTestResources.TEST1001_Failed), context.TargetUri.GetFileName())); } if (fileName.Contains(nameof(FailureLevel.Note))) { context.Logger.Log(this, RuleUtilities.BuildResult(FailureLevel.Note, context, null, nameof(SkimmerBaseTestResources.TEST1001_Note), context.TargetUri.GetFileName())); } else if (fileName.Contains(nameof(ResultKind.Pass))) { context.Logger.Log(this, RuleUtilities.BuildResult(ResultKind.Pass, context, null, nameof(SkimmerBaseTestResources.TEST1001_Pass), context.TargetUri.GetFileName())); } else if (fileName.Contains(nameof(ResultKind.Review))) { context.Logger.Log(this, RuleUtilities.BuildResult(ResultKind.Review, context, null, nameof(SkimmerBaseTestResources.TEST1001_Review), context.TargetUri.GetFileName())); } else if (fileName.Contains(nameof(ResultKind.Open))) { context.Logger.Log(this, RuleUtilities.BuildResult(ResultKind.Open, context, null, nameof(SkimmerBaseTestResources.TEST1001_Open), context.TargetUri.GetFileName())); } else if (fileName.Contains(nameof(ResultKind.Informational))) { context.Logger.Log(this, RuleUtilities.BuildResult(ResultKind.Informational, context, null, nameof(SkimmerBaseTestResources.TEST1001_Information), context.TargetUri.GetFileName())); } }