PM-17755: Fix comparator inconsistency based on Locale (#5762)

This commit is contained in:
David Perez 2025-08-20 15:20:03 -05:00 committed by GitHub
parent a972a40a49
commit 45e20d8c9e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 37 additions and 3 deletions

View File

@ -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)
}
}

View File

@ -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),
)
}
}
}