Architecture rule validation: expect passes
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.