Skip to content
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

Correct alternate calendar handling #109

Merged
merged 2 commits into from
Mar 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ googleMaterial = "com.google.android.material:material:1.8.0"
icu4j = "com.ibm.icu:icu4j:72.1"
junit = "junit:junit:4.13.2"
kotlinPoet = "com.squareup:kotlinpoet:1.12.0"
testParameterInjector = "com.google.testparameterinjector:test-parameter-injector:1.10"
truth = "com.google.truth:truth:1.1.3"
coreLibraryDesugaring = "com.android.tools:desugar_jdk_libs:2.0.2"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,10 @@ private fun CodeBlock.Builder.addCalendarInstance(
applyBlock: (() -> Unit)? = null,
) {
val timeZoneReference = if (timeZoneId == null) "GMT_ZONE" else "getTimeZone($timeZoneId)"
add("%T.getInstance(%T.", Types.Calendar, Types.TimeZone)
add("$timeZoneReference)", *timeZoneIdArgs)
add("%T.getInstance(\n⇥", Types.Calendar)
addStatement("%T.$timeZoneReference,", Types.TimeZone, *timeZoneIdArgs)
addStatement("%T.Builder().setExtension('u', \"ca-iso8601\").build(),", Types.ULocale)
drewhamilton marked this conversation as resolved.
Show resolved Hide resolved
add("⇤)")

if (applyBlock != null) {
add(".apply·{\n⇥")
Expand Down Expand Up @@ -213,4 +215,5 @@ private object Types {
val Calendar = ClassName("android.icu.util", "Calendar")
val FormattedResource = ClassName("app.cash.paraphrase", "FormattedResource")
val TimeZone = ClassName("android.icu.util", "TimeZone")
val ULocale = ClassName("android.icu.util", "ULocale")
}
1 change: 1 addition & 0 deletions tests/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ dependencies {
implementation(libs.junit)
implementation(libs.truth)
implementation(libs.androidTestRunner)
implementation(libs.testParameterInjector)

coreLibraryDesugaring(libs.coreLibraryDesugaring)
}
56 changes: 49 additions & 7 deletions tests/src/main/kotlin/app/cash/paraphrase/tests/LocalesTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,68 @@ package app.cash.paraphrase.tests

import android.icu.util.ULocale
import androidx.test.platform.app.InstrumentationRegistry
import app.cash.paraphrase.FormattedResource
import app.cash.paraphrase.getString
import app.cash.paraphrase.tests.LocalesTest.TestLocale.en_SA
import app.cash.paraphrase.tests.LocalesTest.TestLocale.en_US
import com.google.common.truth.Truth.assertThat
import com.google.testing.junit.testparameterinjector.TestParameter
import com.google.testing.junit.testparameterinjector.TestParameterInjector
import java.time.LocalDate
import java.time.Month
import java.util.Locale
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith

class LocalesTest {
@RunWith(TestParameterInjector::class)
class LocalesTest(
@TestParameter private val testLocale: TestLocale,
) {
@get:Rule val localeRule = LocaleAndTimeZoneRule(
locale = Locale("en", "US"),
locale = testLocale.value,
)

private val context = InstrumentationRegistry.getInstrumentation().context
private val releaseDate = LocalDate.of(2022, Month.MARCH, 24)

@Test fun localeOverrides() {
val resource = FormattedResources.locale_date(releaseDate)
assertThat(context.getString(resource)).isEqualTo("A Mar 24, 2022 B")
assertThat(context.getString(resource, Locale.FRENCH)).isEqualTo("A 24 mars 2022 B")
assertThat(context.getString(resource, ULocale.GERMAN)).isEqualTo("A 24.03.2022 B")
/**
* Must be instantiated after [localeRule] has taken effect, to be sure we're testing with a
* Calendar that was created under the [testLocale].
*/
private lateinit var resource: FormattedResource

@Before fun instantiateResource() {
resource = FormattedResources.locale_date(releaseDate)
}

@Test fun defaultLocale() {
val expected = when (testLocale) {
en_US -> "Mar 24, 2022"
en_SA -> "Sha. 21, 1443 AH"
}
assertThat(context.getString(resource)).isEqualTo("A $expected B")
}

@Test fun franceLocale() {
assertThat(context.getString(resource, Locale.FRANCE)).isEqualTo("A 24 mars 2022 B")
}

@Test fun germanyULocale() {
assertThat(context.getString(resource, ULocale.GERMANY)).isEqualTo("A 24.03.2022 B")
}

@Test fun saudiArabiaLocale() {
assertThat(context.getString(resource, Locale.forLanguageTag("en-SA")))
.isEqualTo("A Sha. 21, 1443 AH B")
}

@Suppress("EnumEntryName", "unused")
enum class TestLocale(
val value: Locale,
) {
en_US(Locale("en", "US")),
en_SA(Locale("en", "SA")),
}
}