Skip to content

Commit a61b2df

Browse files
committed
Soften the requirements of the code to be a REPL command
1 parent 43d3d42 commit a61b2df

File tree

2 files changed

+54
-2
lines changed
  • build-plugin/common-dependencies/src/org/jetbrains/kotlinx/jupyter/common
  • src/test/kotlin/org/jetbrains/kotlinx/jupyter/test

2 files changed

+54
-2
lines changed

build-plugin/common-dependencies/src/org/jetbrains/kotlinx/jupyter/common/commandsUtil.kt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package org.jetbrains.kotlinx.jupyter.common
22

3+
private val replCommandRegex = Regex("""^([\r\n\t ]*\n)?:([A-Za-z0-9]*)[\n\t\r ]*$""")
4+
35
/**
46
* If this function returns true for the [code], it will be interpreted as Jupyter REPL command
57
*/
6-
fun looksLikeReplCommand(code: String): Boolean = code.startsWith(":")
8+
fun looksLikeReplCommand(code: String): Boolean = replCommandRegex.matches(code)
79

810
/**
911
* Throws [IllegalArgumentException] in case [looksLikeReplCommand] returns false for [code]
@@ -22,6 +24,7 @@ fun assertLooksLikeReplCommand(code: String) {
2224
*/
2325
fun replCommandOrNull(code: String): Pair<ReplCommand?, String> {
2426
assertLooksLikeReplCommand(code)
25-
val commandString = code.trim().substring(1)
27+
val match = replCommandRegex.matchEntire(code)!!
28+
val commandString = match.groupValues[2]
2629
return ReplCommand.valueOfOrNull(commandString)?.value to commandString
2730
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package org.jetbrains.kotlinx.jupyter.test
2+
3+
import org.jetbrains.kotlinx.jupyter.common.looksLikeReplCommand
4+
import org.jetbrains.kotlinx.jupyter.common.replCommandOrNull
5+
import org.junit.jupiter.api.Test
6+
import kotlin.test.assertEquals
7+
8+
class CommandsTest {
9+
private fun assertLooksLikeReplCommand(expectedValid: Boolean, code: String) {
10+
val actualValid = looksLikeReplCommand(code)
11+
val expectedValidString = if (expectedValid) "valid" else "invalid"
12+
assertEquals(expectedValid, actualValid, "Validation failed for code \"$code\", expected it to be $expectedValidString command")
13+
}
14+
15+
private fun assertIsCommand(code: String, expectedCommandValue: String) {
16+
assertLooksLikeReplCommand(true, code)
17+
val actualCommandValue = replCommandOrNull(code).second
18+
assertEquals(expectedCommandValue, actualCommandValue)
19+
}
20+
21+
private fun assertNotCommand(code: String) = assertLooksLikeReplCommand(false, code)
22+
23+
@Test
24+
fun `one-line command`() = assertIsCommand(":help", "help")
25+
26+
@Test
27+
fun `command on the third line`() = assertIsCommand("\n:vars", "vars")
28+
29+
@Test
30+
fun `command with the spaces on the first lines`() = assertIsCommand("\t\n \n:myCommand", "myCommand")
31+
32+
@Test
33+
fun `command with the spaces and newlines after it`() = assertIsCommand(":command \n \t\n", "command")
34+
35+
@Test
36+
fun `command with digits`() = assertIsCommand(":usefulCommand42", "usefulCommand42")
37+
38+
@Test
39+
fun `command is tolerant to Windows newlines`() = assertIsCommand("\r \r\n:windows ", "windows")
40+
41+
@Test
42+
fun `command should not be split from the colon`() = assertNotCommand(": help")
43+
44+
@Test
45+
fun `no valuable characters should appear in the command code`() = assertNotCommand(":my command")
46+
47+
@Test
48+
fun `colon should be the first character in line`() = assertNotCommand(" :notHelp")
49+
}

0 commit comments

Comments
 (0)