-
Notifications
You must be signed in to change notification settings - Fork 104
Allow capitalized input #512
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
bfda76d
check if word is capitalized for Translate and Plural command
catreedle c0c22f0
handle capitalized input for German translation
catreedle 3333d81
add handling for non German capitalized words
catreedle 507a563
normalize input for conjugation
catreedle 028af91
remove redundant lookup logic by using a variants list for translatio…
catreedle b9774fc
add unit tests for getTranslationDataForAWord
catreedle 5b42cb1
Minor comment fixes
andrewtavis File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,17 @@ | ||
| // SPDX-License-Identifier: GPL-3.0-or-later | ||
| package be.scri.helpers | ||
|
|
||
| /** | ||
| * Utility object for handling string-related operations. | ||
| */ | ||
| object StringUtils { | ||
| /** | ||
| * Checks if a word is capitalized (i.e., starts with an uppercase letter). | ||
| * @param word The word to check. | ||
| * @return `true` if the word is capitalized, `false` otherwise. | ||
| */ | ||
| fun isWordCapitalized(word: String): Boolean { | ||
| if (word.isEmpty()) return false | ||
| return word[0].isUpperCase() | ||
| } | ||
| } |
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
161 changes: 161 additions & 0 deletions
161
app/src/test/kotlin/be/scri/helpers/data/TranslationDataManagerTest.kt
This file contains hidden or 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,161 @@ | ||
| // SPDX-License-Identifier: GPL-3.0-or-later | ||
| package be.scri.helpers.data | ||
|
|
||
| import android.content.Context | ||
| import android.database.Cursor | ||
| import android.database.sqlite.SQLiteDatabase | ||
| import be.scri.helpers.DatabaseFileManager | ||
| import io.mockk.every | ||
| import io.mockk.mockk | ||
| import io.mockk.verify | ||
| import org.junit.jupiter.api.Assertions.assertEquals | ||
| import org.junit.jupiter.api.BeforeEach | ||
| import org.junit.jupiter.api.Test | ||
|
|
||
| class TranslationDataManagerTest { | ||
| private lateinit var context: Context | ||
| private lateinit var fileManager: DatabaseFileManager | ||
| private lateinit var db: SQLiteDatabase | ||
| private lateinit var manager: TranslationDataManager | ||
|
|
||
| @BeforeEach | ||
| fun setup() { | ||
| context = mockk(relaxed = true) | ||
| fileManager = mockk(relaxed = true) | ||
| db = mockk(relaxed = true) | ||
|
|
||
| manager = TranslationDataManager(context, fileManager) | ||
| } | ||
|
|
||
| private fun createCursorWithResult(result: String): Cursor = | ||
| mockk<Cursor>(relaxed = true) { | ||
| every { moveToFirst() } returns true | ||
| every { getColumnIndexOrThrow(any()) } returns 0 | ||
| every { getString(0) } returns result | ||
| } | ||
|
|
||
| private fun createEmptyCursor(): Cursor = | ||
| mockk<Cursor>(relaxed = true) { | ||
| every { moveToFirst() } returns false | ||
| } | ||
|
|
||
| @Test | ||
| fun `finds translation using lowercase variant when exact capitalized match fails`() { | ||
| every { fileManager.getTranslationDatabase() } returns db | ||
|
|
||
| // First query (exact "Book") fails, second query (lowercase "book") succeeds. | ||
| every { db.rawQuery(any(), any()) } returnsMany | ||
| listOf( | ||
| createEmptyCursor(), | ||
| createCursorWithResult("livre"), | ||
| ) | ||
|
|
||
| val result = manager.getTranslationDataForAWord("en" to "fr", "Book") | ||
|
|
||
| // Should recapitalize the result since input was capitalized. | ||
| assertEquals("Livre", result) | ||
| } | ||
|
|
||
| @Test | ||
| fun `does not recapitalize when input is lowercase`() { | ||
| every { fileManager.getTranslationDatabase() } returns db | ||
|
|
||
| // Exact match succeeds immediately. | ||
| every { db.rawQuery(any(), any()) } returns createCursorWithResult("livre") | ||
|
|
||
| val result = manager.getTranslationDataForAWord("en" to "fr", "book") | ||
|
|
||
| // Input was lowercase, so output stays lowercase. | ||
| assertEquals("livre", result) | ||
| } | ||
|
|
||
| @Test | ||
| fun `returns empty string when no variant matches`() { | ||
| every { fileManager.getTranslationDatabase() } returns db | ||
|
|
||
| // All queries fail. | ||
| every { db.rawQuery(any(), any()) } returns createEmptyCursor() | ||
|
|
||
| val result = manager.getTranslationDataForAWord("en" to "fr", "nonexistent") | ||
|
|
||
| assertEquals("", result) | ||
| } | ||
|
|
||
| @Test | ||
| fun `German capitalized input matches exact`() { | ||
| every { fileManager.getTranslationDatabase() } returns db | ||
|
|
||
| // User types "Buch", database has "Buch" - direct match. | ||
| every { db.rawQuery(any(), any()) } returns createCursorWithResult("book") | ||
|
|
||
| val result = manager.getTranslationDataForAWord("de" to "en", "Buch") | ||
|
|
||
| assertEquals("book", result) | ||
| } | ||
|
|
||
| @Test | ||
| fun `German source tries canonical capitalization as fallback`() { | ||
| every { fileManager.getTranslationDatabase() } returns db | ||
|
|
||
| // Simulate: exact "buch" fails, lowercase "buch" fails, canonical "Buch" succeeds. | ||
| every { db.rawQuery(any(), any()) } returnsMany | ||
| listOf( | ||
| createEmptyCursor(), | ||
| createEmptyCursor(), | ||
| createCursorWithResult("book"), | ||
| ) | ||
|
|
||
| val result = manager.getTranslationDataForAWord("de" to "en", "buch") | ||
|
|
||
| assertEquals("book", result) | ||
| } | ||
|
|
||
| @Test | ||
| fun `German capitalized verb finds translation via lowercase fallback`() { | ||
| every { fileManager.getTranslationDatabase() } returns db | ||
|
|
||
| // User types "Laufen" (capitalized verb), but database has "laufen" (lowercase). | ||
| every { db.rawQuery(any(), any()) } returnsMany | ||
| listOf( | ||
| createEmptyCursor(), | ||
| createCursorWithResult("run"), | ||
| ) | ||
|
|
||
| val result = manager.getTranslationDataForAWord("de" to "en", "Laufen") | ||
|
|
||
| // German doesn't recapitalize, returns result as-is. | ||
| assertEquals("run", result) | ||
| } | ||
|
|
||
| @Test | ||
| fun `returns original word when source and destination are the same`() { | ||
| // No database call should happen. | ||
| val result = manager.getTranslationDataForAWord("en" to "en", "Book") | ||
|
|
||
| assertEquals("Book", result) | ||
| verify(exactly = 0) { fileManager.getTranslationDatabase() } | ||
| } | ||
|
|
||
| @Test | ||
| fun `returns empty string when database is unavailable`() { | ||
| every { fileManager.getTranslationDatabase() } returns null | ||
|
|
||
| val result = manager.getTranslationDataForAWord("en" to "fr", "book") | ||
|
|
||
| assertEquals("", result) | ||
| } | ||
|
|
||
| @Test | ||
| fun `returns original word when source language is null`() { | ||
| val result = manager.getTranslationDataForAWord(null to "fr", "Book") | ||
|
|
||
| assertEquals("Book", result) | ||
| } | ||
|
|
||
| @Test | ||
| fun `returns original word when destination language is null`() { | ||
| val result = manager.getTranslationDataForAWord("en" to null, "Book") | ||
|
|
||
| assertEquals("Book", result) | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.