• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

skuzzle / snapshot-tests / 1

12 Feb 2025 03:38PM UTC coverage: 86.61% (+0.03%) from 86.577%
1

Pull #109

jenkins

skuzzle
Remove old file after merge
Pull Request #109: 2.0 dev

1947 of 2248 relevant lines covered (86.61%)

0.87 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

96.15
/../snapshot-tests-core/src/main/java/de/skuzzle/test/snapshots/impl/SnapshotDslResult.java
1
package de.skuzzle.test.snapshots.impl;
2

3
import java.io.IOException;
4
import java.lang.reflect.Method;
5
import java.nio.file.Files;
6
import java.nio.file.Path;
7
import java.util.Map;
8

9
import de.skuzzle.difftool.DiffRenderer;
10
import de.skuzzle.difftool.SplitDiffRenderer;
11
import de.skuzzle.difftool.UnifiedDiffRenderer;
12
import de.skuzzle.test.snapshots.ContextFiles;
13
import de.skuzzle.test.snapshots.SnapshotFile;
14
import de.skuzzle.test.snapshots.SnapshotFile.SnapshotHeader;
15
import de.skuzzle.test.snapshots.SnapshotNaming;
16
import de.skuzzle.test.snapshots.SnapshotSerializer;
17
import de.skuzzle.test.snapshots.SnapshotTestOptions;
18
import de.skuzzle.test.snapshots.SnapshotTestOptions.NormalizeLineEndings;
19
import de.skuzzle.test.snapshots.impl.SnapshotAssertionInput.TerminalOperation;
20
import de.skuzzle.test.snapshots.validation.Arguments;
21

22
/**
23
 * Aggregates the outcome of a single full DSL invocation. Encapsulates all information
24
 * that are required to perform a single snapshot assertion and provides the logic to
25
 * create a {@link SnapshotAssertionInput} object.
26
 * <p>
27
 * Some state within this class maybe shared with other assertions within the same test
28
 * method.
29
 *
30
 * @author Simon Taddiken
31
 */
