Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ratings): Rewrite tests to make them easier to maintain #178

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,18 @@ internal sealed class Icon(val imageVector: ImageVector) {
object Fill : Icon(imageVector = VitaminIcons.Fill.Star)

companion object {
private const val EMPTY_LOWER_BOUND = 0f
private const val EMPTY_UPPER_BOUND = 0.24f
private const val HALF_LOWER_BOUND = 0.25f
private const val HALF_UPPER_BOUND = 0.75f
private const val FILL_LOWER_BOUND = 0.76f
private const val FILL_UPPER_BOUND = 1f

fun get(index: Int, number: Float): Icon {
val floor = floor(number).toInt()
val decimal = number - index
return when {
index < floor -> Fill
decimal in EMPTY_LOWER_BOUND..EMPTY_UPPER_BOUND -> Empty
decimal in HALF_LOWER_BOUND..HALF_UPPER_BOUND -> Half
decimal in FILL_LOWER_BOUND..FILL_UPPER_BOUND -> Fill
decimal < HALF_LOWER_BOUND -> Empty
decimal <= HALF_UPPER_BOUND -> Half
decimal <= FILL_UPPER_BOUND -> Fill
else -> Empty
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,72 +1,70 @@
package com.decathlon.vitamin.compose.ratings

import org.junit.Assert.*
import org.junit.Assert.assertEquals
import org.junit.Test

class IconTest {
@Test
fun integer() {
val number = 3.0f

assertEquals(Icon.Fill, Icon.get(0, number))
assertEquals(Icon.Fill, Icon.get(1, number))
assertEquals(Icon.Fill, Icon.get(2, number))
assertEquals(Icon.Empty, Icon.get(3, number))
assertEquals(Icon.Empty, Icon.get(4, number))
assertStars(number = 3.0f, Icon.Fill, Icon.Fill, Icon.Fill, Icon.Empty, Icon.Empty)
}

@Test
fun close_above_int() {
val number = 3.2f
assertStars(number = 3.2f, Icon.Fill, Icon.Fill, Icon.Fill, Icon.Empty, Icon.Empty)
}

assertEquals(Icon.Fill, Icon.get(0, number))
assertEquals(Icon.Fill, Icon.get(1, number))
assertEquals(Icon.Fill, Icon.get(2, number))
assertEquals(Icon.Empty, Icon.get(3, number))
assertEquals(Icon.Empty, Icon.get(4, number))
@Test
fun lower_limit_of_half() {
assertStars(number = 3.25f, Icon.Fill, Icon.Fill, Icon.Fill, Icon.Half, Icon.Empty)
}

@Test
fun close_below_half() {
val number = 3.4f

assertEquals(Icon.Fill, Icon.get(0, number))
assertEquals(Icon.Fill, Icon.get(1, number))
assertEquals(Icon.Fill, Icon.get(2, number))
assertEquals(Icon.Half, Icon.get(3, number))
assertEquals(Icon.Empty, Icon.get(4, number))
assertStars(number = 3.4f, Icon.Fill, Icon.Fill, Icon.Fill, Icon.Half, Icon.Empty)
}

@Test
fun strictly_half() {
val number = 3.5f

assertEquals(Icon.Fill, Icon.get(0, number))
assertEquals(Icon.Fill, Icon.get(1, number))
assertEquals(Icon.Fill, Icon.get(2, number))
assertEquals(Icon.Half, Icon.get(3, number))
assertEquals(Icon.Empty, Icon.get(4, number))
assertStars(number = 3.5f, Icon.Fill, Icon.Fill, Icon.Fill, Icon.Half, Icon.Empty)
}

@Test
fun close_above_half() {
val number = 3.6f
assertStars(number = 3.6f, Icon.Fill, Icon.Fill, Icon.Fill, Icon.Half, Icon.Empty)
}

assertEquals(Icon.Fill, Icon.get(0, number))
assertEquals(Icon.Fill, Icon.get(1, number))
assertEquals(Icon.Fill, Icon.get(2, number))
assertEquals(Icon.Half, Icon.get(3, number))
assertEquals(Icon.Empty, Icon.get(4, number))
@Test
fun high_limit_of_half() {
assertStars(number = 3.75f, Icon.Fill, Icon.Fill, Icon.Fill, Icon.Half, Icon.Empty)
}

@Test
fun close_below_int() {
val number = 3.8f
assertStars(number = 3.8f, Icon.Fill, Icon.Fill, Icon.Fill, Icon.Fill, Icon.Empty)
}

@Test
fun close_edge_case_below_int() {
assertStars(number = 3.751f, Icon.Fill, Icon.Fill, Icon.Fill, Icon.Fill, Icon.Empty)
}

assertEquals(Icon.Fill, Icon.get(0, number))
assertEquals(Icon.Fill, Icon.get(1, number))
assertEquals(Icon.Fill, Icon.get(2, number))
assertEquals(Icon.Fill, Icon.get(3, number))
assertEquals(Icon.Empty, Icon.get(4, number))
/**
* Asserts that a given [number] produces an expected list of icons.
*/
@Suppress("LongParameterList")
private fun assertStars(
number: Float,
icon1: Icon,
icon2: Icon,
icon3: Icon,
icon4: Icon,
icon5: Icon,
) {
assertEquals("1st star of $number should be ${icon1::class.simpleName}", icon1, Icon.get(0, number))
assertEquals("2nd star of $number should be ${icon2::class.simpleName}", icon2, Icon.get(1, number))
assertEquals("3rd star of $number should be ${icon3::class.simpleName}", icon3, Icon.get(2, number))
assertEquals("4th star of $number should be ${icon4::class.simpleName}", icon4, Icon.get(3, number))
assertEquals("5th star of $number should be ${icon5::class.simpleName}", icon5, Icon.get(4, number))
}
}
Loading