-
-
Notifications
You must be signed in to change notification settings - Fork 145
Expand file tree
/
Copy pathCommonTests.kt
More file actions
76 lines (67 loc) · 2.85 KB
/
Copy pathCommonTests.kt
File metadata and controls
76 lines (67 loc) · 2.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import com.linuxcommandlibrary.shared.CommandElement
import com.linuxcommandlibrary.shared.MarkdownParser
import com.linuxcommandlibrary.shared.TipSectionElement
import com.linuxcommandlibrary.shared.getCommandList
import com.linuxcommandlibrary.shared.toHtmlFileName
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertTrue
class CommonTests {
@Test
fun testCommandListElements() {
val command = "ps ax | grep firefox"
val elements = command.getCommandList("ps,grep")
assertEquals(elements.count { it is CommandElement.Man }, 2)
}
@Test
fun testHtmlFileName() {
assertEquals("usersgroups".toHtmlFileName(), "usersgroups")
assertEquals("Users & Groups 2".toHtmlFileName(), "usersgroups")
assertEquals("Files & Folders".toHtmlFileName(), "filesfolders")
}
@Test
fun testCommandListWithUrls() {
val command = "install nvm"
val elements = command.getCommandList("url:nvm|https://github.com/nvm-sh/nvm")
assertTrue(elements.any { it is CommandElement.Url })
}
@Test
fun testCommandListEmptyMans() {
val command = "echo hello"
val elements = command.getCommandList("")
assertTrue(elements.all { it is CommandElement.Text })
}
@Test
fun testParseExternalUrlInCodeBlock() {
val elements = MarkdownParser.parseCodeToElements("Source [github.com/x/y](https://github.com/x/y)")
val url = elements.filterIsInstance<CommandElement.Url>().single()
assertEquals("github.com/x/y", url.command)
assertEquals("https://github.com/x/y", url.url)
}
@Test
fun testParseManLinkStillWorks() {
val elements = MarkdownParser.parseCodeToElements("see [tar](/man/tar)")
assertEquals("tar", elements.filterIsInstance<CommandElement.Man>().single().man)
}
@Test
fun testHtmlCommentIsHidden() {
val sections = MarkdownParser.parseMarkdownContent(
"```[Homepage](https://example.org)```\n<!-- verified: 2026-06-09 -->",
)
assertTrue(sections.none { it is TipSectionElement.Text })
assertTrue(sections.any { it is TipSectionElement.Code })
}
@Test
fun testResourcesSectionParsesToLabeledChips() {
// Mirrors the extraction the command detail view model does for RESOURCES chips.
val content = "```[Source code](https://github.com/x/y)```\n\n" +
"```[Documentation](https://x.dev/docs)```\n\n" +
"<!-- verified: 2026-06-09 -->"
val urls = MarkdownParser.parseMarkdownContent(content)
.filterIsInstance<TipSectionElement.Code>()
.flatMap { it.elements }
.filterIsInstance<CommandElement.Url>()
assertEquals(listOf("Source code", "Documentation"), urls.map { it.command })
assertEquals("https://github.com/x/y", urls.first().url)
}
}