Skip to content

Commit 861b609

Browse files
author
Andre Bonna
committed
Change SnapshotMatcher to save ordered to prevent long VCS diffs
1 parent eacbd77 commit 861b609

File tree

8 files changed

+60
-53
lines changed

8 files changed

+60
-53
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ Add to your pom.xml dependencies section:
2828
<dependency>
2929
<groupId>io.github.json-snapshot</groupId>
3030
<artifactId>json-snapshot</artifactId>
31-
<version>1.0.7</version>
31+
<version>1.0.8</version>
3232
</dependency>
3333
```
3434

pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<modelVersion>4.0.0</modelVersion>
66
<groupId>io.github.json-snapshot</groupId>
77
<artifactId>json-snapshot</artifactId>
8-
<version>1.0.7</version>
8+
<version>1.0.8</version>
99
<packaging>jar</packaging>
1010

1111
<name>json-snapshot</name>

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

+4-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77

88
import java.lang.reflect.Method;
99
import java.util.Arrays;
10+
import java.util.Collection;
1011
import java.util.List;
12+
import java.util.Set;
1113

1214
public class Snapshot {
1315

@@ -31,7 +33,7 @@ public class Snapshot {
3133

3234
public void toMatchSnapshot() {
3335

34-
List<String> rawSnapshots = snapshotFile.getRawSnapshots();
36+
Set<String> rawSnapshots = snapshotFile.getRawSnapshots();
3537

3638
String rawSnapshot = getRawSnapshot(rawSnapshots);
3739

@@ -59,7 +61,7 @@ private SnapshotMatchException generateDiffError(String rawSnapshot, String curr
5961
return new SnapshotMatchException(error);
6062
}
6163

62-
private String getRawSnapshot(List<String> rawSnapshots) {
64+
private String getRawSnapshot(Collection<String> rawSnapshots) {
6365
for (String rawSnapshot : rawSnapshots) {
6466

6567
if (rawSnapshot.contains(getSnapshotName())) {

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

+8-4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
import java.io.*;
77
import java.util.ArrayList;
88
import java.util.List;
9+
import java.util.Set;
10+
import java.util.TreeSet;
11+
import java.util.function.Supplier;
912
import java.util.stream.Collectors;
1013
import java.util.stream.Stream;
1114

@@ -17,7 +20,7 @@ public class SnapshotFile {
1720
private String fileName;
1821

1922
@Getter
20-
private List<String> rawSnapshots;
23+
private Set<String> rawSnapshots;
2124

2225
SnapshotFile(String fileName) throws IOException {
2326

@@ -35,14 +38,15 @@ public class SnapshotFile {
3538

3639
String fileText = fileContent.toString();
3740
if (StringUtils.isNotBlank(fileText)) {
38-
rawSnapshots = Stream.of(fileContent.toString().split(SPLIT_STRING)).collect(Collectors.toList());
41+
rawSnapshots = Stream.of(fileContent.toString().split(SPLIT_STRING)).map(String::trim).collect(Collectors.toCollection(
42+
TreeSet::new));
3943
}
4044
else {
41-
rawSnapshots = new ArrayList<>();
45+
rawSnapshots = new TreeSet<>();
4246
}
4347
} catch (IOException e) {
4448
createFile(this.fileName);
45-
rawSnapshots = new ArrayList<>();
49+
rawSnapshots = new TreeSet<>();
4650
}
4751
}
4852

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import java.util.ArrayList;
1818
import java.util.HashMap;
1919
import java.util.List;
20+
import java.util.Set;
2021
import java.util.stream.Collectors;
2122

2223
import static org.mockito.Mockito.atLeastOnce;
@@ -41,7 +42,7 @@ public static void start() {
4142
}
4243

4344
public static void validateSnapshots() {
44-
List<String> rawSnapshots = snapshotFile.getRawSnapshots();
45+
Set<String> rawSnapshots = snapshotFile.getRawSnapshots();
4546
List<String> snapshotNames = calledSnapshots.stream().map(Snapshot::getSnapshotName).collect(Collectors.toList());
4647
List<String> unusedRawSnapshots = new ArrayList<>();
4748

Original file line numberDiff line numberDiff line change
@@ -1,35 +1,35 @@
1-
io.github.jsonSnapshot.SnapshotIntegrationTest.shouldThrowSnapshotMatchException=[
1+
io.github.jsonSnapshot.SnapshotIntegrationTest.shouldMatchSnapshotFour=[
22
{
3-
"id": "anyId5",
4-
"value": 5,
5-
"name": "anyName5"
3+
"id": "anyId4",
4+
"value": 4,
5+
"name": "any\n\n\nName4"
66
}
77
]
88

99

10-
io.github.jsonSnapshot.SnapshotIntegrationTest.shouldMatchSnapshotThree=[
10+
io.github.jsonSnapshot.SnapshotIntegrationTest.shouldMatchSnapshotInsidePrivateMethod=[
1111
{
12-
"id": "anyId3",
13-
"value": 3,
14-
"name": "anyName3"
12+
"id": "anyPrivate",
13+
"value": 5,
14+
"name": "anyPrivate"
1515
}
1616
]
1717

1818

19-
io.github.jsonSnapshot.SnapshotIntegrationTest.shouldMatchSnapshotFour=[
19+
io.github.jsonSnapshot.SnapshotIntegrationTest.shouldMatchSnapshotOne=[
2020
{
21-
"id": "anyId4",
22-
"value": 4,
23-
"name": "any\n\n\nName4"
21+
"id": "anyId1",
22+
"value": 1,
23+
"name": "anyName1"
2424
}
2525
]
2626

2727

28-
io.github.jsonSnapshot.SnapshotIntegrationTest.shouldMatchSnapshotOne=[
28+
io.github.jsonSnapshot.SnapshotIntegrationTest.shouldMatchSnapshotThree=[
2929
{
30-
"id": "anyId1",
31-
"value": 1,
32-
"name": "anyName1"
30+
"id": "anyId3",
31+
"value": 3,
32+
"name": "anyName3"
3333
}
3434
]
3535

@@ -43,11 +43,10 @@ io.github.jsonSnapshot.SnapshotIntegrationTest.shouldMatchSnapshotTwo=[
4343
]
4444

4545

46-
47-
io.github.jsonSnapshot.SnapshotIntegrationTest.shouldMatchSnapshotInsidePrivateMethod=[
46+
io.github.jsonSnapshot.SnapshotIntegrationTest.shouldThrowSnapshotMatchException=[
4847
{
49-
"id": "anyPrivate",
48+
"id": "anyId5",
5049
"value": 5,
51-
"name": "anyPrivate"
50+
"name": "anyName5"
5251
}
5352
]

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import java.io.IOException;
1010
import java.nio.file.Files;
1111
import java.nio.file.Paths;
12+
import java.util.TreeSet;
1213
import java.util.stream.Collectors;
1314
import java.util.stream.Stream;
1415

@@ -47,7 +48,7 @@ public void shouldGetSnapshotNameSuccessfully() {
4748
@Test
4849
public void shouldMatchSnapshotSuccessfully() {
4950
snapshot.toMatchSnapshot();
50-
assertThat(snapshotFile.getRawSnapshots()).isEqualTo(Stream.of(SNAPSHOT).collect(Collectors.toList()));
51+
assertThat(snapshotFile.getRawSnapshots()).isEqualTo(Stream.of(SNAPSHOT).collect(Collectors.toCollection(TreeSet::new)));
5152
}
5253

5354
@Test(expected = SnapshotMatchException.class)

src/test/java/io/github/jsonSnapshot/SnapshotUtilsTest.snap

+23-23
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,26 @@
1+
io.github.jsonSnapshot.SnapshotUtilsTest.shouldExtractArgsFromFakeMethod=[
2+
{
3+
"FakeObject.fakeMethod": [
4+
{
5+
"arg0": "test1",
6+
"arg1": 1,
7+
"arg2": [
8+
"listTest1"
9+
]
10+
},
11+
{
12+
"arg0": "test2",
13+
"arg1": 2,
14+
"arg2": [
15+
"listTest1",
16+
"listTest2"
17+
]
18+
}
19+
]
20+
}
21+
]
22+
23+
124
io.github.jsonSnapshot.SnapshotUtilsTest.shouldExtractArgsFromFakeMethodWithComplexFakeObject=[
225
{
326
"FakeObject.fakeMethodWithComplexObject": [
@@ -41,27 +64,4 @@ io.github.jsonSnapshot.SnapshotUtilsTest.shouldExtractArgsFromFakeMethodWithComp
4164
}
4265
]
4366
}
44-
]
45-
46-
47-
io.github.jsonSnapshot.SnapshotUtilsTest.shouldExtractArgsFromFakeMethod=[
48-
{
49-
"FakeObject.fakeMethod": [
50-
{
51-
"arg0": "test1",
52-
"arg1": 1,
53-
"arg2": [
54-
"listTest1"
55-
]
56-
},
57-
{
58-
"arg0": "test2",
59-
"arg1": 2,
60-
"arg2": [
61-
"listTest1",
62-
"listTest2"
63-
]
64-
}
65-
]
66-
}
6767
]

0 commit comments

Comments
 (0)