blob: 31a68215172f948c02b7d7dd9485ea29e9b48375 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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;
}
}
}
|