🔀 Merge branch 'release/v82'
[Sone.git] / src / test / kotlin / net / pterodactylus / sone / utils / ObjectsTest.kt
1 package net.pterodactylus.sone.utils
2
3 import org.hamcrest.MatcherAssert.*
4 import org.hamcrest.Matchers.*
5 import java.util.concurrent.*
6 import kotlin.test.*
7
8 /**
9  * Unit test for Object utils.
10  */
11 class ObjectsTest {
12
13         @Test
14         fun `non-null value is turned into a list with one element`() {
15                 assertThat(5.asList(), contains(5))
16         }
17
18         @Test
19         fun `null value is turned into empty list`() {
20                 assertThat(null.asList(), empty())
21         }
22
23         @Test
24         fun `exception is thrown for null and true condition`() {
25                 assertFailsWith(IllegalArgumentException::class) {
26                         null.throwOnNullIf(true) { IllegalArgumentException() }
27                 }
28         }
29
30         @Test
31         fun `exception is not thrown for null and false condition`() {
32                 assertThat(null.throwOnNullIf(false) { IllegalArgumentException() }, nullValue())
33         }
34
35         @Test
36         fun `exception is not thrown for any and true condition`() {
37                 val any = Any()
38                 assertThat(any.throwOnNullIf(true) { IllegalArgumentException() }, equalTo(any))
39         }
40
41         @Test
42         fun `exception is not thrown for any and false condition`() {
43                 val any = Any()
44                 assertThat(any.throwOnNullIf(false) { IllegalArgumentException() }, equalTo(any))
45         }
46
47         @Test
48         fun `onNull is executed on null`() {
49                 val called = CountDownLatch(1)
50                 null.onNull { called.countDown() }
51                 assertThat(called.count, equalTo(0L))
52         }
53
54         @Test
55         fun `onNull returns null when called on null`() {
56                 assertThat(null.onNull {}, nullValue())
57         }
58
59         @Test
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))
64         }
65
66         @Test
67         fun `onNull returns object when called on non-null`() {
68                 val any = Any()
69                 assertThat(any.onNull {}, sameInstance(any))
70         }
71
72 }