32
final class SnapshotDslResult {
33

34
    // backup snapshot text that will be used in special case when assertions are disabled
35
    // and null input is given as actual object to snapshot.assertThat(...)
36
    private static final String UNAVAILABLE_BECAUSE_ACTUAL_WAS_NULL = "<<unavailable because actual was null>>";
37

38
    private final SnapshotConfiguration configuration;
39
    private final ResultRecorder resultRecorder;
40
    private final Method testMethod;
41
    private final SnapshotNaming namingStrategy;
42
    private final SnapshotSerializer snapshotSerializer;
43
    private final TerminalOperation operation;
44

45
    // Will be null if user decided to use default directory
46
    private final Path directoryOverride;
47
    private final Object actual;
48

49
    SnapshotDslResult(
50
            SnapshotConfiguration configuration,
51
            ResultRecorder resultRecorder,
52
            Method testMethod,
53
            SnapshotNaming namingStrategy,
54
            Object actual,
55
            TerminalOperation operation,
56
            SnapshotSerializer snapshotSerializer,
57
            Path directoryOverride) {
1✔
58

59
        this.configuration = Arguments.requireNonNull(configuration, "configuration must not be null");
1✔
60
        this.resultRecorder = Arguments.requireNonNull(resultRecorder, "resultRecorder must not be null");
1✔
61
        this.testMethod = Arguments.requireNonNull(testMethod, "testMethod must not be null");
1✔
62
        this.namingStrategy = Arguments.requireNonNull(namingStrategy, "namingStrategy must not be null");
1✔
63

64
        this.operation = Arguments.requireNonNull(operation, "operation must not be null");
1✔
65
        this.snapshotSerializer = Arguments.requireNonNull(snapshotSerializer, "snapshotSerializer must not be null");
1✔
66

67
        // only things that gracefully accept null
68
        this.directoryOverride = directoryOverride;
1✔
69
        this.actual = actual;
1✔
70
    }
1✔
71

72
    private SnapshotHeader determineNextSnapshotHeader(String snapshotName, int snapshotNumber) {
73
        return SnapshotHeader.fromMap(Map.of(
1✔
74
                SnapshotHeader.SNAPSHOT_NUMBER, "" + snapshotNumber,
75
                SnapshotHeader.TEST_METHOD, testMethod.getName(),
1✔
76
                SnapshotHeader.TEST_CLASS, configuration.testClass().getName(),
1✔
77
                SnapshotHeader.SNAPSHOT_NAME, snapshotName,
78
                SnapshotHeader.DYNAMIC_DIRECTORY, "" + (this.directoryOverride != null)));
79
    }
80

81
    private Path determineSnapshotDirectory() throws IOException {
82
        return this.directoryOverride != null
1✔
83
                ? this.directoryOverride
1✔
84
                : this.configuration.determineSnapshotDirectory();
1✔
85
    }
86

87
    private ContextFiles determineContextFiles(Path snapshotDirectory, String snapshotName) {
88
        final String snapshotFileName = InternalSnapshotNaming.getSnapshotFileName(snapshotName);
1✔
89
        final String actualFileName = InternalSnapshotNaming.getSnapshotFileNameActual(snapshotName);
1✔
90
        final String rawFileName = InternalSnapshotNaming.getSnapshotFileNameRaw(snapshotName);
1✔
91

92
        return ContextFiles.of(
1✔
93
                snapshotDirectory.resolve(snapshotFileName),
1✔
94
                snapshotDirectory.resolve(actualFileName),
1✔
95
                snapshotDirectory.resolve(rawFileName));
1✔
96
    }
97

98
    private DiffRenderer determineDiffRenderer(SnapshotTestOptions.DiffFormat diffFormat) {
99
        switch (diffFormat) {
1✔
100
        case UNIFIED:
101
            return UnifiedDiffRenderer.INSTANCE;
1✔
102
        case SPLIT:
103
            return SplitDiffRenderer.INSTANCE;
×
104
        }
105
        throw new IllegalStateException("Unhandled DiffFormat constant: " + diffFormat);
×
106
    }
107

108
    SnapshotAssertionInput createAssertionInput() throws Exception {
109
        final int snapshotNumber = resultRecorder.size();
1✔
110
        final Path snapshotDirectory = determineSnapshotDirectory();
1✔
111
        final String snapshotName = namingStrategy.determineSnapshotName(testMethod, snapshotNumber);
1✔
112
        final ContextFiles contextFilePaths = determineContextFiles(snapshotDirectory, snapshotName);
1✔
113

114
        final boolean disableAssertion = operation == TerminalOperation.DISABLE;
1✔
115
        final boolean forceUpdateSnapshots = configuration.isForceUpdateSnapshots(testMethod)
1✔
116
                || operation == TerminalOperation.FORCE_UPDATE;
117

118
        final boolean snapshotFileAlreadyExists = Files.exists(contextFilePaths.snapshotFile());
1✔
119
        final boolean alwaysPersistActualResult = configuration.alwaysPersistActualResult(testMethod);
1✔
120
        final boolean alwaysPersistRawResult = configuration.alwaysPersistRawResult(testMethod);
1✔
121
        final boolean persistActualResultOnFailure = configuration.persistActualResultOnFailure(testMethod);
1✔
122
        final boolean persistRawResultOnFailure = configuration.persistRawResultOnFailure(testMethod);
1✔
123

124
        final SnapshotHeader snapshotHeader = determineNextSnapshotHeader(snapshotName, snapshotNumber);
1✔
125

126
        final String serializedActual;
127
        final boolean actualWasNull = actual == null;
1✔
128
        if (actualWasNull) {
1✔
129
            serializedActual = UNAVAILABLE_BECAUSE_ACTUAL_WAS_NULL;
1✔
130
        } else {
131
            final NormalizeLineEndings normalizeLineEndings = configuration.normalizeLineEndings(
1✔
132
                    testMethod);
133
            final SnapshotSerializer normalizingSerializer = NormalizeLineEndingsSnapshotSerializer.wrap(
1✔
134
                    snapshotSerializer,
135
                    normalizeLineEndings);
136
            serializedActual = normalizingSerializer.serialize(actual);
1✔
137
        }
138
        final SnapshotFile actualSnapshotFile = SnapshotFile.of(snapshotHeader, serializedActual);
1✔
139

140
        final int lineNumberOffset = configuration.addOffsetToReportedLinenumbers(testMethod)
1✔
141
                ? snapshotHeader.lineNumberOffset()
1✔
142
                : 0;
1✔
143
        final int contextLines = configuration.textDiffContextLines(testMethod);
1✔
144
        final DiffRenderer diffRenderer = determineDiffRenderer(configuration.diffFormat(testMethod));
1✔
145

146
        return new SnapshotAssertionInput(
1✔
147
                snapshotName,
148
                contextFilePaths,
149
                actualSnapshotFile,
150
                actualWasNull,
151
                disableAssertion,
152
                forceUpdateSnapshots,
153
                snapshotFileAlreadyExists,
154
                alwaysPersistActualResult,
155
                alwaysPersistRawResult,
156
                persistActualResultOnFailure,
157
                persistRawResultOnFailure,
158
                lineNumberOffset,
159
                contextLines,
160
                diffRenderer);
161
    }
162
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc