|
| 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