aboutsummaryrefslogtreecommitdiff
path: root/src/test/java
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2023-06-21 23:00:50 +0100
committerMichael Williamson <mike@zwobble.org>2023-06-21 23:00:50 +0100
commit2a4a431bff2a825b83994daee89f164316e7cc31 (patch)
tree1745166d106619ffcfae3eb214ed0a7f14952fc6 /src/test/java
parentf365095e567847620edfa6af6f72b5bc50f9537e (diff)
Replace snapshotting implementation
Diffstat (limited to 'src/test/java')
-rw-r--r--src/test/java/org/shedlang/compiler/backends/wasm/structure/WasmBinaryFormatModuleTests.java20
-rw-r--r--src/test/java/org/shedlang/compiler/testing/ProcessRunner.java4
-rw-r--r--src/test/java/org/shedlang/compiler/testing/snapshots/SnapshotResolver.java24
-rw-r--r--src/test/java/org/shedlang/compiler/testing/snapshots/Snapshotter.java41
4 files changed, 76 insertions, 13 deletions
diff --git a/src/test/java/org/shedlang/compiler/backends/wasm/structure/WasmBinaryFormatModuleTests.java b/src/test/java/org/shedlang/compiler/backends/wasm/structure/WasmBinaryFormatModuleTests.java
index ad63f317..a2594d0a 100644
--- a/src/test/java/org/shedlang/compiler/backends/wasm/structure/WasmBinaryFormatModuleTests.java
+++ b/src/test/java/org/shedlang/compiler/backends/wasm/structure/WasmBinaryFormatModuleTests.java
@@ -1,7 +1,5 @@
package org.shedlang.compiler.backends.wasm.structure;
-import au.com.origin.snapshots.Expect;
-import au.com.origin.snapshots.junit5.SnapshotExtension;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.shedlang.compiler.backends.wasm.structure.ast.WasmModule;
@@ -10,6 +8,8 @@ import org.shedlang.compiler.backends.wasm.structure.ast.imports.WasmImportDescr
import org.shedlang.compiler.backends.wasm.structure.ast.types.WasmFuncType;
import org.shedlang.compiler.backends.wasm.structure.ast.types.WasmValueType;
import org.shedlang.compiler.testing.ProcessRunner;
+import org.shedlang.compiler.testing.snapshots.SnapshotResolver;
+import org.shedlang.compiler.testing.snapshots.Snapshotter;
import java.io.FileOutputStream;
import java.io.IOException;
@@ -17,21 +17,19 @@ import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
-@ExtendWith({SnapshotExtension.class})
+@ExtendWith({SnapshotResolver.class})
public class WasmBinaryFormatModuleTests {
- private Expect expect;
-
@Test
- public void canSerializeEmptyModule() throws IOException, InterruptedException {
+ public void canSerializeEmptyModule(Snapshotter snapshotter) throws IOException, InterruptedException {
var module = WasmModule.EMPTY;
var result = serializeModule(module);
- expect.toMatchSnapshot(result);
+ snapshotter.assertSnapshot(result);
}
@Test
- public void canSerializeModuleWithTypes() throws IOException, InterruptedException {
+ public void canSerializeModuleWithTypes(Snapshotter snapshotter) throws IOException, InterruptedException {
var module = WasmModule.EMPTY
.addType(new WasmFuncType(List.of(), List.of()))
.addType(new WasmFuncType(
@@ -41,11 +39,11 @@ public class WasmBinaryFormatModuleTests {
var result = serializeModule(module);
- expect.toMatchSnapshot(result);
+ snapshotter.assertSnapshot(result);
}
@Test
- public void canSerializeModuleWithImports() throws IOException, InterruptedException {
+ public void canSerializeModuleWithImports(Snapshotter snapshotter) throws IOException, InterruptedException {
var module = WasmModule.EMPTY
.addType(new WasmFuncType(List.of(), List.of()))
.addType(new WasmFuncType(List.of(WasmValueType.I32), List.of()))
@@ -64,7 +62,7 @@ public class WasmBinaryFormatModuleTests {
var result = serializeModule(module);
- expect.toMatchSnapshot(result);
+ snapshotter.assertSnapshot(result);
}
private String serializeModule(WasmModule module) throws IOException, InterruptedException {
diff --git a/src/test/java/org/shedlang/compiler/testing/ProcessRunner.java b/src/test/java/org/shedlang/compiler/testing/ProcessRunner.java
index c1bfd481..8a824424 100644
--- a/src/test/java/org/shedlang/compiler/testing/ProcessRunner.java
+++ b/src/test/java/org/shedlang/compiler/testing/ProcessRunner.java
@@ -22,8 +22,8 @@ public class ProcessRunner {
var stdoutReader = process.inputReader(StandardCharsets.UTF_8);
var stderrReader = process.errorReader(StandardCharsets.UTF_8)
) {
- var stdout = stdoutReader.lines().collect(Collectors.joining());
- var stderr = stderrReader.lines().collect(Collectors.joining());
+ var stdout = stdoutReader.lines().collect(Collectors.joining("\n"));
+ var stderr = stderrReader.lines().collect(Collectors.joining("\n"));
return new ExecutionResult(exitCode, stdout, stderr);
}
diff --git a/src/test/java/org/shedlang/compiler/testing/snapshots/SnapshotResolver.java b/src/test/java/org/shedlang/compiler/testing/snapshots/SnapshotResolver.java
new file mode 100644
index 00000000..c1cca50c
--- /dev/null
+++ b/src/test/java/org/shedlang/compiler/testing/snapshots/SnapshotResolver.java
@@ -0,0 +1,24 @@
+package org.shedlang.compiler.testing.snapshots;
+
+import org.junit.jupiter.api.extension.ExtensionContext;
+import org.junit.jupiter.api.extension.ParameterContext;
+import org.junit.jupiter.api.extension.ParameterResolutionException;
+import org.junit.jupiter.api.extension.ParameterResolver;
+
+public class SnapshotResolver implements ParameterResolver {
+ @Override
+ public boolean supportsParameter(
+ ParameterContext parameterContext,
+ ExtensionContext extensionContext
+ ) throws ParameterResolutionException {
+ return parameterContext.getParameter().getType().equals(Snapshotter.class);
+ }
+
+ @Override
+ public Object resolveParameter(
+ ParameterContext parameterContext,
+ ExtensionContext extensionContext
+ ) throws ParameterResolutionException {
+ return new Snapshotter(extensionContext.getUniqueId());
+ }
+}
diff --git a/src/test/java/org/shedlang/compiler/testing/snapshots/Snapshotter.java b/src/test/java/org/shedlang/compiler/testing/snapshots/Snapshotter.java
new file mode 100644
index 00000000..31a68215
--- /dev/null
+++ b/src/test/java/org/shedlang/compiler/testing/snapshots/Snapshotter.java
@@ -0,0 +1,41 @@
+package org.shedlang.compiler.testing.snapshots;
+
+import org.shedlang.compiler.filesystem.ShedCompilerFiles;
+
+import java.io.IOException;
+import java.nio.file.Files;
+
+import static org.zwobble.precisely.AssertThat.assertThat;
+import static org.zwobble.precisely.Matchers.equalTo;
+
+public class Snapshotter {
+ private final String uniqueId;
+
+ public Snapshotter(String uniqueId) {
+ this.uniqueId = uniqueId;
+ }
+
+ public void assertSnapshot(String actualSnapshot) throws IOException {
+ var snapshotDirectory = ShedCompilerFiles.getRoot().resolve("snapshots");
+
+ var expectedSnapshotPath = snapshotDirectory.resolve(uniqueId + ".expected");
+ var actualSnapshotPath = snapshotDirectory.resolve(uniqueId + ".actual");
+ var actualSnapshotFile = actualSnapshotPath.toFile();
+
+ try {
+ if (Files.exists(expectedSnapshotPath)) {
+ var expectedSnapshot = Files.readString(expectedSnapshotPath);
+ assertThat(actualSnapshot, equalTo(expectedSnapshot));
+ if (actualSnapshotFile.exists()) {
+ actualSnapshotFile.delete();
+ }
+ } else {
+ throw new AssertionError("snapshot does not exist, got:\n" + actualSnapshot);
+ }
+ } catch (AssertionError error) {
+ Files.createDirectories(actualSnapshotPath.getParent());
+ Files.writeString(actualSnapshotPath, actualSnapshot);
+ throw error;
+ }
+ }
+}