Skip to content

Commit

Permalink
fix(ratings): Fix number of stars shown in VitaminRatings composables (
Browse files Browse the repository at this point in the history
  • Loading branch information
olivierperez authored Oct 9, 2023
1 parent 686a6b3 commit 94f29c4
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,22 @@ internal sealed class Icon(val imageVector: ImageVector) {
object Fill : Icon(imageVector = VitaminIcons.Fill.Star)

companion object {
private const val HALF = 0.5f
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 if (floor == index && decimal != 0f) {
if (decimal < HALF) Half else Fill
} else if (index < number) {
Fill
} else {
Empty
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
else -> Empty
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package com.decathlon.vitamin.compose.ratings

import org.junit.Assert.*
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))
}

@Test
fun close_above_int() {
val number = 3.2f

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 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))
}

@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))
}

@Test
fun close_above_half() {
val number = 3.6f

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 close_below_int() {
val number = 3.8f

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))
}
}

0 comments on commit 94f29c4

Please sign in to comment.