1 package net.pterodactylus.sone.utils
3 import org.hamcrest.MatcherAssert.*
4 import org.hamcrest.Matchers.*
5 import java.util.concurrent.*
9 * Unit test for Object utils.
14 fun `non-null value is turned into a list with one element`() {
15 assertThat(5.asList(), contains(5))
19 fun `null value is turned into empty list`() {
20 assertThat(null.asList(), empty())
24 fun `exception is thrown for null and true condition`() {
25 assertFailsWith(IllegalArgumentException::class) {
26 null.throwOnNullIf(true) { IllegalArgumentException() }
31 fun `exception is not thrown for null and false condition`() {
32 assertThat(null.throwOnNullIf(false) { IllegalArgumentException() }, nullValue())
36 fun `exception is not thrown for any and true condition`() {
38 assertThat(any.throwOnNullIf(true) { IllegalArgumentException() }, equalTo(any))
42 fun `exception is not thrown for any and false condition`() {
44 assertThat(any.throwOnNullIf(false) { IllegalArgumentException() }, equalTo(any))
48 fun `onNull is executed on null`() {
49 val called = CountDownLatch(1)
50 null.onNull { called.countDown() }
51 assertThat(called.count, equalTo(0L))
55 fun `onNull returns null when called on null`() {
56 assertThat(null.onNull {}, nullValue())
60 fun `onNull is not executed on non-null`() {
61 val called = CountDownLatch(1)
62 Any().onNull { called.countDown() }
63 assertThat(called.count, equalTo(1L))
67 fun `onNull returns object when called on non-null`() {
69 assertThat(any.onNull {}, sameInstance(any))