Skip to content

Commit 892011c

Browse files
committed
Merge branch 'KTLN-609/ktor-thymeleaf' of github.com:fabiotadashi/kotlin-tutorials into KTLN-609/ktor-thymeleaf
2 parents 454f317 + 31d79ec commit 892011c

File tree

10 files changed

+447
-2
lines changed

10 files changed

+447
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.baeldung.passbyvalue
2+
3+
import org.junit.Assert.assertEquals
4+
import org.junit.Test
5+
6+
class PassByValueOrReferenceUnitTest {
7+
8+
data class SomeObj(var x: Int = 0)
9+
10+
private fun modifyObject(someObj: SomeObj) {
11+
someObj.x = 3
12+
}
13+
14+
private fun modifyValue(value: Int): Int {
15+
return value + 10
16+
}
17+
18+
@Test
19+
fun `Test using pass-by-reference`() {
20+
val obj = SomeObj()
21+
22+
assertEquals( 0, obj.x) // before modify
23+
24+
modifyObject(obj)
25+
26+
assertEquals(3, obj.x) // after modify
27+
}
28+
29+
@Test
30+
fun `Test using pass-by-value`(){
31+
val num = 5
32+
val modifiedNum = modifyValue(num)
33+
34+
assertEquals(5, num)
35+
assertEquals(15, modifiedNum)
36+
}
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.baeldung.iterateArrayReversely
2+
3+
import org.junit.jupiter.api.Test
4+
import kotlin.test.assertEquals
5+
6+
class IterateArrayReverselyUnitTest {
7+
private val array = arrayOf("a", "b", "c", "d", "e", "f")
8+
private val reversedList = listOf("f", "e", "d", "c", "b", "a")
9+
10+
@Test
11+
fun `when using reversed() on index Range, then get expected result`() {
12+
val resultList = mutableListOf<String>()
13+
14+
for (i in array.indices.reversed()) {
15+
resultList += array[i]
16+
}
17+
18+
assertEquals(reversedList, resultList)
19+
}
20+
21+
@Test
22+
fun `when using downTo on indexes, then get expected result`() {
23+
val resultList = mutableListOf<String>()
24+
for (i in (array.lastIndex downTo 0)) {
25+
resultList += array[i]
26+
}
27+
assertEquals(reversedList, resultList)
28+
}
29+
30+
@Test
31+
fun `when using reversed() on the array, then get expected result`() {
32+
val resultList = mutableListOf<String>()
33+
for (element in array.reversed()) {
34+
resultList += element
35+
}
36+
assertEquals(reversedList, resultList)
37+
}
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package typeErasedListToArray
2+
3+
import org.junit.jupiter.api.Assertions.assertArrayEquals
4+
import org.junit.jupiter.api.Test
5+
6+
class typeErasedListToArrayUnitTest {
7+
8+
@Test
9+
fun `convert type-erased list to array using toTypedArray`() {
10+
val intList: List<Any> = listOf(1, 2, 3, 4, 5)
11+
val stringList: List<Any> = listOf("one", "two", "three", "four", "five")
12+
13+
val array1 = intList.toTypedArray()
14+
val array2 = stringList.toTypedArray()
15+
16+
assertArrayEquals(arrayOf(1, 2, 3, 4, 5), array1)
17+
assertArrayEquals(arrayOf("one", "two", "three", "four", "five"), array2)
18+
}
19+
20+
@Test
21+
fun `convert type-erased list to array using toTypedArray with reified and inline`() {
22+
val intList: List<Any> = listOf(1, 2, 3, 4, 5)
23+
val stringList: List<Any> = listOf("one", "two", "three", "four", "five")
24+
25+
val array1 = mapToArray<Int>(intList)
26+
val array2 = mapToArray<String>(stringList)
27+
28+
assertArrayEquals(arrayOf(1, 2, 3, 4, 5), array1)
29+
assertArrayEquals(arrayOf("one", "two", "three", "four", "five"), array2)
30+
}
31+
32+
@Test
33+
fun `convert type-erased list to array using array constructor`() {
34+
val intList: List<Any> = listOf(1, 2, 3, 4, 5)
35+
val stringList: List<Any> = listOf("one", "two", "three", "four", "five")
36+
37+
var array1 = Array(intList.size) { i -> intList[i] as Int}
38+
val array2 = Array(stringList.size) { i -> stringList[i] as String}
39+
40+
assertArrayEquals(arrayOf(1, 2, 3, 4, 5), array1)
41+
assertArrayEquals(arrayOf("one", "two", "three", "four", "five"), array2)
42+
}
43+
44+
inline fun <reified T> mapToArray(list: List<*>): Array<T> {
45+
return list.mapNotNull{ it as? T }.toTypedArray()
46+
}
47+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
package com.baeldung.pair.workingWithTriple
2+
3+
import org.junit.jupiter.api.Test
4+
import kotlin.test.assertEquals
5+
import kotlin.test.assertTrue
6+
7+
data class Player(
8+
val name: String,
9+
var score: Int
10+
)
11+
12+
13+
14+
typealias Match = Triple<Player, Player, String>
15+
16+
fun Match.firstWin() = apply {
17+
first.score++
18+
second.score--
19+
}
20+
21+
fun Match.secondWin() = apply {
22+
first.score--
23+
second.score++
24+
}
25+
26+
class WorkingWithTripleUnitTest {
27+
28+
@Test
29+
fun `when calling toString, then get the expected result`() {
30+
val t = Triple("A", "B", "C")
31+
assertEquals("(A, B, C)", t.toString())
32+
assertEquals("(A, B, C)", "$t")
33+
}
34+
35+
@Test
36+
fun `when assignment with deconstruction, then get the expected result`() {
37+
val (a, b, c) = Triple(42, "Kotlin", Long.MAX_VALUE)
38+
assertTrue { a is Int }
39+
assertEquals(42, a)
40+
41+
assertTrue { b is String }
42+
assertEquals("Kotlin", b)
43+
44+
assertTrue { c is Long }
45+
assertEquals(Long.MAX_VALUE, c)
46+
}
47+
48+
@Test
49+
fun `when calling triple-toList(), then get the expected result`() {
50+
val t = Triple("Java", "Kotlin", "Python")
51+
assertEquals(listOf("Java", "Kotlin", "Python"), t.toList())
52+
}
53+
54+
fun <T> Triple<T, T, T>.reverse() = Triple(third, second, first)
55+
56+
@Test
57+
fun `when reversing a triple, then get the expected result`() {
58+
val t = Triple("x", "y", "z")
59+
assertEquals(Triple("z", "y", "x"), t.reverse())
60+
}
61+
62+
@Test
63+
fun `when using triple store matches, then get the expected result`() {
64+
val kent = Player("Kent", 42)
65+
val eric = Player("Eric", 42)
66+
val tom = Player("Tom", 20)
67+
val john = Player("John", 32)
68+
69+
val matchesTomorrow = listOf(
70+
Triple(kent, tom, "9:00"),
71+
Triple(eric, john, "10:00"),
72+
Triple(kent, eric, "17:00"),
73+
Triple(tom, john, "18:00"),
74+
)
75+
76+
val matchAt10 = matchesTomorrow.first { it.third == "10:00" }
77+
assertEquals(eric, matchAt10.first)
78+
assertEquals(john, matchAt10.second)
79+
80+
//using type alias
81+
val matchesTomorrow2 = listOf(
82+
Match(kent, tom, "9:00"),
83+
Match(eric, john, "10:00"),
84+
Match(kent, eric, "17:00"),
85+
Match(tom, john, "18:00"),
86+
)
87+
88+
val matchAt17 = matchesTomorrow2.first { it.third == "17:00" }
89+
assertEquals(kent, matchAt17.first)
90+
assertEquals(eric, matchAt17.second)
91+
92+
matchAt17.secondWin()
93+
94+
assertEquals(41, matchAt17.first.score)
95+
assertEquals(43, matchAt17.second.score)
96+
97+
}
98+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package com.baeldung.functionAsDefaultArgument
2+
3+
import org.junit.jupiter.api.Test
4+
import kotlin.test.assertEquals
5+
6+
fun greeting(name: String = "Baeldung"): String {
7+
return "Hi $name"
8+
}
9+
10+
fun greeting1(name: String = "Baeldung", buildMessage: (String) -> String): String {
11+
return "Hi $name, ${buildMessage(name)}"
12+
}
13+
14+
fun greeting2(name: String = "Baeldung", buildMessage: (String) -> String = { input: String ->
15+
"your name is ${if (input.length > 3) "long" else "short"}."
16+
}): String {
17+
return "Hi $name, ${buildMessage(name)}"
18+
}
19+
20+
fun greeting3(name: String = "Baeldung", buildMessage: (String) -> String = { _ -> "how do you do?" }): String {
21+
return "Hi $name, ${buildMessage(name)}"
22+
}
23+
24+
class FunctionAsDefaultArgumentUnitTest {
25+
26+
val msgByCharCount: (String) -> String = { input: String ->
27+
"your name has an ${if (input.length % 2 == 1) "odd" else "even"} number of letters."
28+
}
29+
30+
@Test
31+
fun `when calling greeting with or without passing parameter, then get expected result`() {
32+
assertEquals("Hi Baeldung", greeting())
33+
assertEquals("Hi Kai", greeting("Kai"))
34+
}
35+
36+
@Test
37+
fun `when calling greeting1, then get expected result`() {
38+
val msgWithDefaultName = greeting1(buildMessage = msgByCharCount)
39+
assertEquals("Hi Baeldung, your name has an even number of letters.", msgWithDefaultName)
40+
41+
val kaiMsg = greeting1(name = "Kai", buildMessage = msgByCharCount)
42+
assertEquals("Hi Kai, your name has an odd number of letters.", kaiMsg)
43+
}
44+
45+
@Test
46+
fun `when calling greeting2, then get expected result`() {
47+
val result1 = greeting2()
48+
assertEquals("Hi Baeldung, your name is long.", result1)
49+
50+
val result2 = greeting2("Kai")
51+
assertEquals("Hi Kai, your name is short.", result2)
52+
53+
val result3 = greeting2(buildMessage = msgByCharCount)
54+
assertEquals("Hi Baeldung, your name has an even number of letters.", result3)
55+
56+
val result4 = greeting2(name = "Kai", buildMessage = msgByCharCount)
57+
assertEquals("Hi Kai, your name has an odd number of letters.", result4)
58+
}
59+
60+
@Test
61+
fun `when calling greeting3, then get expected result`() {
62+
val result1 = greeting3()
63+
assertEquals("Hi Baeldung, how do you do?", result1)
64+
65+
val result2 = greeting3("Kai")
66+
assertEquals("Hi Kai, how do you do?", result2)
67+
68+
val result3 = greeting3(buildMessage = msgByCharCount)
69+
assertEquals("Hi Baeldung, your name has an even number of letters.", result3)
70+
71+
val result4 = greeting3(name = "Kai", buildMessage = msgByCharCount)
72+
assertEquals("Hi Kai, your name has an odd number of letters.", result4)
73+
}
74+
75+
}

0 commit comments

Comments
 (0)