Skip to content
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

Add architecture tests for test classes and methods #295

Merged
merged 1 commit into from
Oct 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ void testExceptElseWithBeginEndShouldNotAddIssue() {
}

@Test
void testShouldSkipAsmProcedure() {
void testAsmProcedureShouldNotAddIssue() {
CheckVerifier.newVerifier()
.withCheck(new BeginEndRequiredCheck())
.onFile(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,203 @@
/*
* Sonar Delphi Plugin
* Copyright (C) 2024 Integrated Application Development
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
*/
package au.com.integradev.delphi.checks;

import static com.tngtech.archunit.base.DescribedPredicate.not;
import static com.tngtech.archunit.lang.syntax.ArchRuleDefinition.classes;
import static com.tngtech.archunit.lang.syntax.ArchRuleDefinition.methods;

import com.tngtech.archunit.base.DescribedPredicate;
import com.tngtech.archunit.core.domain.JavaClass;
import com.tngtech.archunit.core.domain.JavaClasses;
import com.tngtech.archunit.core.domain.JavaMember;
import com.tngtech.archunit.core.domain.JavaMethod;
import com.tngtech.archunit.core.domain.JavaModifier;
import com.tngtech.archunit.core.domain.properties.HasName;
import com.tngtech.archunit.core.importer.ClassFileImporter;
import com.tngtech.archunit.lang.ArchCondition;
import com.tngtech.archunit.lang.ConditionEvents;
import com.tngtech.archunit.lang.SimpleConditionEvent;
import java.util.List;
import org.junit.jupiter.api.Test;
import org.sonar.plugins.communitydelphi.api.check.DelphiCheck;

class CheckTestNameTest {

private static final JavaClasses CHECKS_PACKAGE =
new ClassFileImporter().importPackages("au.com.integradev.delphi.checks");

private static final DescribedPredicate<JavaMethod> VERIFY_ISSUES =
callCheckVerifierMethod("verifyIssues")
.or(callCheckVerifierMethod("verifyIssueOnFile"))
.or(callCheckVerifierMethod("verifyIssueOnProject"));
private static final DescribedPredicate<JavaMethod> VERIFY_NO_ISSUES =
callMethod("au.com.integradev.delphi.checks.verifier.CheckVerifier.verifyNoIssues");
private static final DescribedPredicate<JavaMethod> CALL_ASSERT_THROW_BY =
callMethod("org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy");
private static final DescribedPredicate<JavaMethod> CALL_ASSERT_NO_EXCEPTION =
callMethod("org.assertj.core.api.AssertionsForClassTypes.assertThatNoException");
private static final String IMPLEMENTATION_DETAIL_PREFIX = "testImplementationDetail";
private static final DescribedPredicate<HasName> TESTING_IMPLEMENTATION_DETAILS =
HasName.Predicates.nameStartingWith(IMPLEMENTATION_DETAIL_PREFIX);

private static final List<Class<?>> METATEST_CLASSES =
List.of(CheckListTest.class, CheckMetadataTest.class, CheckTestNameTest.class);
private static final DescribedPredicate<JavaMember> DECLARED_IN_METATESTS =
new DescribedPredicate<>("declared in meta test classes") {
@Override
public boolean test(JavaMember member) {
return METATEST_CLASSES.contains(member.getOwner().reflect());
}
};

private static final ArchCondition<JavaClass> HAVE_ASSOCIATED_CHECK =
new ArchCondition<>("have an associated check") {
@Override
public void check(JavaClass item, ConditionEvents events) {
String subjectName = item.getName().replaceAll("Test$", "");
boolean hasSubject =
CheckList.getChecks().stream().map(Class::getName).anyMatch(subjectName::equals);

if (!hasSubject) {
String message =
String.format(
"%s does not have an associated subject %s", item.getFullName(), subjectName);
events.add(SimpleConditionEvent.violated(item, message));
}
}
};
private static final ArchCondition<JavaClass> HAVE_ASSOCIATED_TEST =
new ArchCondition<>("have an associated test") {
@Override
public void check(JavaClass item, ConditionEvents events) {
String testName = item.getName() + "Test";
boolean hasTest =
item.getConstructorCallsToSelf().stream()
.anyMatch(
constructorCall ->
constructorCall.getOriginOwner().getName().equals(testName));

if (!hasTest) {
String message =
String.format(
"%s does not have an associated test %s", item.getFullName(), testName);
events.add(SimpleConditionEvent.violated(item, message));
}
}
};

static DescribedPredicate<JavaMethod> callMethod(String methodName) {
return new DescribedPredicate<>("call " + methodName) {
@Override
public boolean test(JavaMethod method) {
String methodPrefix = String.format("%s(", methodName);
return method.getCallsFromSelf().stream()
.anyMatch(call -> call.getTarget().getFullName().startsWith(methodPrefix));
}
};
}

static DescribedPredicate<JavaMethod> callCheckVerifierMethod(String methodName) {
return callMethod(
String.format("au.com.integradev.delphi.checks.verifier.CheckVerifier.%s", methodName));
}

@Test
void testCheckTestsVerifyingIssuesAreNamedCorrectly() {
methods()
.that(VERIFY_ISSUES)
.and(not(TESTING_IMPLEMENTATION_DETAILS))
.should()
.haveNameMatching(".*ShouldAdd(Issues?|QuickFix(es)?)$")
.allowEmptyShould(true)
.check(CHECKS_PACKAGE);
}

@Test
void testCheckTestsVerifyingNoIssuesAreNamedCorrectly() {
methods()
.that(VERIFY_NO_ISSUES)
.and(not(TESTING_IMPLEMENTATION_DETAILS))
.and(not(CALL_ASSERT_THROW_BY))
.should()
.haveNameMatching(".*ShouldNotAddIssues?$")
.allowEmptyShould(true)
.check(CHECKS_PACKAGE);
}

@Test
void testCheckTestsShouldThrowAreNamedCorrectly() {
methods()
.that(CALL_ASSERT_THROW_BY)
.and(not(TESTING_IMPLEMENTATION_DETAILS))
.should()
.haveNameMatching(".*ShouldThrow$")
.allowEmptyShould(true)
.check(CHECKS_PACKAGE);
}

@Test
void testCheckTestsShouldNotThrowAreNamedCorrectly() {
methods()
.that(CALL_ASSERT_NO_EXCEPTION)
.and(not(TESTING_IMPLEMENTATION_DETAILS))
.should()
.haveNameMatching(".*ShouldNotThrow$")
.allowEmptyShould(true)
.check(CHECKS_PACKAGE);
}

@Test
void testCheckTestsShouldBeNamedCorrectly() {
methods()
.that()
.areAnnotatedWith(Test.class)
.and(not(DECLARED_IN_METATESTS))
.should()
.haveNameMatching(".*Should((Not)?(Throw|Add(Issues?|QuickFix(es)?)))")
.orShould()
.haveNameMatching(IMPLEMENTATION_DETAIL_PREFIX + ".*")
.allowEmptyShould(true)
.check(CHECKS_PACKAGE);
}

@Test
void testCheckTestsHaveAnAssociatedCheck() {
classes()
.that()
.haveSimpleNameEndingWith("Test")
.and()
.doNotBelongToAnyOf(CheckListTest.class, CheckMetadataTest.class, CheckTestNameTest.class)
.should(HAVE_ASSOCIATED_CHECK)
.allowEmptyShould(true)
.check(CHECKS_PACKAGE);
}

@Test
void testChecksHaveAnAssociatedTest() {
classes()
.that()
.areAssignableTo(DelphiCheck.class)
.and()
.doNotHaveModifier(JavaModifier.ABSTRACT)
.should(HAVE_ASSOCIATED_TEST)
.allowEmptyShould(true)
.check(CHECKS_PACKAGE);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ void testTwoClassesShouldAddIssue() {
}

@Test
void testMultipleViolationsShouldAddOneIssue() {
void testMultipleViolationsShouldAddIssue() {
CheckVerifier.newVerifier()
.withCheck(new ClassPerFileCheck())
.onFile(
Expand All @@ -99,7 +99,7 @@ void testMultipleViolationsShouldAddOneIssue() {
}

@Test
void testFalsePositiveMetaClass() {
void testClassReferenceShouldNotAddIssue() {
CheckVerifier.newVerifier()
.withCheck(new ClassPerFileCheck())
.onFile(
Expand All @@ -113,7 +113,7 @@ void testFalsePositiveMetaClass() {
}

@Test
void testFalsePositiveClassMethods() {
void testClassMethodsShouldNotAddIssue() {
CheckVerifier.newVerifier()
.withCheck(new ClassPerFileCheck())
.onFile(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ void testTooComplexRoutineShouldAddIssue() {
}

@Test
void testTooComplexSubProcedureShouldOnlyAddIssueForSubProcedure() {
void testTooComplexSubProcedureShouldAddIssue() {
DelphiTestUnitBuilder builder =
new DelphiTestUnitBuilder()
.appendImpl("function Foo: Integer;")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ void testStackOverflowShouldNotAddIssue() {
}

@Test
void testRegexTimeoutCharSequenceSubsequence() {
void testImplementationDetailRegexTimeoutCharSequenceSubsequence() {
CharSequence charSequence = new RegexTimeoutCharSequence("Hello, World!", 0);
assertThat(charSequence.subSequence(0, 5)).asString().isEqualTo("Hello");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ void testConstructorWithoutPrefixShouldAddIssue() {
}

@Test
void testBadPascalCaseAddIssue() {
void testBadPascalCaseShouldAddIssue() {
CheckVerifier.newVerifier()
.withCheck(new ConstructorNameCheck())
.onFile(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ void testTooComplexRoutineShouldAddIssue() {
}

@Test
void testTooComplexNestedRoutineeShouldOnlyAddIssueForNestedRoutine() {
void testTooComplexNestedRoutineShouldAddIssue() {
DelphiTestUnitBuilder builder =
new DelphiTestUnitBuilder()
.appendImpl("function Foo: Integer;") // 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ void testTypeDeclarationShouldNotAddIssue() {
}

@Test
void testIgnorePackage() {
void testPackageShouldNotAddIssue() {
CheckVerifier.newVerifier()
.withCheck(new EmptyFileCheck())
.onFile(DelphiTestFile.fromResource(PACKAGE_FILE))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ void testEmptyExceptionalRoutinesWithCommentsShouldNotAddIssue() {
}

@Test
void testFalsePositiveForwardTypeDeclaration() {
void testForwardTypeDeclarationShouldNotAddIssue() {
CheckVerifier.newVerifier()
.withCheck(new EmptyRoutineCheck())
.onFile(
Expand All @@ -179,7 +179,7 @@ void testFalsePositiveForwardTypeDeclaration() {
}

@Test
void testFalsePositiveOverloadedMethod() {
void testOverloadedMethodShouldNotAddIssue() {
CheckVerifier.newVerifier()
.withCheck(new EmptyRoutineCheck())
.onFile(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ void testFieldNameWithoutPrefixShouldAddIssue() {
}

@Test
void testPublicAndPublishedFieldsShouldNotAddIssue() {
void testPublicAndPublishedFieldsShouldAddIssue() {
CheckVerifier.newVerifier()
.withCheck(new FieldNameCheck())
.onFile(
Expand All @@ -76,7 +76,7 @@ void testPublicAndPublishedFieldsShouldNotAddIssue() {
}

@Test
void testPublicAndPublishedFieldsInMultipleClassesShouldNotAddIssue() {
void testPublicAndPublishedFieldsInMultipleClassesShouldAddIssue() {
CheckVerifier.newVerifier()
.withCheck(new FieldNameCheck())
.onFile(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ void testMultipleParameterDeclarationShouldAddIssue() {
}

@Test
void testConstParameterKeywordShouldPropagateInQuickFix() {
void testConstParameterKeywordShouldAddQuickFix() {
CheckVerifier.newVerifier()
.withCheck(new GroupedParameterDeclarationCheck())
.onFile(
Expand All @@ -71,7 +71,7 @@ void testConstParameterKeywordShouldPropagateInQuickFix() {
}

@Test
void testThreeParametersInQuickFix() {
void testThreeParametersShouldAddQuickFix() {
CheckVerifier.newVerifier()
.withCheck(new GroupedParameterDeclarationCheck())
.onFile(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ void testFileHelperShouldNotAddIssue() {
}

@Test
void testGetUnknownExtendedTypeSimpleName() {
void testImplementationDetailGetUnknownExtendedTypeSimpleName() {
DelphiAst ast =
new DelphiTestUnitBuilder()
.appendDecl("type")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ void testPermittedOverloadsShouldNotAddIssue() {
}

@Test
void testForbiddenOverloadsAddIssues() {
void testForbiddenOverloadsShouldAddIssues() {
CheckVerifier.newVerifier()
.withCheck(new ImplicitDefaultEncodingCheck())
.withStandardLibraryUnit(getSystem())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ void testUppercaseExcludedKeywordShouldNotAddIssue() {
}

@Test
void testUppercaseKeywordShouldAddQuickFixOnToken() {
void testUppercaseRootTokenKeywordShouldAddIssue() {
CheckVerifier.newVerifier()
.withCheck(createCheck())
.onFile(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ void testSAsmProcedureShouldNotAddIssue() {
}

@Test
void testShouldSkipInlineAsm() {
void testInlineAsmShouldNotAddIssue() {
CheckVerifier.newVerifier()
.withCheck(new MissingSemicolonCheck())
.onFile(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ void testMatchingUnitReferenceShouldNotAddIssue() {
}

@Test
void testUnitReferenceMatchingDeclarationAndNotMatchingImportShouldNotAddIssue() {
void testUnitReferenceMatchingDeclarationAndNotMatchingImportShouldAddIssue() {
CheckVerifier.newVerifier()
.withCheck(new MixedNamesCheck())
.withUnitScopeName("System")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ void testNotOverridingMethodShouldAddIssues() {
}

@Test
void testQuickFixesFollowingIncludeDirective() {
void testIssuesFollowingIncludeDirectiveShouldAddQuickFixes() {
CheckVerifier.newVerifier()
.withCheck(new RedundantInheritedCheck())
.onFile(
Expand Down
Loading
Loading