diff options
| author | Cliff Click <cliffc@acm.org> | 2026-10-03 14:03:48 -0700 |
|---|---|---|
| committer | Cliff Click <cliffc@acm.org> | 2026-10-03 14:03:48 -0700 |
| commit | 22df891893d30e9906d0b14fc3ae5d8e67d500b2 (patch) | |
| tree | 2bda142f92febc8b26f1cabd4bb0c46d1864f34b /transpile-tests/src/main/java | |
| parent | c3958e34c0de21df52e57e47e4f47ffe6464f138 (diff) | |
| download | Simple-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/src/main/java')
| -rw-r--r-- | transpile-tests/src/main/java/com/seaofnodes/simple/transpile/AstVisitor.java | 27 | ||||
| -rw-r--r-- | transpile-tests/src/main/java/com/seaofnodes/simple/transpile/JUnitParser.java | 10 |
2 files changed, 23 insertions, 14 deletions
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)); |
