11package io .github .jsonSnapshot ;
22
3+ import com .google .gson .GsonBuilder ;
34import org .apache .commons .lang3 .ArrayUtils ;
45import org .apache .commons .lang3 .StringUtils ;
56import org .assertj .core .util .Arrays ;
1314import java .util .ArrayList ;
1415import java .util .List ;
1516import java .util .Set ;
17+ import java .util .function .Function ;
18+ import java .util .function .Supplier ;
1619import java .util .stream .Collectors ;
1720
1821public class SnapshotMatcher {
@@ -22,12 +25,22 @@ public class SnapshotMatcher {
2225 private static Class clazz = null ;
2326 private static SnapshotFile snapshotFile = null ;
2427 private static List <Snapshot > calledSnapshots = new ArrayList <>();
28+ private static Function <Object , String > jsonFunction ;
2529
2630 public static void start () {
27- start (new DefaultConfig ());
31+ start (new DefaultConfig (), defaultJsonFunction () );
2832 }
2933
3034 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 ;
3144 try {
3245 StackTraceElement stackElement = findStackElement ();
3346 clazz = Class .forName (stackElement .getClassName ());
@@ -67,7 +80,7 @@ public static Snapshot expect(Object firstObject, Object... others) {
6780 Object [] objects = mergeObjects (firstObject , others );
6881 StackTraceElement stackElement = findStackElement ();
6982 Method method = getMethod (stackElement , clazz );
70- Snapshot snapshot = new Snapshot (snapshotFile , clazz , method , objects );
83+ Snapshot snapshot = new Snapshot (snapshotFile , clazz , method , jsonFunction , objects );
7184 validateExpectCall (snapshot );
7285 calledSnapshots .add (snapshot );
7386 return snapshot ;
@@ -76,6 +89,10 @@ public static Snapshot expect(Object firstObject, Object... others) {
7689 }
7790 }
7891
92+ private static Function <Object , String > defaultJsonFunction () {
93+ return (object ) -> new GsonBuilder ().setPrettyPrinting ().create ().toJson (object );
94+ }
95+
7996 private static void validateExpectCall (Snapshot snapshot ) {
8097 for (Snapshot eachSnapshot : calledSnapshots ) {
8198 if (eachSnapshot .getSnapshotName ().equals (snapshot .getSnapshotName ())) {
0 commit comments