diff --git a/test/be/jeffcheasey88/peeratcode/parser/java/FunctionTests.java b/test/be/jeffcheasey88/peeratcode/parser/java/FunctionTests.java new file mode 100644 index 0000000..a037c5a --- /dev/null +++ b/test/be/jeffcheasey88/peeratcode/parser/java/FunctionTests.java @@ -0,0 +1,126 @@ +package be.jeffcheasey88.peeratcode.parser.java; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.util.ArrayList; +import java.util.List; +import java.util.function.BiFunction; + +import org.junit.jupiter.api.Test; + +import be.jeffcheasey88.peeratcode.parser.Bag; +import be.jeffcheasey88.peeratcode.parser.Parser; +import be.jeffcheasey88.peeratcode.parser.Token; +import be.jeffcheasey88.peeratcode.parser.TokenType; +import be.jeffcheasey88.peeratcode.parser.TokenValidator; +import be.jeffcheasey88.peeratcode.parser.state.BuilderStateTree; +import be.jeffcheasey88.peeratcode.parser.state.InitialStateTree; +import be.jeffcheasey88.peeratcode.parser.state.RedirectStateTree; +import be.jeffcheasey88.peeratcode.parser.state.StateTree; + +public class FunctionTests { + + public static StateTree get(){ + + BiFunction function_builder = (parent, bag) -> { + System.out.println(bag); + return null; + }; + + InitialStateTree function = new InitialStateTree<>(); +// function.multiple(annotation); +// BuilderStateTree function_static = function.then((validator) -> validator.validate((token) -> token.getValue().equals("static")) && validator.validate((token) -> token.getValue().equals("{"))).end((a,b) -> a); +// function_static.multiple(function_container); +// function_static.unique((validator) -> validator.validate((token) -> token.getValue().equals("}"))).end((a,b) -> a); + StateTree function_mod = function.then(new RedirectStateTree<>(ModifierTests.get(), (global, local) -> { + Integer mod = global.get("mod"); + if(mod == null) global.set("mod", local.get()); + else global.set("mod", mod+local.get()); + })); + StateTree function_generic = function.then(new RedirectStateTree<>(TypeTests.getGeneric(), (global, local) -> global.set("generic", local.get()))); + StateTree function_type = function.then(new RedirectStateTree<>(TypeTests.get(), (global, local) -> global.set("type", local.get()))); + function_mod.then(function_mod); + function_mod.then(function_generic); + function_mod.then(function_type); + function_generic.then(function_type); + + StateTree function_name = function.then((validator) -> validator.validate( + (token) -> token.getType().equals(TokenType.NAME), + (bag, token) -> bag.set("name", token))); + function_type.then(function_name); + StateTree function_begin = function_name.then((validator) -> validator.validate((token) -> token.getValue().equals("("))); + StateTree function_end = function_begin.then((validator) -> validator.validate((token) -> token.getValue().equals(")"))); + function_end.then((validator) -> validator.validate((token) -> token.getValue().equals("{"))) + .end(function_builder) +// .multiple(function_container) + .unique((validator) -> validator.validate((token) -> token.getValue().equals("}"))).end(); + function_end.then((validator) -> validator.validate((token) -> token.getValue().equals(";"))).end(); + function_end.then((validator) -> validator.validate((token) -> token.getValue().equals("default"))) +// .then(new RedirectStateTree<>(value_container, (global, local) -> global.set(null))) + .then((validator) -> validator.validate((token) -> token.getValue().equals(";"))) + .end(function_builder); + + StateTree function_throws = function_end.then((validator) -> validator.validate((token) -> token.getValue().equals("throws"))) + .then(new RedirectStateTree<>(TypeTests.get(), (global, local) -> { + List throwables = global.get("throws"); + if(throwables == null){ + throwables = new ArrayList<>(); + global.set("throws", throwables); + } + throwables.add(local.get()); + })); + BuilderStateTree function_start_throws = function_throws.then((validator) -> validator.validate((token) -> token.getValue().equals("{"))).end(function_builder); +// function_start_throws.multiple(function_container); + function_start_throws.unique((validator) -> validator.validate((token) -> token.getValue().equals("}"))).end((a,b) -> a); + function_throws.then((validator) -> validator.validate((token) -> token.getValue().equals(","))).then(function_throws); + function_throws.then((validator) -> validator.validate((token) -> token.getValue().equals(";"))).end(function_builder); + + + StateTree function_arg_mod = function_begin.then((validator) -> validator.validate((token) -> token.getValue().equals("final"), (bag, token) -> bag.set("arg_mod", true))); + StateTree function_arg_type = function_begin.then(new RedirectStateTree<>(TypeTests.get(), (global, local) -> global.set("arg_type", local.get()))); + function_arg_mod.then(function_arg_type); + StateTree function_arg_type_array_begin = function_arg_type.then((validator) -> validator.validate((token) -> token.getValue().equals("["), (bag, token) -> bag.set("arg_type", bag.get("arg_type").concat(token)))); + StateTree function_arg_type_array_end = function_arg_type_array_begin.then((validator) -> validator.validate((token) -> token.getValue().equals("]"), (bag, token) -> bag.set("arg_type", bag.get("arg_type").concat(token)))); + function_arg_type_array_end.then(function_arg_type_array_begin); + StateTree function_arg_name = function_arg_type.then((validator) -> validator.validate((token) -> token.getType().equals(TokenType.NAME), (bag, token) -> bag.set("arg_name", token))); + function_arg_type_array_end.then(function_arg_name); + function_arg_type.then((validator) -> + validator.validate((token) -> token.getValue().equals(".")) && + validator.validate((token) -> token.getValue().equals(".")) && + validator.validate((token) -> token.getValue().equals("."), (bag, token) -> bag.set("arg_elips", true))) + .then(function_arg_name); + StateTree function_arg_split = function_arg_name.then((validator) -> validator.validate((token) -> token.getValue().equals(","))); + function_arg_split.then(function_arg_mod); + function_arg_split.then(function_arg_type); + function_arg_name.then(function_end); + + return function; + } + + private static Parser parser = new Parser(){ + { + setTokenizer(ModifierTests.TOKENIZER); + + setStateTree(get()); + } + }; + + JavaElement testCase(String value) throws Exception{ + TokenValidator.TOKENS = 0; + TokenValidator.MAX_VALIDATE = 0; + + JavaElement result = new JavaElement(); + + parser.parse(value, result); + + assertEquals(TokenValidator.TOKENS, TokenValidator.MAX_VALIDATE); + + return result; + } + + @Test + void main() throws Exception{ + JavaElement result = testCase("public static void main(String[] args){}"); + } + +} diff --git a/test/be/jeffcheasey88/peeratcode/parser/java/TypeTests.java b/test/be/jeffcheasey88/peeratcode/parser/java/TypeTests.java index eb7454d..8086c23 100644 --- a/test/be/jeffcheasey88/peeratcode/parser/java/TypeTests.java +++ b/test/be/jeffcheasey88/peeratcode/parser/java/TypeTests.java @@ -16,19 +16,21 @@ import be.jeffcheasey88.peeratcode.parser.state.StateTree; public class TypeTests{ - public static StateTree get(){ - + private static StateTree type; + private static StateTree gen; + + static{ BiConsumer concat = (bag, token) -> { Token value = bag.get(); if(value == null) bag.set(token); else bag.set(value.concat(token)); }; - StateTree type = new StateTree(); + type = new StateTree(); StateTree type_ = type.then((validator) -> validator.validate((token) -> token.getType().equals(TokenType.NAME),concat)); type_.then((validator) -> validator.validate((token) -> token.getValue().equals("."), concat)) .then(type_); - StateTree gen = new StateTree<>(); + gen = new StateTree<>(); type_.then(gen); StateTree type_begin = gen.then((validator) -> validator.validate((token) -> token.getValue().equals("<"), concat)); StateTree type_generic = type_begin.then(new RedirectStateTree<>(type, (global, local) -> global.set(global.get().concat(local.get())))); @@ -60,10 +62,17 @@ public class TypeTests{ type_.end(); type_generic_end.end(); - + } + + public static StateTree get(){ return type; } + public static StateTree getGeneric(){ + return gen; + } + + private static Parser parser = new Parser(){ { setTokenizer(ModifierTests.TOKENIZER); diff --git a/test/be/jeffcheasey88/peeratcode/parser/java/ValueTests.java b/test/be/jeffcheasey88/peeratcode/parser/java/ValueTests.java new file mode 100644 index 0000000..ae78b8f --- /dev/null +++ b/test/be/jeffcheasey88/peeratcode/parser/java/ValueTests.java @@ -0,0 +1,39 @@ +package be.jeffcheasey88.peeratcode.parser.java; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import be.jeffcheasey88.peeratcode.parser.Parser; +import be.jeffcheasey88.peeratcode.parser.TokenValidator; +import be.jeffcheasey88.peeratcode.parser.state.StateTree; + +public class ValueTests { + + public static StateTree get(){ + + StateTree value_container = new StateTree<>(); + + return value_container; + } + + private static Parser parser = new Parser(){ + { + setTokenizer(ModifierTests.TOKENIZER); + + setStateTree(get()); + } + }; + + JavaElement testCase(String value) throws Exception{ + TokenValidator.TOKENS = 0; + TokenValidator.MAX_VALIDATE = 0; + + JavaElement result = new JavaElement(); + + parser.parse(value, result); + + assertEquals(TokenValidator.TOKENS, TokenValidator.MAX_VALIDATE); + + return result; + } + +}