Just a commit
This commit is contained in:
31
kotlin/hello-world/src/main/kotlin/HelloWorld.kt
Normal file
31
kotlin/hello-world/src/main/kotlin/HelloWorld.kt
Normal file
@@ -0,0 +1,31 @@
|
||||
/**
|
||||
* Simple HelloWorld singleton class as defined by the `Kotlin object keyword`.
|
||||
* See: https://kotlinlang.org/docs/reference/object-declarations.html#object-declarations
|
||||
*
|
||||
* As an alternative one could create a class such as:
|
||||
* ```
|
||||
* class HelloWorld(name: String? = "Default Value") {
|
||||
* fun hello(): String {
|
||||
*
|
||||
* }
|
||||
* }
|
||||
* ```
|
||||
* Resulting in a call such as: `HelloWorld("Bob").hello()`
|
||||
* See: https://kotlinlang.org/docs/reference/classes.html#constructors
|
||||
*
|
||||
* In Kotlin we make objects defined as nullable via the trailing `?`, if you try
|
||||
* to assign a null value to any value that isn't nullable a compilation error is thrown.
|
||||
* Kotlin makes sure you are accessing nullable values safely and provides null safe calls
|
||||
* and the use of the elvis operator. See: https://kotlinlang.org/docs/reference/null-safety.html
|
||||
*
|
||||
* You may provide default values on methods, so if an argument is omitted the default is used.
|
||||
* See: https://kotlinlang.org/docs/reference/functions.html#default-arguments
|
||||
*
|
||||
* Kotlin provides String interpolation to make String formatting simple.
|
||||
* See: https://kotlinlang.org/docs/reference/idioms.html#string-interpolation
|
||||
*/
|
||||
object HelloWorld {
|
||||
fun hello(name: String? = "Default Argument"): String {
|
||||
|
||||
}
|
||||
}
|
32
kotlin/hello-world/src/test/kotlin/HelloWorldTest.kt
Normal file
32
kotlin/hello-world/src/test/kotlin/HelloWorldTest.kt
Normal file
@@ -0,0 +1,32 @@
|
||||
import kotlin.test.assertEquals
|
||||
import org.junit.Test
|
||||
|
||||
class HelloWorldTest {
|
||||
|
||||
@Test
|
||||
fun helloNoName() {
|
||||
assertEquals("Hello, World!", HelloWorld.hello())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun helloBlankName() {
|
||||
assertEquals("Hello, World!", HelloWorld.hello(""))
|
||||
assertEquals("Hello, World!", HelloWorld.hello(" "))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun helloNullName() {
|
||||
//This isn't advised in Kotlin but demonstrates the null safety in Kotlin
|
||||
assertEquals("Hello, World!", HelloWorld.hello(null))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun helloSampleName() {
|
||||
assertEquals("Hello, Alice!", HelloWorld.hello("Alice"))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun helloAnotherSampleName() {
|
||||
assertEquals("Hello, Bob!", HelloWorld.hello("Bob"))
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user