Skip to content

Commit

Permalink
Add more tests for LambdaTypeName (#478)
Browse files Browse the repository at this point in the history
* Add more tests for LambdaTypeName

* Wrap LambdaTypeName return type in brackets
  • Loading branch information
Egor Andreevich authored Oct 7, 2018
1 parent 6921368 commit c348f8e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/main/java/com/squareup/kotlinpoet/LambdaTypeName.kt
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class LambdaTypeName internal constructor(
}

parameters.emit(out)
out.emitCode(" -> %T", returnType)
out.emitCode(if (returnType is LambdaTypeName) " -> (%T)" else " -> %T", returnType)

if (nullable) {
out.emit(")")
Expand Down
22 changes: 22 additions & 0 deletions src/test/java/com/squareup/kotlinpoet/LambdaTypeNameTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,26 @@ class LambdaTypeNameTest {
returnType = Unit::class.asTypeName())
}.hasMessageThat().isEqualTo("Parameters with default values are not allowed")
}

@Test fun lambdaReturnType() {
val returnTypeName = LambdaTypeName.get(
parameters = *arrayOf(Int::class.asTypeName()),
returnType = Unit::class.asTypeName())
val typeName = LambdaTypeName.get(
parameters = *arrayOf(Int::class.asTypeName()),
returnType = returnTypeName)
assertThat(typeName.toString())
.isEqualTo("(kotlin.Int) -> ((kotlin.Int) -> kotlin.Unit)")
}

@Test fun lambdaParameterType() {
val parameterTypeName = LambdaTypeName.get(
parameters = *arrayOf(Int::class.asTypeName()),
returnType = Int::class.asTypeName())
val typeName = LambdaTypeName.get(
parameters = *arrayOf(parameterTypeName),
returnType = Unit::class.asTypeName())
assertThat(typeName.toString())
.isEqualTo("((kotlin.Int) -> kotlin.Int) -> kotlin.Unit")
}
}

0 comments on commit c348f8e

Please sign in to comment.