feat: 添加 clampExpiryDays 纯函数钳制保质期天数为非负
This commit is contained in:
parent
8f24706d8c
commit
55d50c3e1d
@ -745,6 +745,17 @@ private fun ArrowRightIcon(color: Color, modifier: Modifier = Modifier) {
|
||||
|
||||
// region Helpers
|
||||
|
||||
/**
|
||||
* 将保质期天数钳制到合法范围 [0, +∞)。
|
||||
*
|
||||
* 天数语义上不能为负(到期日不应早于生产日期)。
|
||||
* 无论来自天数输入框还是日期选择器,写入 [ExpiryRow.days] 前都应经过此函数。
|
||||
*
|
||||
* @param days 原始天数
|
||||
* @return 钳制后的天数,最小为 0
|
||||
*/
|
||||
fun clampExpiryDays(days: Int): Int = days.coerceAtLeast(0)
|
||||
|
||||
private fun LocalDate.toEpochMillis(): Long =
|
||||
this.atStartOfDayIn(TimeZone.UTC).toEpochMilliseconds()
|
||||
|
||||
|
||||
@ -0,0 +1,29 @@
|
||||
package plus.rua.project.ui
|
||||
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertEquals
|
||||
|
||||
class DateCheckerScreenLogicTest {
|
||||
|
||||
// ---- clampExpiryDays ----
|
||||
|
||||
@Test
|
||||
fun clampExpiryDays_positiveValue_unchanged() {
|
||||
assertEquals(30, clampExpiryDays(30))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun clampExpiryDays_zero_unchanged() {
|
||||
assertEquals(0, clampExpiryDays(0))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun clampExpiryDays_negativeValue_clampedToZero() {
|
||||
assertEquals(0, clampExpiryDays(-1))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun clampExpiryDays_largeNegative_clampedToZero() {
|
||||
assertEquals(0, clampExpiryDays(-365))
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user