113 lines
3.5 KiB
Java
113 lines
3.5 KiB
Java
package be.jeffcheasey88.peeratcode.parser.java;
|
|
|
|
import static org.junit.Assert.assertArrayEquals;
|
|
import static org.junit.Assert.assertNotNull;
|
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
|
|
|
import java.io.BufferedReader;
|
|
import java.io.IOException;
|
|
import java.io.Reader;
|
|
import java.util.Arrays;
|
|
|
|
import org.junit.jupiter.api.Test;
|
|
import org.junit.jupiter.api.TestInstance;
|
|
import org.junit.jupiter.api.TestInstance.Lifecycle;
|
|
|
|
import be.jeffcheasey88.peeratcode.parser.java.operations.ConditionalOperation;
|
|
import be.jeffcheasey88.peeratcode.parser.java.operations.ConditionalOperation.IfOperation;
|
|
import be.jeffcheasey88.peeratcode.parser.java.operations.MethodCallOperation;
|
|
|
|
@TestInstance(Lifecycle.PER_CLASS)
|
|
class SearchTest{
|
|
|
|
JavaParser parse(String code) throws Exception{
|
|
BufferedReader reader = new BufferedReader(new Reader(){public int read(char[] cbuf, int off, int len) throws IOException{return 0;}public void close() throws IOException {}}) {
|
|
private boolean read = false;
|
|
@Override
|
|
public String readLine() throws IOException{
|
|
if(read) return null;
|
|
read = true;
|
|
return code;
|
|
}
|
|
};
|
|
|
|
JavaParser parser = new JavaParser(reader);
|
|
parser.parse();
|
|
return parser;
|
|
}
|
|
|
|
@Test
|
|
void methodCallSearch(){
|
|
try {
|
|
JavaParser parser = parse("package be.jeffcheasey88.peeratcode.parser.java; class Test{ void function(){ int i = 0; if(i == 0){ this.none(); } } }");
|
|
|
|
JavaElement element = parser.find(
|
|
(e) -> e instanceof MethodCallOperation,
|
|
(trace) -> true);
|
|
|
|
assertNotNull(element);
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
|
|
}
|
|
|
|
@Test
|
|
void methodCallOutOfIfSearch(){
|
|
try {
|
|
JavaParser parser = parse("package be.jeffcheasey88.peeratcode.parser.java; class Test{ void function(){ int i = 0; if(i == 0){ this.none(); } valid(); } }");
|
|
|
|
JavaElement element = parser.find(
|
|
(e) -> e instanceof MethodCallOperation,
|
|
(trace) -> !trace.stream().anyMatch((e) -> (e instanceof ConditionalOperation)));
|
|
|
|
assertNotNull(element);
|
|
assertEquals("valid()", ((MethodCallOperation)element).getValue());
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
|
|
}
|
|
|
|
@Test
|
|
void siplifiedMethodCallInIfSearch(){
|
|
try {
|
|
JavaParser parser = parse("package be.jeffcheasey88.peeratcode.parser.java; class Test{ void function(){ int i = 0; if(i == 0){ this.none(); } valid(); } }");
|
|
|
|
MethodCallOperation element = parser.find(
|
|
(e, trace) -> {
|
|
return (e instanceof MethodCallOperation) && (trace.get(trace.size()-2) instanceof IfOperation);
|
|
});
|
|
|
|
assertNotNull(element);
|
|
assertEquals("this.none()", element.getValue());
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
|
|
}
|
|
|
|
@Test
|
|
void verifyPath(){
|
|
try {
|
|
JavaParser parser = parse("package be.jeffcheasey88.peeratcode.parser.java; class Test{ void function(){ int i = 0; if(i == 0){ this.none();this.yes();this.none(); } valid(); } }");
|
|
|
|
java.lang.Class[] exceptedStack = { Class.class, Function.class, IfOperation.class, MethodCallOperation.class };
|
|
MethodCallOperation element = parser.find(
|
|
(e, trace) -> {
|
|
if((e instanceof MethodCallOperation) && ((MethodCallOperation)e).getValue().equals("this.yes()")){
|
|
java.lang.Class[] stack = (java.lang.Class[]) trace.stream().map((current) -> current.getClass()).toArray((i) -> new java.lang.Class[i]);
|
|
assertArrayEquals(exceptedStack, stack);
|
|
return true;
|
|
}
|
|
return false;
|
|
});
|
|
|
|
assertNotNull(element);
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
|
|
}
|
|
|
|
}
|