Skip to content

Commit 8077163

Browse files
committed
Allow user to pass function to convert Object to JSON
1 parent a83f0aa commit 8077163

File tree

3 files changed

+27
-9
lines changed

3 files changed

+27
-9
lines changed

src/main/java/io/github/jsonSnapshot/Snapshot.java

+5-6
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
package io.github.jsonSnapshot;
22

3-
import com.google.gson.Gson;
4-
import com.google.gson.GsonBuilder;
53
import org.assertj.core.util.diff.DiffUtils;
64
import org.assertj.core.util.diff.Patch;
75

86
import java.lang.reflect.Method;
97
import java.util.Arrays;
108
import java.util.Collection;
119
import java.util.Set;
10+
import java.util.function.Function;
1211

1312
public class Snapshot {
1413

@@ -18,16 +17,16 @@ public class Snapshot {
1817

1918
private Method method;
2019

21-
private Gson gson;
20+
private Function<Object, String> jsonFunction;
2221

2322
private Object[] current;
2423

25-
Snapshot(SnapshotFile snapshotFile, Class clazz, Method method, Object... current) {
24+
Snapshot(SnapshotFile snapshotFile, Class clazz, Method method, Function<Object, String> jsonFunction, Object... current) {
2625
this.current = current;
2726
this.snapshotFile = snapshotFile;
2827
this.clazz = clazz;
2928
this.method = method;
30-
gson = new GsonBuilder().setPrettyPrinting().create();
29+
this.jsonFunction = jsonFunction;
3130
}
3231

3332
public void toMatchSnapshot() {
@@ -71,7 +70,7 @@ private String getRawSnapshot(Collection<String> rawSnapshots) {
7170
}
7271

7372
private String takeSnapshot() {
74-
return getSnapshotName() + gson.toJson(current);
73+
return getSnapshotName() + jsonFunction.apply(current);
7574
}
7675

7776
public String getSnapshotName() {

src/main/java/io/github/jsonSnapshot/SnapshotMatcher.java

+19-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.github.jsonSnapshot;
22

3+
import com.google.gson.GsonBuilder;
34
import org.apache.commons.lang3.ArrayUtils;
45
import org.apache.commons.lang3.StringUtils;
56
import org.assertj.core.util.Arrays;
@@ -13,6 +14,8 @@
1314
import java.util.ArrayList;
1415
import java.util.List;
1516
import java.util.Set;
17+
import java.util.function.Function;
18+
import java.util.function.Supplier;
1619
import java.util.stream.Collectors;
1720

1821
public 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())) {

src/test/java/io/github/jsonSnapshot/SnapshotTest.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.github.jsonSnapshot;
22

3+
import com.google.gson.GsonBuilder;
34
import org.junit.After;
45
import org.junit.Before;
56
import org.junit.Test;
@@ -32,7 +33,8 @@ public class SnapshotTest {
3233
public void setUp() throws NoSuchMethodException, IOException {
3334
snapshotFile = new SnapshotFile(DEFAULT_CONFIG.getFilePath(), "anyFilePath");
3435
snapshot = new Snapshot(snapshotFile, String.class,
35-
String.class.getDeclaredMethod("toString"), "anyObject");
36+
String.class.getDeclaredMethod("toString"),
37+
(object) -> new GsonBuilder().setPrettyPrinting().create().toJson(object),"anyObject");
3638
}
3739

3840
@After

0 commit comments

Comments
 (0)