test: 異種リストのテストを追加

This commit is contained in:
usbharu 2024-11-17 19:07:06 +09:00
parent 9605c7d7aa
commit ac6c4f132b
Signed by: usbharu
GPG Key ID: 95CBCF7046307B77
3 changed files with 72 additions and 3 deletions

View File

@ -86,16 +86,28 @@ sealed class AstNode {
fun nestedPrint(nest: Int): String { fun nestedPrint(nest: Int): String {
val builder = StringBuilder() val builder = StringBuilder()
for (node in itemNode) { for (node in itemNode) {
builder.append(" ".repeat(nest)).append("- ").append(node.nestedPrint(nest)).append("\n") builder.append(" ".repeat(nest)).append(char).append(" ").append(node.nestedPrint(nest)).append("\n")
} }
return builder.toString().trim('\n') return builder.toString().trim('\n')
} }
protected abstract val char: String
}
data class DecimalListNode(override val itemNode: List<ListItemNode>) : ListNode(itemNode) {
@JsName("with")
constructor(vararg nodes: ListItemNode) : this(nodes.toList())
override val char: String
get() = "1."
} }
data class DecimalListNode(override val itemNode: List<ListItemNode>) : ListNode(itemNode)
data class DiscListNode(override val itemNode: List<ListItemNode>) : ListNode(itemNode) { data class DiscListNode(override val itemNode: List<ListItemNode>) : ListNode(itemNode) {
@JsName("with") @JsName("with")
constructor(vararg nodes: ListItemNode) : this(nodes.toList()) constructor(vararg nodes: ListItemNode) : this(nodes.toList())
override val char: String
get() = "-"
} }
data class ListItemNode(val nodes: MutableList<ListableNode>) : BlockNode() { data class ListItemNode(val nodes: MutableList<ListableNode>) : BlockNode() {

View File

@ -76,9 +76,17 @@ class Parser {
} }
break break
} }
while (iterator.peekOrNull() is Token.List) { val peekOrNull = iterator.peekOrNull()
if (peekOrNull is Token.List) {
iterator.skip() iterator.skip()
if (peekOrNull.type != list.type) {
if (item.isNotEmpty()) {
listItems.add(ListItemNode(item))
}
break
}
} }
if (item.isNotEmpty()) { if (item.isNotEmpty()) {
listItems.add(ListItemNode(item)) listItems.add(ListItemNode(item))
} }

View File

@ -621,4 +621,53 @@ class ParserTest {
), actual ), actual
) )
} }
@Test
fun 異種listネスト() {
val parser = Parser()
val actual = parser.parse(
listOf(
DiscList,
Text("aiueo"),
LineBreak(1),
Whitespace(4, ' '),
DecimalList('1'),
Text("abcd"),
LineBreak(1),
Whitespace(4, ' '),
DecimalList('1'),
Text("efgh"),
LineBreak(1),
DiscList,
Text("hoge")
)
)
println(actual)
println(actual.print())
assertEquals(
RootNode(
BodyNode(
DiscListNode(
ListItemNode(
PlainText("aiueo"),
DecimalListNode(
ListItemNode(
PlainText("abcd"),
),
ListItemNode(
PlainText("efgh"),
)
)
),
ListItemNode(
PlainText("hoge")
)
)
)
), actual
)
}
} }