Skip to content

Commit

Permalink
Use correct unit count for fallback formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
MiSikora committed Dec 27, 2020
1 parent c75f727 commit 431428d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ public class ImperialLengthFormatter internal constructor(
override fun Length<*>.format(unitSeparator: String, context: Context): String {
val parts = distance.abs().formatUnitParts(context, unitSeparator)
val noSignText = when {
parts.isEmpty() -> fallbackFormatter.format(0, context, unitSeparator)
parts.isEmpty() -> {
val fallbackUnitCount = distance.abs().toLength(fallbackFormatter.unit).measure.toLong()
fallbackFormatter.format(fallbackUnitCount, context, unitSeparator)
}
else -> parts.joinToString(partSeparator)
}
return when {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,4 +183,16 @@ internal class ImperialLengthFormatterTest {

formattedDistance shouldBe "10ميل 11ياردة 2قدم 7بوصة"
}

@Test fun `imperial formatter without specified parts has a fallback`() {
val distance = Distance.ofMiles(10) +
Distance.ofYards(11) +
Distance.ofFeet(2) +
Distance.ofInches(7)
val formatter = ImperialLengthFormatter.Builder().build()

val formattedDistance = distance.format(context, formatter = formatter, converter = null)

formattedDistance shouldBe "17611yd"
}
}

0 comments on commit 431428d

Please sign in to comment.