Skip to content

Commit ad8ae55

Browse files
committed
Added a few unit tests for kotlin operator examples.
1 parent 1250605 commit ad8ae55

6 files changed

Lines changed: 85 additions & 1 deletion

File tree

core-kotlin/src/main/kotlin/com/baeldung/operators/Money.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
package com.baeldung.operators
2+
13
import java.math.BigDecimal
24

35
enum class Currency {

core-kotlin/src/main/kotlin/com/baeldung/operators/Page.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
package com.baeldung.operators
2+
13
interface Page<T> {
24
fun pageNumber(): Int
35
fun pageSize(): Int

core-kotlin/src/main/kotlin/com/baeldung/operators/Point.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
package com.baeldung.operators
2+
13
data class Point(val x: Int, val y: Int)
24

35
operator fun Point.unaryMinus() = Point(-x, -y)
@@ -14,7 +16,7 @@ operator fun Point.times(factor: Int): Point = Point(x * factor, y * factor)
1416
operator fun Int.times(point: Point): Point = Point(point.x * this, point.y * this)
1517

1618
class Shape {
17-
private val points = mutableListOf<Point>()
19+
val points = mutableListOf<Point>()
1820

1921
operator fun Point.unaryPlus() {
2022
points.add(this)

core-kotlin/src/main/kotlin/com/baeldung/operators/Utils.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
package com.baeldung.operators
2+
13
import java.math.BigInteger
24

35
operator fun <T> MutableCollection<T>.plusAssign(element: T) {
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.baeldung.operators
2+
3+
import org.junit.Test
4+
import kotlin.test.assertEquals
5+
import kotlin.test.assertTrue
6+
7+
class PageTest {
8+
9+
private val page = PageImpl(1, 10, "Java", "Kotlin", "Scala")
10+
11+
@Test
12+
fun `Get convention should work as expected`() {
13+
assertEquals(page[1], "Kotlin")
14+
assertEquals(page[1, 3], listOf("Kotlin", "Scala"))
15+
}
16+
17+
@Test
18+
fun `In convention should work on a page as expected`() {
19+
assertTrue("Kotlin" in page)
20+
}
21+
22+
}
23+
24+
private class PageImpl<T>(val page: Int, val size: Int, vararg val elements: T) : Page<T> {
25+
override fun pageNumber(): Int = page
26+
override fun pageSize(): Int = size
27+
override fun elements(): MutableList<T> = mutableListOf(*elements)
28+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.baeldung.operators
2+
3+
import org.junit.Test
4+
import kotlin.test.assertEquals
5+
import kotlin.test.assertTrue
6+
7+
class PointTest {
8+
9+
private val p1 = Point(1, 2)
10+
private val p2 = Point(2, 3)
11+
12+
@Test
13+
fun `We should be able to add two points together using +`() {
14+
assertEquals(p1 + p2, Point(3, 5))
15+
}
16+
17+
@Test
18+
fun `We shoud be able to subtract one point from another using -`() {
19+
assertEquals(p1 - p2, Point(-1, -1))
20+
}
21+
22+
@Test
23+
fun `We should be able to multiply two points together with *`() {
24+
assertEquals(p1 * p2, Point(2, 6))
25+
}
26+
27+
@Test
28+
fun `We should be able to divide one point by another`() {
29+
assertEquals(p1 / p2, Point(0, 0))
30+
}
31+
32+
@Test
33+
fun `We should be able to scale a point by an integral factor`() {
34+
assertEquals(p1 * 2, Point(2, 4))
35+
assertEquals(2 * p1, Point(2, 4))
36+
}
37+
38+
@Test
39+
fun `We should be able to add points to an empty shape`() {
40+
val line = shape {
41+
+Point(0, 0)
42+
+Point(1, 3)
43+
}
44+
45+
assertTrue(Point(0, 0) in line.points)
46+
assertTrue(Point(1, 3) in line.points)
47+
}
48+
}

0 commit comments

Comments
 (0)