Skip to content

Commit

Permalink
update ParserTest to kotlin and add failure test case
Browse files Browse the repository at this point in the history
  • Loading branch information
lytefast committed Oct 24, 2020
1 parent 6c43bba commit 177dd2e
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 120 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package com.discord.simpleast.core.node

import android.text.SpannableStringBuilder
import android.text.Spanned
import android.text.style.CharacterStyle
import org.jetbrains.annotations.TestOnly


/**
Expand Down Expand Up @@ -32,8 +32,9 @@ open class StyleNode<RC, T>(val styles: List<T>) : Node<RC>() {
* the text content will be.
*/
@JvmStatic
fun <RC> createWithText(content: String, styles: List<CharacterStyle>): StyleNode<RC, CharacterStyle> {
val styleNode = StyleNode<RC, CharacterStyle>(styles)
@TestOnly
fun <RC, T> wrapText(content: String, styles: List<T>): StyleNode<RC, T> {
val styleNode = StyleNode<RC, T>(styles)
styleNode.addChild(TextNode(content))
return styleNode
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ open class Parser<R, T : Node<R>, S> @JvmOverloads constructor(private val enabl

private val rules = ArrayList<Rule<R, out T, S>>()

@Suppress("unused")
fun addRule(rule: Rule<R, out T, S>) =
this.apply { rules.add(rule) }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,7 @@ object SimpleRenderer {

@JvmStatic
fun <R, S> render(source: CharSequence, rules: Collection<Rule<R, Node<R>, S>>, initialState: S, renderContext: R): SpannableStringBuilder {
val parser = Parser<R, Node<R>, S>()
for (rule in rules) {
parser.addRule(rule)
}

val parser = Parser<R, Node<R>, S>().addRules(rules)
return render(SpannableStringBuilder(), parser.parse(source, initialState), renderContext)
}

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package com.discord.simpleast.core

import android.graphics.Typeface
import android.text.style.CharacterStyle
import android.text.style.StyleSpan
import com.discord.simpleast.core.node.Node
import com.discord.simpleast.core.node.StyleNode
import com.discord.simpleast.core.node.TextNode
import com.discord.simpleast.core.parser.Parser
import com.discord.simpleast.core.simple.SimpleMarkdownRules
import com.discord.simpleast.core.utils.TreeMatcher
import org.junit.Assert
import org.junit.Before
import org.junit.Test


class ParserTest {
private lateinit var parser: Parser<Any, Node<Any>, Any?>
private lateinit var treeMatcher: TreeMatcher

@Before
fun setup() {
parser = Parser<Any, Node<Any>, Any?>()
.addRules(SimpleMarkdownRules.createSimpleMarkdownRules())
treeMatcher = TreeMatcher()
treeMatcher.registerDefaultMatchers()
}

@Test(expected = Parser.ParseException::class)
fun testNoRuleMatch() {
val badParser = Parser<Any, Node<Any>, Any?>()
.addRules(SimpleMarkdownRules.createSimpleMarkdownRules(includeTextRule = false))
badParser.parse("unmatched text", null)
}

@Test
fun testEmptyParse() {
val ast = parser.parse("", null)
Assert.assertTrue(ast.isEmpty())
}

@Test
fun testParseFormattedText() {
val ast = parser.parse("**bold**", null)
val model = listOf<Node<Any>>(
StyleNode.wrapText("bold", listOf(StyleSpan(Typeface.BOLD) as CharacterStyle)),
)
Assert.assertTrue(treeMatcher.matches(model, ast))
}

@Test
fun testParseLeadingFormatting() {
val ast = parser.parse("**bold** and not bold", null)
val model = listOf<Node<Any>>(
StyleNode.wrapText("bold", listOf(StyleSpan(Typeface.BOLD) as CharacterStyle)),
TextNode(" and not bold")
)
Assert.assertTrue(treeMatcher.matches(model, ast))
}

@Test
fun testParseTrailingFormatting() {
val ast = parser.parse("not bold **and bold**", null)
val model = listOf<Node<Any>>(
TextNode("not bold "),
StyleNode.wrapText("and bold", listOf(StyleSpan(Typeface.BOLD) as CharacterStyle))
)
Assert.assertTrue(treeMatcher.matches(model, ast))
}

@Test
fun testNestedFormatting() {
val ast = parser.parse("**bold *and italics* and more bold**", null)

val boldNode: StyleNode<Any, *> = StyleNode(listOf(StyleSpan(Typeface.BOLD) as CharacterStyle))
boldNode.addChild(TextNode("bold "))
boldNode.addChild(StyleNode.wrapText("and italics", listOf(StyleSpan(Typeface.ITALIC) as CharacterStyle)))
boldNode.addChild(TextNode(" and more bold"))

Assert.assertTrue(treeMatcher.matches(listOf(boldNode), ast))
}

@Test
fun testNewlineRule() {
val ast = parser.parse("Some text\n\n\n \n\n\nnewline above", null)
val model: List<Node<*>?> = listOf(
TextNode("Some text"),
TextNode("\n"),
TextNode("\n"),
TextNode<Any>("newline above"))
Assert.assertTrue("actual $ast", treeMatcher.matches(model, ast))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -136,11 +136,11 @@ class MarkdownRulesTest {
val expected = listOf<Node<Any>>(
TextNode("Title"),
TextNode("\n"),
StyleNode.createWithText(
StyleNode.wrapText(
"Header 1",
listOf(StyleSpan(Typeface.BOLD))),
TextNode("\n"),
StyleNode.createWithText(
StyleNode.wrapText(
"Header 2",
listOf(StyleSpan(Typeface.BOLD)))
)
Expand Down Expand Up @@ -206,7 +206,7 @@ class MarkdownRulesTest {
TextNode("Some introduction text"),
TextNode("."),
TextNode("\n"),
StyleNode.createWithText(
StyleNode.wrapText(
"Alt Header",
listOf(StyleSpan(Typeface.BOLD))),
TextNode("\nsome content")
Expand Down

0 comments on commit 177dd2e

Please sign in to comment.