#57 finish FunctionalTimer unit tests

This commit is contained in:
reyniersbram 2023-04-17 18:46:42 +02:00
parent e60a96429c
commit 2b68373230
8 changed files with 171 additions and 21 deletions

View file

@ -1,10 +1,10 @@
package be.ugent.sel.studeez.data.local.models.timer_functional
class FunctionalCustomTimer(studyTime: Int): FunctionalTimer(studyTime) {
class FunctionalCustomTimer(studyTime: Int) : FunctionalTimer(studyTime) {
override fun tick() {
if (time.time == 0) {
view = "Done!"
view = DONE
} else {
time.minOne()
}

View file

@ -1,6 +1,6 @@
package be.ugent.sel.studeez.data.local.models.timer_functional
class FunctionalEndlessTimer() : FunctionalTimer(0){
class FunctionalEndlessTimer() : FunctionalTimer(0) {
override fun hasEnded(): Boolean {
return false

View file

@ -3,25 +3,25 @@ package be.ugent.sel.studeez.data.local.models.timer_functional
class FunctionalPomodoroTimer(
private var studyTime: Int,
private var breakTime: Int, repeats: Int
): FunctionalTimer(studyTime) {
) : FunctionalTimer(studyTime) {
private var breaksRemaining = repeats
private var isInBreak = false
var breaksRemaining = repeats
var isInBreak = false
override fun tick() {
if (time.time == 0 && breaksRemaining == 0){
view = "Done!"
if (time.time == 0 && breaksRemaining == 0) {
view = DONE
return
}
if (time.time == 0) {
if (isInBreak) {
breaksRemaining--
view = "Focus! ($breaksRemaining breaks remaining)"
view = FOCUS_REMAINING(breaksRemaining)
time.time = studyTime
} else {
view = "Take a break!"
time.time =breakTime
view = BREAK
time.time = breakTime
}
isInBreak = !isInBreak
}

View file

@ -2,7 +2,7 @@ package be.ugent.sel.studeez.data.local.models.timer_functional
abstract class FunctionalTimer(initialValue: Int) {
val time: Time = Time(initialValue)
var view: String = "Focus"
var view: String = FOCUS
fun getHoursMinutesSeconds(): HoursMinutesSeconds {
return time.getAsHMS()
@ -12,4 +12,11 @@ abstract class FunctionalTimer(initialValue: Int) {
abstract fun hasEnded(): Boolean
companion object {
const val FOCUS: String = "Focus"
const val DONE: String = "Done!"
const val BREAK: String = "Take a break!"
val FOCUS_REMAINING: (Int) -> String = { n -> "Focus! ($n breaks remaining)" }
}
}

View file

@ -1,10 +1,11 @@
package be.ugent.sel.studeez
import be.ugent.sel.studeez.data.local.models.timer_functional.FunctionalCustomTimer
import be.ugent.sel.studeez.data.local.models.timer_functional.FunctionalTimer
import org.junit.Assert
class FunctionalCustomTimerUnitTest: FunctionalTimerUnitTest() {
override fun setup() {
class FunctionalCustomTimerUnitTest : FunctionalTimerUnitTest() {
override fun setTimer() {
timer = FunctionalCustomTimer(time)
}
@ -18,7 +19,7 @@ class FunctionalCustomTimerUnitTest: FunctionalTimerUnitTest() {
override fun multipleTicks() {
val n = 10
for (i in 1 .. n) {
for (i in 1..n) {
timer.tick()
}
Assert.assertEquals(
@ -31,7 +32,7 @@ class FunctionalCustomTimerUnitTest: FunctionalTimerUnitTest() {
timer = FunctionalCustomTimer(0)
timer.tick()
Assert.assertEquals(
"Done!",
FunctionalTimer.DONE,
timer.view
)
}

View file

@ -0,0 +1,41 @@
package be.ugent.sel.studeez
import be.ugent.sel.studeez.data.local.models.timer_functional.FunctionalEndlessTimer
import be.ugent.sel.studeez.data.local.models.timer_functional.FunctionalTimer
import org.junit.Assert
class FunctionalEndlessTimerUnitTest : FunctionalTimerUnitTest() {
override fun setTimer() {
timer = FunctionalEndlessTimer()
}
override fun testOneTick() {
timer.tick()
Assert.assertEquals(
1,
timer.time.time
)
}
override fun multipleTicks() {
val n = 10
for (i in 1..n) {
timer.tick()
}
Assert.assertEquals(
n,
timer.time.time
)
}
override fun testEnded() {
val n = 1000
for (i in 1..n) {
timer.tick()
Assert.assertEquals(
FunctionalTimer.FOCUS,
timer.view
)
}
}
}

View file

@ -6,13 +6,21 @@ import org.junit.Test
abstract class FunctionalTimerUnitTest {
protected lateinit var timer: FunctionalTimer
private val hours = 4
private val minutes = 20
private val seconds = 39
protected val time = seconds + minutes * 60 + hours * 60 * 60
protected open val hours = 4
protected open val minutes = 20
protected open val seconds = 39
protected var time: Int = 0
@Before
abstract fun setup()
fun setup() {
time = seconds + minutes * 60 + hours * 60 * 60
setTimer()
}
/**
* The timer-property should be set to the right implementation in this method.
*/
abstract fun setTimer()
@Test
abstract fun testOneTick()

View file

@ -0,0 +1,93 @@
package be.ugent.sel.studeez
import be.ugent.sel.studeez.data.local.models.timer_functional.FunctionalPomodoroTimer
import be.ugent.sel.studeez.data.local.models.timer_functional.FunctionalTimer
import org.junit.Assert
import org.junit.Test
class FuntionalPomodoroTimerUnitTest : FunctionalTimerUnitTest() {
private val breakTime = 10
private val breaks = 2
override val hours = 0
override val minutes = 0
override val seconds = 10
private lateinit var pomodoroTimer: FunctionalPomodoroTimer
override fun setTimer() {
pomodoroTimer = FunctionalPomodoroTimer(time, breakTime, breaks)
}
override fun testOneTick() {
pomodoroTimer.tick()
Assert.assertEquals(
time - 1,
pomodoroTimer.time.time,
)
Assert.assertFalse(pomodoroTimer.isInBreak)
Assert.assertEquals(
breaks,
pomodoroTimer.breaksRemaining,
)
Assert.assertEquals(
FunctionalTimer.FOCUS,
pomodoroTimer.view,
)
}
override fun multipleTicks() {
val n = 10
for (i in 1..n) {
pomodoroTimer.tick()
}
Assert.assertEquals(
time - n,
pomodoroTimer.time.time
)
}
override fun testEnded() {
pomodoroTimer = FunctionalPomodoroTimer(0, 0, 0)
pomodoroTimer.tick()
Assert.assertEquals(
FunctionalTimer.DONE,
pomodoroTimer.view,
)
}
@Test
fun switchToBreak() {
for (i in 0..10) {
pomodoroTimer.tick()
}
Assert.assertTrue(pomodoroTimer.isInBreak)
Assert.assertEquals(
FunctionalTimer.BREAK,
pomodoroTimer.view
)
}
@Test
fun switchToStudying() {
for (i in 0..time) {
pomodoroTimer.tick()
}
Assert.assertTrue(pomodoroTimer.isInBreak)
Assert.assertEquals(
FunctionalTimer.BREAK,
pomodoroTimer.view
)
for (i in 0..breakTime) {
pomodoroTimer.tick()
}
Assert.assertFalse(pomodoroTimer.isInBreak)
val breaksRemaining = breaks - 1
Assert.assertEquals(
breaksRemaining,
pomodoroTimer.breaksRemaining
)
Assert.assertEquals(
FunctionalTimer.FOCUS_REMAINING(breaksRemaining),
pomodoroTimer.view
)
}
}