Add test to cover line break

This commit is contained in:
Benoit Marty 2020-09-03 09:16:37 +02:00
parent e73480c0ef
commit ae9afcc393
3 changed files with 80 additions and 4 deletions

View File

@ -10,6 +10,7 @@ Improvements 🙌:
Bugfix 🐛: Bugfix 🐛:
- Display name not shown under Settings/General (#1926) - Display name not shown under Settings/General (#1926)
- Editing message forgets line breaks and markdown (#1939)
- Words containing my name should not trigger notifications (#1781) - Words containing my name should not trigger notifications (#1781)
- Fix changing language issue - Fix changing language issue
- Fix FontSize issue (#1483, #1787) - Fix FontSize issue (#1483, #1787)

View File

@ -30,9 +30,9 @@ import org.matrix.android.sdk.InstrumentedTest
* It will not be possible to test all combinations. For the moment I add a few tests, then, depending on the problem discovered in the wild, * It will not be possible to test all combinations. For the moment I add a few tests, then, depending on the problem discovered in the wild,
* we can add more tests to cover the edge cases. * we can add more tests to cover the edge cases.
* Some tests are suffixed with `_not_passing`, maybe one day we will fix them... * Some tests are suffixed with `_not_passing`, maybe one day we will fix them...
* Element Web should be used as a reference for expected results, but not always. Especially Element Web add lots of `\n` in the * Element Web should be used as a reference for expected results, but not always.
* formatted body, which is quite useless. * Also Element Web does not provide plain text body when formatted text is provided. The body contains what the user has entered. We are doing
* Also Element Web does not provide plain text body when formatted text is provided. The body contains what the user has entered. * the same to be able to edit messages (See #1939)
* See https://matrix.org/docs/spec/client_server/latest#m-room-message-msgtypes * See https://matrix.org/docs/spec/client_server/latest#m-room-message-msgtypes
*/ */
@Suppress("SpellCheckingInspection") @Suppress("SpellCheckingInspection")
@ -81,6 +81,15 @@ class MarkdownParserTest : InstrumentedTest {
) )
} }
@Test
fun parseBoldNewLines() {
testTypeNewLines(
name = "bold",
markdownPattern = "**",
htmlExpectedTag = "strong"
)
}
@Test @Test
fun parseItalic() { fun parseItalic() {
testType( testType(
@ -90,6 +99,15 @@ class MarkdownParserTest : InstrumentedTest {
) )
} }
@Test
fun parseItalicNewLines() {
testTypeNewLines(
name = "italic",
markdownPattern = "*",
htmlExpectedTag = "em"
)
}
@Test @Test
fun parseItalic2() { fun parseItalic2() {
// Element Web format // Element Web format
@ -108,6 +126,15 @@ class MarkdownParserTest : InstrumentedTest {
) )
} }
@Test
fun parseStrikeNewLines() {
testTypeNewLines(
name = "strike",
markdownPattern = "~~",
htmlExpectedTag = "del"
)
}
@Test @Test
fun parseCode() { fun parseCode() {
testType( testType(
@ -117,6 +144,15 @@ class MarkdownParserTest : InstrumentedTest {
) )
} }
@Test
fun parseCodeNewLines() {
testTypeNewLines(
name = "code",
markdownPattern = "`",
htmlExpectedTag = "code"
)
}
@Test @Test
fun parseCode2() { fun parseCode2() {
testType( testType(
@ -126,6 +162,15 @@ class MarkdownParserTest : InstrumentedTest {
) )
} }
@Test
fun parseCode2NewLines() {
testTypeNewLines(
name = "code",
markdownPattern = "``",
htmlExpectedTag = "code"
)
}
@Test @Test
fun parseCode3() { fun parseCode3() {
testType( testType(
@ -135,6 +180,15 @@ class MarkdownParserTest : InstrumentedTest {
) )
} }
@Test
fun parseCode3NewLines() {
testTypeNewLines(
name = "code",
markdownPattern = "```",
htmlExpectedTag = "code"
)
}
@Test @Test
fun parseUnorderedList() { fun parseUnorderedList() {
"- item1".let { markdownParser.parse(it).expect(it, "<ul><li>item1</li></ul>") } "- item1".let { markdownParser.parse(it).expect(it, "<ul><li>item1</li></ul>") }
@ -164,7 +218,7 @@ class MarkdownParserTest : InstrumentedTest {
@Test @Test
fun parseQuote_not_passing() { fun parseQuote_not_passing() {
"> quoted\nline2".let { markdownParser.parse(it).expect(it, "<blockquote><p>quoted<br/>line2</p></blockquote>") } "> quoted\nline2".let { markdownParser.parse(it).expect(it, "<blockquote><p>quoted<br />line2</p></blockquote>") }
} }
@Test @Test
@ -278,6 +332,26 @@ class MarkdownParserTest : InstrumentedTest {
} }
} }
private fun testTypeNewLines(name: String,
markdownPattern: String,
htmlExpectedTag: String) {
// With new line inside the block
"$markdownPattern$name\n$name$markdownPattern"
.let {
markdownParser.parse(it)
.expect(expectedText = it,
expectedFormattedText = "<$htmlExpectedTag>$name<br />$name</$htmlExpectedTag>")
}
// With new line between two blocks
"$markdownPattern$name$markdownPattern\n$markdownPattern$name$markdownPattern"
.let {
markdownParser.parse(it)
.expect(expectedText = it,
expectedFormattedText = "<$htmlExpectedTag>$name</$htmlExpectedTag><$htmlExpectedTag>$name</$htmlExpectedTag>")
}
}
private fun TextContent.expect(expectedText: String, expectedFormattedText: String?) { private fun TextContent.expect(expectedText: String, expectedFormattedText: String?) {
assertEquals("TextContent are not identical", TextContent(expectedText, expectedFormattedText), this) assertEquals("TextContent are not identical", TextContent(expectedText, expectedFormattedText), this)
} }

View File

@ -70,6 +70,7 @@ internal class MarkdownParser @Inject constructor(
// Remove extra space before and after the content // Remove extra space before and after the content
.trim() .trim()
// There is no need to include new line in an html-like source // There is no need to include new line in an html-like source
// But new line can be in embedded code block, so do not remove them
.replace("\n", "") .replace("\n", "")
} }
} }