151 lines
5.3 KiB
Java
151 lines
5.3 KiB
Java
package be.jeffcheasey88.peeratcode.parser.java;
|
|
|
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
|
|
|
import java.io.BufferedReader;
|
|
import java.io.IOException;
|
|
import java.io.Reader;
|
|
|
|
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.ForOperation;
|
|
import be.jeffcheasey88.peeratcode.parser.java.operations.ConditionalOperation.WhileOperation;
|
|
import be.jeffcheasey88.peeratcode.parser.java.operations.DoOperation;
|
|
import be.jeffcheasey88.peeratcode.parser.java.operations.LoopAffectOperation.BreakOperation;
|
|
import be.jeffcheasey88.peeratcode.parser.java.operations.LoopAffectOperation.ContinueOperation;
|
|
import be.jeffcheasey88.peeratcode.parser.java.operations.MethodCallOperation;
|
|
import be.jeffcheasey88.peeratcode.parser.java.operations.SynchronizedOperation;
|
|
|
|
@TestInstance(Lifecycle.PER_CLASS)
|
|
class OperationTest{
|
|
|
|
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 ifOperation(){
|
|
try {
|
|
JavaParser parser = parse("package be.jeffcheasey88.peeratcode.parser.java; class Test{ void function(){ int i = 0; if(i == 0) { return i; } return i; } }");
|
|
Class clazz = parser.getClazz();
|
|
|
|
assertEquals(1, clazz.getChilds().size());
|
|
Function function = (Function) clazz.getChilds().get(0);
|
|
assertEquals(3, function.getChilds().size());
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
|
|
}
|
|
|
|
@Test
|
|
void ifWithNoneOperation(){
|
|
try {
|
|
JavaParser parser = parse("package be.jeffcheasey88.peeratcode.parser.java; class Test{ void function(){ int i = 0; if(i == 0); return i; } }");
|
|
Class clazz = parser.getClazz();
|
|
|
|
assertEquals(1, clazz.getChilds().size());
|
|
Function function = (Function) clazz.getChilds().get(0);
|
|
assertEquals(3, function.getChilds().size());
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
|
|
}
|
|
|
|
@Test
|
|
void doWhileOperation(){
|
|
try {
|
|
JavaParser parser = parse("package be.jeffcheasey88.peeratcode.parser.java; class Test{ void function(){ int i = 0; do{ System.out.println(\"Hello\"); }while(i == 0); return i; } }");
|
|
Class clazz = parser.getClazz();
|
|
|
|
assertEquals(1, clazz.getChilds().size());
|
|
Function function = (Function) clazz.getChilds().get(0);
|
|
assertEquals(4, function.getChilds().size());
|
|
|
|
assertEquals(DoOperation.class, function.getChilds().get(1).getClass());
|
|
DoOperation doOp = (DoOperation)function.getChilds().get(1);
|
|
assertEquals(1, doOp.getChilds().size());
|
|
assertEquals(MethodCallOperation.class, doOp.getChilds().get(0).getClass());
|
|
|
|
assertEquals(WhileOperation.class, function.getChilds().get(2).getClass());
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
|
|
}
|
|
|
|
@Test
|
|
void continueOperation(){
|
|
try {
|
|
JavaParser parser = parse("package be.jeffcheasey88.peeratcode.parser.java; class Test{ void function(){ for(int i = 0; i < 1; i++) continue; } }");
|
|
Class clazz = parser.getClazz();
|
|
|
|
assertEquals(1, clazz.getChilds().size());
|
|
Function function = (Function) clazz.getChilds().get(0);
|
|
assertEquals(1, function.getChilds().size());
|
|
|
|
assertEquals(ForOperation.class, function.getChilds().get(0).getClass());
|
|
ForOperation f = (ForOperation)function.getChilds().get(0);
|
|
assertEquals(1, f.getChilds().size());
|
|
assertEquals(ContinueOperation.class, f.getChilds().get(0).getClass());
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
|
|
}
|
|
|
|
@Test
|
|
void breakOperation(){
|
|
try {
|
|
JavaParser parser = parse("package be.jeffcheasey88.peeratcode.parser.java; class Test{ void function(){ for(int i = 0; i < 1; i++) break ; } }");
|
|
Class clazz = parser.getClazz();
|
|
|
|
assertEquals(1, clazz.getChilds().size());
|
|
Function function = (Function) clazz.getChilds().get(0);
|
|
assertEquals(1, function.getChilds().size());
|
|
|
|
assertEquals(ForOperation.class, function.getChilds().get(0).getClass());
|
|
ForOperation f = (ForOperation)function.getChilds().get(0);
|
|
assertEquals(1, f.getChilds().size());
|
|
assertEquals(BreakOperation.class, f.getChilds().get(0).getClass());
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
|
|
}
|
|
|
|
@Test
|
|
void synchronizedOperation(){
|
|
try {
|
|
JavaParser parser = parse("package be.jeffcheasey88.peeratcode.parser.java; class Test{ void function(){ synchronized(this) { this.none(); } } }");
|
|
Class clazz = parser.getClazz();
|
|
|
|
assertEquals(1, clazz.getChilds().size());
|
|
Function function = (Function) clazz.getChilds().get(0);
|
|
assertEquals(1, function.getChilds().size());
|
|
|
|
assertEquals(SynchronizedOperation.class, function.getChilds().get(0).getClass());
|
|
SynchronizedOperation sync = (SynchronizedOperation)function.getChilds().get(0);
|
|
assertEquals(1, sync.getChilds().size());
|
|
assertEquals(MethodCallOperation.class, sync.getChilds().get(0).getClass());
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
|
|
}
|
|
}
|