Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@
import au.com.origin.snapshots.Snapshot;
import au.com.origin.snapshots.SnapshotFile;
import au.com.origin.snapshots.SnapshotSerializerContext;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import lombok.extern.slf4j.Slf4j;

/**
Expand All @@ -18,22 +15,12 @@ public class ToStringSnapshotSerializer implements SnapshotSerializer {

@Override
public Snapshot apply(Object object, SnapshotSerializerContext gen) {
List<Object> objects = Arrays.asList(object);
String body =
"[\n"
+ objects.stream()
.map(Object::toString)
.map(
it -> {
if (it.contains(SnapshotFile.SPLIT_STRING)) {
log.warn(
"Found 3 consecutive lines in your snapshot \\n\\n\\n. This sequence is reserved as the snapshot separator - replacing with \\n.\\n.\\n");
return it.replaceAll(SnapshotFile.SPLIT_STRING, "\n.\n.\n");
}
return it;
})
.collect(Collectors.joining("\n"))
+ "\n]";
String body = "[\n" + object.toString() + "\n]";
if (body.contains(SnapshotFile.SPLIT_STRING)) {
log.warn(
"Found 3 consecutive lines in your snapshot \\n\\n\\n. This sequence is reserved as the snapshot separator - replacing with \\n.\\n.\\n");
body = body.replaceAll(SnapshotFile.SPLIT_STRING, "\n.\n.\n");
}
return gen.toSnapshot(body);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,24 @@ void shouldReplaceThreeConsecutiveNewLines() {
assertThat(result.getBody()).isEqualTo("[\nJohn\n.\n.\nDoe\n]");
}

@Test
void shouldReplaceTwoConsecutiveNewLinesAtEnd() {
Snapshot result = serializer.apply("John Doe\n\n", mockSnapshotGenerator);
assertThat(result.getBody()).isEqualTo("[\nJohn Doe\n.\n.\n]");
}

@Test
void shouldReplaceTwoConsecutiveNewLinesAtBeginning() {
Snapshot result = serializer.apply("\n\nJohn Doe", mockSnapshotGenerator);
assertThat(result.getBody()).isEqualTo("[\n.\n.\nJohn Doe\n]");
}

@Test
void shouldReplaceIllegalNewlineSequencesEverywhere() {
Snapshot result = serializer.apply("\n\nJohn\n\n\nDoe\n\n", mockSnapshotGenerator);
assertThat(result.getBody()).isEqualTo("[\n.\n.\nJohn\n.\n.\nDoe\n.\n.\n]");
}

@AllArgsConstructor
@Data
private static class Dummy {
Expand Down