-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
FEAT: Method called check anywhere (#234)
- Loading branch information
1 parent
734f041
commit 51adea5
Showing
3 changed files
with
69 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
andy/src/main/java/nl/tudelft/cse1110/andy/codechecker/checks/MethodCalledAnywhere.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
andy/src/test/java/unit/codechecker/checks/MethodCalledAnywhereTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
} |