-
-
Notifications
You must be signed in to change notification settings - Fork 155
Expand file tree
/
Copy pathCommonTests.kt
More file actions
132 lines (116 loc) · 4.91 KB
/
Copy pathCommonTests.kt
File metadata and controls
132 lines (116 loc) · 4.91 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import com.linuxcommandlibrary.shared.CommandElement
import com.linuxcommandlibrary.shared.InstallEntries
import com.linuxcommandlibrary.shared.MarkdownParser
import com.linuxcommandlibrary.shared.TipSectionElement
import com.linuxcommandlibrary.shared.getCommandList
import com.linuxcommandlibrary.shared.getSectionSortPriority
import com.linuxcommandlibrary.shared.toHtmlFileName
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertNull
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)
}
@Test
fun testInstallLineParsesManagerCommandAndPackageUrl() {
val entry = InstallEntries.parseCodeLine("apt: sudo apt install 7zip")!!
assertEquals("apt", entry.manager)
assertEquals("sudo apt install 7zip", entry.command)
assertEquals("7zip", entry.packageName)
assertEquals(
"https://packages.debian.org/search?keywords=7zip",
entry.packageUrl,
)
}
@Test
fun testInstallNixPackageNameAfterHash() {
val entry = InstallEntries.parseCodeLine(
"nix: nix profile install nixpkgs#ripgrep",
)!!
assertEquals("nix", entry.manager)
assertEquals("ripgrep", entry.packageName)
assertEquals(
"https://search.nixos.org/packages?query=ripgrep",
entry.packageUrl,
)
}
@Test
fun testInstallIgnoresUnknownManagerAndGarbage() {
assertNull(InstallEntries.parseCodeLine("foo: bar install baz"))
assertNull(InstallEntries.parseCodeLine("just a plain command"))
assertNull(InstallEntries.parseCodeLine("apt:"))
}
@Test
fun testInstallSectionParsesMultipleManagers() {
val content = """
```apt: sudo apt install 7zip```
```brew: brew install 7zip```
<!-- packages: 2026-07-22 -->
""".trimIndent()
val entries = InstallEntries.parseFromElements(MarkdownParser.parseMarkdownContent(content))
assertEquals(listOf("apt", "brew"), entries.map { it.manager })
assertEquals("https://formulae.brew.sh/formula/7zip", entries.last().packageUrl)
}
@Test
fun testInstallSortsBeforeSeeAlso() {
assertTrue(getSectionSortPriority("INSTALL") < getSectionSortPriority("SEE ALSO"))
assertTrue(getSectionSortPriority("INSTALL") < getSectionSortPriority("RESOURCES"))
assertTrue(getSectionSortPriority("INSTALL") < getSectionSortPriority("HISTORY"))
}
}