Fix bug in EmailSectionExtractor when text ends with an empty line

This commit is contained in:
cketti 2018-02-06 19:14:56 +01:00
parent 990765ac6e
commit 8f334dd0c3
2 changed files with 35 additions and 6 deletions

View file

@ -96,12 +96,10 @@ class EmailSectionExtractor private constructor(val text: String) {
}
private fun completeLastSection() {
if (!isStartOfLine) {
if (quoteDepth == 0) {
sectionBuilder.addSegment(0, sectionStartIndex, text.length)
} else {
sectionBuilder.addSegment(spaces, startOfContentIndex, text.length)
}
if (quoteDepth == 0) {
sectionBuilder.addSegment(0, sectionStartIndex, text.length)
} else if (!isStartOfLine) {
sectionBuilder.addSegment(spaces, startOfContentIndex, text.length)
}
appendSection()

View file

@ -26,6 +26,19 @@ class EmailSectionExtractorTest {
}
}
@Test
fun simpleMessageEndingWithTwoNewlines() {
val message = "Hello\n\n"
val sections = EmailSectionExtractor.extract(message)
assertThat(sections.size).isEqualTo(1)
with(sections[0]) {
assertThat(quoteDepth).isEqualTo(0)
assertThat(toString()).isEqualTo(message)
}
}
@Test
fun quoteFollowedByReply() {
val message = """
@ -81,6 +94,24 @@ class EmailSectionExtractorTest {
}
}
@Test
fun quoteEndingWithEmptyLineButNoNewline() {
val message = """
> Quoted text
> """.trimIndent()
val sections = EmailSectionExtractor.extract(message)
assertThat(sections.size).isEqualTo(1)
with(sections[0]) {
assertThat(quoteDepth).isEqualTo(1)
// Note: "Quoted text\n\n" would be a better representation of the quoted text. The goal of this test is
// not to preserve the current behavior of only ending in one newline, but to make sure we don't add the
// last line twice.
assertThat(toString()).isEqualTo("Quoted text\n")
}
}
@Test
fun chaosQuoting() {
val message = """