File tree 2 files changed +24
-19
lines changed
java-snapshot-testing-core/src
main/java/au/com/origin/snapshots/serializers
test/java/au/com/origin/snapshots/serializers
2 files changed +24
-19
lines changed Original file line number Diff line number Diff line change 3
3
import au .com .origin .snapshots .Snapshot ;
4
4
import au .com .origin .snapshots .SnapshotFile ;
5
5
import au .com .origin .snapshots .SnapshotSerializerContext ;
6
- import java .util .Arrays ;
7
- import java .util .List ;
8
- import java .util .stream .Collectors ;
9
6
import lombok .extern .slf4j .Slf4j ;
10
7
11
8
/**
@@ -18,22 +15,12 @@ public class ToStringSnapshotSerializer implements SnapshotSerializer {
18
15
19
16
@ Override
20
17
public Snapshot apply (Object object , SnapshotSerializerContext gen ) {
21
- List <Object > objects = Arrays .asList (object );
22
- String body =
23
- "[\n "
24
- + objects .stream ()
25
- .map (Object ::toString )
26
- .map (
27
- it -> {
28
- if (it .contains (SnapshotFile .SPLIT_STRING )) {
29
- log .warn (
30
- "Found 3 consecutive lines in your snapshot \\ n\\ n\\ n. This sequence is reserved as the snapshot separator - replacing with \\ n.\\ n.\\ n" );
31
- return it .replaceAll (SnapshotFile .SPLIT_STRING , "\n .\n .\n " );
32
- }
33
- return it ;
34
- })
35
- .collect (Collectors .joining ("\n " ))
36
- + "\n ]" ;
18
+ String body = "[\n " + object .toString () + "\n ]" ;
19
+ if (body .contains (SnapshotFile .SPLIT_STRING )) {
20
+ log .warn (
21
+ "Found 3 consecutive lines in your snapshot \\ n\\ n\\ n. This sequence is reserved as the snapshot separator - replacing with \\ n.\\ n.\\ n" );
22
+ body = body .replaceAll (SnapshotFile .SPLIT_STRING , "\n .\n .\n " );
23
+ }
37
24
return gen .toSnapshot (body );
38
25
}
39
26
Original file line number Diff line number Diff line change @@ -58,6 +58,24 @@ void shouldReplaceThreeConsecutiveNewLines() {
58
58
assertThat (result .getBody ()).isEqualTo ("[\n John\n .\n .\n Doe\n ]" );
59
59
}
60
60
61
+ @ Test
62
+ void shouldReplaceTwoConsecutiveNewLinesAtEnd () {
63
+ Snapshot result = serializer .apply ("John Doe\n \n " , mockSnapshotGenerator );
64
+ assertThat (result .getBody ()).isEqualTo ("[\n John Doe\n .\n .\n ]" );
65
+ }
66
+
67
+ @ Test
68
+ void shouldReplaceTwoConsecutiveNewLinesAtBeginning () {
69
+ Snapshot result = serializer .apply ("\n \n John Doe" , mockSnapshotGenerator );
70
+ assertThat (result .getBody ()).isEqualTo ("[\n .\n .\n John Doe\n ]" );
71
+ }
72
+
73
+ @ Test
74
+ void shouldReplaceIllegalNewlineSequencesEverywhere () {
75
+ Snapshot result = serializer .apply ("\n \n John\n \n \n Doe\n \n " , mockSnapshotGenerator );
76
+ assertThat (result .getBody ()).isEqualTo ("[\n .\n .\n John\n .\n .\n Doe\n .\n .\n ]" );
77
+ }
78
+
61
79
@ AllArgsConstructor
62
80
@ Data
63
81
private static class Dummy {
You can’t perform that action at this time.
0 commit comments