Replace shortNumber() with formatNumber() (#3519)

formatNumber() was existing code to show numbers with suffixes like K, M, etc, so re-use that code and delete shortNumber().

Update the tests to (a) test formatNumber(), and (b) be parameterised.
This commit is contained in:
Nik Clayton 2023-06-10 16:29:26 +02:00 committed by GitHub
commit 071e00774e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 78 additions and 78 deletions

View file

@ -1,49 +1,70 @@
package com.keylesspalace.tusky.util
import org.junit.AfterClass
import org.junit.Assert
import org.junit.BeforeClass
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runners.Parameterized
import java.util.Locale
import kotlin.math.pow
class NumberUtilsTest {
@RunWith(Parameterized::class)
class NumberUtilsTest(private val input: Long, private val want: String) {
companion object {
/** Default locale before this test started */
private lateinit var locale: Locale
@Test
fun zeroShouldBeFormattedAsZero() {
val shortNumber = shortNumber(0)
Assert.assertEquals("0", shortNumber)
}
/**
* Ensure the Locale is ENGLISH so that tests against literal strings like
* "1.0M" later, even if the test host's locale is e.g. GERMAN which would
* normally report "1,0M".
*/
@BeforeClass
@JvmStatic
fun beforeClass() {
locale = Locale.getDefault()
Locale.setDefault(Locale.ENGLISH)
}
@Test
fun negativeValueShouldBeFormattedToNegativeValue() {
val shortNumber = shortNumber(-1)
Assert.assertEquals("-1", shortNumber)
}
@AfterClass
@JvmStatic
fun afterClass() {
Locale.setDefault(locale)
}
@Test
fun positiveValueShouldBeFormattedToPositiveValue() {
val shortNumber = shortNumber(1)
Assert.assertEquals("1", shortNumber)
}
@Test
fun bigNumbersShouldBeShortened() {
var shortNumber = 1L
Assert.assertEquals("1", shortNumber(shortNumber))
for (i in shortLetters.indices) {
if (i == 0) {
continue
}
shortNumber = 1000.0.pow(i.toDouble()).toLong()
Assert.assertEquals("1.0" + shortLetters[i], shortNumber(shortNumber))
@Parameterized.Parameters(name = "formatNumber_{0}")
@JvmStatic
fun data(): Iterable<Any> {
return listOf(
arrayOf(0, "0"),
arrayOf(1, "1"),
arrayOf(-1, "-1"),
arrayOf(999, "999"),
arrayOf(1000, "1.0K"),
arrayOf(1500, "1.5K"),
arrayOf(-1500, "-1.5K"),
arrayOf(1000.0.pow(2).toLong(), "1.0M"),
arrayOf(1000.0.pow(3).toLong(), "1.0G"),
arrayOf(1000.0.pow(4).toLong(), "1.0T"),
arrayOf(1000.0.pow(5).toLong(), "1.0P"),
arrayOf(1000.0.pow(6).toLong(), "1.0E"),
arrayOf(3, "3"),
arrayOf(35, "35"),
arrayOf(350, "350"),
arrayOf(3500, "3.5K"),
arrayOf(-3500, "-3.5K"),
arrayOf(3500 * 1000, "3.5M"),
arrayOf(3500 * 1000.0.pow(2).toLong(), "3.5G"),
arrayOf(3500 * 1000.0.pow(3).toLong(), "3.5T"),
arrayOf(3500 * 1000.0.pow(4).toLong(), "3.5P"),
arrayOf(3500 * 1000.0.pow(5).toLong(), "3.5E")
)
}
}
@Test
fun roundingForNegativeAndPositiveValuesShouldBeTheSame() {
var value = 3492
Assert.assertEquals("-3.5K", shortNumber(-value))
Assert.assertEquals("3.5K", shortNumber(value))
value = 1501
Assert.assertEquals("-1.5K", shortNumber(-value))
Assert.assertEquals("1.5K", shortNumber(value))
fun test() {
Assert.assertEquals(want, formatNumber(input, 1000))
}
}