Skip to content

Commit 9ed815e

Browse files
authored
KTLN-651: Introduction to KotlinPoet (#896)
1 parent 49fb0df commit 9ed815e

File tree

5 files changed

+218
-0
lines changed

5 files changed

+218
-0
lines changed

kotlin-libraries-2/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,11 @@
132132
<artifactId>kotlinx-cli-jvm</artifactId>
133133
<version>0.3.5</version>
134134
</dependency>
135+
<dependency>
136+
<groupId>com.squareup</groupId>
137+
<artifactId>kotlinpoet-jvm</artifactId>
138+
<version>1.16.0</version>
139+
</dependency>
135140
</dependencies>
136141
<build>
137142
<sourceDirectory>${project.basedir}/src/main/kotlin</sourceDirectory>
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.baeldung.kotlin.kotlinpoet
2+
3+
import com.squareup.kotlinpoet.FileSpec
4+
import com.squareup.kotlinpoet.FunSpec
5+
import com.squareup.kotlinpoet.ParameterSpec
6+
import com.squareup.kotlinpoet.TypeSpec
7+
import org.junit.jupiter.api.Assertions.assertEquals
8+
import org.junit.jupiter.api.Test
9+
10+
class FileUnitTest {
11+
@Test
12+
fun fullFile() {
13+
val code = FileSpec.builder("com.baeldung.kotlin.kotlinpoet", "Testing.kt")
14+
.addType(TypeSpec.classBuilder("Testing")
15+
.addFunction(FunSpec.builder("count")
16+
.returns(Int::class)
17+
.addParameter(ParameterSpec.builder("items", List::class).build())
18+
.addStatement("return items.size()")
19+
.build())
20+
.build())
21+
.build()
22+
23+
assertEquals("""|package com.baeldung.kotlin.kotlinpoet
24+
|
25+
|import kotlin.Int
26+
|import kotlin.collections.List
27+
|
28+
|public class Testing {
29+
| public fun count(items: List): Int = items.size()
30+
|}
31+
|""".trimMargin(), code.toString())
32+
}
33+
}
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
package com.baeldung.kotlin.kotlinpoet
2+
3+
import com.squareup.kotlinpoet.FunSpec
4+
import com.squareup.kotlinpoet.ParameterSpec
5+
import org.junit.jupiter.api.Assertions.assertEquals
6+
import org.junit.jupiter.api.Test
7+
8+
class FunctionUnitTest {
9+
@Test
10+
fun emptyFunction() {
11+
val code = FunSpec.builder("test")
12+
.build()
13+
14+
assertEquals(
15+
"""|public fun test() {
16+
|}
17+
|""".trimMargin(),
18+
code.toString())
19+
}
20+
21+
@Test
22+
fun simpleBody() {
23+
val code = FunSpec.builder("test")
24+
.addStatement("println(\"Testing\")")
25+
.build()
26+
27+
assertEquals(
28+
"""|public fun test() {
29+
| println("Testing")
30+
|}
31+
|""".trimMargin(),
32+
code.toString())
33+
}
34+
35+
@Test
36+
fun simpleBodyFormatSpecifier() {
37+
val code = FunSpec.builder("test")
38+
.addStatement("println(%S)", "Testing")
39+
.build()
40+
41+
assertEquals(
42+
"""|public fun test() {
43+
| println("Testing")
44+
|}
45+
|""".trimMargin(),
46+
code.toString())
47+
}
48+
49+
@Test
50+
fun controlFlow() {
51+
val code = FunSpec.builder("test")
52+
.beginControlFlow("if (showOutput)")
53+
.addStatement("println(%S)", "Testing")
54+
.nextControlFlow("else")
55+
.addStatement("println()")
56+
.endControlFlow()
57+
.build()
58+
59+
assertEquals(
60+
"""|public fun test() {
61+
| if (showOutput) {
62+
| println("Testing")
63+
| } else {
64+
| println()
65+
| }
66+
|}
67+
|""".trimMargin(),
68+
code.toString())
69+
}
70+
71+
@Test
72+
fun parameters() {
73+
val code = FunSpec.builder("test")
74+
.addParameter(ParameterSpec.builder("first", String::class).build())
75+
.addParameter(ParameterSpec.builder("second", Int::class).defaultValue("%L", 42).build())
76+
.build()
77+
78+
assertEquals(
79+
"""|public fun test(first: kotlin.String, second: kotlin.Int = 42) {
80+
|}
81+
|""".trimMargin(),
82+
code.toString())
83+
}
84+
85+
@Test
86+
fun returns() {
87+
val code = FunSpec.builder("test")
88+
.returns(String::class)
89+
.build()
90+
91+
assertEquals(
92+
"""|public fun test(): kotlin.String {
93+
|}
94+
|""".trimMargin(),
95+
code.toString())
96+
}
97+
98+
@Test
99+
fun singleExpression() {
100+
val code = FunSpec.builder("test")
101+
.returns(Int::class)
102+
.addStatement("return 5")
103+
.build()
104+
105+
assertEquals(
106+
"""|public fun test(): kotlin.Int = 5
107+
|""".trimMargin(),
108+
code.toString())
109+
}
110+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.baeldung.kotlin.kotlinpoet
2+
3+
import com.squareup.kotlinpoet.KModifier
4+
import com.squareup.kotlinpoet.TypeSpec
5+
import org.junit.jupiter.api.Assertions.assertEquals
6+
import org.junit.jupiter.api.Test
7+
8+
class KotlinPoetUnitTest {
9+
@Test
10+
fun generateEmptyClass() {
11+
val code = TypeSpec.classBuilder("Test")
12+
.addModifiers(KModifier.PROTECTED)
13+
.build()
14+
15+
16+
assertEquals("protected class Test\n", code.toString())
17+
}
18+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.baeldung.kotlin.kotlinpoet
2+
3+
import com.squareup.kotlinpoet.FunSpec
4+
import com.squareup.kotlinpoet.KModifier
5+
import com.squareup.kotlinpoet.PropertySpec
6+
import com.squareup.kotlinpoet.TypeSpec
7+
import org.junit.jupiter.api.Assertions.assertEquals
8+
import org.junit.jupiter.api.Test
9+
10+
class TypeUnitTest {
11+
@Test
12+
fun emptyClass() {
13+
val code = TypeSpec.classBuilder("Test")
14+
.build()
15+
16+
assertEquals("""|public class Test
17+
|""".trimMargin(), code.toString())
18+
}
19+
20+
@Test
21+
fun methods() {
22+
val code = TypeSpec.classBuilder("Test")
23+
.addFunction(FunSpec.builder("doSomething")
24+
.returns(Int::class)
25+
.addParameter("input", String::class)
26+
.addModifiers(KModifier.PRIVATE)
27+
.build())
28+
.build()
29+
30+
assertEquals("""|public class Test {
31+
| private fun doSomething(input: kotlin.String): kotlin.Int {
32+
| }
33+
|}
34+
|""".trimMargin(), code.toString())
35+
}
36+
37+
@Test
38+
fun properties() {
39+
val code = TypeSpec.classBuilder("Test")
40+
.addProperty(PropertySpec.builder("test", String::class)
41+
.addModifiers(KModifier.PRIVATE)
42+
.mutable()
43+
.initializer("%S", "Hello")
44+
.build())
45+
.build()
46+
47+
assertEquals("""|public class Test {
48+
| private var test: kotlin.String = "Hello"
49+
|}
50+
|""".trimMargin(), code.toString())
51+
}
52+
}

0 commit comments

Comments
 (0)