Skip to content

Commit

Permalink
update ParserTest to kotlin
Browse files Browse the repository at this point in the history
  • Loading branch information
lytefast committed Oct 24, 2020
1 parent 7c0412c commit 76f5ff9
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 109 deletions.

This file was deleted.

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

import android.graphics.Typeface
import android.text.style.CharacterStyle
import android.text.style.StyleSpan
import com.discord.simpleast.code.TestRenderContext
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
import java.util.*


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

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

@Test(expected = Parser.ParseException::class)
fun testNoRuleMatch() {
val badParser = Parser<TestRenderContext, Node<TestRenderContext>, 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<TestRenderContext>>(
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<TestRenderContext>>(
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<TestRenderContext>>(
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<*>?> = Arrays.asList(
TextNode("Some text"),
TextNode("\n"),
TextNode("\n"),
TextNode<Any>("newline above"))
Assert.assertTrue("actual $ast", treeMatcher.matches(model, ast))
}
}

0 comments on commit 76f5ff9

Please sign in to comment.