♻️ Generalize test methods
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 1 Mar 2019 20:07:56 +0000 (21:07 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 1 Mar 2019 20:07:56 +0000 (21:07 +0100)
src/test/kotlin/net/pterodactylus/sone/core/PreferencesTest.kt

index ca58c40..199604b 100644 (file)
@@ -311,16 +311,18 @@ class PreferencesTest {
 
        @Test
        fun `setting insertion delay to valid value sends change event`() {
-               preferences.newInsertionDelay = 30
-               verify(eventBus, atLeastOnce()).post(eventsCaptor.capture())
-               assertThat(eventsCaptor.allValues, hasItem(PreferenceChangedEvent("InsertionDelay", 30)))
+               testPreferencesChangedEvent("InsertionDelay", { preferences.newInsertionDelay = it }, 30)
        }
 
        @Test
        fun `setting posts per page to valid value sends change event`() {
-               preferences.newPostsPerPage = 30
+               testPreferencesChangedEvent("PostsPerPage", { preferences.newPostsPerPage = it }, 31)
+       }
+
+       private fun <T : Any> testPreferencesChangedEvent(name: String, setter: (T) -> Unit, value: T) {
+               setter(value)
                verify(eventBus, atLeastOnce()).post(eventsCaptor.capture())
-               assertThat(eventsCaptor.allValues, hasItem(PreferenceChangedEvent("PostsPerPage", 30)))
+               assertThat(eventsCaptor.allValues, hasItem(PreferenceChangedEvent(name, value)))
        }
 
 }