From 06d18924294dfe78eaba8eafe3a5ec559c43aecb Mon Sep 17 00:00:00 2001 From: Thibault Gagnaux Date: Wed, 10 Nov 2021 00:37:04 +0100 Subject: refactor: tests and makes `FregeDTOBuilder` a singleton --- .../gradleplugins/FregePluginFunctionalTest.java | 79 +++++++++++++++------- 1 file changed, 55 insertions(+), 24 deletions(-) (limited to 'src/functionalTest') diff --git a/src/functionalTest/java/ch/fhnw/thga/gradleplugins/FregePluginFunctionalTest.java b/src/functionalTest/java/ch/fhnw/thga/gradleplugins/FregePluginFunctionalTest.java index 1ea8e90..3e36f52 100644 --- a/src/functionalTest/java/ch/fhnw/thga/gradleplugins/FregePluginFunctionalTest.java +++ b/src/functionalTest/java/ch/fhnw/thga/gradleplugins/FregePluginFunctionalTest.java @@ -13,6 +13,7 @@ import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.nio.file.Files; +import java.nio.file.Path; import java.nio.file.Paths; import java.util.stream.Stream; @@ -34,8 +35,13 @@ import org.junit.jupiter.api.io.TempDir; @TestInstance(Lifecycle.PER_CLASS) public class FregePluginFunctionalTest { private static final String NEW_LINE = System.lineSeparator(); + private static final String SIMPLE_FREGE_CODE = String.join(NEW_LINE, + "module ch.fhnw.thga.Completion where", NEW_LINE, + NEW_LINE, + " complete :: Int -> (Int, String)", NEW_LINE, + " complete i = (i, \"Frege rocks\")", NEW_LINE); - private final FregeDTOBuilder fregeBuilder = new FregeDTOBuilder(); + private static FregeDTOBuilder fregeBuilder; @TempDir File testProjectDir; @@ -71,6 +77,14 @@ public class FregePluginFunctionalTest { .buildAndFail(); } + private void setupDefaultFregeProjectStructure(String fregeCode, String fregeFileName, String buildFileConfig) throws Exception { + Files.createDirectories(testProjectDir.toPath().resolve(Paths.get("src", "main", "frege"))); + File fregeFile = testProjectDir.toPath().resolve(Paths.get("src", "main", "frege", fregeFileName)) + .toFile(); + writeToFile(fregeFile, fregeCode); + appendToFile(buildFile, buildFileConfig); + } + @BeforeAll void beforeAll() throws Exception { settingsFile = new File(testProjectDir, "settings.gradle"); @@ -84,14 +98,16 @@ public class FregePluginFunctionalTest { void setup() throws Exception { buildFile = new File(testProjectDir, "build.gradle"); writeToFile(buildFile, createPluginsSection(Stream.of(FREGE_PLUGIN_ID))); + fregeBuilder = FregeDTOBuilder.getInstance(); } @AfterEach void cleanup() { - buildFile.delete(); + testProjectDir.delete(); } @Nested + @TestInstance(Lifecycle.PER_CLASS) @IndicativeSentencesGeneration(separator = " -> ", generator = DisplayNameGenerator.ReplaceUnderscores.class) class Setup_frege_task_works { @@ -124,20 +140,16 @@ public class FregePluginFunctionalTest { } @Nested + @TestInstance(Lifecycle.PER_CLASS) @IndicativeSentencesGeneration(separator = " -> ", generator = DisplayNameGenerator.ReplaceUnderscores.class) class Compile_frege_task_works { @Test - void given_frege_code_in_src_main_frege_and_minimal_build_file_config() throws Exception { - String fregeCode = "module ch.fhnw.thga.Completion where\n\n" + "complete :: Int -> (Int, String)\n" - + "complete i = (i, \"Frege rocks\")\n"; - Files.createDirectories(testProjectDir.toPath().resolve(Paths.get("src", "main", "frege"))); - File completionFr = testProjectDir.toPath().resolve(Paths.get("src", "main", "frege", "Completion.fr")) - .toFile(); - writeToFile(completionFr, fregeCode); + void given_frege_code_in_default_source_dir_and_minimal_build_file_config() throws Exception { + String completionFr = "Completion.fr"; String minimalBuildFileConfig = createFregeSection( fregeBuilder.version("'3.25.84'").release("'3.25alpha'").build()); - appendToFile(buildFile, minimalBuildFileConfig); + setupDefaultFregeProjectStructure(SIMPLE_FREGE_CODE, completionFr, minimalBuildFileConfig); BuildResult result = runGradleTask(COMPILE_FREGE_TASK_NAME); @@ -150,21 +162,43 @@ public class FregePluginFunctionalTest { testProjectDir.getAbsolutePath() + "/build/classes/main/frege/ch/fhnw/thga/Completion.class") .exists()); } + + @Test + void given_frege_code_in_custom_source_dir_and_custom_output_dir_and_minimal_build_file_config() + throws Exception { + Path customMainSourceDir = testProjectDir.toPath().resolve(Paths.get("src", "frege")); + Files.createDirectories(customMainSourceDir); + File completionFr = customMainSourceDir.resolve("Completion.fr").toFile(); + writeToFile(completionFr, SIMPLE_FREGE_CODE); + String minimalBuildFileConfig = createFregeSection(fregeBuilder.version("'3.25.84'").release("'3.25alpha'") + .mainSourceDir("layout.projectDirectory.dir('src/frege')") + .outputDir("layout.buildDirectory.dir('frege')").build()); + appendToFile(buildFile, minimalBuildFileConfig); + + BuildResult result = runGradleTask(COMPILE_FREGE_TASK_NAME); + + assertTrue(project.getTasks().getByName(COMPILE_FREGE_TASK_NAME) instanceof CompileFregeTask); + assertEquals(SUCCESS, result.task(":" + COMPILE_FREGE_TASK_NAME).getOutcome()); + assertTrue( + new File(testProjectDir.getAbsolutePath() + "/build/frege/ch/fhnw/thga/Completion.java").exists()); + assertTrue( + new File(testProjectDir.getAbsolutePath() + "/build/frege/ch/fhnw/thga/Completion.class").exists()); + } } @Nested + @TestInstance(Lifecycle.PER_CLASS) @IndicativeSentencesGeneration(separator = " -> ", generator = DisplayNameGenerator.ReplaceUnderscores.class) class Run_frege_task_works { @Test void given_frege_file_with_main_function() throws Exception { String fregeCode = String.join(NEW_LINE, "module ch.fhnw.thga.Main where", NEW_LINE, NEW_LINE, " main = do", NEW_LINE, " println \"Frege rocks\"", NEW_LINE); - Files.createDirectories(testProjectDir.toPath().resolve(Paths.get("src", "main", "frege"))); - File mainFr = testProjectDir.toPath().resolve(Paths.get("src", "main", "frege", "Main.fr")).toFile(); - writeToFile(mainFr, fregeCode); - String minimalBuildFileConfig = createFregeSection( - fregeBuilder.version("'3.25.84'").release("'3.25alpha'").mainModule("'ch.fhnw.thga.Main'").build()); - appendToFile(buildFile, minimalBuildFileConfig); + String mainFr = "Main.fr"; + String buildFileConfig = createFregeSection( + fregeBuilder.version("'3.25.84'").release("'3.25alpha'") + .mainModule("'ch.fhnw.thga.Main'").build()); + setupDefaultFregeProjectStructure(fregeCode, mainFr, buildFileConfig); BuildResult result = runGradleTask(RUN_FREGE_TASK_NAME); assertTrue(project.getTasks().getByName(RUN_FREGE_TASK_NAME) instanceof RunFregeTask); @@ -174,14 +208,11 @@ public class FregePluginFunctionalTest { @Test void given_frege_file_without_main_function() throws Exception { - String fregeCode = "module ch.fhnw.thga.Completion where\n\n" + "complete :: Int -> (Int, String)\n" - + "complete i = (i, \"Frege rocks\")\n"; - Files.createDirectories(testProjectDir.toPath().resolve(Paths.get("src", "main", "frege"))); - File completeFr = testProjectDir.toPath().resolve(Paths.get("src", "main", "frege", "Complete.fr")).toFile(); - writeToFile(completeFr, fregeCode); - String minimalBuildFileConfig = createFregeSection( - fregeBuilder.version("'3.25.84'").release("'3.25alpha'").mainModule("'ch.fhnw.thga.Completion'").build()); - appendToFile(buildFile, minimalBuildFileConfig); + String completionFr = "Completion.fr"; + String buildFileConfig = createFregeSection( + fregeBuilder.version("'3.25.84'").release("'3.25alpha'") + .mainModule("'ch.fhnw.thga.Completion'").build()); + setupDefaultFregeProjectStructure(SIMPLE_FREGE_CODE, completionFr, buildFileConfig); BuildResult result = runAndFailGradleTask(RUN_FREGE_TASK_NAME); assertTrue(project.getTasks().getByName(RUN_FREGE_TASK_NAME) instanceof RunFregeTask); -- cgit