From 4c0ac30dc82677b2b2db36d114b452fdea537a37 Mon Sep 17 00:00:00 2001 From: Linnea Gräf Date: Sun, 24 Mar 2024 15:54:19 +0100 Subject: Add italics parsing unit tests --- src/main/kotlin/moe/nea/blog/md/ItalicsParser.kt | 6 +- src/main/kotlin/moe/nea/blog/md/MarkdownParser.kt | 7 ++- .../kotlin/moe/nea/blog/md/test/TestItalics.kt | 64 ++++++++++++++++++++++ 3 files changed, 71 insertions(+), 6 deletions(-) create mode 100644 src/test/kotlin/moe/nea/blog/md/test/TestItalics.kt diff --git a/src/main/kotlin/moe/nea/blog/md/ItalicsParser.kt b/src/main/kotlin/moe/nea/blog/md/ItalicsParser.kt index 30b5c38..d3c9d5d 100644 --- a/src/main/kotlin/moe/nea/blog/md/ItalicsParser.kt +++ b/src/main/kotlin/moe/nea/blog/md/ItalicsParser.kt @@ -2,7 +2,7 @@ package moe.nea.blog.md object ItalicsParser : InlineParser { override fun detect(lookback: MarkdownFormat, rest: String): Boolean { - return lookback is Whitespace && "\\*+[^ ].*".toRegex().matches(rest) + return (lookback is Whitespace || lookback is Begin) && "\\*+[^ ].*".toRegex().matches(rest) } override val specialSyntax: Set @@ -48,10 +48,6 @@ object ItalicsParser : InlineParser { if (secondStarCount == 1) firstElement = Italics(firstElement) if (secondStarCount == firstStarCount) { - if (isBold) - firstElement = Bold(firstElement) - if (isItalics) - firstElement = Italics(firstElement) return Pair(firstElement, remainingText) } diff --git a/src/main/kotlin/moe/nea/blog/md/MarkdownParser.kt b/src/main/kotlin/moe/nea/blog/md/MarkdownParser.kt index f8095ad..144eeda 100644 --- a/src/main/kotlin/moe/nea/blog/md/MarkdownParser.kt +++ b/src/main/kotlin/moe/nea/blog/md/MarkdownParser.kt @@ -58,6 +58,10 @@ class MarkdownParser(source: String) { if (text[0] == ' ') return Pair(Whitespace(), text.substring(1)) val nextSpecial = text.indexOfFirst { it in inlineParsers.flatMap { it.specialSyntax } || it == ' ' } + if (nextSpecial == 0) + return Pair(Word(text.substring(0, 1)), text.substring(1)) + if (nextSpecial == -1) + return Pair(Word(text), "") return Pair(Word(text.substring(0, nextSpecial)), text.substring(nextSpecial)) } @@ -103,7 +107,8 @@ class MarkdownParser(source: String) { } fun collapseInlineFormat(sequence: List, trimWhitespace: Boolean): MarkdownFormat { - return FormatSequence(collapseMarkdownFormats(expandMarkdownFormats(sequence), trimWhitespace)) + val formats = collapseMarkdownFormats(expandMarkdownFormats(sequence), trimWhitespace) + return formats.singleOrNull() ?: FormatSequence(formats) } fun readDocument(): Document { diff --git a/src/test/kotlin/moe/nea/blog/md/test/TestItalics.kt b/src/test/kotlin/moe/nea/blog/md/test/TestItalics.kt new file mode 100644 index 0000000..b4b102a --- /dev/null +++ b/src/test/kotlin/moe/nea/blog/md/test/TestItalics.kt @@ -0,0 +1,64 @@ +package moe.nea.blog.md.test + +import moe.nea.blog.md.MarkdownFormat +import moe.nea.blog.md.MarkdownParser +import java.io.ByteArrayOutputStream +import java.io.PrintStream +import kotlin.test.Test +import kotlin.test.assertEquals + +class TestItalics { + + fun parseInline(string: String): MarkdownFormat { + val parser = MarkdownParser(string) + parser.addDefaultParsers() + return parser.parseInlineText(string) + } + + fun assertInlineFormat(expected: String, source: String) { + val format = parseInline(source) + val baos = ByteArrayOutputStream() + format.debugFormat(PrintStream(baos)) + assertEquals(expected, baos.toByteArray().decodeToString()) + } + + @Test + fun testDoubleStartItalicsInBold() { + assertInlineFormat("both just bold", "***both* just bold**") + } + + @Test + fun testDoubleStartBoldInItalics() { + assertInlineFormat("both just italics", "***both** just italics*") + } + + @Test + fun testDoubleStartRestartingItalics() { + assertInlineFormat( + "both just italics both again", + "***both** just italics **both again***" + ) + } + + @Test + fun testStandaloneBold() { + assertInlineFormat("bold", "**bold**") + } + + @Test + fun testStandaloneItalics() { + assertInlineFormat("italic", "*italic*") + } + + @Test + fun testInline() { + assertInlineFormat("left italics middle bold right", "left *italics* middle **bold** right") + } + + @Test + fun testNested() { + assertInlineFormat("a bold b", "*a **bold** b*") + assertInlineFormat("a double italics", "*a *double italics**") + } + +} \ No newline at end of file -- cgit