X-Git-Url: https://git.pterodactylus.net/?p=Sone.git;a=blobdiff_plain;f=src%2Ftest%2Fkotlin%2Fnet%2Fpterodactylus%2Fsone%2Futils%2FObjectsTest.kt;h=76cc72338c66b2a31dfdab08d587227bb4640f42;hp=9e96bfe647994cb28e981f12422db90ed358e831;hb=HEAD;hpb=5ef06b540267732918fa2119c8d9536ef81b0dfd diff --git a/src/test/kotlin/net/pterodactylus/sone/utils/ObjectsTest.kt b/src/test/kotlin/net/pterodactylus/sone/utils/ObjectsTest.kt index 9e96bfe..76cc723 100644 --- a/src/test/kotlin/net/pterodactylus/sone/utils/ObjectsTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/utils/ObjectsTest.kt @@ -1,8 +1,9 @@ package net.pterodactylus.sone.utils -import org.hamcrest.MatcherAssert.assertThat +import org.hamcrest.MatcherAssert.* import org.hamcrest.Matchers.* -import org.junit.Test +import java.util.concurrent.* +import kotlin.test.* /** * Unit test for Object utils. @@ -19,9 +20,11 @@ class ObjectsTest { assertThat(null.asList(), empty()) } - @Test(expected = IllegalArgumentException::class) + @Test fun `exception is thrown for null and true condition`() { - null.throwOnNullIf(true) { IllegalArgumentException() } + assertFailsWith(IllegalArgumentException::class) { + null.throwOnNullIf(true) { IllegalArgumentException() } + } } @Test @@ -41,4 +44,29 @@ class ObjectsTest { assertThat(any.throwOnNullIf(false) { IllegalArgumentException() }, equalTo(any)) } + @Test + fun `onNull is executed on null`() { + val called = CountDownLatch(1) + null.onNull { called.countDown() } + assertThat(called.count, equalTo(0L)) + } + + @Test + fun `onNull returns null when called on null`() { + assertThat(null.onNull {}, nullValue()) + } + + @Test + fun `onNull is not executed on non-null`() { + val called = CountDownLatch(1) + Any().onNull { called.countDown() } + assertThat(called.count, equalTo(1L)) + } + + @Test + fun `onNull returns object when called on non-null`() { + val any = Any() + assertThat(any.onNull {}, sameInstance(any)) + } + }