aboutsummaryrefslogtreecommitdiff
path: root/src/functionalTest/java/ch/fhnw/thga/gradleplugins/RunFregeTaskFunctionalTest.java
blob: ab93b3e7c0b2702c050e9c28229ed58c785b467f (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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package ch.fhnw.thga.gradleplugins;

import static ch.fhnw.thga.gradleplugins.FregeExtension.DEFAULT_RELATIVE_SOURCE_DIR;
import static ch.fhnw.thga.gradleplugins.FregePlugin.RUN_FREGE_TASK_NAME;
import static ch.fhnw.thga.gradleplugins.SharedTaskLogic.NEW_LINE;
import static ch.fhnw.thga.gradleplugins.SharedFunctionalTestLogic.createFregeSection;
import static ch.fhnw.thga.gradleplugins.SharedFunctionalTestLogic.runAndFailGradleTask;
import static ch.fhnw.thga.gradleplugins.SharedFunctionalTestLogic.runGradleTask;
import static ch.fhnw.thga.gradleplugins.SharedFunctionalTestLogic.MINIMAL_BUILD_FILE_CONFIG;
import static ch.fhnw.thga.gradleplugins.SharedFunctionalTestLogic.COMPLETION_FR;
import static ch.fhnw.thga.gradleplugins.SharedFunctionalTestLogic.assertFileDoesNotExist;
import static org.gradle.testkit.runner.TaskOutcome.FAILED;
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.Test;
import org.junit.jupiter.api.io.TempDir;

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

public class RunFregeTaskFunctionalTest
{
    private static final FregeSourceFile MAIN_FR = new FregeSourceFile(
        String.format(
            "%s/%s",
            DEFAULT_RELATIVE_SOURCE_DIR,
            "ch/fhnw/thga/Main.fr"
        ),
        String.join(
            NEW_LINE,
            "module ch.fhnw.thga.Main where",
            NEW_LINE,
            NEW_LINE,
            "  main = do",
            NEW_LINE,
            "    println \"Frege rocks\"",
            NEW_LINE
        )
    );
    @Nested
    @IndicativeSentencesGeneration(
        separator = " -> ",
        generator = DisplayNameGenerator.ReplaceUnderscores.class
    )
    class Run_frege_task_works
    {
        @Test
        void given_frege_file_with_main_function_and_main_module_config(
            @TempDir File testProjectDir)
            throws Exception 
        {
            String mainBuildConfig = createFregeSection(
                FregeDTOBuilder
                .builder()
                .version("'3.25.84'")
                .release("'3.25alpha'")
                .mainModule("'ch.fhnw.thga.Main'")
                .build()
            );
            Project project = FregeProjectBuilder
                .builder()
                .projectRoot(testProjectDir)
                .buildFile(mainBuildConfig)
                .fregeSourceFiles(() -> Stream.of(MAIN_FR))
                .build();
            
            BuildResult result = runGradleTask(testProjectDir, RUN_FREGE_TASK_NAME);
            
            assertTrue(
                project
                .getTasks()
                .getByName(RUN_FREGE_TASK_NAME) 
                instanceof RunFregeTask
            );
            assertEquals(
                SUCCESS,
                result.task(":" + RUN_FREGE_TASK_NAME).getOutcome()
            );
            assertTrue(result.getOutput().contains("Frege rocks"));
        }

        @Test
        void given_frege_file_with_main_function_and_main_module_command_line_option(
            @TempDir File testProjectDir)
            throws Exception
        {
            Project project = FregeProjectBuilder
                .builder()
                .projectRoot(testProjectDir)
                .buildFile(MINIMAL_BUILD_FILE_CONFIG)
                .fregeSourceFiles(() -> Stream.of(MAIN_FR))
                .build();
            
            BuildResult result = runGradleTask(
                testProjectDir,
                RUN_FREGE_TASK_NAME,
                "--mainModule=ch.fhnw.thga.Main");
            
            assertTrue(
                project
                .getTasks()
                .getByName(RUN_FREGE_TASK_NAME) 
                instanceof RunFregeTask
            );
            assertEquals(
                SUCCESS,
                result.task(":" + RUN_FREGE_TASK_NAME).getOutcome()
            );
            assertTrue(result.getOutput().contains("Frege rocks"));
        }

        @Test
        void given_two_frege_files_then_only_the_specified_main_module_is_compiled(
            @TempDir File testProjectDir)
            throws Exception
        {
            Project project = FregeProjectBuilder
                .builder()
                .projectRoot(testProjectDir)
                .buildFile(MINIMAL_BUILD_FILE_CONFIG)
                .fregeSourceFiles(() -> Stream.of(MAIN_FR, COMPLETION_FR))
                .build();
            
            BuildResult result = runGradleTask(
                testProjectDir,
                RUN_FREGE_TASK_NAME,
                "--mainModule=ch.fhnw.thga.Main");
            
            assertTrue(
                project
                .getTasks()
                .getByName(RUN_FREGE_TASK_NAME) 
                instanceof RunFregeTask
            );
            assertEquals(
                SUCCESS,
                result.task(":" + RUN_FREGE_TASK_NAME).getOutcome()
            );
            assertTrue(result.getOutput().contains("Frege rocks"));
            assertFileDoesNotExist(
                testProjectDir,
                "build/classes/main/frege/ch/fhnw/thga/Completion.java"
            );
            assertFileDoesNotExist(
                testProjectDir,
                "build/classes/main/frege/ch/fhnw/thga/Completion.class"
            );
        }
    }
    
    @Nested
    @IndicativeSentencesGeneration(
        separator = " -> ",
        generator = DisplayNameGenerator.ReplaceUnderscores.class
    )
    class Run_frege_task_fails
    {
        @Test
        void given_frege_file_without_main_function(
            @TempDir File testProjectDir)
            throws Exception
        {
            String mainBuildConfig = createFregeSection(
                FregeDTOBuilder
                .builder()
                .version("'3.25.84'")
                .release("'3.25alpha'")
                .mainModule("'ch.fhnw.thga.Main'")
                .build()
            );
            Project project = FregeProjectBuilder
                .builder()
                .projectRoot(testProjectDir)
                .buildFile(mainBuildConfig)
                .fregeSourceFiles(() -> Stream.of(new FregeSourceFile(
                    String.format(
                        "%s/%s",
                        DEFAULT_RELATIVE_SOURCE_DIR,
                        "ch/fhnw/thga/Main.fr"
                    ),
                    String.join(
                        NEW_LINE,
                        "module ch.fhnw.thga.Main where",
                        NEW_LINE,
                        NEW_LINE,
                        "  add a b = a + b",
                        NEW_LINE
                    )
                )))
                .build();

            BuildResult result = runAndFailGradleTask(
                testProjectDir,
                RUN_FREGE_TASK_NAME
            );

            assertTrue(
                project
                .getTasks()
                .getByName(RUN_FREGE_TASK_NAME) 
                instanceof RunFregeTask
            );
            assertEquals(
                FAILED,
                result.task(":" + RUN_FREGE_TASK_NAME).getOutcome()
            );
            assertTrue(result.getOutput().contains("Main method not found"));
        }
    }
}