diff --git a/core-kotlin/src/main/kotlin/com/baeldung/operators/Money.kt b/core-kotlin/src/main/kotlin/com/baeldung/operators/Money.kt index 1bb015ac27..93eb78c5b6 100644 --- a/core-kotlin/src/main/kotlin/com/baeldung/operators/Money.kt +++ b/core-kotlin/src/main/kotlin/com/baeldung/operators/Money.kt @@ -1,3 +1,5 @@ +package com.baeldung.operators + import java.math.BigDecimal enum class Currency { diff --git a/core-kotlin/src/main/kotlin/com/baeldung/operators/Page.kt b/core-kotlin/src/main/kotlin/com/baeldung/operators/Page.kt index db03779b35..1077eb94f9 100644 --- a/core-kotlin/src/main/kotlin/com/baeldung/operators/Page.kt +++ b/core-kotlin/src/main/kotlin/com/baeldung/operators/Page.kt @@ -1,3 +1,5 @@ +package com.baeldung.operators + interface Page { fun pageNumber(): Int fun pageSize(): Int diff --git a/core-kotlin/src/main/kotlin/com/baeldung/operators/Point.kt b/core-kotlin/src/main/kotlin/com/baeldung/operators/Point.kt index 22617a4ee6..e3282e64cc 100644 --- a/core-kotlin/src/main/kotlin/com/baeldung/operators/Point.kt +++ b/core-kotlin/src/main/kotlin/com/baeldung/operators/Point.kt @@ -1,3 +1,5 @@ +package com.baeldung.operators + data class Point(val x: Int, val y: Int) operator fun Point.unaryMinus() = Point(-x, -y) @@ -14,7 +16,7 @@ operator fun Point.times(factor: Int): Point = Point(x * factor, y * factor) operator fun Int.times(point: Point): Point = Point(point.x * this, point.y * this) class Shape { - private val points = mutableListOf() + val points = mutableListOf() operator fun Point.unaryPlus() { points.add(this) diff --git a/core-kotlin/src/main/kotlin/com/baeldung/operators/Utils.kt b/core-kotlin/src/main/kotlin/com/baeldung/operators/Utils.kt index ba33d3e7a2..0f16544f38 100644 --- a/core-kotlin/src/main/kotlin/com/baeldung/operators/Utils.kt +++ b/core-kotlin/src/main/kotlin/com/baeldung/operators/Utils.kt @@ -1,3 +1,5 @@ +package com.baeldung.operators + import java.math.BigInteger operator fun MutableCollection.plusAssign(element: T) { diff --git a/core-kotlin/src/test/kotlin/com/baeldung/operators/PageTest.kt b/core-kotlin/src/test/kotlin/com/baeldung/operators/PageTest.kt new file mode 100644 index 0000000000..4217fc0c08 --- /dev/null +++ b/core-kotlin/src/test/kotlin/com/baeldung/operators/PageTest.kt @@ -0,0 +1,28 @@ +package com.baeldung.operators + +import org.junit.Test +import kotlin.test.assertEquals +import kotlin.test.assertTrue + +class PageTest { + + private val page = PageImpl(1, 10, "Java", "Kotlin", "Scala") + + @Test + fun `Get convention should work as expected`() { + assertEquals(page[1], "Kotlin") + assertEquals(page[1, 3], listOf("Kotlin", "Scala")) + } + + @Test + fun `In convention should work on a page as expected`() { + assertTrue("Kotlin" in page) + } + +} + +private class PageImpl(val page: Int, val size: Int, vararg val elements: T) : Page { + override fun pageNumber(): Int = page + override fun pageSize(): Int = size + override fun elements(): MutableList = mutableListOf(*elements) +} \ No newline at end of file diff --git a/core-kotlin/src/test/kotlin/com/baeldung/operators/PointTest.kt b/core-kotlin/src/test/kotlin/com/baeldung/operators/PointTest.kt new file mode 100644 index 0000000000..168ab6431d --- /dev/null +++ b/core-kotlin/src/test/kotlin/com/baeldung/operators/PointTest.kt @@ -0,0 +1,48 @@ +package com.baeldung.operators + +import org.junit.Test +import kotlin.test.assertEquals +import kotlin.test.assertTrue + +class PointTest { + + private val p1 = Point(1, 2) + private val p2 = Point(2, 3) + + @Test + fun `We should be able to add two points together using +`() { + assertEquals(p1 + p2, Point(3, 5)) + } + + @Test + fun `We shoud be able to subtract one point from another using -`() { + assertEquals(p1 - p2, Point(-1, -1)) + } + + @Test + fun `We should be able to multiply two points together with *`() { + assertEquals(p1 * p2, Point(2, 6)) + } + + @Test + fun `We should be able to divide one point by another`() { + assertEquals(p1 / p2, Point(0, 0)) + } + + @Test + fun `We should be able to scale a point by an integral factor`() { + assertEquals(p1 * 2, Point(2, 4)) + assertEquals(2 * p1, Point(2, 4)) + } + + @Test + fun `We should be able to add points to an empty shape`() { + val line = shape { + +Point(0, 0) + +Point(1, 3) + } + + assertTrue(Point(0, 0) in line.points) + assertTrue(Point(1, 3) in line.points) + } +} \ No newline at end of file