Added a few unit tests for kotlin operator examples.
This commit is contained in:
parent
1250605e19
commit
ad8ae556a8
|
@ -1,3 +1,5 @@
|
|||
package com.baeldung.operators
|
||||
|
||||
import java.math.BigDecimal
|
||||
|
||||
enum class Currency {
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
package com.baeldung.operators
|
||||
|
||||
interface Page<T> {
|
||||
fun pageNumber(): Int
|
||||
fun pageSize(): Int
|
||||
|
|
|
@ -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<Point>()
|
||||
val points = mutableListOf<Point>()
|
||||
|
||||
operator fun Point.unaryPlus() {
|
||||
points.add(this)
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
package com.baeldung.operators
|
||||
|
||||
import java.math.BigInteger
|
||||
|
||||
operator fun <T> MutableCollection<T>.plusAssign(element: T) {
|
||||
|
|
|
@ -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<T>(val page: Int, val size: Int, vararg val elements: T) : Page<T> {
|
||||
override fun pageNumber(): Int = page
|
||||
override fun pageSize(): Int = size
|
||||
override fun elements(): MutableList<T> = mutableListOf(*elements)
|
||||
}
|
|
@ -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)
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue