1
1
package org.jetbrains.kotlin.jupyter.test
2
2
3
+ import org.jetbrains.kotlin.jupyter.ExecutedCodeLogging
4
+ import org.jetbrains.kotlin.jupyter.LibrariesProcessor
5
+ import org.jetbrains.kotlin.jupyter.LibraryDefinition
6
+ import org.jetbrains.kotlin.jupyter.MagicsProcessor
7
+ import org.jetbrains.kotlin.jupyter.OutputConfig
8
+ import org.jetbrains.kotlin.jupyter.ReplOptions
3
9
import org.jetbrains.kotlin.jupyter.parseLibraryName
10
+ import org.jetbrains.kotlin.jupyter.repl.completion.SourceCodeImpl
11
+ import org.jetbrains.kotlin.jupyter.toSourceCodePositionWithNewAbsolute
4
12
import org.junit.jupiter.api.Assertions.assertEquals
13
+ import org.junit.jupiter.api.Assertions.assertNull
14
+ import org.junit.jupiter.api.Assertions.assertTrue
5
15
import org.junit.jupiter.api.Test
6
16
7
17
class ParseArgumentsTests {
@@ -32,4 +42,118 @@ class ParseArgumentsTests {
32
42
assertEquals(" arg2" , args[1 ].name)
33
43
assertEquals(" val2" , args[1 ].value)
34
44
}
35
- }
45
+ }
46
+
47
+ class ParseMagicsTests {
48
+
49
+ private class TestReplOptions : ReplOptions {
50
+ override var trackClasspath = false
51
+ override var executedCodeLogging = ExecutedCodeLogging .Off
52
+ override var writeCompiledClasses = false
53
+ override var outputConfig = OutputConfig ()
54
+ }
55
+
56
+ private val options = TestReplOptions ()
57
+
58
+ private fun test (code : String , expectedProcessedCode : String , librariesChecker : (List <LibraryDefinition >) -> Unit = {}) {
59
+ val processor = MagicsProcessor (options, LibrariesProcessor (testResolverConfig.libraries))
60
+ with (processor.processMagics(code, true )) {
61
+ assertEquals(expectedProcessedCode, this .code)
62
+ librariesChecker(libraries)
63
+ }
64
+ }
65
+
66
+ @Test
67
+ fun `single magic` () {
68
+ test(" %use krangl" , " " ) { libs ->
69
+ assertEquals(1 , libs.size)
70
+ }
71
+ }
72
+
73
+ @Test
74
+ fun `trailing newlines should be left` () {
75
+ test(" \n %use krangl\n\n " , " \n\n\n " ){ libs ->
76
+ assertEquals(1 , libs.size)
77
+ }
78
+ }
79
+
80
+ @Test
81
+ fun `multiple magics` () {
82
+ test(
83
+ """
84
+ %use lets-plot, krangl
85
+
86
+ fun f() = 42
87
+ %trackClasspath
88
+ val x = 9
89
+
90
+ """ .trimIndent(),
91
+ """
92
+
93
+
94
+ fun f() = 42
95
+
96
+ val x = 9
97
+
98
+ """ .trimIndent()
99
+ ){ libs ->
100
+ assertEquals(2 , libs.size)
101
+ }
102
+
103
+ assertTrue(options.trackClasspath)
104
+ }
105
+
106
+ @Test
107
+ fun `wrong magics should be tolerated` () {
108
+ test(
109
+ """
110
+ %use lets-plot
111
+ %use wrongLib
112
+ val x = 9
113
+ %wrongMagic
114
+ fun f() = 42
115
+ %trackExecution -generated
116
+ """ .trimIndent(),
117
+ """
118
+
119
+
120
+ val x = 9
121
+
122
+ fun f() = 42
123
+
124
+ """ .trimIndent()
125
+ ){ libs ->
126
+ assertEquals(1 , libs.size)
127
+ }
128
+
129
+ assertEquals(ExecutedCodeLogging .Generated , options.executedCodeLogging)
130
+ }
131
+
132
+ @Test
133
+ fun `source location is correctly transformed` () {
134
+ val sourceText = """
135
+ fun g() = 99
136
+ %use lets-plot
137
+ %use wrongLib
138
+ val x = 9
139
+ """ .trimIndent()
140
+
141
+ val resultText = """
142
+ fun g() = 99
143
+
144
+
145
+ val x = 9
146
+ """ .trimIndent()
147
+
148
+ test(sourceText, resultText) { libs ->
149
+ assertEquals(1 , libs.size)
150
+ }
151
+
152
+ val source = SourceCodeImpl (1 , sourceText)
153
+ val result = SourceCodeImpl (1 , resultText)
154
+ val cursor = sourceText.indexOf(" lets-plot" )
155
+
156
+ val actualPos = cursor.toSourceCodePositionWithNewAbsolute(source, result)
157
+ assertNull(actualPos)
158
+ }
159
+ }
0 commit comments