forked from ankidroid/Anki-Android
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(Settings): getters and setters use the same key
- Loading branch information
Showing
2 changed files
with
77 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
AnkiDroid/src/test/java/com/ichi2/anki/settings/SettingsTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} | ||
} | ||
} |