Skip to content

Commit 2fec157

Browse files
committed
feat: add tests
1 parent 934c02c commit 2fec157

2 files changed

Lines changed: 512 additions & 0 deletions

File tree

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
package com.github.xepozz.crontab.language.parser
2+
3+
import com.github.xepozz.crontab.language.psi.CrontabTypes
4+
import com.intellij.lexer.Lexer
5+
import com.intellij.psi.tree.IElementType
6+
import com.intellij.testFramework.LexerTestCase
7+
8+
class CrontabLexerTest : LexerTestCase() {
9+
override fun createLexer(): Lexer = CrontabLexerAdapter()
10+
11+
override fun getDirPath(): String = ""
12+
13+
private fun collectTokens(text: String): List<Pair<IElementType, String>> {
14+
val lexer = createLexer()
15+
lexer.start(text)
16+
val tokens = mutableListOf<Pair<IElementType, String>>()
17+
while (lexer.tokenType != null) {
18+
tokens.add(lexer.tokenType!! to text.substring(lexer.tokenStart, lexer.tokenEnd))
19+
lexer.advance()
20+
}
21+
return tokens
22+
}
23+
24+
private fun contentTokens(text: String) = collectTokens(text).filter { it.first == CrontabTypes.CONTENT }
25+
26+
private fun eolTokens(text: String) = collectTokens(text).filter { it.first == CrontabTypes.EOL }
27+
28+
fun testSimpleCommand() {
29+
val tokens = contentTokens("* * * * * echo hello")
30+
assertEquals(1, tokens.size)
31+
assertEquals("echo hello", tokens[0].second)
32+
}
33+
34+
fun testBackslashContinuation() {
35+
val tokens = contentTokens(
36+
"""
37+
* * * * * echo hello \
38+
world
39+
""".trimIndent()
40+
)
41+
assertEquals(1, tokens.size)
42+
assertEquals("echo hello \\\n world", tokens[0].second)
43+
}
44+
45+
fun testMultipleBackslashContinuations() {
46+
val tokens = contentTokens(
47+
"""
48+
* * * * * echo \
49+
hello \
50+
world
51+
""".trimIndent()
52+
)
53+
assertEquals(1, tokens.size)
54+
assertEquals("echo \\\n hello \\\n world", tokens[0].second)
55+
}
56+
57+
fun testBackslashContinuationFollowedByNextLine() {
58+
val text = """
59+
* * * * * echo \
60+
hello
61+
* * * * * second
62+
""".trimIndent()
63+
val content = contentTokens(text)
64+
assertEquals(2, content.size)
65+
assertEquals("echo \\\n hello", content[0].second)
66+
assertEquals("second", content[1].second)
67+
assertEquals(1, eolTokens(text).size)
68+
}
69+
70+
fun testNoBackslashContinuation() {
71+
val text = """
72+
* * * * * echo hello
73+
* * * * * world
74+
""".trimIndent()
75+
val content = contentTokens(text)
76+
assertEquals(2, content.size)
77+
assertEquals("echo hello", content[0].second)
78+
assertEquals("world", content[1].second)
79+
assertEquals(1, eolTokens(text).size)
80+
}
81+
82+
fun testBackslashInMiddleOfCommand() {
83+
val tokens = contentTokens("""* * * * * echo he\llo""")
84+
assertEquals(1, tokens.size)
85+
assertEquals("echo he\\llo", tokens[0].second)
86+
}
87+
88+
fun testCommandEndingWithBackslashAtEof() {
89+
val tokens = contentTokens("""* * * * * echo hello\""")
90+
assertEquals(1, tokens.size)
91+
assertEquals("echo hello\\", tokens[0].second)
92+
}
93+
94+
fun testThreeSimpleCommands() {
95+
val text = """
96+
* * * * * first
97+
0 12 * * * second
98+
30 6 1 * * third
99+
""".trimIndent()
100+
val content = contentTokens(text)
101+
assertEquals(3, content.size)
102+
assertEquals("first", content[0].second)
103+
assertEquals("second", content[1].second)
104+
assertEquals("third", content[2].second)
105+
assertEquals(2, eolTokens(text).size)
106+
}
107+
108+
fun testContinuationBetweenSimpleCommands() {
109+
val text = """
110+
* * * * * first
111+
0 12 * * * echo \
112+
hello
113+
30 6 1 * * third
114+
""".trimIndent()
115+
val content = contentTokens(text)
116+
assertEquals(3, content.size)
117+
assertEquals("first", content[0].second)
118+
assertEquals("echo \\\n hello", content[1].second)
119+
assertEquals("third", content[2].second)
120+
assertEquals(2, eolTokens(text).size)
121+
}
122+
123+
fun testMultipleContinuationCommands() {
124+
val text = """
125+
* * * * * a \
126+
b
127+
0 12 * * * c \
128+
d \
129+
e
130+
""".trimIndent()
131+
val content = contentTokens(text)
132+
assertEquals(2, content.size)
133+
assertEquals("a \\\n b", content[0].second)
134+
assertEquals("c \\\n d \\\n e", content[1].second)
135+
assertEquals(1, eolTokens(text).size)
136+
}
137+
138+
fun testCommandsWithCommentsAndVariables() {
139+
val text = """
140+
# comment
141+
PATH=/usr/bin
142+
* * * * * run \
143+
cmd
144+
0 12 * * * other
145+
""".trimIndent()
146+
val content = contentTokens(text)
147+
assertEquals(3, content.size)
148+
assertEquals("/usr/bin", content[0].second)
149+
assertEquals("run \\\n cmd", content[1].second)
150+
assertEquals("other", content[2].second)
151+
152+
val comments = collectTokens(text).filter { it.first == CrontabTypes.SINGLE_COMMENT }
153+
assertEquals(1, comments.size)
154+
}
155+
156+
fun testCommandsWithShortcutSchedule() {
157+
val text = """
158+
@reboot startup
159+
* * * * * every_minute \
160+
continued
161+
@daily cleanup
162+
""".trimIndent()
163+
val commandContents = contentTokens(text).map { it.second }
164+
assertTrue("startup" in commandContents)
165+
assertTrue("every_minute \\\n continued" in commandContents)
166+
assertTrue("cleanup" in commandContents)
167+
}
168+
}

0 commit comments

Comments
 (0)