aboutsummaryrefslogtreecommitdiff
path: root/src/functionalTest/java/ch/fhnw/thga/gradleplugins/CompileFregeTaskFunctionalTest.java
blob: a0512c0ab09fe69f53fb89eece281a679bd0031d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package ch.fhnw.thga.gradleplugins;

import static ch.fhnw.thga.gradleplugins.FregePlugin.COMPILE_FREGE_TASK_NAME;
import static ch.fhnw.thga.gradleplugins.SharedFunctionalTestLogic.createFregeSection;
import static ch.fhnw.thga.gradleplugins.SharedFunctionalTestLogic.runGradleTask;
import static org.gradle.testkit.runner.TaskOutcome.SUCCESS;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.File;
import java.util.stream.Stream;

import org.gradle.api.Project;
import org.gradle.testkit.runner.BuildResult;
import org.junit.jupiter.api.DisplayNameGenerator;
import org.junit.jupiter.api.IndicativeSentencesGeneration;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

import ch.fhnw.thga.gradleplugins.fregeproject.FregeProjectBuilder;
import ch.fhnw.thga.gradleplugins.fregeproject.FregeSourceFile;
import ch.fhnw.thga.gradleplugins.fregeproject.ProjectRoot;

public class CompileFregeTaskFunctionalTest
{
    private static final String NEW_LINE               = System.lineSeparator();
    private static final FregeSourceFile COMPLETION_FR = new FregeSourceFile(
        "src/main/frege/ch/fhnw/thga/Completion.fr",
        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
        )
    );
    @Nested
    @IndicativeSentencesGeneration(
        separator = " -> ",
        generator = DisplayNameGenerator.ReplaceUnderscores.class
    )
    class Compile_frege_task_works {

        @Test
        void given_frege_code_in_default_source_dir_and_minimal_build_file_config(
            @TempDir File testProjectDir)
            throws Exception
        {
            String minimalBuildFileConfig = createFregeSection(
                FregeDTOBuilder
                .builder()
                .version("'3.25.84'")
                .release("'3.25alpha'")
                .build()
            );

            Project project = FregeProjectBuilder
                .builder()
                .projectRoot(testProjectDir)
                .buildFile(minimalBuildFileConfig)
                .fregeSourceFiles(() -> Stream.of(COMPLETION_FR))
                .build();
            
            BuildResult result = runGradleTask(testProjectDir, COMPILE_FREGE_TASK_NAME);

            assertTrue(
                project
                .getTasks()
                .getByName(COMPILE_FREGE_TASK_NAME) 
                instanceof CompileFregeTask
            );
            assertEquals(
                SUCCESS,
                result.task(":" + COMPILE_FREGE_TASK_NAME).getOutcome()
            );
            assertTrue(testProjectDir
                .toPath()
                .resolve("build/classes/main/frege/ch/fhnw/thga/Completion.java")
                .toFile()
                .exists());
            assertTrue(testProjectDir
                .toPath()
                .resolve("build/classes/main/frege/ch/fhnw/thga/Completion.class")
                .toFile()
                .exists());
        }
        @Test
        void given_frege_code_and_many_compiler_flags(
            @TempDir File testProjectDir)
            throws Exception
        {
            String buildConfigWithCompilerFlags = createFregeSection(
                FregeDTOBuilder
                .builder()
                .version("'3.25.84'")
                .release("'3.25alpha'")
                .compilerFlags("['-v', '-make', '-O', '-hints']")
                .build()
            );
            Project project = FregeProjectBuilder
                .builder()
                .projectRoot(testProjectDir)
                .buildFile(buildConfigWithCompilerFlags)
                .fregeSourceFiles(() -> Stream.of(COMPLETION_FR))
                .build();
            
            BuildResult result = runGradleTask(testProjectDir, COMPILE_FREGE_TASK_NAME);

            assertTrue(
                project
                .getTasks()
                .getByName(COMPILE_FREGE_TASK_NAME) 
                instanceof CompileFregeTask
            );
            assertEquals(
                SUCCESS,
                result.task(":" + COMPILE_FREGE_TASK_NAME).getOutcome()
            );
            assertTrue(testProjectDir
                .toPath()
                .resolve("build/classes/main/frege/ch/fhnw/thga/Completion.java")
                .toFile()
                .exists()
            );
            assertTrue(testProjectDir
                .toPath()
                .resolve("build/classes/main/frege/ch/fhnw/thga/Completion.class")
                .toFile()
                .exists()
            );
        }
        @Test
        void given_frege_code_in_custom_source_and_output_dir_and_minimal_build_file_config(
            @TempDir File testProjectDir
        )
            throws Exception
        {
            String customSourceAndOutputBuildFileConfig = createFregeSection(
                FregeDTOBuilder
                .builder()
                .version("'3.25.84'")
                .release("'3.25alpha'")
                .mainSourceDir("layout.projectDirectory.dir('src/frege')")
                .outputDir("layout.buildDirectory.dir('frege')")
                .build()
            );
            Project project = FregeProjectBuilder
                .builder()
                .projectRoot(testProjectDir)
                .buildFile(customSourceAndOutputBuildFileConfig)
                .fregeSourceFiles(() -> Stream.of(new FregeSourceFile(
                    "src/frege/ch/fhnw/thga/Completion.fr",
                    COMPLETION_FR.getFregeSourceCode())))
                .build();

            BuildResult result = runGradleTask(testProjectDir, COMPILE_FREGE_TASK_NAME);
            System.out.println(result.getOutput());
            assertTrue(
                project
                .getTasks()
                .getByName(COMPILE_FREGE_TASK_NAME)
                instanceof CompileFregeTask
            );
            assertEquals(
                SUCCESS,
                result.task(":" + COMPILE_FREGE_TASK_NAME).getOutcome()
            );
            assertTrue(testProjectDir
                .toPath()
                .resolve("build/frege/ch/fhnw/thga/Completion.java")
                .toFile()
                .exists()
            );
            assertTrue(testProjectDir
                .toPath()
                .resolve("build/frege/ch/fhnw/thga/Completion.class")
                .toFile()
                .exists()
            );
        }
    } 
}