-
Notifications
You must be signed in to change notification settings - Fork 662
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Only use fallback error messages for non-Spain Spanish (#7147)
* Only use fallback error messages for non-Spain Spanish * Add tests * Update changelog
- Loading branch information
1 parent
ca96453
commit de448cd
Showing
4 changed files
with
83 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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 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
61 changes: 61 additions & 0 deletions
61
payments-core/src/test/java/com/stripe/android/StripeErrorMappingTest.kt
This file contains 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,61 @@ | ||
package com.stripe.android | ||
|
||
import android.content.Context | ||
import androidx.test.core.app.ApplicationProvider | ||
import com.google.common.truth.Truth.assertThat | ||
import com.stripe.android.core.StripeError | ||
import com.stripe.android.networking.withLocalizedMessage | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
import org.robolectric.RobolectricTestRunner | ||
import java.util.Locale | ||
|
||
@RunWith(RobolectricTestRunner::class) | ||
class StripeErrorMappingTest { | ||
|
||
private val context: Context = ApplicationProvider.getApplicationContext() | ||
|
||
@Test | ||
fun `Uses backend message for non-Spanish locale`() { | ||
withLocale(Locale.US) { | ||
val error = StripeError( | ||
code = "incorrect_number", | ||
message = "The backend message", | ||
) | ||
val updatedError = error.withLocalizedMessage(context) | ||
assertThat(updatedError).isEqualTo(error) | ||
} | ||
} | ||
|
||
@Test | ||
fun `Uses backend message for Spanish locale from Spain`() { | ||
withLocale(Locale("es", "es")) { | ||
val error = StripeError( | ||
code = "incorrect_number", | ||
message = "The backend message", | ||
) | ||
val updatedError = error.withLocalizedMessage(context) | ||
assertThat(updatedError).isEqualTo(error) | ||
} | ||
} | ||
|
||
@Test | ||
fun `Uses client message for Spanish locale from outside Spain`() { | ||
withLocale(Locale("es", "ar")) { | ||
val error = StripeError( | ||
code = "incorrect_number", | ||
message = "The backend message", | ||
) | ||
val updatedError = error.withLocalizedMessage(context) | ||
assertThat(updatedError).isNotEqualTo(error) | ||
} | ||
} | ||
|
||
private inline fun <T> withLocale(locale: Locale, block: () -> T): T { | ||
val original = Locale.getDefault() | ||
Locale.setDefault(locale) | ||
val result = block() | ||
Locale.setDefault(original) | ||
return result | ||
} | ||
} |
This file contains 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