diff --git a/test-gen-plugin/src/main/java/com/dylibso/chicory/maven/JavaTestGen.java b/test-gen-plugin/src/main/java/com/dylibso/chicory/maven/JavaTestGen.java index 316a60fa4..37a9b6c4b 100644 --- a/test-gen-plugin/src/main/java/com/dylibso/chicory/maven/JavaTestGen.java +++ b/test-gen-plugin/src/main/java/com/dylibso/chicory/maven/JavaTestGen.java @@ -2,6 +2,7 @@ import com.dylibso.chicory.maven.wast.Command; import com.dylibso.chicory.maven.wast.CommandType; +import com.dylibso.chicory.maven.wast.WasmValue; import com.dylibso.chicory.maven.wast.Wast; import com.fasterxml.jackson.databind.ObjectMapper; import com.github.javaparser.ast.CompilationUnit; @@ -24,6 +25,8 @@ import java.util.stream.Collectors; import static com.dylibso.chicory.maven.StringUtils.*; +import static com.dylibso.chicory.maven.wast.ActionType.INVOKE; +import static com.github.javaparser.StaticJavaParser.parseClassOrInterfaceType; public class JavaTestGen { @@ -32,16 +35,18 @@ public class JavaTestGen { private final Log log; private final File baseDir; private final File sourceTargetFolder; + private final List excludedTests; - public JavaTestGen(Log log, File baseDir, File sourceTargetFolder) { + public JavaTestGen(Log log, File baseDir, File sourceTargetFolder, List excludedTests) { this.log = log; this.baseDir = baseDir; this.sourceTargetFolder = sourceTargetFolder; + this.excludedTests = excludedTests; } - private String INSTANCE_NAME = "instance"; + private static final String INSTANCE_NAME = "instance"; - public void generate(File specFile, File wasmFilesFolder, List excludedTests) { + public void generate(File specFile, File wasmFilesFolder) { Wast wast; try { wast = mapper.readValue(specFile, Wast.class); @@ -77,7 +82,7 @@ public void generate(File specFile, File wasmFilesFolder, List excludedT var testClass = cu.addClass(testName); - MethodDeclaration method = null; + MethodDeclaration method; int testNumber = 0; int moduleInstantiationNumber = 0; for (var cmd: wast.getCommands()) { @@ -86,7 +91,7 @@ public void generate(File specFile, File wasmFilesFolder, List excludedT switch (cmd.getType()) { case MODULE: testClass.addFieldWithInitializer( - new ClassOrInterfaceType("Instance"), + parseClassOrInterfaceType("Instance"), INSTANCE_NAME + moduleInstantiationNumber++, generateModuleInstantiation(cmd, wasmFilesFolder)); break; @@ -134,7 +139,7 @@ private Optional generateFieldExport(String varName, Command cmd, in if (cmd.getAction() != null && cmd.getAction().getField() != null) { var declarator = new VariableDeclarator() .setName(varName) - .setType(new ClassOrInterfaceType("ExportFunction")) + .setType(parseClassOrInterfaceType("ExportFunction")) .setInitializer(new NameExpr(INSTANCE_NAME + instanceNumber + ".getExport(\"" + cmd.getAction().getField() + "\")")); Expression varDecl = new VariableDeclarationExpr(declarator); return Optional.of(varDecl); @@ -160,12 +165,12 @@ private List generateAssert(String varName, Command cmd) { } } - String invocationMethod = null; - switch (cmd.getAction().getType()) { - case INVOKE: - var args = Arrays.stream(cmd.getAction().getArgs()).map(arg -> arg.toWasmValue()).collect(Collectors.joining(", ")); - invocationMethod = ".apply(" + args + ")"; - break; + String invocationMethod; + if (cmd.getAction().getType() == INVOKE) { + var args = Arrays.stream(cmd.getAction().getArgs()).map(WasmValue::toWasmValue).collect(Collectors.joining(", ")); + invocationMethod = ".apply(" + args + ")"; + } else { + throw new IllegalArgumentException("Unhandled action type " + cmd.getAction().getType()); } switch (cmd.getType()) { diff --git a/test-gen-plugin/src/main/java/com/dylibso/chicory/maven/StringUtils.java b/test-gen-plugin/src/main/java/com/dylibso/chicory/maven/StringUtils.java index e2fdc9b37..bcfb4b437 100644 --- a/test-gen-plugin/src/main/java/com/dylibso/chicory/maven/StringUtils.java +++ b/test-gen-plugin/src/main/java/com/dylibso/chicory/maven/StringUtils.java @@ -10,7 +10,7 @@ public static String capitalize(String in) { public static String escapedCamelCase(String in) { var escaped = StringEscapeUtils.escapeJava(in); - var sb = new StringBuffer(); + var sb = new StringBuilder(); var capitalize = false; for (var i = 0; i < escaped.length(); i++) { var character = escaped.charAt(i); diff --git a/test-gen-plugin/src/main/java/com/dylibso/chicory/maven/TestGenMojo.java b/test-gen-plugin/src/main/java/com/dylibso/chicory/maven/TestGenMojo.java index d5164b717..ee6db761a 100644 --- a/test-gen-plugin/src/main/java/com/dylibso/chicory/maven/TestGenMojo.java +++ b/test-gen-plugin/src/main/java/com/dylibso/chicory/maven/TestGenMojo.java @@ -94,7 +94,7 @@ public void execute() throws MojoExecutionException { // Instantiate the utilities var testSuiteDownloader = new TestSuiteDownloader(log); var wast2Json = new Wast2JsonWrapper(log, wabtDownloadTargetFolder, wabtReleasesURL, wabtVersion, osName, compiledWastTargetFolder); - var testGen = new JavaTestGen(log, project.getBasedir(), sourceDestinationFolder); + var testGen = new JavaTestGen(log, project.getBasedir(), sourceDestinationFolder, clean(excludedTests)); // Create destination folders compiledWastTargetFolder.mkdirs(); @@ -113,7 +113,7 @@ public void execute() throws MojoExecutionException { throw new IllegalArgumentException("Wast file " + wastFile.getAbsolutePath() + " not found"); } var wasmFilesFolder = wast2Json.execute(testsuiteFolder.toPath().resolve(spec).toFile()); - testGen.generate(wasmFilesFolder.toPath().resolve(SPEC_JSON).toFile(), wasmFilesFolder, clean(excludedTests)); + testGen.generate(wasmFilesFolder.toPath().resolve(SPEC_JSON).toFile(), wasmFilesFolder); } } catch (Exception e) { throw new RuntimeException(e); diff --git a/test-gen-plugin/src/main/java/com/dylibso/chicory/maven/TestSuiteDownloader.java b/test-gen-plugin/src/main/java/com/dylibso/chicory/maven/TestSuiteDownloader.java index cf870921b..50f200b47 100644 --- a/test-gen-plugin/src/main/java/com/dylibso/chicory/maven/TestSuiteDownloader.java +++ b/test-gen-plugin/src/main/java/com/dylibso/chicory/maven/TestSuiteDownloader.java @@ -5,6 +5,7 @@ import java.io.File; import java.nio.file.Files; +import java.nio.file.Path; import java.util.Comparator; import static java.util.Collections.singleton; @@ -22,7 +23,7 @@ public void downloadTestsuite(String testSuiteRepo, String testSuiteRepoRef, Fil log.warn("Testsuite folder exists but looks corrupted, replacing."); Files.walk(testSuiteFolder.toPath()) .sorted(Comparator.reverseOrder()) - .map(x -> x.toFile()) + .map(Path::toFile) .forEach(File::delete); } else { log.debug("Testsuite detected, using the cached version."); diff --git a/test-gen-plugin/src/main/java/com/dylibso/chicory/maven/Wast2JsonWrapper.java b/test-gen-plugin/src/main/java/com/dylibso/chicory/maven/Wast2JsonWrapper.java index 7cd502e35..019dc8599 100644 --- a/test-gen-plugin/src/main/java/com/dylibso/chicory/maven/Wast2JsonWrapper.java +++ b/test-gen-plugin/src/main/java/com/dylibso/chicory/maven/Wast2JsonWrapper.java @@ -64,7 +64,7 @@ private File executeWast2Json(File wastFile) { ProcessBuilder pb = new ProcessBuilder(command); pb.directory(new File(".")); pb.inheritIO(); - Process ps = null; + Process ps; try { ps = pb.start(); ps.waitFor(10, TimeUnit.SECONDS); @@ -83,14 +83,13 @@ private File executeWast2Json(File wastFile) { return targetFolder; } - private File downloadAndExtract(URL url) { + private void downloadAndExtract(URL url) { wabtDownloadTargetFolder.mkdirs(); final File finalDestination = new File(wabtDownloadTargetFolder, new File(url.getFile()).getName()); try (ReadableByteChannel readableByteChannel = Channels.newChannel(url.openStream()); FileOutputStream fileOutputStream = new FileOutputStream(finalDestination)) { fileOutputStream.getChannel().transferFrom(readableByteChannel, 0, Long.MAX_VALUE); - return finalDestination; } catch (IOException e) { throw new IllegalArgumentException("Error downloading : " + url, e); }