mirror of
https://github.com/bitwarden/android.git
synced 2025-12-10 09:56:45 -06:00
PM-17755: Fix comparator inconsistency based on Locale (#5762)
This commit is contained in:
parent
a972a40a49
commit
45e20d8c9e
@ -38,8 +38,9 @@ private fun compareCharsSpecialCharsWithPrecedence(c1: Char, c2: Char): Int {
|
||||
}
|
||||
|
||||
else -> {
|
||||
val upperCaseStr1 = c1.toString().uppercase(Locale.getDefault())
|
||||
val upperCaseStr2 = c2.toString().uppercase(Locale.getDefault())
|
||||
// Use Locale.ROOT for consistent, locale-insensitive comparison
|
||||
val upperCaseStr1 = c1.toString().uppercase(Locale.ROOT)
|
||||
val upperCaseStr2 = c2.toString().uppercase(Locale.ROOT)
|
||||
upperCaseStr1.compareTo(upperCaseStr2)
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,10 +1,25 @@
|
||||
package com.bitwarden.core.data.repository.util
|
||||
|
||||
import org.junit.Test
|
||||
import org.junit.jupiter.api.AfterEach
|
||||
import org.junit.jupiter.api.Assertions.assertEquals
|
||||
import org.junit.jupiter.api.BeforeEach
|
||||
import org.junit.jupiter.api.Test
|
||||
import java.util.Locale
|
||||
|
||||
class SpecialCharWithPrecedenceComparatorTest {
|
||||
|
||||
private lateinit var defaultLocale: Locale
|
||||
|
||||
@BeforeEach
|
||||
fun setup() {
|
||||
defaultLocale = Locale.getDefault()
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
fun tearDown() {
|
||||
Locale.setDefault(defaultLocale)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `Sorting with comparator should return expected result of sorted string`() {
|
||||
val unsortedList = listOf(
|
||||
@ -41,4 +56,22 @@ class SpecialCharWithPrecedenceComparatorTest {
|
||||
unsortedList.sortedWith(SpecialCharWithPrecedenceComparator),
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `comparator should return consistent values across locales`() {
|
||||
val unsortedList = listOf("i", "z", "j")
|
||||
val sortedList = listOf("i", "j", "z")
|
||||
val locales = listOf(
|
||||
Locale.forLanguageTag("tr-TR"),
|
||||
Locale.US,
|
||||
)
|
||||
|
||||
locales.forEach { locale ->
|
||||
Locale.setDefault(locale)
|
||||
assertEquals(
|
||||
sortedList,
|
||||
unsortedList.sortedWith(SpecialCharWithPrecedenceComparator),
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user