peer-at-code-parser-java/test/ForCase.java
2023-09-10 18:22:53 +02:00

111 lines
3.2 KiB
Java

import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
import be.jeffcheasey88.peeratcode.parser.Parser;
import be.jeffcheasey88.peeratcode.parser.TokenType;
import be.jeffcheasey88.peeratcode.parser.TokenValidator;
import be.jeffcheasey88.peeratcode.parser.Tokenizer;
import be.jeffcheasey88.peeratcode.parser.java.JavaElement;
import be.jeffcheasey88.peeratcode.parser.state.InitialStateTree;
import be.jeffcheasey88.peeratcode.parser.state.StateTree;
public class ForCase {
private static Parser<JavaElement> parser = new Parser<JavaElement>(){
{
setTokenizer(new Tokenizer());
InitialStateTree<JavaElement> operation = new InitialStateTree<>();
StateTree<JavaElement> operation_for = operation.then((validator) -> validator.validate((token) -> token.getValue().equals("for")))
.then((validator) -> validator.validate((token) -> token.getValue().equals("(")));
StateTree<JavaElement> operation_for_first_part = operation_for.then((validator) -> validator.validate((token) -> token.getValue().equals(";")));
StateTree<JavaElement> operation_for_assign = operation_for.then((validator) -> validator.validate((token) -> token.getType().equals(TokenType.NAME)));
StateTree<JavaElement> operation_for_assign_end = operation_for_assign.then((validator) -> validator.validate((token) -> token.getValue().equals("=")))
.then((validator) -> validator.validate((token) -> token.getType().equals(TokenType.NAME)));
operation_for_assign.then(operation_for_assign);
operation_for_assign_end.then((validator) -> validator.validate((token) -> token.getValue().equals(",")))
.then(operation_for_assign);
operation_for_assign_end.then(operation_for_first_part);
StateTree<JavaElement> operation_for_second_part = operation_for_first_part.then((validator) -> validator.validate((token) -> token.getValue().equals(";")));
StateTree<JavaElement> operation_for_end = operation_for_second_part.then((validator) -> validator.validate((token) -> token.getValue().equals(")")))
.end((a,b) -> a);
setStateTree(operation);
}
};
void testCase(String value) throws Exception{
TokenValidator.TOKENS = 0;
TokenValidator.MAX_VALIDATE = 0;
parser.parse(value, new JavaElement());
assertEquals(TokenValidator.TOKENS, TokenValidator.MAX_VALIDATE);
}
@Test
void empty() throws Exception{
testCase("for(;;)");
}
@Test
void case1() throws Exception{
testCase("for(i=4;;)");
}
@Test
void case2() throws Exception{
testCase("for(i=4,j=3;;)");
}
@Test
void case3() throws Exception{
testCase("for(int i=4;;)");
}
@Test
void case4() throws Exception{
testCase("for(int i=4,j=3;;)");
}
@Test
void case5() throws Exception{
testCase("for(int i;;)");
}
@Test
void case6() throws Exception{
testCase("for(int i =0,j;;)");
}
@Test
void case7() throws Exception{
testCase("for(;value;)");
}
@Test
void case8() throws Exception{
testCase("for(;;i++)");
}
@Test
void case9() throws Exception{
testCase("for(final int i = 4;;i++)");
}
@Test
void case10() throws Exception{
testCase("for(private static final int i=4,k;value;)");
}
}