diff --git a/relast.preprocessor b/relast.preprocessor index 55ed9bd0f909000b01065307208801659af0af52..aeb1e3f9676705f3a05a54bbce175151825b0d7e 160000 --- a/relast.preprocessor +++ b/relast.preprocessor @@ -1 +1 @@ -Subproject commit 55ed9bd0f909000b01065307208801659af0af52 +Subproject commit aeb1e3f9676705f3a05a54bbce175151825b0d7e diff --git a/src/main/java/org/jastadd/preprocessor/Main.java b/src/main/java/org/jastadd/preprocessor/Main.java index 4c986b65e098923fed316e54ea8b3e234a7f70f9..c92c49d3ed5a87d5bf1b8ca0f7ccc5b22c789f4e 100644 --- a/src/main/java/org/jastadd/preprocessor/Main.java +++ b/src/main/java/org/jastadd/preprocessor/Main.java @@ -1,5 +1,6 @@ package org.jastadd.preprocessor; +import org.jastadd.option.BooleanOption; import org.jastadd.option.ValueOption; import org.jastadd.relast.ast.Document; import org.jastadd.relast.ast.Program; @@ -19,9 +20,10 @@ public class Main extends org.jastadd.relast.compiler.RelAstProcessor { private static final String MUSTACHE_TEMPLATE = "Navigation"; protected ValueOption optionErrorHandling; + protected BooleanOption optionPrintYaml; public Main() { - super("JastAdd Preprocessor", true); + super("Navigation Attribute Generator", true); } public static void main(String[] args) { @@ -39,6 +41,8 @@ public class Main extends org.jastadd.relast.compiler.RelAstProcessor { super.initOptions(); optionErrorHandling = addOption(new ValueOption("errorHandling", "null|optional|exception|<exceptionClass> to return null, Optional.empty() or an exception (extending RuntimeException) in case of an error.").acceptAnyValue()); optionErrorHandling.defaultValue("null"); + optionPrintYaml = addOption(new BooleanOption("printYaml", "create YAML file used by Mustache in the ouput directory")); + optionPrintYaml.defaultValue(false); } @Override @@ -49,8 +53,6 @@ public class Main extends org.jastadd.relast.compiler.RelAstProcessor { boolean useOptionals = false; String value = optionErrorHandling.value(); - System.out.println("value is " + value); - System.out.println("value is matched " + optionErrorHandling.isMatched()); switch (value) { case "null": // this is the default case @@ -89,12 +91,14 @@ public class Main extends org.jastadd.relast.compiler.RelAstProcessor { Path yamlFile = outputBasePath.resolve(navigation.getFileName()); Path jragFile = yamlFile.getParent().resolve(MUSTACHE_TEMPLATE + ".jrag"); - writeToFile(yamlFile, navigation.prettyPrint(false)); + if (optionPrintYaml.value() != null && optionPrintYaml.value()) { + writeToFile(yamlFile, navigation.prettyPrint(false)); + } try { - javaMustache(MUSTACHE_TEMPLATE, yamlFile.toString(), jragFile.toString()); + javaMustache(MUSTACHE_TEMPLATE, navigation.prettyPrint(), jragFile.toString()); } catch (IOException e) { - throw new CompilerException("Unable to expand template"); + throw new CompilerException("Unable to expand template", e); } return 0; diff --git a/src/test/java/org/jastadd/preprocessor/PreprocessorTest.java b/src/test/java/org/jastadd/preprocessor/PreprocessorTest.java index 42c6e7c1811f565a44bd2c9efdbf073f89562fbc..ab6e4cdf38c69e097d3401a53223f1902e4f5ccf 100644 --- a/src/test/java/org/jastadd/preprocessor/PreprocessorTest.java +++ b/src/test/java/org/jastadd/preprocessor/PreprocessorTest.java @@ -1,15 +1,11 @@ package org.jastadd.preprocessor; import org.jastadd.relast.tests.RelAstProcessorTestBase; -import org.junit.jupiter.api.Test; - -import java.io.IOException; -import java.nio.file.Paths; +import org.junit.jupiter.api.BeforeAll; public class PreprocessorTest extends RelAstProcessorTestBase { - - @Test - void testSimpleInheritance() throws IOException, InterruptedException { - directoryTest(Main.class, Paths.get("src/test/resources/SimpleInheritance")); + @BeforeAll + static void init() { + mainClass = Main.class; } } diff --git a/src/test/resources/SimpleInheritance/config.yaml b/src/test/resources/SimpleInheritance/config.yaml index 9ce6448c157d3e6eb3fb32c39c863e2f1f0b321f..b0616c67de1e87bb5484722b2a447d7339b65a90 100644 --- a/src/test/resources/SimpleInheritance/config.yaml +++ b/src/test/resources/SimpleInheritance/config.yaml @@ -1,30 +1,49 @@ - name: "SimpleInheritance (default)" + compare: true + out: "default/out" + expected: "default/expected" args: - "--inputBaseDir=in" - - "--outputBaseDir=out/default/" + - "--outputBaseDir=default/out" - "Example.relast" - name: "SimpleInheritance (null)" + compare: true + out: "null/out" + expected: "null/expected" args: - "--inputBaseDir=in" - - "--outputBaseDir=out/null/" + - "--outputBaseDir=null/out" + - "--printYaml" - "--errorHandling=null" - "Example.relast" - name: "SimpleInheritance (java.lang.Optional)" + compare: true + out: "optional/out" + expected: "optional/expected" args: - "--inputBaseDir=in" - - "--outputBaseDir=out/optional/" + - "--outputBaseDir=optional/out" + - "--printYaml" - "--errorHandling=optional" - "Example.relast" - name: "SimpleInheritance (default exception)" + compare: true + out: "exception/out" + expected: "exception/expected" args: - "--inputBaseDir=in" - - "--outputBaseDir=out/exception/" + - "--outputBaseDir=exception/out" + - "--printYaml" - "--errorHandling=exception" - "Example.relast" - name: "SimpleInheritance (custom exception)" + compare: true + out: "customexception/out" + expected: "customexception/expected" args: - "--inputBaseDir=in" - - "--outputBaseDir=out/customexception/" + - "--outputBaseDir=customexception/out" + - "--printYaml" - "--errorHandling=java.lang.ClassCastException" - "Example.relast" - name: "SimpleInheritance (wrong exception)" @@ -35,6 +54,6 @@ - "Invalid argument 'not.a.Class' for parameter 'errorHandling': Class not found (name must be qualified)." args: - "--inputBaseDir=in" - - "--outputBaseDir=out/castexception/" + - "--outputBaseDir=out/does_not_matter_will_fail" - "--errorHandling=not.a.Class" - "Example.relast" diff --git a/src/test/resources/SimpleInheritance/customexception/expected/Navigation.jrag b/src/test/resources/SimpleInheritance/customexception/expected/Navigation.jrag new file mode 100644 index 0000000000000000000000000000000000000000..55d5db559fdc30acca9761132ae030dc91b579bd --- /dev/null +++ b/src/test/resources/SimpleInheritance/customexception/expected/Navigation.jrag @@ -0,0 +1,33 @@ +aspect Navigation { + + /** Tests if A is a A1. + * @return 'true' if this is a A1, otherwise 'false' + */ + syn A A.isA1() = false; + eq A1.isA1() = true; + + /** Tests if A is a A2. + * @return 'true' if this is a A2, otherwise 'false' + */ + syn A A.isA2() = false; + eq A2.isA2() = true; + + /** casts a A into a A1 if possible. + * @return 'this' cast to a A1 or a + */ + syn A A.asA1(); + eq A.asA1() { + throw new java.lang.ClassCastException(); + } + eq A1.asA1() = this; + + /** casts a A into a A2 if possible. + * @return 'this' cast to a A2 or a + */ + syn A A.asA2(); + eq A.asA2() { + throw new java.lang.ClassCastException(); + } + eq A2.asA2() = this; + +} diff --git a/src/test/resources/SimpleInheritance/customexception/expected/Navigation.yaml b/src/test/resources/SimpleInheritance/customexception/expected/Navigation.yaml new file mode 100644 index 0000000000000000000000000000000000000000..395f10c9047d3fb3b281d1e942d32ab64b53bb21 --- /dev/null +++ b/src/test/resources/SimpleInheritance/customexception/expected/Navigation.yaml @@ -0,0 +1,8 @@ +useExceptions: true +useOptionals: false +exceptionName: java.lang.ClassCastException +types: + - typeName: A + subtypes: + - subtypeName: A1 + - subtypeName: A2 diff --git a/src/test/resources/SimpleInheritance/default/expected/Navigation.jrag b/src/test/resources/SimpleInheritance/default/expected/Navigation.jrag new file mode 100644 index 0000000000000000000000000000000000000000..122676a2e46ea161ee0c4e23edefd436771f71c5 --- /dev/null +++ b/src/test/resources/SimpleInheritance/default/expected/Navigation.jrag @@ -0,0 +1,29 @@ +aspect Navigation { + + /** Tests if A is a A1. + * @return 'true' if this is a A1, otherwise 'false' + */ + syn A A.isA1() = false; + eq A1.isA1() = true; + + /** Tests if A is a A2. + * @return 'true' if this is a A2, otherwise 'false' + */ + syn A A.isA2() = false; + eq A2.isA2() = true; + + /** casts a A into a A1 if possible. + * @return 'this' cast to a A1 or 'null' + */ + syn A A.asA1(); + eq A.asA1() = null; + eq A1.asA1() = this; + + /** casts a A into a A2 if possible. + * @return 'this' cast to a A2 or 'null' + */ + syn A A.asA2(); + eq A.asA2() = null; + eq A2.asA2() = this; + +} diff --git a/src/test/resources/SimpleInheritance/exception/expected/Navigation.jrag b/src/test/resources/SimpleInheritance/exception/expected/Navigation.jrag new file mode 100644 index 0000000000000000000000000000000000000000..fff733380e95bb6fba171ba4b47508d8e27c932a --- /dev/null +++ b/src/test/resources/SimpleInheritance/exception/expected/Navigation.jrag @@ -0,0 +1,33 @@ +aspect Navigation { + + /** Tests if A is a A1. + * @return 'true' if this is a A1, otherwise 'false' + */ + syn A A.isA1() = false; + eq A1.isA1() = true; + + /** Tests if A is a A2. + * @return 'true' if this is a A2, otherwise 'false' + */ + syn A A.isA2() = false; + eq A2.isA2() = true; + + /** casts a A into a A1 if possible. + * @return 'this' cast to a A1 or a + */ + syn A A.asA1(); + eq A.asA1() { + throw new RuntimeException(); + } + eq A1.asA1() = this; + + /** casts a A into a A2 if possible. + * @return 'this' cast to a A2 or a + */ + syn A A.asA2(); + eq A.asA2() { + throw new RuntimeException(); + } + eq A2.asA2() = this; + +} diff --git a/src/test/resources/SimpleInheritance/exception/expected/Navigation.yaml b/src/test/resources/SimpleInheritance/exception/expected/Navigation.yaml new file mode 100644 index 0000000000000000000000000000000000000000..0296f226505d3459a12541ed78cde66c962f43e4 --- /dev/null +++ b/src/test/resources/SimpleInheritance/exception/expected/Navigation.yaml @@ -0,0 +1,8 @@ +useExceptions: true +useOptionals: false +exceptionName: RuntimeException +types: + - typeName: A + subtypes: + - subtypeName: A1 + - subtypeName: A2 diff --git a/src/test/resources/SimpleInheritance/null/expected/Navigation.jrag b/src/test/resources/SimpleInheritance/null/expected/Navigation.jrag new file mode 100644 index 0000000000000000000000000000000000000000..122676a2e46ea161ee0c4e23edefd436771f71c5 --- /dev/null +++ b/src/test/resources/SimpleInheritance/null/expected/Navigation.jrag @@ -0,0 +1,29 @@ +aspect Navigation { + + /** Tests if A is a A1. + * @return 'true' if this is a A1, otherwise 'false' + */ + syn A A.isA1() = false; + eq A1.isA1() = true; + + /** Tests if A is a A2. + * @return 'true' if this is a A2, otherwise 'false' + */ + syn A A.isA2() = false; + eq A2.isA2() = true; + + /** casts a A into a A1 if possible. + * @return 'this' cast to a A1 or 'null' + */ + syn A A.asA1(); + eq A.asA1() = null; + eq A1.asA1() = this; + + /** casts a A into a A2 if possible. + * @return 'this' cast to a A2 or 'null' + */ + syn A A.asA2(); + eq A.asA2() = null; + eq A2.asA2() = this; + +} diff --git a/src/test/resources/SimpleInheritance/null/expected/Navigation.yaml b/src/test/resources/SimpleInheritance/null/expected/Navigation.yaml new file mode 100644 index 0000000000000000000000000000000000000000..c43dcb642e7ddae22004ea0d2a85451128424cd8 --- /dev/null +++ b/src/test/resources/SimpleInheritance/null/expected/Navigation.yaml @@ -0,0 +1,8 @@ +useExceptions: false +useOptionals: false +exceptionName: java.lang.RuntimeException +types: + - typeName: A + subtypes: + - subtypeName: A1 + - subtypeName: A2 diff --git a/src/test/resources/SimpleInheritance/optional/expected/Navigation.jrag b/src/test/resources/SimpleInheritance/optional/expected/Navigation.jrag new file mode 100644 index 0000000000000000000000000000000000000000..fe1ab5f1e8afc2901c56f94b468f484acb4d21c3 --- /dev/null +++ b/src/test/resources/SimpleInheritance/optional/expected/Navigation.jrag @@ -0,0 +1,29 @@ +aspect Navigation { + + /** Tests if A is a A1. + * @return 'true' if this is a A1, otherwise 'false' + */ + syn A A.isA1() = false; + eq A1.isA1() = true; + + /** Tests if A is a A2. + * @return 'true' if this is a A2, otherwise 'false' + */ + syn A A.isA2() = false; + eq A2.isA2() = true; + + /** casts a A into a A1 if possible. + * @return an Optional of 'this' cast to a A1 or an empty Optional + */ + syn A A.asA1(); + eq A.asA1() = java.util.Optional.empty(); + eq .asA1() = java.util.Optional.of(this); + + /** casts a A into a A2 if possible. + * @return an Optional of 'this' cast to a A2 or an empty Optional + */ + syn A A.asA2(); + eq A.asA2() = java.util.Optional.empty(); + eq .asA2() = java.util.Optional.of(this); + +} diff --git a/src/test/resources/SimpleInheritance/optional/expected/Navigation.yaml b/src/test/resources/SimpleInheritance/optional/expected/Navigation.yaml new file mode 100644 index 0000000000000000000000000000000000000000..48259078f9ce4351b8d860f02095a577d3d1e971 --- /dev/null +++ b/src/test/resources/SimpleInheritance/optional/expected/Navigation.yaml @@ -0,0 +1,8 @@ +useExceptions: false +useOptionals: true +exceptionName: java.lang.RuntimeException +types: + - typeName: A + subtypes: + - subtypeName: A1 + - subtypeName: A2