-
Notifications
You must be signed in to change notification settings - Fork 4
/
SpecoEoTest.java
161 lines (154 loc) · 5.87 KB
/
SpecoEoTest.java
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
/*
* The MIT License (MIT)
*
* Copyright (c) 2022 Objectionary
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.eolang.speco;
import java.io.IOException;
import java.io.StringWriter;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.Arrays;
import java.util.Map;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.SystemUtils;
import org.eolang.jucs.ClasspathSource;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.condition.DisabledOnOs;
import org.junit.jupiter.api.condition.OS;
import org.junit.jupiter.api.io.TempDir;
import org.junit.jupiter.params.ParameterizedTest;
import org.yaml.snakeyaml.Yaml;
/**
* Tests that check entire Speco algorithm applied directly to raw EO programs.
*
* @since 0.0.1
* @todo #63:30min enable convertsFromEO test:
* we need to remove @disable annotation for these test
* when we provide the rule 3 implementation and add an appropriate transformation.
*/
class SpecoEoTest {
/**
* The number of lines that the command "eoc link" outputs.
*/
private static final int INTENT = 11;
/**
* Integration test for conversation from EO.
* @param pack Pack this test data
* @param temp Temporary test dir
* @throws IOException Iff IO error
*/
@Tag("fast")
@Disabled
@DisabledOnOs(OS.WINDOWS)
@ParameterizedTest
@ClasspathSource(value = "org/eolang/speco/packs", glob = "**.yaml")
void convertsFromEo(final String pack, @TempDir final Path temp) throws IOException {
final Map<String, Object> script = new Yaml().load(pack);
MatcherAssert.assertThat(
"Unexpected transformation result",
Files.readString(SpecoEoTest.run(script, temp).resolve("app.eo")),
Matchers.equalTo(script.get("after").toString())
);
}
/**
* Integration test for compilation and result checking program converted from EO.
* @todo #32:30min investigate problem with @DisableOnOs(OS.Windows),
* this is an issue of junit -- https://github.com/junit-team/junit5/issues/2811,
* maybe try to manually delete each created file or use a pat
* or get rid of the @TempDir.
* @param pack Pack this test data
* @param temp Temporary test dir
* @throws java.io.IOException Iff IO error
*/
@Tag("slow")
@DisabledOnOs(OS.WINDOWS)
@ParameterizedTest
@ClasspathSource(value = "org/eolang/speco/packs", glob = "**.yaml")
void compilesFromEo(final String pack, @TempDir final Path temp)
throws IOException, InterruptedException {
final Map<String, Object> script = new Yaml().load(pack);
MatcherAssert.assertThat(
"Unexpected execution result",
SpecoEoTest.dataize(SpecoEoTest.run(script, temp).toString()),
Matchers.equalTo(
script.get("result").toString().split("\\r?\\n")
)
);
}
/**
* Runs Speco.
*
* @param script Yaml data object
* @param temp Path to the temporary dir
* @return Path to the output dir
* @throws IOException Iff IO error
*/
private static Path run(final Map<String, Object> script, final Path temp)
throws IOException {
final Path input = temp.resolve("input");
final Path output = temp.resolve("output");
Files.createDirectories(input);
Files.writeString(
input.resolve("app.eo"),
script.get("before").toString(),
StandardOpenOption.CREATE
);
new Speco(input, output, true).exec();
return output;
}
/**
* Compiles and dataize EO program.
*
* @param target Path to the dir with target EO program
* @return List of lines in output
* @throws IOException Iff IO error
*/
private static String[] dataize(final String target) throws IOException, InterruptedException {
final String executor;
final String flag;
if (SystemUtils.IS_OS_WINDOWS) {
executor = "cmd";
flag = "/c";
} else {
executor = "bash";
flag = "-c";
}
final Process process = new ProcessBuilder(
executor,
flag,
String.format("eoc link -s %s && eoc --alone dataize app && eoc clean", target)
).start();
process.waitFor();
final StringWriter writer = new StringWriter();
IOUtils.copy(process.getInputStream(), writer, Charset.defaultCharset());
process.getInputStream().close();
process.destroy();
final String[] output = writer.toString().split("\\r?\\n");
writer.close();
return Arrays.copyOfRange(output, SpecoEoTest.INTENT, output.length - 1);
}
}