🚧 Add option for “strict filtering”
[Sone.git] / src / test / kotlin / net / pterodactylus / sone / core / PreferencesLoaderTest.kt
1 package net.pterodactylus.sone.core
2
3 import com.google.common.eventbus.*
4 import net.pterodactylus.sone.fcp.FcpInterface.*
5 import net.pterodactylus.util.config.*
6 import org.hamcrest.MatcherAssert.*
7 import org.hamcrest.Matchers.*
8 import org.junit.*
9
10 /**
11  * Unit test for [PreferencesLoader].
12  */
13 class PreferencesLoaderTest {
14
15         @Suppress("UnstableApiUsage")
16         private val eventBus = EventBus()
17         private val preferences = Preferences(eventBus)
18         private val configuration = Configuration(MapConfigurationBackend())
19         private val preferencesLoader = PreferencesLoader(preferences)
20
21         @Before
22         fun setupConfiguration() {
23                 setupIntValue("InsertionDelay", 15)
24                 setupIntValue("PostsPerPage", 25)
25                 setupIntValue("ImagesPerPage", 12)
26                 setupIntValue("CharactersPerPost", 150)
27                 setupIntValue("PostCutOffLength", 300)
28                 setupBooleanValue("RequireFullAccess", true)
29                 setupBooleanValue("ActivateFcpInterface", true)
30                 setupIntValue("FcpFullAccessRequired", 1)
31         }
32
33         private fun setupIntValue(optionName: String, value: Int) {
34                 configuration.getIntValue("Option/$optionName").value = value
35         }
36
37         private fun setupBooleanValue(optionName: String, value: Boolean) {
38                 configuration.getBooleanValue("Option/$optionName").value = value
39         }
40
41         @Test
42         fun `configuration is loaded correctly`() {
43                 preferencesLoader.loadFrom(configuration)
44                 assertThat(preferences.insertionDelay, equalTo(15))
45                 assertThat(preferences.postsPerPage, equalTo(25))
46                 assertThat(preferences.imagesPerPage, equalTo(12))
47                 assertThat(preferences.charactersPerPost, equalTo(150))
48                 assertThat(preferences.postCutOffLength, equalTo(300))
49                 assertThat(preferences.requireFullAccess, equalTo(true))
50                 assertThat(preferences.fcpInterfaceActive, equalTo(true))
51                 assertThat(preferences.fcpFullAccessRequired, equalTo(FullAccessRequired.WRITING))
52         }
53
54         @Test
55         fun `configuration is loaded correctly with cut off length minus one`() {
56                 setupIntValue("PostCutOffLength", -1)
57                 preferencesLoader.loadFrom(configuration)
58                 assertThat(preferences.postCutOffLength, not(equalTo(-1)))
59         }
60
61 }