Skip to content

failing rules also show how often a rule was violated #24

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 30, 2017
Merged

failing rules also show how often a rule was violated #24

merged 2 commits into from
Sep 30, 2017

Conversation

hankem
Copy link
Member

@hankem hankem commented Sep 30, 2017

Especially when working with legacy code, rules may have a large number of violations.
It is often helpful to have this number explicitly displayed, for example to easily judge whether a refactoring reduces the number or not.

I hereby agree to the terms of the ArchUnit Contributor License Agreement.

@codecholeric
Copy link
Collaborator

Thanks for the PR 😃
I'll gladly merge this, do you think, you could add a test for this improved feature though? E.g. in ArchRuleTest

@Test
public void reports_number_of_violations() {
    EvaluationResult result = all(classes()).should(addFixedNumberOfViolations(3)).evaluate(javaClassesViaReflection(Object.class, String.class));

    assertThat(result.getFailureReport().toString()).contains("(6 times)");
}

where

private static ArchCondition<JavaClass> addFixedNumberOfViolations(final int number) {
    return new ArchCondition<JavaClass>(String.format("be violated exactly %d times", number)) {
        @Override
        public void check(JavaClass item, ConditionEvents events) {
            for (int i = 0; i < number; i++) {
                events.add(SimpleConditionEvent.violated(item, item.getSimpleName() + " violation " + i));
            }
        }
    };
}

I'd hate to destroy the number of violations in half a year 😉

@hankem
Copy link
Member Author

hankem commented Sep 30, 2017

Of course – especially given that you pointed out both the appropriate location and the implementaion! ;)

@codecholeric codecholeric merged commit fb29898 into TNG:master Sep 30, 2017
@hankem hankem deleted the number-of-violations branch September 30, 2017 16:20
codecholeric pushed a commit that referenced this pull request Feb 21, 2021
codecholeric added a commit that referenced this pull request Feb 21, 2021
failing rules also show how often a rule was violated
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants