From ae9afcc3932b4fd2dd859f1450538c224f6ae2d3 Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Thu, 3 Sep 2020 09:16:37 +0200 Subject: [PATCH] Add test to cover line break --- CHANGES.md | 1 + .../session/room/send/MarkdownParserTest.kt | 82 ++++++++++++++++++- .../session/room/send/MarkdownParser.kt | 1 + 3 files changed, 80 insertions(+), 4 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 9a41f3c4ef..9d2a711e84 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -10,6 +10,7 @@ Improvements 🙌: Bugfix 🐛: - 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) - Fix changing language issue - Fix FontSize issue (#1483, #1787) diff --git a/matrix-sdk-android/src/androidTest/java/org/matrix/android/sdk/internal/session/room/send/MarkdownParserTest.kt b/matrix-sdk-android/src/androidTest/java/org/matrix/android/sdk/internal/session/room/send/MarkdownParserTest.kt index 2be535b16e..3a25b92c2a 100644 --- a/matrix-sdk-android/src/androidTest/java/org/matrix/android/sdk/internal/session/room/send/MarkdownParserTest.kt +++ b/matrix-sdk-android/src/androidTest/java/org/matrix/android/sdk/internal/session/room/send/MarkdownParserTest.kt @@ -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, * we can add more tests to cover the edge cases. * 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 - * 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. + * Element Web should be used as a reference for expected results, but not always. + * 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 + * the same to be able to edit messages (See #1939) * See https://matrix.org/docs/spec/client_server/latest#m-room-message-msgtypes */ @Suppress("SpellCheckingInspection") @@ -81,6 +81,15 @@ class MarkdownParserTest : InstrumentedTest { ) } + @Test + fun parseBoldNewLines() { + testTypeNewLines( + name = "bold", + markdownPattern = "**", + htmlExpectedTag = "strong" + ) + } + @Test fun parseItalic() { testType( @@ -90,6 +99,15 @@ class MarkdownParserTest : InstrumentedTest { ) } + @Test + fun parseItalicNewLines() { + testTypeNewLines( + name = "italic", + markdownPattern = "*", + htmlExpectedTag = "em" + ) + } + @Test fun parseItalic2() { // Element Web format @@ -108,6 +126,15 @@ class MarkdownParserTest : InstrumentedTest { ) } + @Test + fun parseStrikeNewLines() { + testTypeNewLines( + name = "strike", + markdownPattern = "~~", + htmlExpectedTag = "del" + ) + } + @Test fun parseCode() { testType( @@ -117,6 +144,15 @@ class MarkdownParserTest : InstrumentedTest { ) } + @Test + fun parseCodeNewLines() { + testTypeNewLines( + name = "code", + markdownPattern = "`", + htmlExpectedTag = "code" + ) + } + @Test fun parseCode2() { testType( @@ -126,6 +162,15 @@ class MarkdownParserTest : InstrumentedTest { ) } + @Test + fun parseCode2NewLines() { + testTypeNewLines( + name = "code", + markdownPattern = "``", + htmlExpectedTag = "code" + ) + } + @Test fun parseCode3() { testType( @@ -135,6 +180,15 @@ class MarkdownParserTest : InstrumentedTest { ) } + @Test + fun parseCode3NewLines() { + testTypeNewLines( + name = "code", + markdownPattern = "```", + htmlExpectedTag = "code" + ) + } + @Test fun parseUnorderedList() { "- item1".let { markdownParser.parse(it).expect(it, "") } @@ -164,7 +218,7 @@ class MarkdownParserTest : InstrumentedTest { @Test fun parseQuote_not_passing() { - "> quoted\nline2".let { markdownParser.parse(it).expect(it, "

quoted
line2

") } + "> quoted\nline2".let { markdownParser.parse(it).expect(it, "

quoted
line2

") } } @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
$name") + } + + // With new line between two blocks + "$markdownPattern$name$markdownPattern\n$markdownPattern$name$markdownPattern" + .let { + markdownParser.parse(it) + .expect(expectedText = it, + expectedFormattedText = "<$htmlExpectedTag>$name<$htmlExpectedTag>$name") + } + } + private fun TextContent.expect(expectedText: String, expectedFormattedText: String?) { assertEquals("TextContent are not identical", TextContent(expectedText, expectedFormattedText), this) } diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/send/MarkdownParser.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/send/MarkdownParser.kt index 6ae3df187a..b493897d8a 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/send/MarkdownParser.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/send/MarkdownParser.kt @@ -70,6 +70,7 @@ internal class MarkdownParser @Inject constructor( // Remove extra space before and after the content .trim() // 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", "") } }