-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
ProcessorTest.kt
599 lines (508 loc) · 23.9 KB
/
ProcessorTest.kt
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
@file:Suppress("TestFunctionName")
package composeuiviewcontroller
import com.github.guilhe.kmp.composeuiviewcontroller.common.FILE_NAME_ARGS
import com.github.guilhe.kmp.composeuiviewcontroller.common.TEMP_FILES_FOLDER
import com.github.guilhe.kmp.composeuiviewcontroller.ksp.EmptyFrameworkBaseNameException
import com.github.guilhe.kmp.composeuiviewcontroller.ksp.InvalidParametersException
import com.github.guilhe.kmp.composeuiviewcontroller.ksp.ProcessorProvider
import com.github.guilhe.kmp.composeuiviewcontroller.ksp.composeUIViewControllerAnnotationName
import com.github.guilhe.kmp.composeuiviewcontroller.ksp.composeUIViewControllerStateAnnotationName
import com.tschuchort.compiletesting.KotlinCompilation
import com.tschuchort.compiletesting.SourceFile
import com.tschuchort.compiletesting.SourceFile.Companion.kotlin
import com.tschuchort.compiletesting.kspIncremental
import com.tschuchort.compiletesting.kspSourcesDir
import com.tschuchort.compiletesting.symbolProcessorProviders
import org.jetbrains.kotlin.compiler.plugin.ExperimentalCompilerApi
import org.junit.After
import org.junit.Assert.assertEquals
import org.junit.Assert.assertTrue
import org.junit.Rule
import org.junit.Test
import org.junit.rules.TemporaryFolder
import java.io.File
import kotlin.test.assertContains
@OptIn(ExperimentalCompilerApi::class)
class ProcessorTest {
@get:Rule
val tempFolder = TemporaryFolder(File("build/tmp/test-module/src").also { it.mkdirs() })
private lateinit var tempArgs: File
private fun prepareCompilation(vararg sourceFiles: SourceFile): KotlinCompilation {
val build = File(tempFolder.root.parentFile.parentFile.parentFile.parentFile.path) //we need to reach module's ./build
File(build, TEMP_FILES_FOLDER).apply { mkdirs() }
tempArgs = File(build, "$TEMP_FILES_FOLDER/$FILE_NAME_ARGS").apply { writeText("[]") }
return KotlinCompilation().apply {
workingDir = tempFolder.root
inheritClassPath = true
symbolProcessorProviders = listOf(ProcessorProvider())
sources = sourceFiles.asList()
verbose = false
kspIncremental = false
}
}
@After
fun clean() {
tempArgs.delete()
}
@Test
fun `Not using @ComposeUIViewController will not generate any file`() {
val code = """
package com.mycomposable.test
import $composeUIViewControllerStateAnnotationName
@Composable
fun ScreenA(state: ViewState) { }
@Composable
fun ScreenB(@ComposeUIViewControllerState state: ViewState) { }
""".trimIndent()
val compilation = prepareCompilation(kotlin("Screen.kt", code))
val result = compilation.compile()
assertEquals(result.exitCode, KotlinCompilation.ExitCode.OK)
assertTrue(
compilation.kspSourcesDir
.walkTopDown()
.filter { it.extension == "kt" || it.extension == "swift" }
.toList()
.isEmpty()
)
}
@Test
fun `When frameworkBaseName is provided via ModulesJson it overrides @ComposeUIViewController frameworkBaseName value`() {
val code = """
package com.mycomposable.test
import $composeUIViewControllerAnnotationName
import $composeUIViewControllerStateAnnotationName
data class ViewState(val field: Int)
@ComposeUIViewController("ComposablesFramework")
@Composable
fun Screen(@ComposeUIViewControllerState state: ViewState) { }
""".trimIndent()
val compilation = prepareCompilation(kotlin("Screen.kt", code))
tempArgs.writeText("""[{"name":"module-test","packageNames":["com.mycomposable.test"],"frameworkBaseName":"MyFramework","experimentalNamespaceFeature":false}]""")
val result = compilation.compile()
assertEquals(result.exitCode, KotlinCompilation.ExitCode.OK)
val generatedSwiftFiles = compilation.kspSourcesDir
.walkTopDown()
.filter { it.name == "ScreenUIViewControllerRepresentable.swift" }
assertContains(generatedSwiftFiles.first().readText(), "import MyFramework")
}
@Test
fun `Empty frameworkBaseName in ModulesJson falls back to frameworkBaseName in @ComposeUIViewController`() {
val code = """
package com.mycomposable.test
import $composeUIViewControllerAnnotationName
import $composeUIViewControllerStateAnnotationName
data class ViewState(val field: Int)
@ComposeUIViewController("ComposablesFramework")
@Composable
fun Screen(@ComposeUIViewControllerState state: ViewState) { }
""".trimIndent()
val compilation = prepareCompilation(kotlin("Screen.kt", code))
tempArgs.writeText("""[{"name":"module-test","packageNames":["com.mycomposable.test"],"frameworkBaseName":"","experimentalNamespaceFeature":false}]""")
val result = compilation.compile()
assertEquals(result.exitCode, KotlinCompilation.ExitCode.OK)
val generatedSwiftFiles = compilation.kspSourcesDir
.walkTopDown()
.filter { it.name == "ScreenUIViewControllerRepresentable.swift" }
assertContains(generatedSwiftFiles.first().readText(), "import ComposablesFramework")
}
@Test
fun `Empty frameworkBaseName in ModulesJson and @ComposeUIViewController throws EmptyFrameworkBaseNameException`() {
val code = """
package com.mycomposable.test
import $composeUIViewControllerAnnotationName
import $composeUIViewControllerStateAnnotationName
data class ViewState(val field: Int)
@ComposeUIViewController
@Composable
fun Screen(@ComposeUIViewControllerState state: ViewState) { }
""".trimIndent()
val compilation = prepareCompilation(kotlin("Screen.kt", code))
val result = compilation.compile()
assertEquals(result.exitCode, KotlinCompilation.ExitCode.COMPILATION_ERROR)
assertContains(result.messages, EmptyFrameworkBaseNameException().message!!)
}
@Test
fun `Not using @ComposeUIViewControllerState will generate files without state`() {
val code = """
package com.mycomposable.test
import $composeUIViewControllerAnnotationName
data class SomeClass(val field: Int)
@ComposeUIViewController("MyFramework")
@Composable
fun Screen(data: SomeClass, value: Int, callBack: () -> Unit) { }
""".trimIndent()
val compilation = prepareCompilation(kotlin("Screen.kt", code))
val result = compilation.compile()
assertEquals(result.exitCode, KotlinCompilation.ExitCode.OK)
val expectedKotlinOutput = """
@file:Suppress("unused")
package com.mycomposable.test
import androidx.compose.ui.window.ComposeUIViewController
import platform.UIKit.UIViewController
object ScreenUIViewController {
fun make(data: SomeClass, value: Int, callBack: () -> Unit): UIViewController {
return ComposeUIViewController {
Screen(data, value, callBack)
}
}
}
""".trimIndent()
val generatedKotlinFiles = compilation.kspSourcesDir
.walkTopDown()
.filter { it.name == "ScreenUIViewController.kt" }
.toList()
assertTrue(generatedKotlinFiles.isNotEmpty())
val generatedKotlinFile = generatedKotlinFiles.first().readText()
assertEquals(generatedKotlinFile, expectedKotlinOutput)
val expectedSwiftOutput = """
import SwiftUI
import MyFramework
public struct ScreenRepresentable: UIViewControllerRepresentable {
let data: SomeClass
let value: KotlinInt
let callBack: () -> Void
public func makeUIViewController(context: Context) -> UIViewController {
ScreenUIViewController().make(data: data, value: value, callBack: callBack)
}
public func updateUIViewController(_ uiViewController: UIViewController, context: Context) {
//unused
}
}
""".trimIndent()
val generatedSwiftFiles = compilation.kspSourcesDir
.walkTopDown()
.filter { it.name == "ScreenUIViewControllerRepresentable.swift" }
.toList()
assertTrue(generatedSwiftFiles.isNotEmpty())
val generatedSwiftFile = generatedSwiftFiles.first().readText()
assertEquals(generatedSwiftFile, expectedSwiftOutput)
}
@Test
fun `Only 1 @ComposeUIViewControllerState is allowed`() {
val code = """
package com.mycomposable.test
import $composeUIViewControllerAnnotationName
import $composeUIViewControllerStateAnnotationName
@ComposeUIViewController
@Composable
fun Screen(@ComposeUIViewControllerState state: ViewState, @ComposeUIViewControllerState state2: ViewState) { }
""".trimIndent()
val compilation = prepareCompilation(kotlin("Screen.kt", code))
val result = compilation.compile()
assertEquals(result.exitCode, KotlinCompilation.ExitCode.COMPILATION_ERROR)
}
@Test
fun `Only 1 @ComposeUIViewControllerState and N function parameters (excluding @Composable) are allowed`() {
val code = """
package com.mycomposable.test
import $composeUIViewControllerAnnotationName
import $composeUIViewControllerStateAnnotationName
@ComposeUIViewController
@Composable
fun Screen(
@ComposeUIViewControllerState state: ViewState,
callBackA: () -> Unit,
callBackB: (Int) -> Unit,
@Composable content: () -> Unit
) { }
""".trimIndent()
val compilation = prepareCompilation(kotlin("Screen.kt", code))
val result = compilation.compile()
assertEquals(result.exitCode, KotlinCompilation.ExitCode.COMPILATION_ERROR)
assertContains(result.messages, InvalidParametersException().message!!)
}
@Test
fun `Composable functions properly using @ComposeUIViewController and @ComposeUIViewControllerState will generate respective UIViewController and UIViewControllerRepresentable files`() {
val code = """
package com.mycomposable.test
import $composeUIViewControllerAnnotationName
import $composeUIViewControllerStateAnnotationName
private data class ViewAState(val field: Int)
private data class ViewBState(val field: Int)
@ComposeUIViewController("MyFramework")
@Composable
fun ScreenA(@ComposeUIViewControllerState state: ViewAState) { }
private fun dummy()
@ComposeUIViewController("MyFramework")
@Composable
fun ScreenB(@ComposeUIViewControllerState state: ViewBState) { }
""".trimIndent()
val compilation = prepareCompilation(kotlin("Screen.kt", code))
val result = compilation.compile()
assertEquals(result.exitCode, KotlinCompilation.ExitCode.OK)
assertEquals(
compilation.kspSourcesDir
.walkTopDown()
.filter {
it.name == "ScreenAUIViewController.kt" || it.name == "ScreenAUIViewControllerRepresentable.swift" ||
it.name == "ScreenBUIViewController.kt" || it.name == "ScreenBUIViewControllerRepresentable.swift"
}
.toList()
.size,
4
)
val expectedKotlinOutput = """
@file:Suppress("unused")
package com.mycomposable.test
import androidx.compose.runtime.mutableStateOf
import androidx.compose.ui.window.ComposeUIViewController
import platform.UIKit.UIViewController
object ScreenAUIViewController {
private val state = mutableStateOf<ViewAState?>(null)
fun make(): UIViewController {
return ComposeUIViewController {
state.value?.let { ScreenA(it) }
}
}
fun update(state: ViewAState) {
this.state.value = state
}
}
""".trimIndent()
val generatedKotlinFiles = compilation.kspSourcesDir
.walkTopDown()
.filter { it.name == "ScreenAUIViewController.kt" }
val generatedKotlinFile = generatedKotlinFiles.first().readText()
assertEquals(generatedKotlinFile, expectedKotlinOutput)
val expectedSwiftOutput = """
import SwiftUI
import MyFramework
public struct ScreenARepresentable: UIViewControllerRepresentable {
@Binding var state: ViewAState
public func makeUIViewController(context: Context) -> UIViewController {
ScreenAUIViewController().make()
}
public func updateUIViewController(_ uiViewController: UIViewController, context: Context) {
ScreenAUIViewController().update(state: state)
}
}
""".trimIndent()
val generatedSwiftFiles = compilation.kspSourcesDir
.walkTopDown()
.filter { it.name == "ScreenAUIViewControllerRepresentable.swift" }
val generatedSwiftFile = generatedSwiftFiles.first().readText()
assertEquals(generatedSwiftFile, expectedSwiftOutput)
}
@Test
fun `Composable functions from different files are parsed once only once`() {
val data = """
package com.mycomposable.data
data class ViewState(val field: Int)
""".trimIndent()
val codeA = """
package com.mycomposable.test
import $composeUIViewControllerAnnotationName
import $composeUIViewControllerStateAnnotationName
import com.mycomposable.data.*
@ComposeUIViewController("MyFramework")
@Composable
fun ScreenA(@ComposeUIViewControllerState state: ViewState) { }
""".trimIndent()
val codeB = """
package com.mycomposable.test
import $composeUIViewControllerAnnotationName
import $composeUIViewControllerStateAnnotationName
import com.mycomposable.data.*
@ComposeUIViewController("MyFramework")
@Composable
fun ScreenB(@ComposeUIViewControllerState state: ViewState) { }
""".trimIndent()
val compilation = prepareCompilation(kotlin("ScreenA.kt", codeA), kotlin("ScreenB.kt", codeB), kotlin("Data.kt", data))
val result = compilation.compile()
assertEquals(result.exitCode, KotlinCompilation.ExitCode.OK)
assertEquals(
compilation.kspSourcesDir.walkTopDown()
.filter {
it.name == "ScreenAUIViewController.kt" || it.name == "ScreenAUIViewControllerRepresentable.swift" ||
it.name == "ScreenBUIViewController.kt" || it.name == "ScreenBUIViewControllerRepresentable.swift"
}
.toList()
.size,
4
)
}
@Test
fun `Composable functions in the same file are parsed once only once`() {
val code = """
package com.mycomposable.test
import $composeUIViewControllerAnnotationName
import $composeUIViewControllerStateAnnotationName
private data class ViewState(val field: Int)
@ComposeUIViewController("MyFramework")
@Composable
fun ScreenA(@ComposeUIViewControllerState state: ViewState) { }
@ComposeUIViewController("MyFramework")
@Composable
fun ScreenB(@ComposeUIViewControllerState state: ViewState, callBackA: () -> Unit) { }
@ComposeUIViewController("MyFramework")
@Composable
fun ScreenC(@ComposeUIViewControllerState state: ViewState, callBackA: () -> Unit, callBackB: () -> Unit) { }
""".trimIndent()
val compilation = prepareCompilation(kotlin("Screen.kt", code, isMultiplatformCommonSource = true))
val result = compilation.compile()
assertEquals(result.exitCode, KotlinCompilation.ExitCode.OK)
assertEquals(
compilation.kspSourcesDir.walkTopDown()
.filter {
it.name == "ScreenAUIViewController.kt" || it.name == "ScreenAUIViewControllerRepresentable.swift" ||
it.name == "ScreenBUIViewController.kt" || it.name == "ScreenBUIViewControllerRepresentable.swift" ||
it.name == "ScreenCUIViewController.kt" || it.name == "ScreenCUIViewControllerRepresentable.swift"
}
.toList()
.size,
6
)
}
@Test
fun `Function parameter with Kotlin primitive type will map to Swift expected type`() {
val code = """
package com.mycomposable.test
import $composeUIViewControllerAnnotationName
import $composeUIViewControllerStateAnnotationName
import com.mycomposable.data.ViewState
private data class ViewState(val field: Int)
@ComposeUIViewController("MyFramework")
@Composable
fun Screen(
@ComposeUIViewControllerState state: ViewState,
callBackA: () -> Unit,
callBackB: (List) -> Unit,
callBackC: (MutableList) -> Unit,
callBackD: (Map) -> Unit,
callBackE: (MutableMap) -> Unit,
callBackF: (Byte) -> Unit,
callBackG: (UByte) -> Unit,
callBackH: (Short) -> Unit,
callBackI: (UShort) -> Unit,
callBackJ: (Int) -> Unit,
callBackK: (UInt) -> Unit,
callBackL: (Long) -> Unit,
callBackM: (ULong) -> Unit,
callBackN: (Float) -> Unit,
callBackO: (Double) -> Unit,
callBackP: (Boolean) -> Unit
) { }
""".trimIndent()
val compilation = prepareCompilation(kotlin("Screen.kt", code))
val result = compilation.compile()
val generatedSwiftFiles = compilation.kspSourcesDir.walkTopDown()
.filter { it.name == "ScreenUIViewControllerRepresentable.swift" }
.toList()
assertTrue(generatedSwiftFiles.isNotEmpty())
assertEquals(result.exitCode, KotlinCompilation.ExitCode.OK)
val expectedSwiftTypes = listOf(
"Void",
"Array",
"NSMutableArray",
"Dictionary",
"NSMutableDictionary",
"KotlinByte",
"KotlinUByte",
"KotlinShort",
"KotlinUShort",
"KotlinInt",
"KotlinUInt",
"KotlinLong",
"KotlinULong",
"KotlinFloat",
"KotlinDouble",
"KotlinBoolean"
)
val generatedCodeFile = generatedSwiftFiles.first().readText()
for (expectedType in expectedSwiftTypes) {
assertTrue(generatedCodeFile.contains(expectedType))
}
}
@Test
fun `Types imported from different KMP modules will not produce Swift files by default`() {
val data = """
package com.mycomposable.data
data class Data(val field: Int)
""".trimIndent()
val code = """
package com.mycomposable.test
import $composeUIViewControllerAnnotationName
import $composeUIViewControllerStateAnnotationName
import com.mycomposable.data.Data
@ComposeUIViewController("MyFramework")
@Composable
fun Screen(data: Data) { }
""".trimIndent()
val compilation = prepareCompilation(kotlin("Screen.kt", code), kotlin("Data.kt", data))
tempArgs.writeText(
"""
[
{"name":"module-test","packageNames":["com.mycomposable.test"],"frameworkBaseName":"MyFramework","experimentalNamespaceFeature":false},
{"name":"module-data","packageNames":["com.mycomposable.data"],"frameworkBaseName":"MyFramework2","experimentalNamespaceFeature":false}
]
""".trimIndent()
)
val result = compilation.compile()
assertEquals(result.exitCode, KotlinCompilation.ExitCode.OK)
val generatedSwiftFiles = compilation.kspSourcesDir
.walkTopDown()
.filter { it.name == "ScreenUIViewControllerRepresentable.swift" }
.toList()
assertTrue(generatedSwiftFiles.isNotEmpty())
val expectedSwiftOutput = """
import SwiftUI
import MyFramework
public struct ScreenRepresentable: UIViewControllerRepresentable {
let data: Data
public func makeUIViewController(context: Context) -> UIViewController {
ScreenUIViewController().make(data: data)
}
public func updateUIViewController(_ uiViewController: UIViewController, context: Context) {
//unused
}
}
""".trimIndent()
assertEquals(generatedSwiftFiles.first().readText(), expectedSwiftOutput)
}
@Test
fun `Types imported from different KMP modules will produce Swift files with composed types when experimentalNamespaceFeature flag is true`() {
val data = """
package com.mycomposable.data
data class Data(val field: Int)
""".trimIndent()
val code = """
package com.mycomposable.test
import $composeUIViewControllerAnnotationName
import $composeUIViewControllerStateAnnotationName
import com.mycomposable.data.Data
@ComposeUIViewController("MyFramework")
@Composable
fun Screen(data: Data) { }
""".trimIndent()
val compilation = prepareCompilation(kotlin("Screen.kt", code), kotlin("Data.kt", data))
tempArgs.writeText(
"""
[
{"name":"module-test","packageNames":["com.mycomposable.test"],"frameworkBaseName":"MyFramework","experimentalNamespaceFeature":true},
{"name":"module-data","packageNames":["com.mycomposable.data"],"frameworkBaseName":"MyFramework2","experimentalNamespaceFeature":true}
]
""".trimIndent()
)
val result = compilation.compile()
assertEquals(result.exitCode, KotlinCompilation.ExitCode.OK)
val generatedSwiftFiles = compilation.kspSourcesDir
.walkTopDown()
.filter { it.name == "ScreenUIViewControllerRepresentable.swift" }
.toList()
assertTrue(generatedSwiftFiles.isNotEmpty())
val expectedSwiftOutput = """
import SwiftUI
import MyFramework
public struct ScreenRepresentable: UIViewControllerRepresentable {
let data: Module_dataData
public func makeUIViewController(context: Context) -> UIViewController {
ScreenUIViewController().make(data: data)
}
public func updateUIViewController(_ uiViewController: UIViewController, context: Context) {
//unused
}
}
""".trimIndent()
assertEquals(generatedSwiftFiles.first().readText(), expectedSwiftOutput)
}
}