Skip to content

Commit

Permalink
Add localized Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinBoulongne committed May 8, 2024
1 parent 30a7301 commit eded749
Showing 1 changed file with 56 additions and 21 deletions.
77 changes: 56 additions & 21 deletions src/test/kotlin/com/infomaniak/lib/core/FormatterFileSizeTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,42 +19,77 @@
package com.infomaniak.lib.core

import android.content.Context
import android.content.res.Configuration
import androidx.test.core.app.ApplicationProvider
import com.infomaniak.lib.core.utils.FormatterFileSize.formatShortFileSize
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import java.util.Locale

@RunWith(RobolectricTestRunner::class)
class FormatterFileSizeTest {

private val context = ApplicationProvider.getApplicationContext<Context>()

@Test
fun `1_890 short IEC`() {
val result = context.formatShortFileSize(bytes = 1_890L, valueOnly = true, shortValue = true, iecUnits = true)
println("=== Result is : [$result] ===")
assert(result == "1.8")
}
fun testAllFormatterFileSizePossibilities() {

@Test
fun `1_890 long IEC`() {
val result = context.formatShortFileSize(bytes = 1_890L, valueOnly = true, shortValue = false, iecUnits = true)
println("=== Result is : [$result] ===")
assert(result == "1.85")
}
val bytesList = listOf(1_890L)
val iecUnitsLists = listOf(true, false)
val shortValueList = listOf(true, false)
val valueOnlyList = listOf(true, false)
val localeList = listOf(Locale.ENGLISH, Locale.FRENCH)
val expectedList = mutableListOf(
"1.8",
"1.8",
"1.8 kB",
"1.8 Ko",
"1.85",
"1.85",
"1.85 kB",
"1.85 Ko",
"1.9",
"1.9",
"1.9 kB",
"1.9 Ko",
"1.89",
"1.89",
"1.89 kB",
"1.89 Ko",
)

@Test
fun `1_890 short SI`() {
val result = context.formatShortFileSize(bytes = 1_890L, valueOnly = true, shortValue = true, iecUnits = false)
println("=== Result is : [$result] ===")
assert(result == "1.9")
bytesList.forEach { bytes ->
iecUnitsLists.forEach { iecUnits ->
shortValueList.forEach { shortValue ->
valueOnlyList.forEach { valueOnly ->
localeList.forEach { locale ->

val localizedContext = createLocalizedContext(context, locale)
val result = localizedContext.formatShortFileSize(bytes, valueOnly, shortValue, iecUnits)

println(
"===> Result is ${"[$result]".padStart(length = 9)}. " +
"Parameters were [bytes: $bytes], " +
"[iecUnits: ${"$iecUnits".padStart(length = 5)}], " +
"[shortValue: ${"$shortValue".padStart(length = 5)}], " +
"[valueOnly: ${"$valueOnly".padStart(length = 5)}], " +
"[locale: $locale].",
)

val expected = expectedList.removeFirstOrNull()
assert(result == expected)
}
}
}
}
}

assert(expectedList.isEmpty())
}

@Test
fun `1_890 long SI`() {
val result = context.formatShortFileSize(bytes = 1_890L, valueOnly = true, shortValue = false, iecUnits = false)
println("=== Result is : [$result] ===")
assert(result == "1.89")
private fun createLocalizedContext(context: Context, locale: Locale): Context {
val localizedConfiguration = Configuration(context.resources.configuration).apply { setLocale(locale) }
return context.createConfigurationContext(localizedConfiguration)
}
}

0 comments on commit eded749

Please sign in to comment.