Replace unbookmark page with Kotlin version
[Sone.git] / src / test / kotlin / net / pterodactylus / sone / utils / OptionalsTest.kt
1 package net.pterodactylus.sone.utils
2
3 import com.google.common.base.Optional
4 import org.hamcrest.MatcherAssert.assertThat
5 import org.hamcrest.Matchers.equalTo
6 import org.hamcrest.Matchers.nullValue
7 import org.junit.Test
8 import java.util.concurrent.atomic.AtomicBoolean
9
10 /**
11  * Test for [Optional] utils.
12  */
13 class OptionalsTest {
14
15         @Test
16         fun `present optional can be transformed with let`() {
17                 val optional = Optional.of(1)
18                 assertThat(optional.let { it + 1 }, equalTo(2))
19         }
20
21         @Test
22         fun `empty optional is transform to null with let`() {
23                 val optional = Optional.absent<Int>()
24                 assertThat(optional.let { it + 1 }, nullValue())
25         }
26
27         @Test
28         fun `present optional can be processed with also`() {
29                 val called = AtomicBoolean(false)
30                 Optional.of(1).also { if (it == 1) called.set(true) }
31                 assertThat(called.get(), equalTo(true))
32         }
33
34         @Test
35         fun `absent optional is not processed with also`() {
36                 val called = AtomicBoolean(false)
37                 Optional.absent<Int>().also { called.set(true) }
38                 assertThat(called.get(), equalTo(false))
39         }
40
41         @Test
42         fun `1 as optional is correct optional`() {
43                 val optional = 1.asOptional()
44                 assertThat(optional.get(), equalTo(1))
45         }
46
47         @Test
48         fun `null as optional is asent optional`() {
49                 val optional = null.asOptional()
50                 assertThat(optional.isPresent, equalTo(false))
51         }
52
53 }