diff --git a/README.md b/README.md index edbc321c..45571d23 100644 --- a/README.md +++ b/README.md @@ -298,8 +298,11 @@ Andy provides different checks for JUnit, Mockito, and JQWik tests: - `NumberOfTests`: checks whether the test suite has a minimum number of tests. - `TestMethodsHaveAssertions`: checks whether all test methods have assertions. - `LoopInTestMethods`: checks whether there is a loop in a test method. - - `UseOfStringLiterals`: checks whether there is a string literal in a test method. - - `MethodCalledInTestMethod`: checks whether a method was invoked in a test method. + - `UseOfStringLiterals`: checks whether there is a string of at least a specified length in a test method. + - `AnnotatedMethod`: checks whether a method with a specified annotation exists. + - `MethodCalled`: checks whether a method was invoked anywhere in the test code. + - `MethodCalledInTestMethod`: checks whether a method was invoked in a test method (annotated with `@Test`, `@ParameterizedTest`, `@Property`, or `@Example`). + - `MethodCalledInProvideMethod`: checks whether a method was invoked in a method with the `@Provide` annotation. - Mockito: - `MockClass`: Checks whether a class was mocked in the test suite. diff --git a/andy/src/main/java/nl/tudelft/cse1110/andy/codechecker/checks/AnnotatedMethod.java b/andy/src/main/java/nl/tudelft/cse1110/andy/codechecker/checks/AnnotatedMethod.java new file mode 100644 index 00000000..6682795a --- /dev/null +++ b/andy/src/main/java/nl/tudelft/cse1110/andy/codechecker/checks/AnnotatedMethod.java @@ -0,0 +1,44 @@ +package nl.tudelft.cse1110.andy.codechecker.checks; + +import org.eclipse.jdt.core.dom.MarkerAnnotation; +import org.eclipse.jdt.core.dom.Name; +import org.eclipse.jdt.core.dom.NormalAnnotation; +import org.eclipse.jdt.core.dom.SingleMemberAnnotation; + +public class AnnotatedMethod extends Check { + + private boolean annotationIdentified = false; + private String annotation; + + public AnnotatedMethod(String annotation) { + this.annotation = annotation; + } + + @Override + public boolean visit(MarkerAnnotation node) { + checkIfThisIsTheAnnotation(node.getTypeName()); + return true; + } + + @Override + public boolean visit(NormalAnnotation node) { + checkIfThisIsTheAnnotation(node.getTypeName()); + return true; + } + + @Override + public boolean visit(SingleMemberAnnotation node) { + checkIfThisIsTheAnnotation(node.getTypeName()); + return true; + } + + private void checkIfThisIsTheAnnotation(Name name) { + if (name.getFullyQualifiedName().equals(annotation)) + annotationIdentified = true; + } + + @Override + public boolean result() { + return annotationIdentified; + } +} diff --git a/andy/src/main/java/nl/tudelft/cse1110/andy/codechecker/checks/MethodCalled.java b/andy/src/main/java/nl/tudelft/cse1110/andy/codechecker/checks/MethodCalled.java new file mode 100644 index 00000000..7a3bf4dc --- /dev/null +++ b/andy/src/main/java/nl/tudelft/cse1110/andy/codechecker/checks/MethodCalled.java @@ -0,0 +1,12 @@ +package nl.tudelft.cse1110.andy.codechecker.checks; + +public class MethodCalled extends MethodCalledInTestMethod { + public MethodCalled(String methodToBeCalled) { + super(methodToBeCalled); + } + + @Override + protected boolean isInTheAnnotatedMethod(){ + return true; + } +} diff --git a/andy/src/main/java/nl/tudelft/cse1110/andy/codechecker/checks/MethodCalledInProvideMethod.java b/andy/src/main/java/nl/tudelft/cse1110/andy/codechecker/checks/MethodCalledInProvideMethod.java new file mode 100644 index 00000000..3425b776 --- /dev/null +++ b/andy/src/main/java/nl/tudelft/cse1110/andy/codechecker/checks/MethodCalledInProvideMethod.java @@ -0,0 +1,15 @@ +package nl.tudelft.cse1110.andy.codechecker.checks; + +import java.util.Set; + +public class MethodCalledInProvideMethod extends MethodCalledInTestMethod { + + public MethodCalledInProvideMethod(String methodToBeCalled) { + super(methodToBeCalled); + } + + @Override + protected Set annotations() { + return Set.of("Provide"); + } +} diff --git a/andy/src/main/java/nl/tudelft/cse1110/andy/codechecker/checks/WithinTestMethod.java b/andy/src/main/java/nl/tudelft/cse1110/andy/codechecker/checks/WithinTestMethod.java index 5a48d4d9..8149307e 100644 --- a/andy/src/main/java/nl/tudelft/cse1110/andy/codechecker/checks/WithinTestMethod.java +++ b/andy/src/main/java/nl/tudelft/cse1110/andy/codechecker/checks/WithinTestMethod.java @@ -10,6 +10,7 @@ public abstract class WithinTestMethod extends WithinAnnotatedMethod { add("Test"); // junit add("ParameterizedTest"); // junit add("Property"); // jqwik + add("Example"); // jqwik }}; protected Set annotations() {