BAEL-2129 Void Type in Kotlin

-Moved the code from core-kotlin to the new extension core-kotlin-2
This commit is contained in:
Dhawal Kapil
2019-02-03 23:07:10 +05:30
parent 47c1eed6bd
commit b17a130cf1

View File

@@ -0,0 +1,63 @@
package com.baeldung.voidtypes
import org.junit.jupiter.api.Test
import kotlin.test.assertNull
import kotlin.test.assertTrue
class VoidTypesUnitTest {
// Un-commenting below methods will result into compilation error
// as the syntax used is incorrect and is used for explanation in tutorial.
// fun returnTypeAsVoidAttempt1(): Void {
// println("Trying with Void as return type")
// }
// fun returnTypeAsVoidAttempt2(): Void {
// println("Trying with Void as return type")
// return null
// }
fun returnTypeAsVoidSuccess(): Void? {
println("Function can have Void as return type")
return null
}
fun unitReturnTypeForNonMeaningfulReturns(): Unit {
println("No meaningful return")
}
fun unitReturnTypeIsImplicit() {
println("Unit Return type is implicit")
}
fun alwaysThrowException(): Nothing {
throw IllegalArgumentException()
}
fun invokeANothingOnlyFunction() {
alwaysThrowException()
var name = "Tom"
}
@Test
fun givenJavaVoidFunction_thenMappedToKotlinUnit() {
assertTrue(System.out.println() is Unit)
}
@Test
fun givenVoidReturnType_thenReturnsNullOnly() {
assertNull(returnTypeAsVoidSuccess())
}
@Test
fun givenUnitReturnTypeDeclared_thenReturnsOfTypeUnit() {
assertTrue(unitReturnTypeForNonMeaningfulReturns() is Unit)
}
@Test
fun givenUnitReturnTypeNotDeclared_thenReturnsOfTypeUnit() {
assertTrue(unitReturnTypeIsImplicit() is Unit)
}
}