Skip to content

Instantly share code, notes, and snippets.

@AdamMc331
Last active June 24, 2021 00:26
Show Gist options
  • Select an option

  • Save AdamMc331/206b5c7890b99344c96f69f284fb27ed to your computer and use it in GitHub Desktop.

Select an option

Save AdamMc331/206b5c7890b99344c96f69f284fb27ed to your computer and use it in GitHub Desktop.
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
}
}
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")
}
}
@ngengesenior

Copy link
Copy Markdown

Great. The unit tests are comprehensive and simplified

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment