Skip to content

Commit

Permalink
test(Settings): getters and setters use the same key
Browse files Browse the repository at this point in the history
  • Loading branch information
BrayanDSO committed Jan 14, 2025
1 parent 91a9281 commit b94d911
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 2 deletions.
6 changes: 4 additions & 2 deletions AnkiDroid/src/main/java/com/ichi2/anki/settings/Settings.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ object Settings {
defValue: Boolean,
): Boolean = prefs.getBoolean(key, defValue)

private fun putBoolean(
@VisibleForTesting
fun putBoolean(
key: String,
value: Boolean,
) {
Expand All @@ -45,7 +46,8 @@ object Settings {
defValue: String?,
): String? = prefs.getString(key, defValue)

private fun putString(
@VisibleForTesting
fun putString(
key: String,
value: String,
) {
Expand Down
73 changes: 73 additions & 0 deletions AnkiDroid/src/test/java/com/ichi2/anki/settings/SettingsTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* Copyright (c) 2025 Brayan Oliveira <[email protected]>
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation; either version 3 of the License, or (at your option) any later
* version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.ichi2.anki.settings

import com.github.ivanshafran.sharedpreferencesmock.SPMockBuilder
import com.ichi2.anki.AnkiDroidApp
import org.hamcrest.MatcherAssert.assertThat
import org.hamcrest.Matchers.equalTo
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.mockito.ArgumentMatchers.anyInt
import org.mockito.Mockito.anyBoolean
import org.mockito.Mockito.anyString
import org.mockito.Mockito.doAnswer
import org.mockito.Mockito.spy
import org.mockito.kotlin.whenever
import kotlin.reflect.KMutableProperty
import kotlin.reflect.KVisibility
import kotlin.reflect.full.memberProperties

class SettingsTest {
@BeforeEach
fun setup() {
AnkiDroidApp.sharedPreferencesTestingOverride =
SPMockBuilder().createSharedPreferences()
}

@Test
fun `getters and setters use the same key`() {
val settingsSpy = spy(Settings)
var key = ""

doAnswer { invocation ->
key = invocation.arguments[0] as String
invocation.callRealMethod()
}.run {
whenever(settingsSpy).getBoolean(anyString(), anyBoolean())
whenever(settingsSpy).putBoolean(anyString(), anyBoolean())
whenever(settingsSpy).getString(anyString(), anyString())
whenever(settingsSpy).putString(anyString(), anyString())
whenever(settingsSpy).getInt(anyString(), anyInt())
}

for (property in Settings::class.memberProperties) {
if (property.visibility != KVisibility.PUBLIC || property !is KMutableProperty<*>) continue

property.getter.call(settingsSpy)
val getterKey = key

when (property.returnType.classifier) {
Boolean::class -> property.setter.call(settingsSpy, false)
String::class -> property.setter.call(settingsSpy, "foo")
else -> continue
}
val setterKey = key

assertThat("The getter and setter of '${property.name}' use the same key", getterKey, equalTo(setterKey))
}
}
}

0 comments on commit b94d911

Please sign in to comment.