-
Notifications
You must be signed in to change notification settings - Fork 16
✨ Add Snapshot.ignore(String... jsonPaths)
method
#10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,21 @@ | ||
package io.github.jsonSnapshot; | ||
|
||
import com.jayway.jsonpath.DocumentContext; | ||
import com.jayway.jsonpath.JsonPath; | ||
import com.jayway.jsonpath.spi.json.JacksonJsonProvider; | ||
import org.assertj.core.util.diff.DiffUtils; | ||
import org.assertj.core.util.diff.Patch; | ||
|
||
import java.lang.reflect.Method; | ||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.Collection; | ||
import java.util.List; | ||
import java.util.Set; | ||
import java.util.function.Function; | ||
import java.util.stream.Collectors; | ||
|
||
import org.assertj.core.util.diff.DiffUtils; | ||
import org.assertj.core.util.diff.Patch; | ||
import static io.github.jsonSnapshot.SnapshotMatcher.defaultJsonFunction; | ||
|
||
public class Snapshot { | ||
|
||
|
@@ -19,6 +27,8 @@ public class Snapshot { | |
|
||
private Function<Object, String> jsonFunction; | ||
|
||
private final List<String> pathsToIgnore = new ArrayList<>(); | ||
|
||
private Object[] current; | ||
|
||
Snapshot( | ||
|
@@ -65,11 +75,11 @@ private SnapshotMatchException generateDiffError(String rawSnapshot, String curr | |
+ currentObject.trim() | ||
+ "\n\n" | ||
+ patch | ||
.getDeltas() | ||
.stream() | ||
.map(delta -> delta.toString() + "\n") | ||
.reduce(String::concat) | ||
.get(); | ||
.getDeltas() | ||
.stream() | ||
.map(delta -> delta.toString() + "\n") | ||
.reduce(String::concat) | ||
.get(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Avoid formatting-only changes |
||
return new SnapshotMatchException(error); | ||
} | ||
|
||
|
@@ -83,11 +93,39 @@ private String getRawSnapshot(Collection<String> rawSnapshots) { | |
return null; | ||
} | ||
|
||
private Object ignorePaths(Object input) { | ||
DocumentContext context = JsonPath.using(new JacksonJsonProvider()).parse(defaultJsonFunction().apply(input)); | ||
this.pathsToIgnore.forEach(path -> context.delete(path)); | ||
String root = "$"; | ||
return context.read(root); | ||
} | ||
|
||
private Object[] getCurrentWithoutIgnoredPaths() { | ||
if (pathsToIgnore.isEmpty() || current == null) { | ||
return current; | ||
} | ||
|
||
return Arrays.asList(current).stream() | ||
.map(this::ignorePaths) | ||
.collect(Collectors.toList()) | ||
.toArray(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
} | ||
|
||
private String takeSnapshot() { | ||
return getSnapshotName() + jsonFunction.apply(current); | ||
return getSnapshotName() + jsonFunction.apply(getCurrentWithoutIgnoredPaths()); | ||
} | ||
|
||
public String getSnapshotName() { | ||
return clazz.getName() + "." + method.getName() + "="; | ||
} | ||
|
||
/** | ||
* Ignore fields when comparing object and snapshot. | ||
* | ||
* @param jsonPathsToIgnore A list of paths to ignore in <a href="https://github.com/json-path/JsonPath">JsonPath</a> syntax. | ||
*/ | ||
public Snapshot ignoring(String... jsonPathsToIgnore) { | ||
this.pathsToIgnore.addAll(Arrays.asList(jsonPathsToIgnore)); | ||
return this; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -73,4 +73,28 @@ void shouldThrowStackOverflowError() { | |
|
||
assertThrows(SnapshotMatchException.class, () -> expect(fakeObject1).toMatchSnapshot()); | ||
} | ||
|
||
@Test | ||
public void shouldMatchSnapshotAndIgnoreTopLevelFieldMethod() { | ||
expect(FakeObject.builder().id("anyId6").value(6).name("anyName6").build()) | ||
.ignoring("value") | ||
.toMatchSnapshot(); | ||
} | ||
|
||
@Test | ||
public void shouldMatchSnapshotAndIgnoreSeveralNestedFieldsMethod() { | ||
FakeObject grandChild = FakeObject.builder().id("grandChild7").value(7).build(); | ||
FakeObject child = FakeObject.builder().id("child7").value(7).fakeObject(grandChild).build(); | ||
expect(FakeObject.builder().id("anyId7").value(7).name("anyName7").fakeObject(child).build()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd extract creation of test data into a variable, maybe something like: FakeObject testObject = FakeObject.builder()
.id("anyId7").value(7)
.name("anyName7").fakeObject(FakeObject.builder()
.id("child7").value(7)
.fakeObject(FakeObject.builder()
.id("grandChild7").value(7)
.build()
).build()
).build(). |
||
.ignoring("value", "fakeObject.id", "fakeObject.fakeObject") | ||
.toMatchSnapshot(); | ||
} | ||
|
||
@Test | ||
public void shouldMatchSnapshotAndIgnoreEverythingMethod() { | ||
FakeObject child = FakeObject.builder().id("child8").value(8).build(); | ||
expect(FakeObject.builder().id("anyId8").value(8).name("anyName8").fakeObject(child).build()) | ||
.ignoring("$..*") | ||
.toMatchSnapshot(); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any way to do this without an extra dependency?
Jackson already uses
JsonPointer
s, maybe that could be used to specify ignored elements as well?