-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
95 additions
and
109 deletions.
There are no files selected for viewing
109 changes: 0 additions & 109 deletions
109
simpleast-core/src/test/java/com/discord/simpleast/core/ParserTest.java
This file was deleted.
Oops, something went wrong.
95 changes: 95 additions & 0 deletions
95
simpleast-core/src/test/java/com/discord/simpleast/core/ParserTest.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,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)) | ||
} | ||
} |