ArchUnitNET icon indicating copy to clipboard operation
ArchUnitNET copied to clipboard

Architecture rule validation: expect passes

Open simonthum opened this issue 3 years ago • 0 comments

Hi,

I would like to ask about a rule validation feature I came up with.

Most of my rules are written so that they validate types. However I managed to write some that did not fail on my architecture because they did not match any type before checks ran, due to a typo.

I can identify this using ...Should().Exist().AndShould()..., but preferred to write my own check routine to avoid repeating that clause.

In fact I think this should be the default checking behaviour and implemented it for XUnit:

        public static void CheckRule(Architecture architecture, IArchRule archRule, bool expectPasses = true)
        {
            bool hasPasses = false, hasFailures = false;
            
            foreach (var evaluationResult in archRule.Evaluate(architecture))
            {
                hasPasses |= evaluationResult.Passed;
                hasFailures |= !evaluationResult.Passed;

                if (hasFailures)
                    break;
            }

            if (hasFailures)
            {
                throw new FailedArchRuleException(architecture, archRule);
            }
            if (expectPasses && !hasPasses)
            {
                throw new XunitException("An architecture rule did not produce any passes, please specify expectPasses to be false if that is fine.");
            }
        }

The effect is similar to ...Should().Exist().AndShould()... early in the rule, but is on by default.

That way it is a breaking change, but I think the added validation is worth it. What do people think?

I'd write the code for all Unit Test frameworks if consensus is reached.

simonthum avatar Oct 16 '22 16:10 simonthum