Skip to content

Commit

Permalink
FEAT: Method called check anywhere (#234)
Browse files Browse the repository at this point in the history
  • Loading branch information
sandemiroren1 authored Jul 11, 2023
1 parent 734f041 commit 51adea5
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,7 @@ Andy provides different checks for JUnit, Mockito, and JQWik tests:
- `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.
- `MethodCalledAnywhere`: checks whether a method was invoked in any scope.

- Mockito:
- `MockClass`: Checks whether a class was mocked in the test suite.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package nl.tudelft.cse1110.andy.codechecker.checks;

import org.eclipse.jdt.core.dom.MethodInvocation;

public class MethodCalledAnywhere extends Check {
private final String methodToBeCalled;
private boolean methodWasCalled = false;

public MethodCalledAnywhere(String methodToBeCalled) {
this.methodToBeCalled = methodToBeCalled;
}

@Override
public boolean visit(MethodInvocation mi) {

String methodName = mi.getName().toString();

if (methodToBeCalled.equals(methodName))
methodWasCalled = true;

return super.visit(mi);
}


@Override
public boolean result() {
return methodWasCalled;
}

public String toString() {
return methodToBeCalled + " method is called";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package unit.codechecker.checks;

import nl.tudelft.cse1110.andy.codechecker.checks.Check;
import nl.tudelft.cse1110.andy.codechecker.checks.MethodCalledAnywhere;
import nl.tudelft.cse1110.andy.codechecker.checks.MethodCalledInTestMethod;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;

import static org.assertj.core.api.Assertions.assertThat;

public class MethodCalledAnywhereTest extends ChecksBaseTest {

@ParameterizedTest
@CsvSource(value={
"update, true",
"save, true",
"persist, true",
"retrieve, true",
"ceil, false",
})
void methodInvocationsInTestMethod(String methodName, boolean expectation) {
Check check = new MethodCalledAnywhere(methodName);
run("MethodCalled.java", check);
assertThat(check.result()).isEqualTo(expectation);
}

@Test
void shouldIgnoreAnonymousClasses() { // was breaking in midterm 2021
Check check = new MethodCalledInTestMethod("getPoints");
run("StackOverflowTestWithAnonymousClass.java", check);
assertThat(check.result()).isTrue();
}

}

0 comments on commit 51adea5

Please sign in to comment.