Locale-agnostic case conversions by default #KT-43023
This commit is contained in:
@@ -16,7 +16,7 @@ data class Item(val name: String, val rating: Int) : Comparable<Item> {
|
||||
|
||||
@SharedImmutable
|
||||
val STRING_CASE_INSENSITIVE_ORDER: Comparator<String> =
|
||||
compareBy { it: String -> it.toUpperCase() }.thenBy { it.toLowerCase() }.thenBy { it }
|
||||
compareBy { it: String -> it.uppercase() }.thenBy { it.lowercase() }.thenBy { it }
|
||||
|
||||
class OrderingTest {
|
||||
val v1 = Item("wine", 9)
|
||||
|
||||
@@ -1369,7 +1369,7 @@ class ArraysTest {
|
||||
assertEquals(mapOf("Alice" to 5, "Bob" to 3, "Carol" to 5), itemsWithTheirLength)
|
||||
|
||||
val updatedLength = items.copyOfRange(1, 3)
|
||||
.associateWithTo(itemsWithTheirLength.toMutableMap()) { name -> name.toLowerCase().count { it in "aeuio" } }
|
||||
.associateWithTo(itemsWithTheirLength.toMutableMap()) { name -> name.lowercase().count { it in "aeuio" } }
|
||||
|
||||
assertEquals(mapOf("Alice" to 5, "Bob" to 1, "Carol" to 2), updatedLength)
|
||||
}
|
||||
|
||||
@@ -489,7 +489,7 @@ class CollectionTest {
|
||||
assertEquals(mapOf("Alice" to 5, "Bob" to 3, "Carol" to 5), itemsWithTheirLength)
|
||||
|
||||
val updatedLength =
|
||||
items.drop(1).associateWithTo(itemsWithTheirLength.toMutableMap()) { name -> name.toLowerCase().count { it in "aeuio" }}
|
||||
items.drop(1).associateWithTo(itemsWithTheirLength.toMutableMap()) { name -> name.lowercase().count { it in "aeuio" }}
|
||||
|
||||
assertEquals(mapOf("Alice" to 5, "Bob" to 1, "Carol" to 2), updatedLength)
|
||||
}
|
||||
@@ -1099,7 +1099,7 @@ class CollectionTest {
|
||||
}
|
||||
|
||||
@Test fun sortedWith() {
|
||||
val comparator = compareBy<String> { it.toUpperCase().reversed() }
|
||||
val comparator = compareBy<String> { it.uppercase().reversed() }
|
||||
val data = listOf("cat", "dad", "BAD")
|
||||
|
||||
expect(listOf("BAD", "dad", "cat")) { data.sortedWith(comparator) }
|
||||
|
||||
@@ -485,7 +485,7 @@ abstract class IterableTests<T : Iterable<String>>(val createFrom: (Array<out St
|
||||
|
||||
@Test
|
||||
fun mapAndJoinToString() {
|
||||
val result = data.joinToString(separator = "-") { it.toUpperCase() }
|
||||
val result = data.joinToString(separator = "-") { it.uppercase() }
|
||||
assertEquals("FOO-BAR", result)
|
||||
}
|
||||
|
||||
|
||||
@@ -284,7 +284,7 @@ class MapTest {
|
||||
}
|
||||
|
||||
@Test fun createWithSelectorForKeyAndValue() {
|
||||
val map = listOf("a", "bb", "ccc").associateBy({ it.length }, { it.toUpperCase() })
|
||||
val map = listOf("a", "bb", "ccc").associateBy({ it.length }, { it.uppercase() })
|
||||
assertEquals(3, map.size)
|
||||
assertEquals("A", map[1])
|
||||
assertEquals("BB", map[2])
|
||||
@@ -292,7 +292,7 @@ class MapTest {
|
||||
}
|
||||
|
||||
@Test fun createWithPairSelector() {
|
||||
val map = listOf("a", "bb", "ccc").associate { it.length to it.toUpperCase() }
|
||||
val map = listOf("a", "bb", "ccc").associate { it.length to it.uppercase() }
|
||||
assertEquals(3, map.size)
|
||||
assertEquals("A", map[1])
|
||||
assertEquals("BB", map[2])
|
||||
|
||||
@@ -718,7 +718,7 @@ public class SequenceTest {
|
||||
assertEquals(mapOf("Alice" to 5, "Bob" to 3, "Carol" to 5), itemsWithTheirLength)
|
||||
|
||||
val updatedLength =
|
||||
items.drop(1).associateWithTo(itemsWithTheirLength.toMutableMap()) { name -> name.toLowerCase().count { it in "aeuio" }}
|
||||
items.drop(1).associateWithTo(itemsWithTheirLength.toMutableMap()) { name -> name.lowercase().count { it in "aeuio" }}
|
||||
|
||||
assertEquals(mapOf("Alice" to 5, "Bob" to 1, "Carol" to 2), updatedLength)
|
||||
}
|
||||
|
||||
@@ -56,7 +56,7 @@ class RegexTest {
|
||||
|
||||
@Test fun matchIgnoreCase() {
|
||||
for (input in listOf("ascii", "shrödinger"))
|
||||
assertTrue(input.toUpperCase().matches(input.toLowerCase().toRegex(RegexOption.IGNORE_CASE)))
|
||||
assertTrue(input.uppercase().matches(input.lowercase().toRegex(RegexOption.IGNORE_CASE)))
|
||||
}
|
||||
|
||||
@Test fun matchSequence() {
|
||||
|
||||
@@ -289,7 +289,7 @@ class StringNumberConversionTest {
|
||||
}
|
||||
|
||||
@Test fun shortToStringWithRadix() {
|
||||
assertEquals("7FFF", 0x7FFF.toShort().toString(radix = 16).toUpperCase())
|
||||
assertEquals("7FFF", 0x7FFF.toShort().toString(radix = 16).uppercase())
|
||||
assertEquals("-8000", (-0x8000).toShort().toString(radix = 16))
|
||||
assertEquals("-sfs", (-29180).toShort().toString(radix = 32))
|
||||
|
||||
@@ -328,7 +328,7 @@ class StringNumberConversionTest {
|
||||
}
|
||||
|
||||
@Test fun ushortToStringWithRadix() {
|
||||
assertEquals("7FFF", 0x7FFF.toUShort().toString(radix = 16).toUpperCase())
|
||||
assertEquals("7FFF", 0x7FFF.toUShort().toString(radix = 16).uppercase())
|
||||
assertEquals("8000", 0x8000.toUShort().toString(radix = 16))
|
||||
assertEquals("ffff", UShort.MAX_VALUE.toString(radix = 16))
|
||||
|
||||
|
||||
@@ -8,7 +8,6 @@ package test.text
|
||||
import kotlin.test.*
|
||||
import test.*
|
||||
import test.collections.behaviors.iteratorBehavior
|
||||
import test.collections.behaviors.setBehavior
|
||||
import test.collections.compare
|
||||
import kotlin.math.sign
|
||||
import kotlin.native.concurrent.SharedImmutable
|
||||
@@ -290,18 +289,26 @@ class StringTest {
|
||||
}
|
||||
|
||||
@Test fun capitalize() {
|
||||
assertEquals("A", "A".capitalize())
|
||||
assertEquals("A", "a".capitalize())
|
||||
assertEquals("Abcd", "abcd".capitalize())
|
||||
assertEquals("Abcd", "Abcd".capitalize())
|
||||
fun testCapitalize(expected: String, string: String) {
|
||||
assertEquals(expected, string.capitalize())
|
||||
assertEquals(expected, string.replaceFirstChar { it.uppercase() })
|
||||
}
|
||||
testCapitalize("A", "A")
|
||||
testCapitalize("A", "a")
|
||||
testCapitalize("Abcd", "abcd")
|
||||
testCapitalize("Abcd", "Abcd")
|
||||
}
|
||||
|
||||
@Test fun decapitalize() {
|
||||
assertEquals("a", "A".decapitalize())
|
||||
assertEquals("a", "a".decapitalize())
|
||||
assertEquals("abcd", "abcd".decapitalize())
|
||||
assertEquals("abcd", "Abcd".decapitalize())
|
||||
assertEquals("uRL", "URL".decapitalize())
|
||||
fun testDecapitalize(expected: String, string: String) {
|
||||
assertEquals(expected, string.decapitalize())
|
||||
assertEquals(expected, string.replaceFirstChar { it.lowercase() })
|
||||
}
|
||||
testDecapitalize("a", "A")
|
||||
testDecapitalize("a", "a")
|
||||
testDecapitalize("abcd", "abcd")
|
||||
testDecapitalize("abcd", "Abcd")
|
||||
testDecapitalize("uRL", "URL")
|
||||
}
|
||||
|
||||
@Test fun slice() {
|
||||
@@ -1450,7 +1457,7 @@ class StringTest {
|
||||
assertEquals("[v-e-r-y-l-o-n-g-s-t-r-oops]", result2)
|
||||
|
||||
val data3 = "a1/b".toList()
|
||||
val result3 = data3.joinToString() { it.toUpperCase().toString() }
|
||||
val result3 = data3.joinToString() { it.uppercase() }
|
||||
assertEquals("A, 1, /, B", result3)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user