Merge pull request #69 from SELab1/testing

Testing
This commit is contained in:
brreynie 2023-04-17 21:57:22 +02:00 committed by GitHub Enterprise
commit b3466442b2
12 changed files with 329 additions and 49 deletions

View file

@ -1,16 +1,16 @@
package be.ugent.sel.studeez.data.local.models.timer_functional 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() { override fun tick() {
if (time.getTime() == 0) { if (time.time == 0) {
view = "Done!" view = DONE
} else { } else {
time.minOne() time.minOne()
} }
} }
override fun hasEnded(): Boolean { override fun hasEnded(): Boolean {
return time.getTime() == 0 return time.time == 0
} }
} }

View file

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

View file

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

View file

@ -1,19 +1,22 @@
package be.ugent.sel.studeez.data.local.models.timer_functional package be.ugent.sel.studeez.data.local.models.timer_functional
abstract class FunctionalTimer(initialValue: Int) { abstract class FunctionalTimer(initialValue: Int) {
protected val time: Time = Time(initialValue) val time: Time = Time(initialValue)
protected var view: String = "Focus" var view: String = FOCUS
fun getHoursMinutesSeconds(): HoursMinutesSeconds { fun getHoursMinutesSeconds(): HoursMinutesSeconds {
return time.getAsHMS() return time.getAsHMS()
} }
fun getViewString(): String {
return view
}
abstract fun tick() abstract fun tick()
abstract fun hasEnded(): Boolean 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

@ -2,7 +2,7 @@ package be.ugent.sel.studeez.data.local.models.timer_functional
class Time(initialTime: Int) { class Time(initialTime: Int) {
private var time = initialTime var time = initialTime
fun minOne() { fun minOne() {
time-- time--
@ -12,14 +12,6 @@ class Time(initialTime: Int) {
time++ time++
} }
fun setTime(newTime: Int) {
time = newTime
}
fun getTime(): Int {
return time
}
fun getAsHMS(): HoursMinutesSeconds { fun getAsHMS(): HoursMinutesSeconds {
val hours: Int = time / (60 * 60) val hours: Int = time / (60 * 60)
val minutes: Int = (time / (60)) % 60 val minutes: Int = (time / (60)) % 60

View file

@ -50,7 +50,7 @@ private fun Timer(viewModel: SessionViewModel = hiltViewModel()) {
fontSize = 80.sp fontSize = 80.sp
) )
Text( Text(
text = viewModel.getTimer().getViewString(), text = viewModel.getTimer().view,
modifier = Modifier.fillMaxWidth(), modifier = Modifier.fillMaxWidth(),
textAlign = TextAlign.Center, textAlign = TextAlign.Center,
fontWeight = FontWeight.Light, fontWeight = FontWeight.Light,

View file

@ -1,17 +0,0 @@
package be.ugent.sel.studeez
import org.junit.Test
import org.junit.Assert.*
/**
* Example local unit test, which will execute on the development machine (host).
*
* See [testing documentation](http://d.android.com/tools/testing).
*/
class ExampleUnitTest {
@Test
fun addition_isCorrect() {
assertEquals(4, 2 + 2)
}
}

View file

@ -0,0 +1,44 @@
package be.ugent.sel.studeez.timer_functional
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
import org.junit.Test
class FunctionalCustomTimerUnitTest : FunctionalTimerUnitTest() {
override fun setTimer() {
timer = FunctionalCustomTimer(time)
}
@Test
override fun testOneTick() {
timer.tick()
Assert.assertEquals(
time - 1,
timer.time.time,
)
}
@Test
override fun multipleTicks() {
val n = 10
for (i in 1..n) {
timer.tick()
}
Assert.assertEquals(
time - n,
timer.time.time,
)
}
@Test
override fun testEnded() {
timer = FunctionalCustomTimer(0)
timer.tick()
Assert.assertTrue(timer.hasEnded())
Assert.assertEquals(
FunctionalTimer.DONE,
timer.view
)
}
}

View file

@ -0,0 +1,46 @@
package be.ugent.sel.studeez.timer_functional
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
import org.junit.Test
class FunctionalEndlessTimerUnitTest : FunctionalTimerUnitTest() {
override fun setTimer() {
timer = FunctionalEndlessTimer()
}
@Test
override fun testOneTick() {
timer.tick()
Assert.assertEquals(
1,
timer.time.time
)
}
@Test
override fun multipleTicks() {
val n = 10
for (i in 1..n) {
timer.tick()
}
Assert.assertEquals(
n,
timer.time.time
)
}
@Test
override fun testEnded() {
val n = 1000
for (i in 1..n) {
timer.tick()
Assert.assertFalse(timer.hasEnded())
Assert.assertEquals(
FunctionalTimer.FOCUS,
timer.view
)
}
}
}

View file

@ -0,0 +1,33 @@
package be.ugent.sel.studeez.timer_functional
import be.ugent.sel.studeez.data.local.models.timer_functional.FunctionalTimer
import org.junit.Before
import org.junit.Test
abstract class FunctionalTimerUnitTest {
protected lateinit var timer: FunctionalTimer
protected open val hours = 4
protected open val minutes = 20
protected open val seconds = 39
protected var time: Int = 0
@Before
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()
@Test
abstract fun multipleTicks()
@Test
abstract fun testEnded()
}

View file

@ -0,0 +1,98 @@
package be.ugent.sel.studeez.timer_functional
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)
}
@Test
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,
)
}
@Test
override fun multipleTicks() {
val n = 10
for (i in 1..n) {
pomodoroTimer.tick()
}
Assert.assertEquals(
time - n,
pomodoroTimer.time.time
)
}
@Test
override fun testEnded() {
pomodoroTimer = FunctionalPomodoroTimer(0, 0, 0)
pomodoroTimer.tick()
Assert.assertTrue(pomodoroTimer.hasEnded())
Assert.assertEquals(
FunctionalTimer.DONE,
pomodoroTimer.view,
)
}
@Test
fun switchToBreak() {
for (i in 0..10) {
pomodoroTimer.tick()
}
Assert.assertFalse(pomodoroTimer.hasEnded())
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
)
}
}

View file

@ -0,0 +1,81 @@
package be.ugent.sel.studeez.timer_functional
import be.ugent.sel.studeez.data.local.models.timer_functional.HoursMinutesSeconds
import be.ugent.sel.studeez.data.local.models.timer_functional.Time
import org.junit.Assert
import org.junit.Before
import org.junit.Test
class TimeUnitTest {
private val hours = 4
private val minutes = 20
private val seconds = 39
private val time: Time = Time(seconds + minutes * 60 + hours * 60 * 60)
@Before
fun setup() {
}
@Test
fun formatTime() {
Assert.assertEquals(
HoursMinutesSeconds(
hours.toString().padStart(2, '0'),
minutes.toString().padStart(2, '0'),
seconds.toString().padStart(2, '0'),
),
time.getAsHMS(),
)
}
@Test
fun getTime() {
Assert.assertEquals(
seconds + minutes * 60 + hours * 60 * 60,
time.time,
)
}
@Test
fun minOne() {
time.minOne()
Assert.assertEquals(
(seconds + minutes * 60 + hours * 60 * 60) - 1,
time.time,
)
}
@Test
fun plusOne() {
time.plusOne()
Assert.assertEquals(
(seconds + minutes * 60 + hours * 60 * 60) + 1,
time.time,
)
}
@Test
fun minMultiple() {
val n = 10
for (i in 1 .. n) {
time.minOne()
}
Assert.assertEquals(
(seconds + minutes * 60 + hours * 60 * 60) - n,
time.time,
)
}
@Test
fun plusMultiple() {
val n = 10
for (i in 1 .. n) {
time.plusOne()
}
Assert.assertEquals(
(seconds + minutes * 60 + hours * 60 * 60) + n,
time.time,
)
}
}