Skip to content

Commit

Permalink
Improve test coverage, republish under the new GitHub username
Browse files Browse the repository at this point in the history
  • Loading branch information
hanggrian committed Aug 6, 2024
1 parent 2cf4516 commit 63daf9d
Show file tree
Hide file tree
Showing 17 changed files with 368 additions and 98 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
build/
local.properties

# IDEA
# IntelliJ
*.iml
.idea/
out/
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ repositories {
mavenCentral()
}
dependencies {
implementation "com.hendraanggrian:javapoet-dsl:$version"
implementation "com.hanggrian:javapoet-dsl:$version"
}
```

Expand Down
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ subprojects {
configure<com.vanniktech.maven.publish.MavenPublishBaseExtension> {
configure(
com.vanniktech.maven.publish.KotlinJvm(
com.vanniktech.maven.publish.JavadocJar.Dokka("dokkaJavadoc")
com.vanniktech.maven.publish.JavadocJar.Dokka("dokkaJavadoc"),
)
)
publishToMavenCentral(com.vanniktech.maven.publish.SonatypeHost.CENTRAL_PORTAL)
Expand Down
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ kotlinx-kover = "org.jetbrains.kotlinx.kover:0.8.3"
ktlint = "org.jlleitschuh.gradle.ktlint:12.1.1"
maven-publish = "com.vanniktech.maven.publish.base:0.29.0"
git-publish = "org.ajoberstar.git-publish:4.2.2"
pages = "com.hendraanggrian.pages:0.1"
pages = "com.hanggrian.pages:0.1"

[libraries]
# lint
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ public fun buildJavaFile(
configuration: JavaFileBuilder.() -> Unit,
): JavaFile {
contract { callsInPlace(configuration, InvocationKind.EXACTLY_ONCE) }
return JavaFileBuilder(packageName).apply(configuration).build()
return JavaFileBuilder(packageName)
.apply(configuration)
.build()
}

/** Wrapper of [JavaFile.Builder], providing DSL support as a replacement to Java builder. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,10 +155,6 @@ public class MethodSpecBuilder(private val nativeBuilder: MethodSpec.Builder) :
nativeBuilder.addModifiers(*modifiers)
}

public fun modifiers(modifiers: Iterable<Modifier>) {
nativeBuilder.addModifiers(modifiers)
}

public fun typeVariables(typeVariables: Iterable<TypeVariableName>) {
nativeBuilder.addTypeVariables(typeVariables)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -257,9 +257,5 @@ public class ParameterSpecBuilder(private val nativeBuilder: ParameterSpec.Build
nativeBuilder.addModifiers(*modifiers)
}

public fun modifiers(modifiers: Iterable<Modifier>) {
nativeBuilder.addModifiers(modifiers)
}

public fun build(): ParameterSpec = nativeBuilder.build()
}
2 changes: 2 additions & 0 deletions javapoet-dsl/src/test/kotlin/com/example/Dummies.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.example

class Class1

annotation class Annotation1

annotation class Annotation2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import com.example.Annotation2
import com.example.Annotation3
import com.example.Annotation4
import com.example.Annotation5
import com.example.Annotation6
import com.example.Annotation7
import com.google.common.truth.Truth.assertThat
import com.squareup.javapoet.AnnotationSpec
import com.squareup.javapoet.CodeBlock
Expand All @@ -17,17 +19,21 @@ class AnnotationSpecHandlerTest {
assertThat(
buildFieldSpec(STRING, "test") {
annotation(Annotation1::class.name)
annotation(Annotation2::class)
annotation<Annotation3>()
annotation(Annotation4::class.name) { member("name4", "value4") }
annotation(Annotation5::class) { member("name5", "value5") }
annotation(Annotation2::class.java)
annotation(Annotation3::class)
annotation<Annotation4>()
annotation(Annotation5::class.name) { member("name5", "value5") }
annotation(Annotation6::class.java) { member("name6", "value6") }
annotation(Annotation7::class) { member("name7", "value7") }
}.annotations,
).containsExactly(
AnnotationSpec.builder(Annotation1::class.java).build(),
AnnotationSpec.builder(Annotation2::class.java).build(),
AnnotationSpec.builder(Annotation3::class.java).build(),
AnnotationSpec.builder(Annotation4::class.java).addMember("name4", "value4").build(),
AnnotationSpec.builder(Annotation4::class.java).build(),
AnnotationSpec.builder(Annotation5::class.java).addMember("name5", "value5").build(),
AnnotationSpec.builder(Annotation6::class.java).addMember("name6", "value6").build(),
AnnotationSpec.builder(Annotation7::class.java).addMember("name7", "value7").build(),
)
}

Expand All @@ -37,12 +43,14 @@ class AnnotationSpecHandlerTest {
buildFieldSpec(STRING, "test") {
annotations {
Annotation1::class.name { member("name1", "value1") }
Annotation2::class { member("name2", "value2") }
Annotation2::class.java { member("name2", "value2") }
Annotation3::class { member("name3", "value3") }
}
}.annotations,
).containsExactly(
AnnotationSpec.builder(Annotation1::class.java).addMember("name1", "value1").build(),
AnnotationSpec.builder(Annotation2::class.java).addMember("name2", "value2").build(),
AnnotationSpec.builder(Annotation3::class.java).addMember("name3", "value3").build(),
)
}
}
Expand Down
109 changes: 95 additions & 14 deletions javapoet-dsl/src/test/kotlin/com/hanggrian/javapoet/CodeBlockTest.kt
Original file line number Diff line number Diff line change
@@ -1,35 +1,116 @@
package com.hanggrian.javapoet

import com.google.common.truth.Truth.assertThat
import com.squareup.javapoet.CodeBlock
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFails
import kotlin.test.assertFalse
import kotlin.test.assertTrue

class CodeBlockTest {
private val expected =
CodeBlock
.builder()
.addStatement("int total = 0")
.beginControlFlow("for (int i = 0; i < 10; i++)")
.addStatement("total += i")
.endControlFlow()
.build()

@Test
fun simple() {
assertEquals(
expected,
@Test
fun isEmpty() {
buildCodeBlock {
assertTrue(isEmpty())
append("text")
assertFalse(isEmpty())
}
}

@Test
fun isNotEmpty() {
buildCodeBlock {
assertFalse(isNotEmpty())
append("text")
assertTrue(isNotEmpty())
}
}

@Test
fun controlFlow() {
assertThat(
buildCodeBlock {
beginControlFlow("format", "arg")
nextControlFlow("format", "arg")
endControlFlow("format", "arg")
},
).isEqualTo(
CodeBlock
.builder()
.beginControlFlow("format", "arg")
.nextControlFlow("format", "arg")
.endControlFlow("format", "arg")
.build(),
)
}

@Test
fun append() {
assertThat(
buildCodeBlock {
append("text")
append(codeBlockOf("some code"))
},
).isEqualTo(
CodeBlock
.builder()
.add("text")
.add(CodeBlock.of("some code"))
.build(),
)
}

@Test
fun appendLine() {
assertThat(
buildCodeBlock {
appendLine()
appendLine("text")
appendLine(codeBlockOf("some code"))
},
).isEqualTo(
CodeBlock
.builder()
.addStatement("")
.addStatement("text")
.addStatement(CodeBlock.of("some code"))
.build(),
)
}

@Test
fun clear() {
buildCodeBlock {
assertFalse(isNotEmpty())
append("text")
clear()
assertTrue(isEmpty())
}
}

@Test
fun `Simple example`() {
assertThat(
buildCodeBlock {
appendLine("int total = 0")
beginControlFlow("for (int i = 0; i < 10; i++)")
appendLine("total += i")
endControlFlow()
},
).isEqualTo(
CodeBlock
.builder()
.addStatement("int total = 0")
.beginControlFlow("for (int i = 0; i < 10; i++)")
.addStatement("total += i")
.endControlFlow()
.build(),
)
}

@Test
fun escapeSpecialChar() {
fun `Escape special characters`() {
assertFails { codeBlockOf("100$") }
assertEquals("100$", "${codeBlockOf("100$$")}")
assertEquals("100\$S", "${codeBlockOf("100$\$S")}")
Expand Down
Loading

0 comments on commit 63daf9d

Please sign in to comment.