File tree Expand file tree Collapse 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 Expand file tree Collapse file tree 2 files changed +24
-19
lines changed Original file line number Diff line number Diff line change 33import au .com .origin .snapshots .Snapshot ;
44import au .com .origin .snapshots .SnapshotFile ;
55import au .com .origin .snapshots .SnapshotSerializerContext ;
6- import java .util .Arrays ;
7- import java .util .List ;
8- import java .util .stream .Collectors ;
96import lombok .extern .slf4j .Slf4j ;
107
118/**
@@ -18,22 +15,12 @@ public class ToStringSnapshotSerializer implements SnapshotSerializer {
1815
1916 @ Override
2017 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+ }
3724 return gen .toSnapshot (body );
3825 }
3926
Original file line number Diff line number Diff line change @@ -58,6 +58,24 @@ void shouldReplaceThreeConsecutiveNewLines() {
5858 assertThat (result .getBody ()).isEqualTo ("[\n John\n .\n .\n Doe\n ]" );
5959 }
6060
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+
6179 @ AllArgsConstructor
6280 @ Data
6381 private static class Dummy {
You can’t perform that action at this time.
0 commit comments