-
Notifications
You must be signed in to change notification settings - Fork 659
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix issues in expiry date visual transformation (#6411)
* Fix issues in expiry date visual transformation * Update ExpiryDateVisualTransformationTest * Update detekt baseline
- Loading branch information
1 parent
475f055
commit ece01bb
Showing
3 changed files
with
81 additions
and
35 deletions.
There are no files selected for viewing
48 changes: 39 additions & 9 deletions
48
...e/src/test/java/com/stripe/android/ui/core/elements/ExpiryDateVisualTransformationTest.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 |
---|---|---|
@@ -1,34 +1,64 @@ | ||
package com.stripe.android.ui.core.elements | ||
|
||
import androidx.compose.ui.text.AnnotatedString | ||
import com.google.common.truth.Truth | ||
import androidx.compose.ui.text.input.TransformedText | ||
import com.google.common.truth.Truth.assertThat | ||
import com.stripe.android.uicore.elements.ExpiryDateVisualTransformation | ||
import org.junit.Test | ||
|
||
internal class ExpiryDateVisualTransformationTest { | ||
|
||
private val transform = ExpiryDateVisualTransformation() | ||
|
||
@Test | ||
fun `verify 19 get separated between 1 and 9`() { | ||
Truth.assertThat(transform.filter(AnnotatedString("19")).text.text) | ||
.isEqualTo("1 / 9") | ||
val result = transform.filter(AnnotatedString("19")) | ||
assertThat(result.text.text).isEqualTo("1 / 9") | ||
assertCorrectMapping(original = "19", result) | ||
} | ||
|
||
@Test | ||
fun `verify 123 get separated between 2 and 3`() { | ||
Truth.assertThat(transform.filter(AnnotatedString("123")).text.text) | ||
.isEqualTo("12 / 3") | ||
val result = transform.filter(AnnotatedString("123")) | ||
assertThat(result.text.text).isEqualTo("12 / 3") | ||
assertCorrectMapping(original = "123", result) | ||
} | ||
|
||
@Test | ||
fun `verify 143 get separated between 1 and 4`() { | ||
val result = transform.filter(AnnotatedString("143")) | ||
assertThat(result.text.text).isEqualTo("1 / 43") | ||
assertCorrectMapping(original = "143", result) | ||
} | ||
|
||
@Test | ||
fun `verify 093 get separated between 9 and 3`() { | ||
Truth.assertThat(transform.filter(AnnotatedString("093")).text.text) | ||
.isEqualTo("09 / 3") | ||
val result = transform.filter(AnnotatedString("093")) | ||
assertThat(result.text.text).isEqualTo("09 / 3") | ||
assertCorrectMapping(original = "093", result) | ||
} | ||
|
||
@Test | ||
fun `verify 53 get separated between 5 and 3`() { | ||
Truth.assertThat(transform.filter(AnnotatedString("53")).text.text) | ||
.isEqualTo("5 / 3") | ||
val result = transform.filter(AnnotatedString("53")) | ||
assertThat(result.text.text).isEqualTo("5 / 3") | ||
assertCorrectMapping(original = "53", result) | ||
} | ||
|
||
private fun assertCorrectMapping( | ||
original: String, | ||
result: TransformedText, | ||
) { | ||
val transformed = result.text.text | ||
|
||
for (offset in 0..original.length) { | ||
val transformedOffset = result.offsetMapping.originalToTransformed(offset) | ||
assertThat(transformedOffset).isIn(0..transformed.length) | ||
} | ||
|
||
for (offset in 0..result.text.text.length) { | ||
val originalOffset = result.offsetMapping.transformedToOriginal(offset) | ||
assertThat(originalOffset).isIn(0..original.length) | ||
} | ||
} | ||
} |
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