aboutsummaryrefslogtreecommitdiff
path: root/src/functionalTest/java/ch/fhnw/thga/gradleplugins/ReplFregeTaskFunctionalTest.java
blob: 792883df950a8dbe0ffef034c92674d5345344b6 (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
package ch.fhnw.thga.gradleplugins;

import static ch.fhnw.thga.gradleplugins.FregeExtension.DEFAULT_RELATIVE_SOURCE_DIR;
import static ch.fhnw.thga.gradleplugins.FregePlugin.REPL_FREGE_TASK_NAME;
import static ch.fhnw.thga.gradleplugins.SharedFunctionalTestLogic.COMPLETION_FR;
import static ch.fhnw.thga.gradleplugins.SharedFunctionalTestLogic.MINIMAL_BUILD_FILE_CONFIG;
import static ch.fhnw.thga.gradleplugins.SharedTaskLogic.NEW_LINE;
import static ch.fhnw.thga.gradleplugins.SharedFunctionalTestLogic.assertFileDoesNotExist;
import static ch.fhnw.thga.gradleplugins.SharedFunctionalTestLogic.assertFileExists;
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 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.nio.file.Paths;
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 ReplFregeTaskFunctionalTest
{
    @Nested
    @IndicativeSentencesGeneration(
        generator = DisplayNameGenerator.ReplaceUnderscores.class)
    class Repl_frege_task_works
    {
        @Test
        void given_minimal_build_file_config_with_repl_module(
            @TempDir File testProjectDir)
            throws Exception
        {
            String replModuleConfig = createFregeSection(
                FregeDTOBuilder
                .builder()
                .version("'3.25.84'")
                .release("'3.25alpha'")
                .replModule("'ch.fhnw.thga.Completion'")
                .build()
            );
            Project project = FregeProjectBuilder
                .builder()
                .projectRoot(testProjectDir)
                .buildFile(replModuleConfig)
                .fregeSourceFiles(() -> Stream.of(COMPLETION_FR))
                .build();
                                                                                         
            BuildResult result = runGradleTask(testProjectDir, REPL_FREGE_TASK_NAME);
                                                                                         
            assertTrue(
                project
                .getTasks()
                .getByName(REPL_FREGE_TASK_NAME)
                instanceof ReplFregeTask);
            assertEquals(
                SUCCESS,
                result.task(":" + REPL_FREGE_TASK_NAME).getOutcome());
            assertTrue(result.getOutput().contains("java -cp"));
            assertTrue(result.getOutput().contains("frege3.25.84.jar"));
            assertTrue(result.getOutput().contains(
                Paths.get(COMPLETION_FR.getFregeModulePath()).normalize().toString())
            );
            assertFileDoesNotExist(
                testProjectDir,
                "build/classes/main/frege/ch/fhnw/thga/Completion.java"
            );
            assertFileDoesNotExist(
                testProjectDir,
                "build/classes/main/frege/ch/fhnw/thga/Completion.class"
            );
        }

        @Test
        void given_dependent_frege_files_with_command_line_repl_module_option(
            @TempDir File testProjectDir)
            throws Exception
        {
            String frobCode = String.join(
                NEW_LINE,
                "module ch.fhnw.thga.Frob where",
                NEW_LINE,
                NEW_LINE,
                "import ch.fhnw.thga.Completion (complete)",
                NEW_LINE,
                "frob i = complete $ i + i",
                NEW_LINE
            );
            FregeSourceFile frob_FR = new FregeSourceFile(
                String.format(
                    "%s/%s",
                    DEFAULT_RELATIVE_SOURCE_DIR,
                    "ch/fhnw/thga/Frob.fr"
                ),
                frobCode);
            Project project = FregeProjectBuilder
                .builder()
                .projectRoot(testProjectDir)
                .buildFile(MINIMAL_BUILD_FILE_CONFIG)
                .fregeSourceFiles(() -> Stream.of(COMPLETION_FR, frob_FR))
                .build();
            
            BuildResult result = runGradleTask(
                testProjectDir,
                REPL_FREGE_TASK_NAME,
                "--replModule=ch.fhnw.thga.Frob"
            );
            
            assertTrue(
                project
                .getTasks()
                .getByName(REPL_FREGE_TASK_NAME)
                instanceof ReplFregeTask);
            assertEquals(
                SUCCESS,
                result.task(":" + REPL_FREGE_TASK_NAME).getOutcome());
            assertTrue(result.getOutput().contains("java -cp"));
            assertTrue(result.getOutput().contains("frege3.25.84.jar"));
            assertTrue(result.getOutput().contains(
                Paths.get(frob_FR.getFregeModulePath()).normalize().toString())
            );
            assertFileExists(
                testProjectDir,
                "build/classes/main/frege/ch/fhnw/thga/Completion.java"
            );
            assertFileExists(
                testProjectDir,
                "build/classes/main/frege/ch/fhnw/thga/Completion.class"
            );
            assertFileDoesNotExist(
                testProjectDir,
                "build/classes/main/frege/ch/fhnw/thga/Frob.java"
            );
            assertFileDoesNotExist(
                testProjectDir,
                "build/classes/main/frege/ch/fhnw/thga/Frob.class"
            );
        }
    }

    @Nested
    @IndicativeSentencesGeneration(
        generator = DisplayNameGenerator.ReplaceUnderscores.class)
    class Repl_frege_task_fails
    {
        @Test
        void given_minimal_build_file_config_without_repl_module(
            @TempDir File testProjectDir)
            throws Exception
        {
            Project project = FregeProjectBuilder
                .builder()
                .projectRoot(testProjectDir)
                .buildFile(MINIMAL_BUILD_FILE_CONFIG)
                .fregeSourceFiles(() -> Stream.of(COMPLETION_FR))
                .build();
                                                                                         
            BuildResult result = runAndFailGradleTask(testProjectDir, REPL_FREGE_TASK_NAME);

            assertTrue(
                project
                .getTasks()
                .getByName(REPL_FREGE_TASK_NAME)
                instanceof ReplFregeTask
            );
            assertEquals(
                FAILED,
                result.task(":" + REPL_FREGE_TASK_NAME).getOutcome()
            );
        }
    }
}