-
-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix bare URLs are getting cleared on Markdown
- Loading branch information
1 parent
d53040b
commit 7fe40f8
Showing
4 changed files
with
255 additions
and
168 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
177 changes: 177 additions & 0 deletions
177
...tlin/com.mohamedrejeb.richeditor/parser/markdown/RichTextStateMarkdownParserDecodeTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,177 @@ | ||
package com.mohamedrejeb.richeditor.parser.markdown | ||
|
||
import androidx.compose.ui.text.SpanStyle | ||
import androidx.compose.ui.text.TextRange | ||
import androidx.compose.ui.text.font.FontStyle | ||
import androidx.compose.ui.text.font.FontWeight | ||
import androidx.compose.ui.text.input.TextFieldValue | ||
import androidx.compose.ui.text.style.TextDecoration | ||
import com.mohamedrejeb.richeditor.annotation.ExperimentalRichTextApi | ||
import com.mohamedrejeb.richeditor.model.RichSpan | ||
import com.mohamedrejeb.richeditor.model.RichTextState | ||
import com.mohamedrejeb.richeditor.paragraph.RichParagraph | ||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
|
||
class RichTextStateMarkdownParserDecodeTest { | ||
|
||
/** | ||
* Decode tests | ||
*/ | ||
|
||
@Test | ||
fun testDecodeBold() { | ||
val expectedText = "Hello World!" | ||
val state = RichTextState() | ||
|
||
state.toggleSpanStyle(SpanStyle(fontWeight = FontWeight.Bold)) | ||
state.onTextFieldValueChange( | ||
TextFieldValue( | ||
text = expectedText, | ||
selection = TextRange(expectedText.length) | ||
) | ||
) | ||
|
||
val markdown = RichTextStateMarkdownParser.decode(state) | ||
val actualText = state.annotatedString.text | ||
|
||
assertEquals( | ||
expected = expectedText, | ||
actual = actualText, | ||
) | ||
|
||
assertEquals( | ||
expected = "**$expectedText**", | ||
actual = markdown | ||
) | ||
} | ||
|
||
@Test | ||
fun testDecodeItalic() { | ||
val expectedText = "Hello World!" | ||
val state = RichTextState() | ||
|
||
state.toggleSpanStyle(SpanStyle(fontStyle = FontStyle.Italic)) | ||
state.onTextFieldValueChange( | ||
TextFieldValue( | ||
text = expectedText, | ||
selection = TextRange(expectedText.length) | ||
) | ||
) | ||
|
||
val markdown = RichTextStateMarkdownParser.decode(state) | ||
val actualText = state.annotatedString.text | ||
|
||
assertEquals( | ||
expected = expectedText, | ||
actual = actualText, | ||
) | ||
|
||
assertEquals( | ||
expected = "*$expectedText*", | ||
actual = markdown | ||
) | ||
} | ||
|
||
@Test | ||
fun testDecodeLineThrough() { | ||
val expectedText = "Hello World!" | ||
val state = RichTextState() | ||
|
||
state.toggleSpanStyle(SpanStyle(textDecoration = TextDecoration.LineThrough)) | ||
state.onTextFieldValueChange( | ||
TextFieldValue( | ||
text = expectedText, | ||
selection = TextRange(expectedText.length) | ||
) | ||
) | ||
|
||
val markdown = RichTextStateMarkdownParser.decode(state) | ||
val actualText = state.annotatedString.text | ||
|
||
assertEquals( | ||
expected = expectedText, | ||
actual = actualText, | ||
) | ||
|
||
assertEquals( | ||
expected = "~~$expectedText~~", | ||
actual = markdown | ||
) | ||
} | ||
|
||
@OptIn(ExperimentalRichTextApi::class) | ||
@Test | ||
fun testDecodeUnderline() { | ||
val expectedText = "Hello World!" | ||
val state = RichTextState( | ||
initialRichParagraphList = listOf( | ||
RichParagraph().also { | ||
it.children.add( | ||
RichSpan( | ||
text = expectedText, | ||
paragraph = it, | ||
spanStyle = SpanStyle(textDecoration = TextDecoration.Underline) | ||
) | ||
) | ||
} | ||
) | ||
) | ||
|
||
val markdown = RichTextStateMarkdownParser.decode(state) | ||
val actualText = state.annotatedString.text | ||
|
||
assertEquals( | ||
expected = expectedText, | ||
actual = actualText, | ||
) | ||
|
||
assertEquals( | ||
expected = "<u>$expectedText</u>", | ||
actual = markdown | ||
) | ||
} | ||
|
||
@OptIn(ExperimentalRichTextApi::class) | ||
@Test | ||
fun testDecodeLineBreak() { | ||
val state = RichTextState( | ||
initialRichParagraphList = listOf( | ||
RichParagraph().also { | ||
it.children.add( | ||
RichSpan( | ||
text = "Hello", | ||
paragraph = it | ||
) | ||
) | ||
}, | ||
RichParagraph(), | ||
RichParagraph(), | ||
RichParagraph(), | ||
RichParagraph().also { | ||
it.children.add( | ||
RichSpan( | ||
text = "World!", | ||
paragraph = it | ||
) | ||
) | ||
} | ||
) | ||
) | ||
|
||
val markdown = RichTextStateMarkdownParser.decode(state) | ||
|
||
assertEquals( | ||
expected = | ||
""" | ||
Hello | ||
<br> | ||
<br> | ||
World! | ||
""".trimIndent(), | ||
actual = markdown, | ||
) | ||
} | ||
|
||
} |
Oops, something went wrong.