1
1
package io .github .jsonSnapshot ;
2
2
3
+ import com .google .gson .GsonBuilder ;
3
4
import org .apache .commons .lang3 .ArrayUtils ;
4
5
import org .apache .commons .lang3 .StringUtils ;
5
6
import org .assertj .core .util .Arrays ;
13
14
import java .util .ArrayList ;
14
15
import java .util .List ;
15
16
import java .util .Set ;
17
+ import java .util .function .Function ;
18
+ import java .util .function .Supplier ;
16
19
import java .util .stream .Collectors ;
17
20
18
21
public class SnapshotMatcher {
@@ -22,12 +25,22 @@ public class SnapshotMatcher {
22
25
private static Class clazz = null ;
23
26
private static SnapshotFile snapshotFile = null ;
24
27
private static List <Snapshot > calledSnapshots = new ArrayList <>();
28
+ private static Function <Object , String > jsonFunction ;
25
29
26
30
public static void start () {
27
- start (new DefaultConfig ());
31
+ start (new DefaultConfig (), defaultJsonFunction () );
28
32
}
29
33
30
34
public static void start (SnapshotConfig config ) {
35
+ start (config , defaultJsonFunction ());
36
+ }
37
+
38
+ public static void start (Function <Object , String > jsonFunction ) {
39
+ start (new DefaultConfig (), jsonFunction );
40
+ }
41
+
42
+ public static void start (SnapshotConfig config , Function <Object , String > jsonFunction ) {
43
+ SnapshotMatcher .jsonFunction = jsonFunction ;
31
44
try {
32
45
StackTraceElement stackElement = findStackElement ();
33
46
clazz = Class .forName (stackElement .getClassName ());
@@ -67,7 +80,7 @@ public static Snapshot expect(Object firstObject, Object... others) {
67
80
Object [] objects = mergeObjects (firstObject , others );
68
81
StackTraceElement stackElement = findStackElement ();
69
82
Method method = getMethod (stackElement , clazz );
70
- Snapshot snapshot = new Snapshot (snapshotFile , clazz , method , objects );
83
+ Snapshot snapshot = new Snapshot (snapshotFile , clazz , method , jsonFunction , objects );
71
84
validateExpectCall (snapshot );
72
85
calledSnapshots .add (snapshot );
73
86
return snapshot ;
@@ -76,6 +89,10 @@ public static Snapshot expect(Object firstObject, Object... others) {
76
89
}
77
90
}
78
91
92
+ private static Function <Object , String > defaultJsonFunction () {
93
+ return (object ) -> new GsonBuilder ().setPrettyPrinting ().create ().toJson (object );
94
+ }
95
+
79
96
private static void validateExpectCall (Snapshot snapshot ) {
80
97
for (Snapshot eachSnapshot : calledSnapshots ) {
81
98
if (eachSnapshot .getSnapshotName ().equals (snapshot .getSnapshotName ())) {
0 commit comments