Last active
June 24, 2021 00:26
-
-
Save AdamMc331/206b5c7890b99344c96f69f284fb27ed to your computer and use it in GitHub Desktop.
This file contains hidden or 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
val cameroonNumberTranslator = object : OffsetMapping { | |
override fun originalToTransformed(offset: Int): Int { | |
// [offset [0 - 2] remain the same] | |
if (offset <= 2) return offset | |
// [3 - 5] transformed to [4 - 6] respectively | |
if (offset <= 5) return offset + 1 | |
// [6 - 8] transformed to [8 - 10] respectively | |
if (offset <= 8 ) return offset + 2 | |
return 11 // Total number of digits, plus two hyphens | |
} | |
override fun transformedToOriginal(offset: Int): Int { | |
if (offset <= 2) return offset | |
if (offset <= 6) return offset -1 | |
if (offset <= 10) return offset - 2 | |
return 9 // Total number of digits, without two hyphens | |
} | |
} |
This file contains hidden or 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
class PhoneNumberVisualTransformationTest { | |
private val transformation = PhoneNumberVisualTransformation() | |
@Test | |
fun transformJustZipCode() { | |
val input = "111" | |
val output = transformation.filter(AnnotatedString(input)) | |
assertThat(output.text.text).isEqualTo("111-") | |
} | |
@Test | |
fun transformZipCodeAndFirstThree() { | |
val input = "111222" | |
val output = transformation.filter(AnnotatedString(input)) | |
assertThat(output.text.text).isEqualTo("111-222-") | |
} | |
@Test | |
fun transformStandardPhoneNumber() { | |
val transformation = PhoneNumberVisualTransformation() | |
val text = "1112223333" | |
val output = transformation.filter(AnnotatedString(text)) | |
assertThat(output.text.text).isEqualTo("111-222-3333") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Great. The unit tests are comprehensive and simplified