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=1c2d7f57f0210846340ab0147ad54e55246f8b65;hb=HEAD;hpb=db8c77f40b82bf516255318f20a824d676b4fa53 diff --git a/src/test/kotlin/net/pterodactylus/sone/utils/ObjectsTest.kt b/src/test/kotlin/net/pterodactylus/sone/utils/ObjectsTest.kt index 1c2d7f5..76cc723 100644 --- a/src/test/kotlin/net/pterodactylus/sone/utils/ObjectsTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/utils/ObjectsTest.kt @@ -1,9 +1,9 @@ package net.pterodactylus.sone.utils -import org.hamcrest.MatcherAssert.assertThat -import org.hamcrest.Matchers.contains -import org.hamcrest.Matchers.empty -import org.junit.Test +import org.hamcrest.MatcherAssert.* +import org.hamcrest.Matchers.* +import java.util.concurrent.* +import kotlin.test.* /** * Unit test for Object utils. @@ -20,4 +20,53 @@ class ObjectsTest { assertThat(null.asList(), empty()) } + @Test + fun `exception is thrown for null and true condition`() { + assertFailsWith(IllegalArgumentException::class) { + null.throwOnNullIf(true) { IllegalArgumentException() } + } + } + + @Test + fun `exception is not thrown for null and false condition`() { + assertThat(null.throwOnNullIf(false) { IllegalArgumentException() }, nullValue()) + } + + @Test + fun `exception is not thrown for any and true condition`() { + val any = Any() + assertThat(any.throwOnNullIf(true) { IllegalArgumentException() }, equalTo(any)) + } + + @Test + fun `exception is not thrown for any and false condition`() { + val any = Any() + 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)) + } + }