Unit Test for workspace & Parser type

This commit is contained in:
jeffcheasey88 2023-07-19 10:02:24 +02:00
parent 39a20c42a0
commit 5edc98ad3e
6 changed files with 95 additions and 7 deletions

View file

@ -2,5 +2,7 @@
<classpath> <classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/> <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
<classpathentry kind="src" path="src"/> <classpathentry kind="src" path="src"/>
<classpathentry kind="src" path="test"/>
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/5"/>
<classpathentry kind="output" path="bin"/> <classpathentry kind="output" path="bin"/>
</classpath> </classpath>

View file

@ -6,6 +6,7 @@ import java.util.function.Function;
public class TokenValidator{ public class TokenValidator{
public static int MAX_VALIDATE = 0; public static int MAX_VALIDATE = 0;
public static int TOKENS = 0;
private Token[] elements; private Token[] elements;
private int index; private int index;
@ -29,7 +30,10 @@ public class TokenValidator{
public boolean validate(Function<Token, Boolean> action){ public boolean validate(Function<Token, Boolean> action){
if(validated >= this.elements.length) return false; if(validated >= this.elements.length) return false;
if(action.apply(this.elements[validated])){ if(action.apply(this.elements[validated])){
if(validated+1 > MAX_VALIDATE) MAX_VALIDATE = validated+1; if(validated+1 > MAX_VALIDATE){
MAX_VALIDATE = validated+1;
TOKENS = elements.length;
}
this.validated++; this.validated++;
return true; return true;
} }

View file

@ -20,19 +20,19 @@ public class JavaFile extends JavaElement{
JavaFile setPackage(Bag bag){ JavaFile setPackage(Bag bag){
this.pack = bag.<List<Token>>get(); this.pack = bag.<List<Token>>get();
System.out.println("setPackage "+pack); // System.out.println("setPackage "+pack);
return this; return this;
} }
JavaFile addImport(Bag bag){ JavaFile addImport(Bag bag){
this.imports.add(bag.<List<Token>>get()); this.imports.add(bag.<List<Token>>get());
System.out.println("addImport "+imports.get(imports.size()-1)); // System.out.println("addImport "+imports.get(imports.size()-1));
return this; return this;
} }
Class setClass(Class clazz){ Class setClass(Class clazz){
this.mainClazz = clazz; this.mainClazz = clazz;
System.out.println("setClass "+clazz); // System.out.println("setClass "+clazz);
return clazz; return clazz;
} }
} }

View file

@ -11,6 +11,7 @@ import java.util.List;
import be.jeffcheasey88.peeratcode.parser.Parser; import be.jeffcheasey88.peeratcode.parser.Parser;
import be.jeffcheasey88.peeratcode.parser.Token; import be.jeffcheasey88.peeratcode.parser.Token;
import be.jeffcheasey88.peeratcode.parser.TokenType;
import be.jeffcheasey88.peeratcode.parser.Tokenizer; import be.jeffcheasey88.peeratcode.parser.Tokenizer;
import be.jeffcheasey88.peeratcode.parser.state.BuilderStateTree; import be.jeffcheasey88.peeratcode.parser.state.BuilderStateTree;
import be.jeffcheasey88.peeratcode.parser.state.RedirectStateTree; import be.jeffcheasey88.peeratcode.parser.state.RedirectStateTree;
@ -20,6 +21,10 @@ public class JavaParser extends Parser<JavaFile> {
public static long time; public static long time;
static {
time = System.currentTimeMillis();
}
public static void main(String[] args) throws Exception{ public static void main(String[] args) throws Exception{
File file = new File("C:\\Users\\jeffc\\eclipse-workspace\\peer-at-code-backend\\src\\be\\jeffcheasey88\\peeratcode\\routes\\Result.java"); File file = new File("C:\\Users\\jeffc\\eclipse-workspace\\peer-at-code-backend\\src\\be\\jeffcheasey88\\peeratcode\\routes\\Result.java");
BufferedReader reader = new BufferedReader(new FileReader(file)); BufferedReader reader = new BufferedReader(new FileReader(file));
@ -49,12 +54,43 @@ public class JavaParser extends Parser<JavaFile> {
})) mod = true; })) mod = true;
return mod; return mod;
}); });
modifier.end((a,b) -> a);
StateTree<JavaFile> type = new StateTree<JavaFile>().then((validator) ->
validator.validate(
(token) -> token.getType().equals(TokenType.NAME),
(bag, token) -> bag.set(token)));
StateTree<JavaFile> type_generic_begin = type.then((validator) -> validator.validate(
(token) -> token.getValue().equals("<"),
(bag, token) -> bag.set(bag.<Token>get().concat(token))));
StateTree<JavaFile> type_generic_name = type_generic_begin.then((validator) ->
validator.validate(
(token) -> token.getType().equals(TokenType.NAME),
(bag, token) -> bag.set(token)));
StateTree<JavaFile> type_generic_split = type_generic_name.then((validator) -> validator.validate(
(token) -> token.getValue().equals(","),
(bag, token) -> bag.set(bag.<Token>get().concat(token))));
StateTree<JavaFile> type_generic_end = type_generic_name.then((validator) -> validator.validate(
(token) -> token.getValue().equals(">"),
(bag, token) -> bag.set(bag.<Token>get().concat(token)))).loop();
type.end((a,b) -> a);
type_generic_begin.then(type_generic_name);
type_generic_split.then(type_generic_name);
type_generic_end.then(type_generic_name);
type_generic_end.then(type_generic_split);
type_generic_end.end((a,b) -> a);
StateTree<JavaFile> clazz_ = new StateTree<>(); StateTree<JavaFile> clazz_ = new StateTree<>();
StateTree<JavaFile> clazz = new StateTree<>(); StateTree<JavaFile> clazz = new StateTree<>();
clazz_.then(new RedirectStateTree<>(modifier, (bag) -> "modifier")); StateTree<JavaFile> clazz_base = clazz.then((validator) -> validator.validate((token) -> token.getValue().equals("class")))
.then(new RedirectStateTree<>(type, (bag) -> "name"));
clazz_base.then((validator) -> validator.validate((token) -> token.getValue().equals("{")))
.end((javafile, bag) -> new Class(bag));
StateTree<JavaFile> clazz_mod = clazz_.then(new RedirectStateTree<>(modifier, (bag) -> "modifier"));
clazz_mod.then(clazz);
StateTree<JavaFile> importState = new StateTree<>(); StateTree<JavaFile> importState = new StateTree<>();
importState.then((validator) -> { importState.then((validator) -> {
@ -94,7 +130,7 @@ public class JavaParser extends Parser<JavaFile> {
return false; return false;
}).<JavaElement>end((javafile, bag) -> javafile.setPackage(bag)); }).<JavaElement>end((javafile, bag) -> javafile.setPackage(bag));
pack.multiple(importState); pack.multiple(importState);
pack.thenNoChild(clazz_); pack.multiple(clazz_);
System.out.println((System.currentTimeMillis()-time)+"ms"); System.out.println((System.currentTimeMillis()-time)+"ms");

View file

@ -25,7 +25,6 @@ public class StateTree<E>{
TokenValidator validator = new TokenValidator(tokenizer.getTokens().toArray(new Token[0])); TokenValidator validator = new TokenValidator(tokenizer.getTokens().toArray(new Token[0]));
while(validator.hasNext()){ while(validator.hasNext()){
BuilderStateTree<E, ?> build = internalSeed(validator, container); BuilderStateTree<E, ?> build = internalSeed(validator, container);
System.out.println("Validate "+validator.MAX_VALIDATE+"/"+tokenizer.getTokens().size()+" tokens !");
if(build == null) break; if(build == null) break;
build.build(validator, container); build.build(validator, container);
} }

47
test/GlobalCover.java Normal file
View file

@ -0,0 +1,47 @@
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;
import org.junit.jupiter.api.Test;
import be.jeffcheasey88.peeratcode.parser.TokenValidator;
import be.jeffcheasey88.peeratcode.parser.java.JavaFile;
import be.jeffcheasey88.peeratcode.parser.java.JavaParser;
class GlobalCover {
@Test
void workspace() throws Exception{
JavaParser parser = new JavaParser();
List<File> files = files(new File("C:\\Users\\jeffc\\eclipse-workspace"));
int validated = 0;
double tokens = 0;
int count = 0;
long time = System.currentTimeMillis();
for(File file : files){
if(file.getName().endsWith(".java")){
parser.parse(new BufferedReader(new FileReader(file)), new JavaFile());
validated += TokenValidator.MAX_VALIDATE;
tokens += TokenValidator.TOKENS;
TokenValidator.MAX_VALIDATE = 0;
TokenValidator.TOKENS = 0;
count++;
}
}
System.out.println("Load time : "+((System.currentTimeMillis()-time)/1000.0)+"s");
System.out.println(((validated/tokens)*100.0)+"% validated in "+count+" files !");
}
private List<File> files(File dir){
List<File> result = new ArrayList<>();
if(dir.isDirectory()){
for(File file : dir.listFiles()) result.addAll(files(file));
}else result.add(dir);
return result;
}
}