Skip to content

Commit 40d70f4

Browse files
committed
Suppress Exception and add documentation
1 parent 907384b commit 40d70f4

File tree

4 files changed

+34
-14
lines changed

4 files changed

+34
-14
lines changed

snapshot-matcher-example/src/test/java/com/zenika/snapshotmatcherexample/SimpleTypesTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public class SimpleTypesTest {
99

1010
@Test
1111
public void testString() {
12-
String s = "Mystring";
12+
String s = "MyString";
1313

1414
assertThat(s, matchesSnapshot());
1515
}
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
"Mystring"
1+
"MyString"

snapshot-matcher/src/main/java/com/zenika/snapshotmatcher/MatcherException.java

Lines changed: 0 additions & 12 deletions
This file was deleted.

snapshot-matcher/src/main/java/com/zenika/snapshotmatcher/SnapshotMatcher.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,20 @@
2222
import difflib.Patch;
2323

2424
public class SnapshotMatcher<T> extends TypeSafeMatcher<T> {
25+
/**
26+
* Factory method to instantiate a snapshot matcher with the given type
27+
*
28+
* @param <T> Type of object to snapshot
29+
* @return The snapshot matcher instance
30+
*/
2531
@Factory
2632
public static <T> SnapshotMatcher<T> matchesSnapshot() {
2733
return new SnapshotMatcher<>();
2834
}
2935

36+
/**
37+
* Private constructor, use factory method {@link SnapshotMatcher#matchesSnapshot()} create a new matcher
38+
*/
3039
private SnapshotMatcher() {
3140
}
3241

@@ -46,6 +55,12 @@ public boolean matchesSafely(T o) {
4655
}
4756
}
4857

58+
/**
59+
* Perform serialization of o and save result to snapshotPath
60+
*
61+
* @param o Object to serialize
62+
* @param snapshotPath Path to file to create
63+
*/
4964
private void createSnapshot(T o, Path snapshotPath) {
5065
try {
5166
Files.createDirectories(snapshotPath.getParent());
@@ -59,6 +74,13 @@ private void createSnapshot(T o, Path snapshotPath) {
5974
}
6075
}
6176

77+
/**
78+
* Compares snapshot at the given path with the given object
79+
*
80+
* @param o Actual object to serialize then compare
81+
* @param snapshotPath Path to the corresponding snapshot file
82+
* @return true if snapshot matches the actual, false otherwise
83+
*/
6284
private boolean compareSnapshot(T o, Path snapshotPath) {
6385
try (BufferedReader reader = Files.newBufferedReader(snapshotPath, Charset.forName("UTF-8"))) {
6486
List<String> actual = asList(objectMapper.writeValueAsString(o).split(System.lineSeparator()));
@@ -96,6 +118,11 @@ public void describeTo(Description description) {
96118
description.appendText("Object should match snapshot at " + getPath().toString());
97119
}
98120

121+
/**
122+
* Find out path of the snapshot using caller name and class
123+
*
124+
* @return Path to the snapshot file
125+
*/
99126
private Path getPath() {
100127
StackTraceElement caller = getCaller();
101128

@@ -105,6 +132,11 @@ private Path getPath() {
105132
return Paths.get(String.format("src/test/resources/snapshots/%s/%s.json", callerClassName, callerMethodName));
106133
}
107134

135+
/**
136+
* Find out caller StackTraceElement, ie. the test method which instantiated the matcher
137+
*
138+
* @return StackTraceElement of the test method
139+
*/
108140
private StackTraceElement getCaller() {
109141
final StackTraceElement[] stackTraceElements = Thread.currentThread().getStackTrace();
110142

0 commit comments

Comments
 (0)