summaryrefslogtreecommitdiff
path: root/transpile-tests
diff options
context:
space:
mode:
authorCliff Click <cliffc@acm.org>2026-10-03 14:03:48 -0700
committerCliff Click <cliffc@acm.org>2026-10-03 14:03:48 -0700
commit22df891893d30e9906d0b14fc3ae5d8e67d500b2 (patch)
tree2bda142f92febc8b26f1cabd4bb0c46d1864f34b /transpile-tests
parentc3958e34c0de21df52e57e47e4f47ffe6464f138 (diff)
downloadSimple-22df891893d30e9906d0b14fc3ae5d8e67d500b2.tar.gz
Simple-22df891893d30e9906d0b14fc3ae5d8e67d500b2.zip
Chapter renumber, and split ch 17
Renumber chapters 10a, 10b, 11, 12, 13, 14 -> 10, 11, 13, 12, [11,14]. Split ch 17 into a 17a and 17b; shuffle around im/mutable syntax to be consistent, and present it more slowly. THe new immutable syntax is pushed through all later chapters
Diffstat (limited to 'transpile-tests')
-rw-r--r--transpile-tests/README.md4
-rw-r--r--transpile-tests/src/main/java/com/seaofnodes/simple/transpile/AstVisitor.java27
-rw-r--r--transpile-tests/src/main/java/com/seaofnodes/simple/transpile/JUnitParser.java10
-rw-r--r--transpile-tests/src/test/java/com/seaofnodes/simple/transpile/TranspilerTest.java2
4 files changed, 29 insertions, 14 deletions
diff --git a/transpile-tests/README.md b/transpile-tests/README.md
index aafd3161..82e17f71 100644
--- a/transpile-tests/README.md
+++ b/transpile-tests/README.md
@@ -5,6 +5,10 @@ It can parse the JUnit tests to automatically translate most of the boilerplate.
Some manual tweaks will still be required though, because only common patterns are detected
(e.g. 01: testSimpleProgram, testZero; 04: testVarArg; 08: testRegress5; 09: testMeet, ...)
+Chapter discovery includes suffixes such as `17a` and `17b`. Tests that use
+computed Java inputs or unsupported assertions are skipped individually, so
+the remaining tests in the same class can still be exported.
+
## Usage
Just call `JUnitParser.parseRepository` to get a list of parsed test classes for each chapter,
diff --git a/transpile-tests/src/main/java/com/seaofnodes/simple/transpile/AstVisitor.java b/transpile-tests/src/main/java/com/seaofnodes/simple/transpile/AstVisitor.java
index bd174d4b..1c9352d5 100644
--- a/transpile-tests/src/main/java/com/seaofnodes/simple/transpile/AstVisitor.java
+++ b/transpile-tests/src/main/java/com/seaofnodes/simple/transpile/AstVisitor.java
@@ -16,7 +16,7 @@ class AstVisitor extends TreeScanner<Void, Void> {
@Override
public Void visitClass(ClassTree node, Void unused) {
var name = node.getSimpleName().toString();
- if (name.matches("Chapter\\d+Test")) {
+ if (name.matches("Chapter\\d+[a-z]?Test")) {
var backup = className;
className = name;
super.visitClass(node, unused);
@@ -30,7 +30,16 @@ class AstVisitor extends TreeScanner<Void, Void> {
if (className != null) {
current = new TestMethod();
current.name = node.getName().toString();
- super.visitMethod(node, unused);
+ try {
+ super.visitMethod(node, unused);
+ } catch (IllegalArgumentException unsupported) {
+ // Only export the common literal-based patterns we understand.
+ // A computed input must not discard the rest of its test class.
+ current = null;
+ return null;
+ } finally {
+ inCatch = false;
+ }
results.compute(className, (k, v) -> {
if (v == null)
v = new TestClass(className, new ArrayList<>());
@@ -54,13 +63,13 @@ class AstVisitor extends TreeScanner<Void, Void> {
if (methodSelect.endsWith("assertEquals")) {
if (inCatch) {
if (args.size() != 2)
- throw new RuntimeException("Unexpected number of arguments " + node);
+ throw new IllegalArgumentException("Unexpected number of arguments " + node);
if (args.get(1).toString().contains("e.getMessage()")) {
current.parseErrorMessage = (String) literal(args.get(0));
}
} else {
if (args.size() != 2)
- throw new RuntimeException("Unexpected number of arguments " + node);
+ throw new IllegalArgumentException("Unexpected number of arguments " + node);
if (args.get(1).toString().contains("evaluate(")) {
// visit evaluate(..) or evaluate(..).toString()
var eval = args.get(1).accept(new TreeScanner<MethodInvocationTree, Void>() {
@@ -100,7 +109,7 @@ class AstVisitor extends TreeScanner<Void, Void> {
}
if (methodSelect.endsWith("prettyPrint")) {
if (args.size() < 2 || args.size() > 3 || !args.get(0).toString().equals("stop") || !args.get(1).toString().equals("99"))
- throw new RuntimeException("unexpected prettyPrint args " + node);
+ throw new IllegalArgumentException("unexpected prettyPrint args " + node);
current.irPrinter = true;
current.irPrinterLLVM = args.size() == 3 && (Boolean) literal(args.get(2));
}
@@ -122,11 +131,11 @@ class AstVisitor extends TreeScanner<Void, Void> {
case String s -> {
var parts = s.split("[()]");
if (parts.length != 2 | !parts[0].equals("TypeInteger.constant"))
- throw new RuntimeException("Unexpected Argument " + s);
+ throw new IllegalArgumentException("Unexpected Argument " + s);
yield new TestMethod.Arg.IntConstant(Long.parseLong(parts[1]));
}
};
- default -> throw new RuntimeException("Parser constructor with unexpected arguments: " + node);
+ default -> throw new IllegalArgumentException("Parser constructor with unexpected arguments: " + node);
}
current.parserInput = (String) literal(args.get(0));
}
@@ -143,6 +152,8 @@ class AstVisitor extends TreeScanner<Void, Void> {
}
private Object literal(ExpressionTree t) {
- return ((LiteralTree) t).getValue();
+ if (t instanceof LiteralTree literal)
+ return literal.getValue();
+ throw new IllegalArgumentException("Expected a literal: " + t);
}
}
diff --git a/transpile-tests/src/main/java/com/seaofnodes/simple/transpile/JUnitParser.java b/transpile-tests/src/main/java/com/seaofnodes/simple/transpile/JUnitParser.java
index 2db2a08c..440427e0 100644
--- a/transpile-tests/src/main/java/com/seaofnodes/simple/transpile/JUnitParser.java
+++ b/transpile-tests/src/main/java/com/seaofnodes/simple/transpile/JUnitParser.java
@@ -28,7 +28,7 @@ public class JUnitParser {
Arrays.sort(inputs);
for (File file : inputs) {
var chapter = file.getName();
- if (chapter.matches("chapter\\d+")) {
+ if (chapter.matches("chapter\\d+[a-z]?") && Files.isRegularFile(file.toPath().resolve("pom.xml"))) {
chapters.put(chapter, parseChapter(file.toPath()));
}
}
@@ -44,7 +44,7 @@ public class JUnitParser {
Files.walkFileTree(input, Set.of(), 20, new SimpleFileVisitor<>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
- if (file.getFileName().toString().matches("Chapter\\d+Test\\.java")) {
+ if (file.getFileName().toString().matches("Chapter\\d+[a-z]?Test\\.java")) {
tests.add(file);
}
return FileVisitResult.CONTINUE;
@@ -59,10 +59,8 @@ public class JUnitParser {
var task = (JavacTask) compiler.getTask(null, null, null, null, null, compilationUnits);
var visitor = new AstVisitor();
- for (var tree : task.parse()) {
- try { tree.accept(visitor, null); }
- catch( IllegalArgumentException iae ) {}
- }
+ for (var tree : task.parse())
+ tree.accept(visitor, null);
var testClasses = new ArrayList<>(visitor.results.values());
testClasses.sort(comparing(TestClass::name));
diff --git a/transpile-tests/src/test/java/com/seaofnodes/simple/transpile/TranspilerTest.java b/transpile-tests/src/test/java/com/seaofnodes/simple/transpile/TranspilerTest.java
index a2c3a782..fb6384ee 100644
--- a/transpile-tests/src/test/java/com/seaofnodes/simple/transpile/TranspilerTest.java
+++ b/transpile-tests/src/test/java/com/seaofnodes/simple/transpile/TranspilerTest.java
@@ -15,6 +15,8 @@ public class TranspilerTest {
assertEquals("transpile-tests", cwd.getFileName().toString());
var all = JUnitParser.parseRepository(cwd.getParent());
assertTrue(all.size() >= 14);
+ assertTrue(all.get("chapter17a").stream().anyMatch(c -> c.name().equals("Chapter17aTest")));
+ assertTrue(all.get("chapter17b").stream().anyMatch(c -> c.name().equals("Chapter17bTest")));
var chapter09 = all.get("chapter09");
assertEquals(9, chapter09.size());