-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLayoutSnapshotAssertions.java
More file actions
384 lines (347 loc) · 16.9 KB
/
LayoutSnapshotAssertions.java
File metadata and controls
384 lines (347 loc) · 16.9 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
package com.demcha.compose.testing.layout;
import com.demcha.compose.document.api.DocumentSession;
import com.demcha.compose.document.snapshot.LayoutSnapshot;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.Objects;
/**
* Assertion helpers for deterministic post-layout snapshots.
*
* <p>The intended workflow is:</p>
* <ol>
* <li>compose the document into the canonical {@link DocumentSession}</li>
* <li>call one of the {@code assertMatches(...)} overloads</li>
* <li>optionally render the same session to PDF for human inspection</li>
* </ol>
*
* <p>By default, expected baselines are resolved under
* {@code src/test/resources/layout-snapshots}. On mismatch, an
* {@code .actual.json} artifact is written under
* {@code target/visual-tests/layout-snapshots}. Local baseline updates are
* opt-in via {@value #UPDATE_PROPERTY}.</p>
*
* @author Artem Demchyshyn
*/
public final class LayoutSnapshotAssertions {
/**
* System property that enables overwriting expected JSON baselines.
*/
public static final String UPDATE_PROPERTY = "graphcompose.updateSnapshots";
private static final Path EXPECTED_ROOT = Path.of("src", "test", "resources", "layout-snapshots");
private static final Path ACTUAL_ROOT = Path.of("target", "visual-tests", "layout-snapshots");
private LayoutSnapshotAssertions() {
}
/**
* Resolves and compares a snapshot using a slash-delimited logical path.
*
* <p>For example, passing {@code templates/invoice/invoice_standard_layout}
* compares against
* {@code src/test/resources/layout-snapshots/templates/invoice/invoice_standard_layout.json}.</p>
*
* @param document composed document whose layout should be snapshotted
* @param snapshotPath logical snapshot path relative to the default snapshot root
* @throws Exception if snapshot extraction or comparison fails
*/
public static void assertMatches(DocumentSession document, String snapshotPath) throws Exception {
SnapshotTarget target = parseSnapshotPath(snapshotPath);
assertMatches(document, target.snapshotName(), target.folders());
}
/**
* Resolves and compares a snapshot using an explicit file name plus folders.
*
* @param document composed document whose layout should be snapshotted
* @param snapshotName file name without the {@code .json} suffix
* @param folders optional folder segments under the default snapshot root
* @throws Exception if snapshot extraction or comparison fails
*/
public static void assertMatches(DocumentSession document, String snapshotName, String... folders) throws Exception {
assertMatches(
document.layoutSnapshot(),
EXPECTED_ROOT,
ACTUAL_ROOT,
Boolean.getBoolean(UPDATE_PROPERTY),
snapshotName,
folders);
}
/**
* Resolves and compares a snapshot using a slash-delimited logical path and
* caller-provided baseline roots.
*
* @param document composed document whose layout should be snapshotted
* @param expectedRoot root folder that stores committed JSON baselines
* @param actualRoot root folder for mismatch artifacts
* @param snapshotPath logical snapshot path relative to {@code expectedRoot}
* @throws Exception if snapshot extraction or comparison fails
*/
public static void assertMatches(DocumentSession document,
Path expectedRoot,
Path actualRoot,
String snapshotPath) throws Exception {
SnapshotTarget target = parseSnapshotPath(snapshotPath);
assertMatches(document, expectedRoot, actualRoot, target.snapshotName(), target.folders());
}
/**
* Resolves and compares a snapshot using an explicit file name plus folders
* and caller-provided baseline roots.
*
* @param document composed document whose layout should be snapshotted
* @param expectedRoot root folder that stores committed JSON baselines
* @param actualRoot root folder for mismatch artifacts
* @param snapshotName file name without the {@code .json} suffix
* @param folders optional folder segments under {@code expectedRoot}
* @throws Exception if snapshot extraction or comparison fails
*/
public static void assertMatches(DocumentSession document,
Path expectedRoot,
Path actualRoot,
String snapshotName,
String... folders) throws Exception {
assertMatches(
document.layoutSnapshot(),
expectedRoot,
actualRoot,
Boolean.getBoolean(UPDATE_PROPERTY),
snapshotName,
folders);
}
/**
* Resolves and compares a precomputed snapshot using a slash-delimited
* logical path.
*
* @param snapshot resolved snapshot to compare
* @param snapshotPath logical snapshot path relative to the default snapshot root
* @throws IOException if reading or writing snapshot files fails
*/
public static void assertMatches(LayoutSnapshot snapshot, String snapshotPath) throws IOException {
SnapshotTarget target = parseSnapshotPath(snapshotPath);
assertMatches(snapshot, target.snapshotName(), target.folders());
}
/**
* Resolves and compares a precomputed snapshot using an explicit file name
* plus folders.
*
* @param snapshot resolved snapshot to compare
* @param snapshotName file name without the {@code .json} suffix
* @param folders optional folder segments under the default snapshot root
* @throws IOException if reading or writing snapshot files fails
*/
public static void assertMatches(LayoutSnapshot snapshot, String snapshotName, String... folders) throws IOException {
assertMatches(
snapshot,
EXPECTED_ROOT,
ACTUAL_ROOT,
Boolean.getBoolean(UPDATE_PROPERTY),
snapshotName,
folders);
}
/**
* Resolves and compares a legacy engine snapshot using an explicit file name
* plus folders. New canonical callers should pass {@link LayoutSnapshot}.
*
* @param snapshot resolved legacy engine snapshot to compare
* @param snapshotName file name without the {@code .json} suffix
* @param folders optional folder segments under the default snapshot root
* @throws IOException if reading or writing snapshot files fails
*/
public static void assertMatches(com.demcha.compose.engine.debug.LayoutSnapshot snapshot,
String snapshotName,
String... folders) throws IOException {
assertMatches(
snapshot,
EXPECTED_ROOT,
ACTUAL_ROOT,
Boolean.getBoolean(UPDATE_PROPERTY),
snapshotName,
folders);
}
/**
* Compares a precomputed layout snapshot against a slash-delimited logical
* path under caller-provided baseline roots.
*
* @param snapshot resolved snapshot to compare
* @param expectedRoot root folder that stores committed JSON baselines
* @param actualRoot root folder for mismatch artifacts
* @param snapshotPath logical snapshot path relative to {@code expectedRoot}
* @throws IOException if reading or writing snapshot files fails
*/
public static void assertMatches(LayoutSnapshot snapshot,
Path expectedRoot,
Path actualRoot,
String snapshotPath) throws IOException {
SnapshotTarget target = parseSnapshotPath(snapshotPath);
assertMatches(snapshot, expectedRoot, actualRoot, Boolean.getBoolean(UPDATE_PROPERTY), target.snapshotName(), target.folders());
}
/**
* Compares a legacy engine snapshot against a slash-delimited logical path
* under caller-provided baseline roots.
*
* @param snapshot resolved legacy engine snapshot to compare
* @param expectedRoot root folder that stores committed JSON baselines
* @param actualRoot root folder for mismatch artifacts
* @param snapshotPath logical snapshot path relative to {@code expectedRoot}
* @throws IOException if reading or writing snapshot files fails
*/
public static void assertMatches(com.demcha.compose.engine.debug.LayoutSnapshot snapshot,
Path expectedRoot,
Path actualRoot,
String snapshotPath) throws IOException {
SnapshotTarget target = parseSnapshotPath(snapshotPath);
assertMatches(snapshot, expectedRoot, actualRoot, Boolean.getBoolean(UPDATE_PROPERTY), target.snapshotName(), target.folders());
}
/**
* Compares a precomputed layout snapshot against an explicit file name plus
* folders under caller-provided baseline roots.
*
* @param snapshot resolved snapshot to compare
* @param expectedRoot root folder that stores committed JSON baselines
* @param actualRoot root folder for mismatch artifacts
* @param snapshotName file name without the {@code .json} suffix
* @param folders optional folder segments under {@code expectedRoot}
* @throws IOException if reading or writing snapshot files fails
*/
public static void assertMatches(LayoutSnapshot snapshot,
Path expectedRoot,
Path actualRoot,
String snapshotName,
String... folders) throws IOException {
assertMatches(snapshot, expectedRoot, actualRoot, Boolean.getBoolean(UPDATE_PROPERTY), snapshotName, folders);
}
/**
* Compares a legacy engine snapshot against an explicit file name plus
* folders under caller-provided baseline roots.
*
* @param snapshot resolved legacy engine snapshot to compare
* @param expectedRoot root folder that stores committed JSON baselines
* @param actualRoot root folder for mismatch artifacts
* @param snapshotName file name without the {@code .json} suffix
* @param folders optional folder segments under {@code expectedRoot}
* @throws IOException if reading or writing snapshot files fails
*/
public static void assertMatches(com.demcha.compose.engine.debug.LayoutSnapshot snapshot,
Path expectedRoot,
Path actualRoot,
String snapshotName,
String... folders) throws IOException {
assertMatches(snapshot, expectedRoot, actualRoot, Boolean.getBoolean(UPDATE_PROPERTY), snapshotName, folders);
}
static void assertMatches(LayoutSnapshot snapshot,
Path expectedRoot,
Path actualRoot,
boolean updateSnapshots,
String snapshotPath) throws IOException {
SnapshotTarget target = parseSnapshotPath(snapshotPath);
assertMatches(snapshot, expectedRoot, actualRoot, updateSnapshots, target.snapshotName(), target.folders());
}
static void assertMatches(LayoutSnapshot snapshot,
Path expectedRoot,
Path actualRoot,
boolean updateSnapshots,
String snapshotName,
String... folders) throws IOException {
assertMatchesJson(LayoutSnapshotJson.toJson(Objects.requireNonNull(snapshot, "snapshot")),
expectedRoot,
actualRoot,
updateSnapshots,
snapshotName,
folders);
}
static void assertMatches(com.demcha.compose.engine.debug.LayoutSnapshot snapshot,
Path expectedRoot,
Path actualRoot,
boolean updateSnapshots,
String snapshotPath) throws IOException {
SnapshotTarget target = parseSnapshotPath(snapshotPath);
assertMatches(snapshot, expectedRoot, actualRoot, updateSnapshots, target.snapshotName(), target.folders());
}
static void assertMatches(com.demcha.compose.engine.debug.LayoutSnapshot snapshot,
Path expectedRoot,
Path actualRoot,
boolean updateSnapshots,
String snapshotName,
String... folders) throws IOException {
assertMatchesJson(LayoutSnapshotJson.toJson(Objects.requireNonNull(snapshot, "snapshot")),
expectedRoot,
actualRoot,
updateSnapshots,
snapshotName,
folders);
}
private static void assertMatchesJson(String actualJson,
Path expectedRoot,
Path actualRoot,
boolean updateSnapshots,
String snapshotName,
String... folders) throws IOException {
Objects.requireNonNull(expectedRoot, "expectedRoot");
Objects.requireNonNull(actualRoot, "actualRoot");
Objects.requireNonNull(snapshotName, "snapshotName");
Path expectedPath = resolveExpectedPath(expectedRoot, snapshotName, folders);
Path actualPath = resolveActualPath(actualRoot, snapshotName, folders);
if (updateSnapshots) {
writeFile(expectedPath, actualJson);
Files.deleteIfExists(actualPath);
return;
}
if (Files.notExists(expectedPath)) {
writeFile(actualPath, actualJson);
throw new AssertionError("Missing expected layout snapshot at %s. Generated actual snapshot at %s. Re-run with -D%s=true to accept it."
.formatted(expectedPath.toAbsolutePath(), actualPath.toAbsolutePath(), UPDATE_PROPERTY));
}
String expectedJson = LayoutSnapshotJson.normalizeLineEndings(Files.readString(expectedPath));
if (!expectedJson.endsWith("\n")) {
expectedJson = expectedJson + "\n";
}
if (!expectedJson.equals(actualJson)) {
writeFile(actualPath, actualJson);
throw new AssertionError("Layout snapshot mismatch for %s. Expected: %s Actual: %s. Re-run with -D%s=true to update the baseline."
.formatted(snapshotName, expectedPath.toAbsolutePath(), actualPath.toAbsolutePath(), UPDATE_PROPERTY));
}
Files.deleteIfExists(actualPath);
}
private static Path resolveExpectedPath(Path root, String snapshotName, String... folders) {
return resolvePath(root, snapshotName + ".json", folders);
}
private static Path resolveActualPath(Path root, String snapshotName, String... folders) {
return resolvePath(root, snapshotName + ".actual.json", folders);
}
private static Path resolvePath(Path root, String fileName, String... folders) {
Path current = root;
for (String folder : folders) {
current = current.resolve(folder);
}
return current.resolve(fileName);
}
private static void writeFile(Path path, String content) throws IOException {
Path parent = path.getParent();
if (parent != null) {
Files.createDirectories(parent);
}
Files.writeString(path, content);
}
private static SnapshotTarget parseSnapshotPath(String snapshotPath) {
String normalized = snapshotPath == null ? "" : snapshotPath.trim().replace('\\', '/');
while (normalized.startsWith("/")) {
normalized = normalized.substring(1);
}
while (normalized.endsWith("/")) {
normalized = normalized.substring(0, normalized.length() - 1);
}
if (normalized.isBlank()) {
throw new IllegalArgumentException("snapshotPath must not be blank");
}
String[] segments = Arrays.stream(normalized.split("/"))
.map(String::trim)
.filter(segment -> !segment.isBlank())
.toArray(String[]::new);
if (segments.length == 0) {
throw new IllegalArgumentException("snapshotPath must contain at least one segment");
}
String snapshotName = segments[segments.length - 1];
String[] folders = Arrays.copyOf(segments, segments.length - 1);
return new SnapshotTarget(snapshotName, folders);
}
private record SnapshotTarget(String snapshotName, String[] folders) {
}
}