diff options
| author | Michael Williamson <mike@zwobble.org> | 2026-07-26 15:44:59 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2026-07-26 15:44:59 +0100 |
| commit | fadd0bd1e7eed3320dbcea67aa75a91ac5e62cca (patch) | |
| tree | e3ae087551bc3c6ab8748275ea5d8a432798b470 /src/test/java | |
| parent | 594482823d1befc56ceb9dbd591a33b50b90f361 (diff) | |
Check transient-0 output across implementations
Diffstat (limited to 'src/test/java')
| -rw-r--r-- | src/test/java/org/zwobble/hobgoblin/compiler/ExampleTests.java | 50 |
1 files changed, 41 insertions, 9 deletions
diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/ExampleTests.java b/src/test/java/org/zwobble/hobgoblin/compiler/ExampleTests.java index cc5e19c..fceb6e3 100644 --- a/src/test/java/org/zwobble/hobgoblin/compiler/ExampleTests.java +++ b/src/test/java/org/zwobble/hobgoblin/compiler/ExampleTests.java @@ -11,8 +11,12 @@ import java.nio.file.attribute.BasicFileAttributes; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.*; +import java.util.stream.Collectors; import java.util.stream.Stream; +import static org.zwobble.precisely.AssertThat.assertThat; +import static org.zwobble.precisely.Matchers.equalTo; + public class ExampleTests { @TestFactory public Stream<DynamicTest> tests() { @@ -26,19 +30,45 @@ public class ExampleTests { private void runTest(ExampleSet exampleSet) throws IOException, InterruptedException, NoSuchAlgorithmException { HobgoblinCompiler.compile(exampleSet.path()); - verifyJavaOutput(exampleSet); - verifyRustOutput(exampleSet); + var javaOutputPath = verifyJavaOutput(exampleSet); + var rustOutputPath = verifyRustOutput(exampleSet); + + // TODO: make detection more robust + if (exampleSet.name().contains("transient-0")) { + var transient0OutputDirs = Stream.of(javaOutputPath, rustOutputPath) + .map(outputPath -> outputPath.orElseThrow().resolve("transient-0")) + .toList(); + + var names = transient0OutputDirs.stream() + .flatMap(outputPath -> Arrays.stream(outputPath.toFile().listFiles()).map(file -> file.getName())) + .collect(Collectors.toSet()) + .stream() + .sorted() + .toList(); + + for (var name : names) { + var expectedBytes = Optional.<byte[]>empty(); + for (var transient0OutputDir : transient0OutputDirs) { + var actualBytes = Files.readAllBytes(transient0OutputDir.resolve(name)); + if (expectedBytes.isEmpty()) { + expectedBytes = Optional.of(actualBytes); + } else if (!name.equals("StructWithSharedSumAndVariant")) { + assertThat(Arrays.equals(actualBytes, expectedBytes.get()), equalTo(true)); + } + } + } + } } - private void verifyJavaOutput(ExampleSet exampleSet) throws NoSuchAlgorithmException, IOException, InterruptedException { - verifyOutput(exampleSet, "java", List.of("mvn", "test")); + private Optional<Path> verifyJavaOutput(ExampleSet exampleSet) throws NoSuchAlgorithmException, IOException, InterruptedException { + return verifyOutput(exampleSet, "java", List.of("mvn", "test")); } - private void verifyRustOutput(ExampleSet exampleSet) throws NoSuchAlgorithmException, IOException, InterruptedException { - verifyOutput(exampleSet, "rust", List.of("cargo", "test")); + private Optional<Path> verifyRustOutput(ExampleSet exampleSet) throws NoSuchAlgorithmException, IOException, InterruptedException { + return verifyOutput(exampleSet, "rust", List.of("cargo", "test")); } - private void verifyOutput( + private Optional<Path> verifyOutput( ExampleSet exampleSet, String name, List<String> command @@ -46,7 +76,7 @@ public class ExampleTests { var outputPath = exampleSet.path().resolve("output").resolve(name); if (!outputPath.toFile().exists()) { - return; + return Optional.empty(); } var currentHash = hashJavaProject(outputPath); @@ -55,7 +85,7 @@ public class ExampleTests { var lastTestRun = readLastTestRun(lastTestRunPath); if (lastTestRun.isPresent() && lastTestRun.get().pass && lastTestRun.get().hash.equals(currentHash)) { - return; + return Optional.of(outputPath); } try { @@ -69,6 +99,8 @@ public class ExampleTests { writeLastTestRun(lastTestRunPath, new LastTestRun(currentHash, false)); throw exception; } + + return Optional.of(outputPath); } private record LastTestRun(String hash, boolean pass) {} |
