Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.baeldung.kotlinvsjava

import org.junit.Test
import kotlin.test.assertEquals

class CompanionObjectTest {

@Test
fun givenAClassWithCompanionObject_whenCallingMethodTheSameAsStaticOne_thenWeGetAResult() {
assertEquals("A", A.returnClassName())
}

}

class A {
companion object {
fun returnClassName(): String {
return "A"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.baeldung.kotlinvsjava

import org.junit.Test
import kotlin.test.assertEquals

class ConstructorTests {

@Test
fun givenAClassWithPrimaryConstructor_whenCreatingAnInstance_thenWeGetObject() {
var example = Example(1, "Example")

assertEquals(1, example.id)
assertEquals("Example", example.name)
}

}

class Example constructor(val id: Int, var name: String)
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.baeldung.kotlinvsjava

import org.junit.Test
import kotlin.test.assertEquals
import kotlin.test.assertFalse

class DataClassTest {

@Test
fun givenASampleDataClass_whenCallingToStringMethod_thenItReturnsAllProperties() {
val student = Student(1, "John", "Smith")

assertEquals(1, student.id)
assertEquals("John", student.name)
assertEquals("Smith", student.lastName)
assertEquals("Student(id=1, name=John, lastName=Smith)", student.toString())
}

@Test
fun givenASampleDataClass_whenCreatingACopyWithGeneratedFunction_thenItReturnsACopyWithRequestedChanges() {
val student = Student(1, "John", "Smith")
val student2 = student.copy(id = 2, name = "Anne")

assertEquals(2, student2.id)
assertEquals("Anne", student2.name)
assertEquals("Smith", student2.lastName)
assertEquals("Student(id=2, name=Anne, lastName=Smith)", student2.toString())
assertFalse(student.equals(student2))
}

}

data class Student(val id: Int, val name: String, val lastName: String)
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.baeldung.kotlinvsjava

import org.junit.Test
import kotlin.test.assertEquals

class DelegationTest {

@Test
fun givenAClassWithDelegation_whenCallDelegatedMethod_thenWeGetAResultDefinedInPassedObject() {
val car = Car(V6Engine())

assertEquals("Vroom", car.makeSound())
}

}

interface Engine {
fun makeSound(): String
}

class V6Engine: Engine {
override fun makeSound(): String {
return "Vroom"
}
}

class Car(e: Engine) : Engine by e
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.baeldung.kotlinvsjava

import java.io.IOException
import kotlin.test.Test
import kotlin.test.assertEquals

class ExceptionsTest {

@Test
fun givenATryExpression_whenReturning5InLastExpressionOfTryBlock_thenWeGet5() {
val value: Int = try { 5 } catch (e: IOException) { 6 }

assertEquals(5, value)
}

@Test
fun givenATryExpression_whenReturning6InLastExpressionOfCatchBlock_thenWeGet6() {
val value: Int = try { funThrowingException() } catch (e: IOException) { 6 }

assertEquals(6, value)
}

@org.junit.Test(expected = IllegalArgumentException::class)
fun givenANullString_whenUsingElvisOperator_thenExceptionIsThrown() {
val sampleString: String? = null

val length: Int = sampleString?.length ?: throw IllegalArgumentException("String must not be null")
}

private fun funThrowingException(): Nothing {
throw IOException()
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.baeldung.kotlinvsjava

import org.junit.Test
import kotlin.test.assertEquals

class ExtensionFunctionsTest {

@Test
fun givenAStringWithAnExtensionFunction_whenCallingThatFunction_thenItConcatenatesStrings() {
val sampleString = "ABC"
val concatenatedString = sampleString.appendString("DEF")

assertEquals("ABCDEF", concatenatedString)
}

@Test
fun givenAStringWithAnExtensionProperty_whenReadingProperty_thenItReturnsLengthOfString() {
val sampleString = "ABC"

assertEquals(3, sampleString.size)
}

fun String.appendString(str : String): String {
return plus(str)
}

val String.size: Int
get() = length

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package com.baeldung.kotlinvsjava

import org.junit.Test
import kotlin.test.assertEquals

class FunctionsTest {

@Test
fun givenALambdaExpressionConcatenatingString_whenUsingTheFunctionWithAAndBString_thenWeGetAB() {
val concat: (String, String) -> String = { a, b -> a + b }

assertEquals("AB", concat("A","B"))
}

@Test
fun givenAnAnonymousFunctionConcatenatingString_whenUsingTheFunctionWithAAndBString_thenWeGetAB() {
val concat: (String, String) -> String = fun(a: String, b: String): String { return a + b }

assertEquals("AB", concat("A","B"))
}

@Test
fun givenAnPlusMethodOfString_whenUsingTheFunctionWithAAndBString_thenWeGetAB() {
val concat = String::plus

assertEquals("AB", concat("A","B"))
}

@Test
fun givenAStringConstractorAssignedToFunction_whenUsingFunctionReference_thenWeGetNewString() {
val concat = ::String

assertEquals("A", concat().plus("A"))
}

@Test
fun givenAClassImplementingAFunctionType_whenUsingTheFunctionWithAAndBString_thenWeGetAB() {
val concat = StringConcatenation()

assertEquals("AB", concat("A", "B"))
}

@Test
fun givenALambdaExpressionWithReceiver_whenUsingTheFunctionWithReceiver_thenWeGetABC() {
val concat: String.(String, String) -> String = { a, b -> plus(a).plus(b) }

assertEquals("ABC", "A".concat("B", "C"))
}

@Test
fun givenALambdaExpressionWithinLambdaExpression_whenUsingTheFunction_thenWeGetAB() {
val concat: (String) -> ((String) -> String) = { a -> {b -> a + b} }

assertEquals("AB", (concat("A")("B")))
}

@Test
fun given3NestedLambdaExpression_whenUsingTheFunction_thenWeGetABC() {
val concat: (String) -> (String) -> (String) -> String = { a -> {b -> { c -> a + b + c} } }

assertEquals("ABC", concat("A")("B")("C"))
}

}

class StringConcatenation: (String, String) -> String {
override fun invoke(p1: String, p2: String): String {
return p1 + p2
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.baeldung.kotlinvsjava

import org.junit.Test
import kotlin.math.absoluteValue
import kotlin.test.assertEquals
import kotlin.test.assertTrue

class IsOperatorTest {

@Test
fun givenSampleValue_whenUsingIsOperatorInIfStatement_thenItCastsAutomaticallyToString() {
val value: Any = "string"

if(value is String) {
assertEquals(6, value.length)
}
}

@Test
fun givenSampleValue_whenUsingIsOperatorWithAndOperator_thenItCastsAutomaticallyToString() {
val value: Any = "string"

assertTrue(value is String && value.length == 6)
}

@Test
fun givenSampleValue_whenUsingWithWhenOperator_thenItCastsAutomaticallyToString() {
val value: Any = "string"

when(value) {
is String -> assertEquals(6, value.length)
is Int -> assertEquals(6, value.absoluteValue)
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package com.baeldung.kotlinvsjava

import kotlin.test.Test
import java.lang.NullPointerException
import kotlin.test.assertEquals
import kotlin.test.assertNotNull
import kotlin.test.assertNull

class NullSafetyTest {

@Test
fun givenStringAndNull_whenUsingSafeCallOperatorWithLengthMethod_thenReturnsLengthForStringAndNullForNull() {
val stringValue: String? = "string"
val nullValue: String? = null

assertNotNull(stringValue?.length)
assertNull(nullValue?.length)
}

@Test(expected = NullPointerException::class)
fun givenNullReference_whenUsingTheNotNullAssertionOperator_thenItThrowsNullPointerException() {
val stringValue: String? = "string"
val nullValue: String? = null

assertNotNull(stringValue!!.length)
nullValue!!.length
}

@Test
fun givenStringAndNull_whenUsingElvisOperator_thenItTestsAgainstNullAndReturnsTheProperValue() {
val stringValue: String? = "string"
val nullValue: String? = null

val shouldBeLength: Int = stringValue?.length ?: -1
val souldBeMinusOne: Int = nullValue?.length ?: -1

assertEquals(6, shouldBeLength)
assertEquals(-1, souldBeMinusOne)
}

@Test
fun givenString_whenCastingToInt_thenItReturnsNull() {
val stringValue: String? = "string"

val intValue: Int? = stringValue as? Int

assertNull(intValue)
}

@Test
fun givenCollectionWithNulls_whenFilterNonNull_thenItReturnsCollectionWithoutNulls() {
val list: List<String?> = listOf("a", "b", null)
val nonNullList = list.filterNotNull()

assertEquals(2, nonNullList.size)
assertEquals(nonNullList, listOf("a", "b"))
}

@Test
fun givenCollectionWithNulls_whenLetWithSafeCallOperator_thenItOmitsNulls() {
val list: List<String?> = listOf("a", "b", null)
for(elem in list) {
elem?.let { assertNotNull(it) }
}
}

}
Loading