Skip to content

Commit f89eb5e

Browse files
authored
merge: More readable negative test expects (#1266)
Fix #1265
2 parents efb8707 + ba6e5d3 commit f89eb5e

File tree

5 files changed

+46
-49
lines changed

5 files changed

+46
-49
lines changed

cli-impl/src/test/java/org/aya/test/LibraryTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public class LibraryTest {
4747
@ParameterizedTest
4848
@ValueSource(strings = {"success"})
4949
public void testOnDisk(@NotNull String libName) throws IOException {
50-
var libRoot = TestRunner.DEFAULT_TEST_DIR.resolve(libName);
50+
var libRoot = TestRunner.TEST_DIR.resolve(libName);
5151

5252
FileUtil.deleteRecursively(libRoot.resolve("build"));
5353
// Full rebuild
@@ -143,7 +143,7 @@ public void assertDelegate() {
143143
}
144144
}
145145

146-
public static final Path DIR = TestRunner.DEFAULT_TEST_DIR.resolve("success");
146+
public static final Path DIR = TestRunner.TEST_DIR.resolve("success");
147147

148148
private static int compile(@NotNull Path root) throws IOException {
149149
return compile(TestRunner.flags(), root);

cli-impl/src/test/java/org/aya/test/TestRunner.java

Lines changed: 40 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// Use of this source code is governed by the MIT license that can be found in the LICENSE.md file.
33
package org.aya.test;
44

5-
import com.intellij.openapi.util.text.Strings;
65
import kala.collection.Seq;
76
import kala.collection.SeqView;
87
import org.aya.cli.single.CompilerFlags;
@@ -25,17 +24,18 @@
2524
import java.nio.file.Paths;
2625
import java.util.Locale;
2726

28-
import static org.junit.jupiter.api.Assertions.assertEquals;
27+
import static org.junit.jupiter.api.Assertions.assertTrue;
2928
import static org.junit.jupiter.api.Assertions.fail;
3029

3130
public class TestRunner {
32-
public static final @NotNull Path DEFAULT_TEST_DIR = Paths.get("src", "test", "resources").toAbsolutePath();
33-
public static final @NotNull Path TMP_FILE = DEFAULT_TEST_DIR.resolve("tmp.aya");
31+
public static final @NotNull Path TEST_DIR = Paths.get("src", "test", "resources").toAbsolutePath();
32+
private static final @NotNull Path FIXTURE_DIR = TEST_DIR.resolve("negative");
33+
private static final @NotNull Path TMP_FILE = TEST_DIR.resolve("tmp.aya");
3434
public static final @NotNull SourceFileLocator LOCATOR = new SourceFileLocator() { };
3535
@BeforeAll public static void startDash() { Global.NO_RANDOM_NAME = true; }
3636

3737
@Test public void negative() throws Exception {
38-
Seq.of(
38+
var toCheck = Seq.of(
3939
ParseError.class,
4040
ExprTyckError.class,
4141
GoalAndMeta.class,
@@ -45,7 +45,19 @@ public class TestRunner {
4545
TerckError.class,
4646
PatCohError.class,
4747
ClassError.class
48-
).forEachChecked(TestRunner::expectFixture);
48+
).mapNotNullChecked(TestRunner::expectFixture);
49+
if (toCheck.isNotEmpty()) {
50+
new ProcessBuilder("git", "add", FIXTURE_DIR.toString())
51+
.inheritIO()
52+
.start()
53+
.waitFor();
54+
}
55+
for (var file : toCheck) {
56+
var name = file.toString();
57+
var proc = new ProcessBuilder("git", "diff", "--cached", name).start();
58+
var output = new String(proc.getInputStream().readAllBytes());
59+
assertTrue(output.isBlank(), output);
60+
}
4961
Files.deleteIfExists(TMP_FILE);
5062
}
5163

@@ -60,55 +72,40 @@ public static void main(String... args) throws Exception {
6072
new TestRunner().negative();
6173
}
6274

63-
private static String instantiateVars(String template) {
64-
return template.replace("$FILE", TMP_FILE.toString());
65-
}
66-
67-
private static String instantiateHoles(String template) {
75+
private static String replaceFileName(String template) {
6876
return template.replace(TMP_FILE.toString(), "$FILE");
6977
}
7078

71-
private static void checkOutput(Path expectedOutFile, String hookOut) {
72-
try {
73-
var output = Strings.convertLineSeparators(hookOut);
74-
var expected = instantiateVars(Strings.convertLineSeparators(
75-
Files.readString(expectedOutFile, StandardCharsets.UTF_8)));
76-
assertEquals(expected, output, expectedOutFile.getFileName().toString());
77-
} catch (IOException e) {
78-
fail("error reading file " + expectedOutFile.toAbsolutePath());
79-
}
80-
}
81-
82-
private static void expectFixture(Class<?> fixturesClass) throws IllegalAccessException, IOException {
83-
var result = runFixtureClass(fixturesClass);
84-
var expectedOutFile = DEFAULT_TEST_DIR
85-
.resolve("negative")
86-
.resolve(fixturesClass.getSimpleName() + ".txt");
79+
/// @return not null for a file to check, null if we're good to go
80+
private static Path expectFixture(Class<?> fixturesClass) throws IllegalAccessException, IOException, InterruptedException {
81+
var result = replaceFileName(runFixtureClass(fixturesClass));
82+
var expectedOutFile = FIXTURE_DIR.resolve(fixturesClass.getSimpleName() + ".txt");
8783
if (Files.exists(expectedOutFile)) {
88-
checkOutput(expectedOutFile, result);
84+
writeWorkflow(expectedOutFile, result);
85+
return expectedOutFile;
8986
} else {
90-
System.out.println(); // add line break after `--->`
91-
generateWorkflow(expectedOutFile, result);
87+
System.out.println(); // add line break before `NOTE`
88+
writeWorkflow(expectedOutFile, result);
89+
System.out.printf(Locale.getDefault(),
90+
"""
91+
NOTE: write the following output to `%s`.
92+
----------------------------------------
93+
%s
94+
----------------------------------------
95+
""",
96+
expectedOutFile.getFileName(),
97+
result
98+
);
9299
}
100+
return null;
93101
}
94102

95-
private static void generateWorkflow(Path expectedOutFile, String hookOut) {
96-
hookOut = instantiateHoles(hookOut);
103+
private static void writeWorkflow(Path expectedOutFile, String hookOut) {
97104
try {
98105
FileUtil.writeString(expectedOutFile, hookOut);
99106
} catch (IOException e) {
100107
fail("error generating todo file " + expectedOutFile.toAbsolutePath());
101108
}
102-
System.out.printf(Locale.getDefault(),
103-
"""
104-
NOTE: write the following output to `%s`.
105-
----------------------------------------
106-
%s
107-
----------------------------------------
108-
""",
109-
expectedOutFile.getFileName(),
110-
hookOut
111-
);
112109
}
113110

114111
private static String runFixtureClass(Class<?> fixturesClass)
@@ -141,7 +138,7 @@ private static void runSingleCase(String code, SingleFileCompiler compiler) thro
141138
}
142139

143140
public static @NotNull CompilerFlags flags() {
144-
var modulePaths = SeqView.of(DEFAULT_TEST_DIR.resolve("shared/src"));
141+
var modulePaths = SeqView.of(TEST_DIR.resolve("shared/src"));
145142
return new CompilerFlags(CompilerFlags.Message.ASCII,
146143
false, false, null, modulePaths, null);
147144
}

gradle/libs.versions.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ java = "22"
99
# https://github.com/JetBrains/java-annotations
1010
annotations = "26.0.1"
1111
# https://github.com/Glavo/kala-common
12-
kala = "0.76.0"
12+
kala = "0.78.0"
1313
# https://picocli.info
1414
picocli = "4.7.6"
1515
# https://repo1.maven.org/maven2/org/aya-prover/upstream/build-util
16-
aya-upstream = "0.0.31"
16+
aya-upstream = "0.0.33"
1717
# https://github.com/jline/jline3
1818
jline = "3.28.0"
1919
# https://junit.org/junit5

producer/src/main/java/org/aya/producer/AyaParserImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
public record AyaParserImpl(@NotNull Reporter reporter) implements GenericAyaParser {
2626
public @NotNull GenericNode<?> parseNode(@NotNull String code) {
2727
var parser = new AyaFleetParser();
28-
return new MarkerNodeWrapper(code, parser.parse(code));
28+
return new MarkerNodeWrapper(parser.parse(code), code);
2929
}
3030

3131
@Override public @NotNull WithPos<Expr> expr(@NotNull String code, @NotNull SourcePos sourcePos) {

producer/src/main/java/org/aya/producer/flcl/FlclParser.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public FlclParser(@NotNull Reporter reporter, @NotNull SourceFile file) {
3131

3232
public @NotNull FlclToken.File computeAst() {
3333
var text = file.sourceCode();
34-
var node = new MarkerNodeWrapper(text, new FlclFleetParser().parse(text));
34+
var node = new MarkerNodeWrapper(new FlclFleetParser().parse(text), text);
3535
node.childrenOfType(FlclPsiElementTypes.RULE).forEach(rule -> {
3636
var idChildren = rule.childrenOfType(FlclPsiElementTypes.ID)
3737
.map(MarkerNodeWrapper::tokenText)

0 commit comments

Comments
 (0)