Move test sources from jvmTest to commonTest for KMP migration
This commit is contained in:
parent
22d289d6cb
commit
232be1440e
@ -27,8 +27,8 @@ import org.isoron.platform.time.getToday
|
||||
import org.isoron.platform.time.getWeekdaySequence
|
||||
import org.isoron.platform.time.resetToday
|
||||
import org.isoron.platform.time.setToday
|
||||
import org.junit.Assert.assertArrayEquals
|
||||
import org.junit.Test
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertContentEquals
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.test.assertFailsWith
|
||||
import kotlin.test.assertTrue
|
||||
@ -102,22 +102,22 @@ class DatesTest {
|
||||
@Test
|
||||
fun testCountWeekdayOccurrencesInMonth() {
|
||||
// February 2018 (28 days, starts on Thursday)
|
||||
assertArrayEquals(
|
||||
assertContentEquals(
|
||||
arrayOf(4, 4, 4, 4, 4, 4, 4),
|
||||
countWeekdayOccurrencesInMonth(LocalDate(2018, 2, 1))
|
||||
)
|
||||
// February 2020 (leap, 29 days, starts on Saturday)
|
||||
assertArrayEquals(
|
||||
assertContentEquals(
|
||||
arrayOf(5, 4, 4, 4, 4, 4, 4),
|
||||
countWeekdayOccurrencesInMonth(LocalDate(2020, 2, 1))
|
||||
)
|
||||
// April 2020 (30 days, starts on Wednesday)
|
||||
assertArrayEquals(
|
||||
assertContentEquals(
|
||||
arrayOf(4, 4, 4, 4, 5, 5, 4),
|
||||
countWeekdayOccurrencesInMonth(LocalDate(2020, 4, 1))
|
||||
)
|
||||
// August 2020 (31 days, starts on Saturday)
|
||||
assertArrayEquals(
|
||||
assertContentEquals(
|
||||
arrayOf(5, 5, 5, 4, 4, 4, 4),
|
||||
countWeekdayOccurrencesInMonth(LocalDate(2020, 8, 1))
|
||||
)
|
||||
@ -0,0 +1,55 @@
|
||||
/*
|
||||
* Copyright (C) 2016-2025 Álinson Santos Xavier <git@axavier.org>
|
||||
*
|
||||
* This file is part of Loop Habit Tracker.
|
||||
*
|
||||
* Loop Habit Tracker is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by the
|
||||
* Free Software Foundation, either version 3 of the License, or (at your
|
||||
* option) any later version.
|
||||
*
|
||||
* Loop Habit Tracker is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||
* more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package org.isoron.uhabits.core
|
||||
|
||||
import org.isoron.platform.io.TestDatabaseHelper
|
||||
import org.isoron.platform.time.LocalDate
|
||||
import org.isoron.platform.time.setToday
|
||||
import org.isoron.uhabits.core.commands.CommandRunner
|
||||
import org.isoron.uhabits.core.models.HabitList
|
||||
import org.isoron.uhabits.core.models.ModelFactory
|
||||
import org.isoron.uhabits.core.models.memory.MemoryModelFactory
|
||||
import org.isoron.uhabits.core.tasks.SingleThreadTaskRunner
|
||||
import org.isoron.uhabits.core.test.HabitFixtures
|
||||
import kotlin.test.BeforeTest
|
||||
|
||||
open class BaseUnitTest {
|
||||
protected open lateinit var habitList: HabitList
|
||||
protected lateinit var fixtures: HabitFixtures
|
||||
protected lateinit var modelFactory: ModelFactory
|
||||
protected lateinit var taskRunner: SingleThreadTaskRunner
|
||||
protected open lateinit var commandRunner: CommandRunner
|
||||
|
||||
@BeforeTest
|
||||
open fun setUp() {
|
||||
setToday(LocalDate(2015, 1, 25))
|
||||
val memoryModelFactory = MemoryModelFactory()
|
||||
habitList = memoryModelFactory.buildHabitList()
|
||||
fixtures = HabitFixtures(memoryModelFactory, habitList)
|
||||
modelFactory = memoryModelFactory
|
||||
taskRunner = SingleThreadTaskRunner()
|
||||
commandRunner = CommandRunner(taskRunner)
|
||||
}
|
||||
|
||||
companion object {
|
||||
fun buildMemoryDatabase(): org.isoron.platform.io.Database {
|
||||
return TestDatabaseHelper.createEmptyDatabase()
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -20,8 +20,8 @@ package org.isoron.uhabits.core.commands
|
||||
|
||||
import org.isoron.uhabits.core.BaseUnitTest
|
||||
import org.isoron.uhabits.core.models.Habit
|
||||
import org.junit.Before
|
||||
import org.junit.Test
|
||||
import kotlin.test.BeforeTest
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertFalse
|
||||
import kotlin.test.assertTrue
|
||||
|
||||
@ -29,8 +29,7 @@ class ArchiveHabitsCommandTest : BaseUnitTest() {
|
||||
private lateinit var command: ArchiveHabitsCommand
|
||||
private lateinit var habit: Habit
|
||||
|
||||
@Before
|
||||
@Throws(Exception::class)
|
||||
@BeforeTest
|
||||
override fun setUp() {
|
||||
super.setUp()
|
||||
habit = fixtures.createShortHabit()
|
||||
@ -18,24 +18,21 @@
|
||||
*/
|
||||
package org.isoron.uhabits.core.commands
|
||||
|
||||
import org.hamcrest.CoreMatchers.equalTo
|
||||
import org.hamcrest.MatcherAssert.assertThat
|
||||
import org.isoron.uhabits.core.BaseUnitTest
|
||||
import org.isoron.uhabits.core.models.Habit
|
||||
import org.isoron.uhabits.core.models.PaletteColor
|
||||
import org.junit.Before
|
||||
import org.junit.Test
|
||||
import java.util.LinkedList
|
||||
import kotlin.test.BeforeTest
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertEquals
|
||||
|
||||
class ChangeHabitColorCommandTest : BaseUnitTest() {
|
||||
private lateinit var command: ChangeHabitColorCommand
|
||||
private lateinit var selected: LinkedList<Habit>
|
||||
private lateinit var selected: MutableList<Habit>
|
||||
|
||||
@Before
|
||||
@Throws(Exception::class)
|
||||
@BeforeTest
|
||||
override fun setUp() {
|
||||
super.setUp()
|
||||
selected = LinkedList()
|
||||
selected = mutableListOf()
|
||||
for (i in 0..2) {
|
||||
val habit = fixtures.createShortHabit()
|
||||
habit.color = PaletteColor(i + 1)
|
||||
@ -54,13 +51,13 @@ class ChangeHabitColorCommandTest : BaseUnitTest() {
|
||||
|
||||
private fun checkNewColors() {
|
||||
for (habit in selected) {
|
||||
assertThat(habit.color, equalTo(PaletteColor(0)))
|
||||
assertEquals(PaletteColor(0), habit.color)
|
||||
}
|
||||
}
|
||||
|
||||
private fun checkOriginalColors() {
|
||||
var k = 0
|
||||
for (habit in selected)
|
||||
assertThat(habit.color, equalTo(PaletteColor(++k)))
|
||||
assertEquals(PaletteColor(++k), habit.color)
|
||||
}
|
||||
}
|
||||
@ -18,22 +18,20 @@
|
||||
*/
|
||||
package org.isoron.uhabits.core.commands
|
||||
|
||||
import org.hamcrest.CoreMatchers.equalTo
|
||||
import org.hamcrest.MatcherAssert.assertThat
|
||||
import org.isoron.uhabits.core.BaseUnitTest
|
||||
import org.isoron.uhabits.core.models.Habit
|
||||
import org.isoron.uhabits.core.models.Reminder
|
||||
import org.isoron.uhabits.core.models.WeekdayList
|
||||
import org.junit.Before
|
||||
import org.junit.Test
|
||||
import kotlin.test.BeforeTest
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.test.assertTrue
|
||||
|
||||
class CreateHabitCommandTest : BaseUnitTest() {
|
||||
private lateinit var command: CreateHabitCommand
|
||||
private lateinit var model: Habit
|
||||
|
||||
@Before
|
||||
@Throws(Exception::class)
|
||||
@BeforeTest
|
||||
override fun setUp() {
|
||||
super.setUp()
|
||||
model = fixtures.createEmptyHabit()
|
||||
@ -46,8 +44,8 @@ class CreateHabitCommandTest : BaseUnitTest() {
|
||||
fun testExecute() {
|
||||
assertTrue(habitList.isEmpty)
|
||||
command.run()
|
||||
assertThat(habitList.size(), equalTo(1))
|
||||
assertEquals(1, habitList.size())
|
||||
val habit = habitList.getByPosition(0)
|
||||
assertThat(habit.name, equalTo(model.name))
|
||||
assertEquals(model.name, habit.name)
|
||||
}
|
||||
}
|
||||
@ -23,8 +23,8 @@ import org.isoron.platform.time.getToday
|
||||
import org.isoron.uhabits.core.BaseUnitTest
|
||||
import org.isoron.uhabits.core.models.Entry
|
||||
import org.isoron.uhabits.core.models.Habit
|
||||
import org.junit.Before
|
||||
import org.junit.Test
|
||||
import kotlin.test.BeforeTest
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertEquals
|
||||
|
||||
class CreateRepetitionCommandTest : BaseUnitTest() {
|
||||
@ -32,8 +32,7 @@ class CreateRepetitionCommandTest : BaseUnitTest() {
|
||||
private lateinit var habit: Habit
|
||||
private lateinit var today: LocalDate
|
||||
|
||||
@Before
|
||||
@Throws(Exception::class)
|
||||
@BeforeTest
|
||||
override fun setUp() {
|
||||
super.setUp()
|
||||
habit = fixtures.createShortHabit()
|
||||
@ -18,23 +18,20 @@
|
||||
*/
|
||||
package org.isoron.uhabits.core.commands
|
||||
|
||||
import org.hamcrest.CoreMatchers.equalTo
|
||||
import org.hamcrest.MatcherAssert.assertThat
|
||||
import org.isoron.uhabits.core.BaseUnitTest
|
||||
import org.isoron.uhabits.core.models.Habit
|
||||
import org.junit.Before
|
||||
import org.junit.Test
|
||||
import java.util.*
|
||||
import kotlin.test.BeforeTest
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertEquals
|
||||
|
||||
class DeleteHabitsCommandTest : BaseUnitTest() {
|
||||
private lateinit var command: DeleteHabitsCommand
|
||||
private lateinit var selected: LinkedList<Habit>
|
||||
private lateinit var selected: MutableList<Habit>
|
||||
|
||||
@Before
|
||||
@Throws(Exception::class)
|
||||
@BeforeTest
|
||||
override fun setUp() {
|
||||
super.setUp()
|
||||
selected = LinkedList()
|
||||
selected = mutableListOf()
|
||||
|
||||
// Habits that should be deleted
|
||||
for (i in 0..2) {
|
||||
@ -52,9 +49,9 @@ class DeleteHabitsCommandTest : BaseUnitTest() {
|
||||
|
||||
@Test
|
||||
fun testExecute() {
|
||||
assertThat(habitList.size(), equalTo(4))
|
||||
assertEquals(4, habitList.size())
|
||||
command.run()
|
||||
assertThat(habitList.size(), equalTo(1))
|
||||
assertThat(habitList.getByPosition(0).name, equalTo("extra"))
|
||||
assertEquals(1, habitList.size())
|
||||
assertEquals("extra", habitList.getByPosition(0).name)
|
||||
}
|
||||
}
|
||||
@ -18,15 +18,14 @@
|
||||
*/
|
||||
package org.isoron.uhabits.core.commands
|
||||
|
||||
import org.hamcrest.MatcherAssert.assertThat
|
||||
import org.hamcrest.Matchers.equalTo
|
||||
import org.isoron.platform.time.LocalDate
|
||||
import org.isoron.platform.time.getToday
|
||||
import org.isoron.uhabits.core.BaseUnitTest
|
||||
import org.isoron.uhabits.core.models.Frequency
|
||||
import org.isoron.uhabits.core.models.Habit
|
||||
import org.junit.Before
|
||||
import org.junit.Test
|
||||
import kotlin.test.BeforeTest
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertEquals
|
||||
|
||||
class EditHabitCommandTest : BaseUnitTest() {
|
||||
private lateinit var command: EditHabitCommand
|
||||
@ -34,8 +33,7 @@ class EditHabitCommandTest : BaseUnitTest() {
|
||||
private lateinit var modified: Habit
|
||||
private lateinit var today: LocalDate
|
||||
|
||||
@Before
|
||||
@Throws(Exception::class)
|
||||
@BeforeTest
|
||||
override fun setUp() {
|
||||
super.setUp()
|
||||
habit = fixtures.createShortHabit()
|
||||
@ -54,9 +52,9 @@ class EditHabitCommandTest : BaseUnitTest() {
|
||||
fun testExecute() {
|
||||
command = EditHabitCommand(habitList, habit.id!!, modified)
|
||||
val originalScore = habit.scores[today].value
|
||||
assertThat(habit.name, equalTo("original"))
|
||||
assertEquals("original", habit.name)
|
||||
command.run()
|
||||
assertThat(habit.name, equalTo("modified"))
|
||||
assertThat(habit.scores[today].value, equalTo(originalScore))
|
||||
assertEquals("modified", habit.name)
|
||||
assertEquals(originalScore, habit.scores[today].value)
|
||||
}
|
||||
}
|
||||
@ -20,8 +20,8 @@ package org.isoron.uhabits.core.commands
|
||||
|
||||
import org.isoron.uhabits.core.BaseUnitTest
|
||||
import org.isoron.uhabits.core.models.Habit
|
||||
import org.junit.Before
|
||||
import org.junit.Test
|
||||
import kotlin.test.BeforeTest
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertFalse
|
||||
import kotlin.test.assertTrue
|
||||
|
||||
@ -29,8 +29,7 @@ class UnarchiveHabitsCommandTest : BaseUnitTest() {
|
||||
private lateinit var command: UnarchiveHabitsCommand
|
||||
private lateinit var habit: Habit
|
||||
|
||||
@Before
|
||||
@Throws(Exception::class)
|
||||
@BeforeTest
|
||||
override fun setUp() {
|
||||
super.setUp()
|
||||
habit = fixtures.createShortHabit()
|
||||
@ -19,16 +19,15 @@
|
||||
|
||||
package org.isoron.uhabits.core.models
|
||||
|
||||
import org.hamcrest.MatcherAssert.assertThat
|
||||
import org.hamcrest.core.IsEqual.equalTo
|
||||
import org.isoron.platform.time.LocalDate
|
||||
import org.isoron.platform.time.TruncateField
|
||||
import org.isoron.uhabits.core.models.Entry.Companion.NO
|
||||
import org.isoron.uhabits.core.models.Entry.Companion.UNKNOWN
|
||||
import org.isoron.uhabits.core.models.Entry.Companion.YES_AUTO
|
||||
import org.isoron.uhabits.core.models.Entry.Companion.YES_MANUAL
|
||||
import org.junit.Test
|
||||
import kotlin.random.Random
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertContentEquals
|
||||
import kotlin.test.assertEquals
|
||||
|
||||
class EntryListTest {
|
||||
@ -146,27 +145,27 @@ class EntryListTest {
|
||||
truncateField = TruncateField.MONTH,
|
||||
isNumerical = true
|
||||
)
|
||||
assertThat(byMonth.size, equalTo(17))
|
||||
assertThat(byMonth[0], equalTo(Entry(LocalDate(2014, 6, 1), 230)))
|
||||
assertThat(byMonth[6], equalTo(Entry(LocalDate(2013, 12, 1), 1988)))
|
||||
assertThat(byMonth[12], equalTo(Entry(LocalDate(2013, 5, 1), 1271)))
|
||||
assertEquals(17, byMonth.size)
|
||||
assertEquals(Entry(LocalDate(2014, 6, 1), 230), byMonth[0])
|
||||
assertEquals(Entry(LocalDate(2013, 12, 1), 1988), byMonth[6])
|
||||
assertEquals(Entry(LocalDate(2013, 5, 1), 1271), byMonth[12])
|
||||
|
||||
val byQuarter = entries.getKnown().groupedSum(
|
||||
truncateField = TruncateField.QUARTER,
|
||||
isNumerical = true
|
||||
)
|
||||
assertThat(byQuarter.size, equalTo(6))
|
||||
assertThat(byQuarter[0], equalTo(Entry(LocalDate(2014, 4, 1), 3263)))
|
||||
assertThat(byQuarter[3], equalTo(Entry(LocalDate(2013, 7, 1), 3838)))
|
||||
assertThat(byQuarter[5], equalTo(Entry(LocalDate(2013, 1, 1), 4975)))
|
||||
assertEquals(6, byQuarter.size)
|
||||
assertEquals(Entry(LocalDate(2014, 4, 1), 3263), byQuarter[0])
|
||||
assertEquals(Entry(LocalDate(2013, 7, 1), 3838), byQuarter[3])
|
||||
assertEquals(Entry(LocalDate(2013, 1, 1), 4975), byQuarter[5])
|
||||
|
||||
val byYear = entries.getKnown().groupedSum(
|
||||
truncateField = TruncateField.YEAR,
|
||||
isNumerical = true
|
||||
)
|
||||
assertThat(byYear.size, equalTo(2))
|
||||
assertThat(byYear[0], equalTo(Entry(LocalDate(2014, 1, 1), 8227)))
|
||||
assertThat(byYear[1], equalTo(Entry(LocalDate(2013, 1, 1), 16172)))
|
||||
assertEquals(2, byYear.size)
|
||||
assertEquals(Entry(LocalDate(2014, 1, 1), 8227), byYear[0])
|
||||
assertEquals(Entry(LocalDate(2013, 1, 1), 16172), byYear[1])
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -190,27 +189,27 @@ class EntryListTest {
|
||||
truncateField = TruncateField.MONTH,
|
||||
isNumerical = false
|
||||
)
|
||||
assertThat(byMonth.size, equalTo(17))
|
||||
assertThat(byMonth[0], equalTo(Entry(LocalDate(2014, 6, 1), 1_000)))
|
||||
assertThat(byMonth[6], equalTo(Entry(LocalDate(2013, 12, 1), 7_000)))
|
||||
assertThat(byMonth[12], equalTo(Entry(LocalDate(2013, 5, 1), 6_000)))
|
||||
assertEquals(17, byMonth.size)
|
||||
assertEquals(Entry(LocalDate(2014, 6, 1), 1_000), byMonth[0])
|
||||
assertEquals(Entry(LocalDate(2013, 12, 1), 7_000), byMonth[6])
|
||||
assertEquals(Entry(LocalDate(2013, 5, 1), 6_000), byMonth[12])
|
||||
|
||||
val byQuarter = entries.getKnown().groupedSum(
|
||||
truncateField = TruncateField.QUARTER,
|
||||
isNumerical = false
|
||||
)
|
||||
assertThat(byQuarter.size, equalTo(6))
|
||||
assertThat(byQuarter[0], equalTo(Entry(LocalDate(2014, 4, 1), 15_000)))
|
||||
assertThat(byQuarter[3], equalTo(Entry(LocalDate(2013, 7, 1), 17_000)))
|
||||
assertThat(byQuarter[5], equalTo(Entry(LocalDate(2013, 1, 1), 20_000)))
|
||||
assertEquals(6, byQuarter.size)
|
||||
assertEquals(Entry(LocalDate(2014, 4, 1), 15_000), byQuarter[0])
|
||||
assertEquals(Entry(LocalDate(2013, 7, 1), 17_000), byQuarter[3])
|
||||
assertEquals(Entry(LocalDate(2013, 1, 1), 20_000), byQuarter[5])
|
||||
|
||||
val byYear = entries.getKnown().groupedSum(
|
||||
truncateField = TruncateField.YEAR,
|
||||
isNumerical = false
|
||||
)
|
||||
assertThat(byYear.size, equalTo(2))
|
||||
assertThat(byYear[0], equalTo(Entry(LocalDate(2014, 1, 1), 34_000)))
|
||||
assertThat(byYear[1], equalTo(Entry(LocalDate(2013, 1, 1), 66_000)))
|
||||
assertEquals(2, byYear.size)
|
||||
assertEquals(Entry(LocalDate(2014, 1, 1), 34_000), byYear[0])
|
||||
assertEquals(Entry(LocalDate(2013, 1, 1), 66_000), byYear[1])
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -242,7 +241,7 @@ class EntryListTest {
|
||||
Entry(day(11), NO)
|
||||
)
|
||||
val actual = EntryList.buildEntriesFromInterval(entries, intervals)
|
||||
assertThat(actual, equalTo(expected))
|
||||
assertEquals(expected, actual)
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -260,7 +259,7 @@ class EntryListTest {
|
||||
EntryList.Interval(day(29), day(27), day(23))
|
||||
)
|
||||
EntryList.snapIntervalsTogether(original)
|
||||
assertThat(original, equalTo(expected))
|
||||
assertEquals(expected, original)
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -274,7 +273,7 @@ class EntryListTest {
|
||||
EntryList.Interval(day(13), day(8), day(7))
|
||||
)
|
||||
EntryList.snapIntervalsTogether(original)
|
||||
assertThat(original, equalTo(expected))
|
||||
assertEquals(expected, original)
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -290,7 +289,7 @@ class EntryListTest {
|
||||
EntryList.Interval(day(23), day(23), day(17))
|
||||
)
|
||||
val actual = EntryList.buildIntervals(Frequency.WEEKLY, entries)
|
||||
assertThat(actual, equalTo(expected))
|
||||
assertEquals(expected, actual)
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -306,7 +305,7 @@ class EntryListTest {
|
||||
EntryList.Interval(day(23), day(23), day(23))
|
||||
)
|
||||
val actual = EntryList.buildIntervals(Frequency.DAILY, entries)
|
||||
assertThat(actual, equalTo(expected))
|
||||
assertEquals(expected, actual)
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -324,7 +323,7 @@ class EntryListTest {
|
||||
EntryList.Interval(day(23), day(22), day(17))
|
||||
)
|
||||
val actual = EntryList.buildIntervals(Frequency.TWO_TIMES_PER_WEEK, entries)
|
||||
assertThat(actual, equalTo(expected))
|
||||
assertEquals(expected, actual)
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -339,7 +338,7 @@ class EntryListTest {
|
||||
EntryList.Interval(day(30), day(30), day(28))
|
||||
)
|
||||
val actual = EntryList.buildIntervals(Frequency(1, 3), entries)
|
||||
assertThat(actual, equalTo(expected))
|
||||
assertEquals(expected, actual)
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -376,9 +375,9 @@ class EntryListTest {
|
||||
val monthStart = LocalDate(2015, month + 1, 1)
|
||||
val actualCount = freq[monthStart]
|
||||
if (monthCount[month] == 0) {
|
||||
assertThat(actualCount, equalTo(null))
|
||||
assertEquals(null, actualCount)
|
||||
} else {
|
||||
assertThat(actualCount, equalTo(weekdayCount[month]))
|
||||
assertContentEquals(weekdayCount[month], actualCount)
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -25,7 +25,7 @@ import org.isoron.uhabits.core.models.Entry.Companion.UNKNOWN
|
||||
import org.isoron.uhabits.core.models.Entry.Companion.YES_AUTO
|
||||
import org.isoron.uhabits.core.models.Entry.Companion.YES_MANUAL
|
||||
import org.isoron.uhabits.core.models.Entry.Companion.nextToggleValue
|
||||
import org.junit.Test
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertEquals
|
||||
|
||||
class EntryTest {
|
||||
@ -18,15 +18,13 @@
|
||||
*/
|
||||
package org.isoron.uhabits.core.models
|
||||
|
||||
import org.hamcrest.CoreMatchers.equalTo
|
||||
import org.hamcrest.CoreMatchers.not
|
||||
import org.hamcrest.MatcherAssert.assertThat
|
||||
import org.isoron.uhabits.core.BaseUnitTest
|
||||
import org.junit.Assert.assertThrows
|
||||
import org.junit.Test
|
||||
import java.io.IOException
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertContentEquals
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.test.assertFailsWith
|
||||
import kotlin.test.assertFalse
|
||||
import kotlin.test.assertNotEquals
|
||||
import kotlin.test.assertNull
|
||||
|
||||
class HabitListTest : BaseUnitTest() {
|
||||
@ -34,7 +32,6 @@ class HabitListTest : BaseUnitTest() {
|
||||
private lateinit var activeHabits: HabitList
|
||||
private lateinit var reminderHabits: HabitList
|
||||
|
||||
@Throws(Exception::class)
|
||||
override fun setUp() {
|
||||
super.setUp()
|
||||
habitsArray = ArrayList()
|
||||
@ -59,25 +56,25 @@ class HabitListTest : BaseUnitTest() {
|
||||
|
||||
@Test
|
||||
fun testSize() {
|
||||
assertThat(habitList.size(), equalTo(10))
|
||||
assertThat(activeHabits.size(), equalTo(6))
|
||||
assertThat(reminderHabits.size(), equalTo(4))
|
||||
assertEquals(10, habitList.size())
|
||||
assertEquals(6, activeHabits.size())
|
||||
assertEquals(4, reminderHabits.size())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testGetByPosition() {
|
||||
assertThat(habitList.getByPosition(0), equalTo(habitsArray[0]))
|
||||
assertThat(habitList.getByPosition(3), equalTo(habitsArray[3]))
|
||||
assertThat(habitList.getByPosition(9), equalTo(habitsArray[9]))
|
||||
assertThat(activeHabits.getByPosition(0), equalTo(habitsArray[2]))
|
||||
assertThat(reminderHabits.getByPosition(1), equalTo(habitsArray[3]))
|
||||
assertEquals(habitsArray[0], habitList.getByPosition(0))
|
||||
assertEquals(habitsArray[3], habitList.getByPosition(3))
|
||||
assertEquals(habitsArray[9], habitList.getByPosition(9))
|
||||
assertEquals(habitsArray[2], activeHabits.getByPosition(0))
|
||||
assertEquals(habitsArray[3], reminderHabits.getByPosition(1))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testGetById() {
|
||||
val habit1 = habitsArray[0]
|
||||
val habit2 = habitList.getById(habit1.id!!)
|
||||
assertThat(habit1, equalTo(habit2))
|
||||
assertEquals(habit1, habit2)
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -100,41 +97,41 @@ class HabitListTest : BaseUnitTest() {
|
||||
}
|
||||
|
||||
list.primaryOrder = HabitList.Order.BY_POSITION
|
||||
assertThat(list.getByPosition(0), equalTo(h3))
|
||||
assertThat(list.getByPosition(1), equalTo(h1))
|
||||
assertThat(list.getByPosition(2), equalTo(h4))
|
||||
assertThat(list.getByPosition(3), equalTo(h2))
|
||||
assertEquals(h3, list.getByPosition(0))
|
||||
assertEquals(h1, list.getByPosition(1))
|
||||
assertEquals(h4, list.getByPosition(2))
|
||||
assertEquals(h2, list.getByPosition(3))
|
||||
list.primaryOrder = HabitList.Order.BY_NAME_DESC
|
||||
assertThat(list.getByPosition(0), equalTo(h4))
|
||||
assertThat(list.getByPosition(1), equalTo(h3))
|
||||
assertThat(list.getByPosition(2), equalTo(h2))
|
||||
assertThat(list.getByPosition(3), equalTo(h1))
|
||||
assertEquals(h4, list.getByPosition(0))
|
||||
assertEquals(h3, list.getByPosition(1))
|
||||
assertEquals(h2, list.getByPosition(2))
|
||||
assertEquals(h1, list.getByPosition(3))
|
||||
list.primaryOrder = HabitList.Order.BY_NAME_ASC
|
||||
assertThat(list.getByPosition(0), equalTo(h1))
|
||||
assertThat(list.getByPosition(1), equalTo(h2))
|
||||
assertThat(list.getByPosition(2), equalTo(h3))
|
||||
assertThat(list.getByPosition(3), equalTo(h4))
|
||||
assertEquals(h1, list.getByPosition(0))
|
||||
assertEquals(h2, list.getByPosition(1))
|
||||
assertEquals(h3, list.getByPosition(2))
|
||||
assertEquals(h4, list.getByPosition(3))
|
||||
list.primaryOrder = HabitList.Order.BY_NAME_ASC
|
||||
list.remove(h1)
|
||||
list.add(h1)
|
||||
assertThat(list.getByPosition(0), equalTo(h1))
|
||||
assertEquals(h1, list.getByPosition(0))
|
||||
list.primaryOrder = HabitList.Order.BY_COLOR_ASC
|
||||
list.secondaryOrder = HabitList.Order.BY_NAME_ASC
|
||||
assertThat(list.getByPosition(0), equalTo(h3))
|
||||
assertThat(list.getByPosition(1), equalTo(h4))
|
||||
assertThat(list.getByPosition(2), equalTo(h1))
|
||||
assertThat(list.getByPosition(3), equalTo(h2))
|
||||
assertEquals(h3, list.getByPosition(0))
|
||||
assertEquals(h4, list.getByPosition(1))
|
||||
assertEquals(h1, list.getByPosition(2))
|
||||
assertEquals(h2, list.getByPosition(3))
|
||||
list.primaryOrder = HabitList.Order.BY_COLOR_DESC
|
||||
list.secondaryOrder = HabitList.Order.BY_NAME_ASC
|
||||
assertThat(list.getByPosition(0), equalTo(h1))
|
||||
assertThat(list.getByPosition(1), equalTo(h2))
|
||||
assertThat(list.getByPosition(2), equalTo(h4))
|
||||
assertThat(list.getByPosition(3), equalTo(h3))
|
||||
assertEquals(h1, list.getByPosition(0))
|
||||
assertEquals(h2, list.getByPosition(1))
|
||||
assertEquals(h4, list.getByPosition(2))
|
||||
assertEquals(h3, list.getByPosition(3))
|
||||
list.primaryOrder = HabitList.Order.BY_POSITION
|
||||
assertThat(list.getByPosition(0), equalTo(h3))
|
||||
assertThat(list.getByPosition(1), equalTo(h1))
|
||||
assertThat(list.getByPosition(2), equalTo(h4))
|
||||
assertThat(list.getByPosition(3), equalTo(h2))
|
||||
assertEquals(h3, list.getByPosition(0))
|
||||
assertEquals(h1, list.getByPosition(1))
|
||||
assertEquals(h4, list.getByPosition(2))
|
||||
assertEquals(h2, list.getByPosition(3))
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -154,21 +151,20 @@ class HabitListTest : BaseUnitTest() {
|
||||
val actualSequence = IntArray(10)
|
||||
for (j in 0..9) {
|
||||
val habit = habitList.getByPosition(j)
|
||||
assertThat(habit.position, equalTo(j))
|
||||
actualSequence[j] = Math.toIntExact(habit.id!!)
|
||||
assertEquals(j, habit.position)
|
||||
actualSequence[j] = habit.id!!.toInt()
|
||||
}
|
||||
assertThat(actualSequence, equalTo(expectedSequence[i]))
|
||||
assertContentEquals(expectedSequence[i], actualSequence)
|
||||
}
|
||||
assertThat(activeHabits.indexOf(habitsArray[5]), equalTo(0))
|
||||
assertThat(activeHabits.indexOf(habitsArray[2]), equalTo(1))
|
||||
assertEquals(0, activeHabits.indexOf(habitsArray[5]))
|
||||
assertEquals(1, activeHabits.indexOf(habitsArray[2]))
|
||||
}
|
||||
|
||||
@Test
|
||||
@Throws(Exception::class)
|
||||
fun testReorder_withInvalidArguments() {
|
||||
val h1 = habitsArray[0]
|
||||
val h2 = fixtures.createEmptyHabit()
|
||||
assertThrows(IllegalArgumentException::class.java) {
|
||||
assertFailsWith<IllegalArgumentException> {
|
||||
habitList.reorder(h1, h2)
|
||||
}
|
||||
}
|
||||
@ -186,7 +182,6 @@ class HabitListTest : BaseUnitTest() {
|
||||
}
|
||||
|
||||
@Test
|
||||
@Throws(IOException::class)
|
||||
fun testWriteCSV() {
|
||||
val list = modelFactory.buildHabitList()
|
||||
val h1 = fixtures.createEmptyHabit()
|
||||
@ -211,57 +206,52 @@ class HabitListTest : BaseUnitTest() {
|
||||
001,Meditate,YES_NO,Did you meditate this morning?,this is a test description,1,1,#FF8F00,,,,false
|
||||
002,Run,NUMERICAL,How many miles did you run today?,,1,1,#E64A19,miles,AT_LEAST,2.0,false
|
||||
003,Wake up early,YES_NO,Did you wake up before 6am?,,2,3,#AFB42B,,,,false
|
||||
|
||||
|
||||
""".trimIndent()
|
||||
assertThat(list.writeCSV(), equalTo(expectedCSV))
|
||||
assertEquals(expectedCSV, list.writeCSV())
|
||||
}
|
||||
|
||||
@Test
|
||||
@Throws(Exception::class)
|
||||
fun testAdd() {
|
||||
val h1 = fixtures.createEmptyHabit()
|
||||
assertFalse(h1.isArchived)
|
||||
assertNull(h1.id)
|
||||
assertThat(habitList.indexOf(h1), equalTo(-1))
|
||||
assertEquals(-1, habitList.indexOf(h1))
|
||||
habitList.add(h1)
|
||||
h1.id!!
|
||||
assertThat(habitList.indexOf(h1), not(equalTo(-1)))
|
||||
assertThat(activeHabits.indexOf(h1), not(equalTo(-1)))
|
||||
assertNotEquals(-1, habitList.indexOf(h1))
|
||||
assertNotEquals(-1, activeHabits.indexOf(h1))
|
||||
}
|
||||
|
||||
@Test
|
||||
@Throws(Exception::class)
|
||||
fun testAdd_withFilteredList() {
|
||||
assertThrows(IllegalStateException::class.java) {
|
||||
assertFailsWith<IllegalStateException> {
|
||||
activeHabits.add(fixtures.createEmptyHabit())
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@Throws(Exception::class)
|
||||
fun testRemove_onFilteredList() {
|
||||
assertThrows(IllegalStateException::class.java) {
|
||||
assertFailsWith<IllegalStateException> {
|
||||
activeHabits.remove(fixtures.createEmptyHabit())
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@Throws(Exception::class)
|
||||
fun testReorder_onFilteredList() {
|
||||
val h1 = fixtures.createEmptyHabit()
|
||||
val h2 = fixtures.createEmptyHabit()
|
||||
assertThrows(IllegalStateException::class.java) {
|
||||
assertFailsWith<IllegalStateException> {
|
||||
activeHabits.reorder(h1, h2)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@Throws(Exception::class)
|
||||
fun testReorder_onSortedList() {
|
||||
habitList.primaryOrder = HabitList.Order.BY_SCORE_DESC
|
||||
val h1 = habitsArray[1]
|
||||
val h2 = habitsArray[2]
|
||||
assertThrows(IllegalStateException::class.java) {
|
||||
assertFailsWith<IllegalStateException> {
|
||||
habitList.reorder(h1, h2)
|
||||
}
|
||||
}
|
||||
@ -18,20 +18,16 @@
|
||||
*/
|
||||
package org.isoron.uhabits.core.models
|
||||
|
||||
import org.hamcrest.CoreMatchers.`is`
|
||||
import org.hamcrest.MatcherAssert.assertThat
|
||||
import org.hamcrest.core.IsEqual.equalTo
|
||||
import org.isoron.platform.time.getToday
|
||||
import org.isoron.uhabits.core.BaseUnitTest
|
||||
import org.junit.Assert.assertNotEquals
|
||||
import org.junit.Test
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.test.assertFalse
|
||||
import kotlin.test.assertNotEquals
|
||||
import kotlin.test.assertTrue
|
||||
|
||||
class HabitTest : BaseUnitTest() {
|
||||
|
||||
@Throws(Exception::class)
|
||||
override fun setUp() {
|
||||
super.setUp()
|
||||
}
|
||||
@ -53,22 +49,21 @@ class HabitTest : BaseUnitTest() {
|
||||
val habit = modelFactory.buildHabit()
|
||||
habit.copyFrom(model)
|
||||
assertEquals(habit.isArchived, model.isArchived)
|
||||
assertThat(habit.isArchived, `is`(model.isArchived))
|
||||
assertThat(habit.color, `is`(model.color))
|
||||
assertThat(habit.frequency, equalTo(model.frequency))
|
||||
assertThat(habit.reminder, equalTo(model.reminder))
|
||||
assertEquals(model.isArchived, habit.isArchived)
|
||||
assertEquals(model.color, habit.color)
|
||||
assertEquals(model.frequency, habit.frequency)
|
||||
assertEquals(model.reminder, habit.reminder)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun test_hasReminder() {
|
||||
val h = modelFactory.buildHabit()
|
||||
assertThat(h.hasReminder(), `is`(false))
|
||||
assertEquals(false, h.hasReminder())
|
||||
h.reminder = Reminder(8, 30, WeekdayList.EVERY_DAY)
|
||||
assertThat(h.hasReminder(), `is`(true))
|
||||
assertEquals(true, h.hasReminder())
|
||||
}
|
||||
|
||||
@Test
|
||||
@Throws(Exception::class)
|
||||
fun test_isCompleted() {
|
||||
val h = modelFactory.buildHabit()
|
||||
assertFalse(h.isCompletedToday())
|
||||
@ -78,7 +73,6 @@ class HabitTest : BaseUnitTest() {
|
||||
}
|
||||
|
||||
@Test
|
||||
@Throws(Exception::class)
|
||||
fun test_isEntered() {
|
||||
val h = modelFactory.buildHabit()
|
||||
assertFalse(h.isEnteredToday())
|
||||
@ -88,7 +82,6 @@ class HabitTest : BaseUnitTest() {
|
||||
}
|
||||
|
||||
@Test
|
||||
@Throws(Exception::class)
|
||||
fun test_isCompleted_numerical() {
|
||||
val h = modelFactory.buildHabit()
|
||||
h.type = HabitType.NUMERICAL
|
||||
@ -117,12 +110,11 @@ class HabitTest : BaseUnitTest() {
|
||||
}
|
||||
|
||||
@Test
|
||||
@Throws(Exception::class)
|
||||
fun testURI() {
|
||||
assertTrue(habitList.isEmpty)
|
||||
val h = modelFactory.buildHabit()
|
||||
habitList.add(h)
|
||||
assertThat(h.id, equalTo(0L))
|
||||
assertThat(h.uriString, equalTo("content://org.isoron.uhabits/habit/0"))
|
||||
assertEquals(0L, h.id)
|
||||
assertEquals("content://org.isoron.uhabits/habit/0", h.uriString)
|
||||
}
|
||||
}
|
||||
@ -18,23 +18,19 @@
|
||||
*/
|
||||
package org.isoron.uhabits.core.models
|
||||
|
||||
import org.hamcrest.MatcherAssert.assertThat
|
||||
import org.hamcrest.number.IsCloseTo
|
||||
import org.hamcrest.number.OrderingComparison
|
||||
import org.isoron.platform.time.LocalDate
|
||||
import org.isoron.platform.time.getToday
|
||||
import org.isoron.uhabits.core.BaseUnitTest
|
||||
import org.isoron.uhabits.core.models.Entry.Companion.SKIP
|
||||
import org.junit.Before
|
||||
import org.junit.Test
|
||||
import kotlin.test.BeforeTest
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertTrue
|
||||
|
||||
open class BaseScoreListTest : BaseUnitTest() {
|
||||
protected lateinit var habit: Habit
|
||||
protected lateinit var today: LocalDate
|
||||
|
||||
@Before
|
||||
@Throws(Exception::class)
|
||||
@BeforeTest
|
||||
override fun setUp() {
|
||||
super.setUp()
|
||||
today = getToday()
|
||||
@ -44,19 +40,23 @@ open class BaseScoreListTest : BaseUnitTest() {
|
||||
var current = today
|
||||
val scores = habit.scores
|
||||
for (expectedValue in expectedValues) {
|
||||
assertThat(scores[current].value, IsCloseTo.closeTo(expectedValue, E))
|
||||
assertCloseTo(expectedValue, scores[current].value, E)
|
||||
current = current.minus(1)
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
const val E = 1e-6
|
||||
|
||||
fun assertCloseTo(expected: Double, actual: Double, epsilon: Double) {
|
||||
val diff = kotlin.math.abs(expected - actual)
|
||||
assertTrue(diff <= epsilon, "Expected $expected ± $epsilon, but got $actual (diff=$diff)")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class YesNoScoreListTest : BaseScoreListTest() {
|
||||
@Before
|
||||
@Throws(Exception::class)
|
||||
@BeforeTest
|
||||
override fun setUp() {
|
||||
super.setUp()
|
||||
habit = fixtures.createEmptyHabit()
|
||||
@ -161,12 +161,12 @@ class YesNoScoreListTest : BaseScoreListTest() {
|
||||
values.add(Entry.NO)
|
||||
}
|
||||
check(values)
|
||||
assertThat(habit.scores[today].value, IsCloseTo.closeTo(2 / 3.0, E))
|
||||
assertCloseTo(2 / 3.0, habit.scores[today].value, E)
|
||||
|
||||
// Missing 2 repetitions out of 4 per week, the score should converge to 50%
|
||||
habit.frequency = Frequency(4, 7)
|
||||
habit.recompute()
|
||||
assertThat(habit.scores[today].value, IsCloseTo.closeTo(0.5, E))
|
||||
assertCloseTo(0.5, habit.scores[today].value, E)
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -195,7 +195,7 @@ class YesNoScoreListTest : BaseScoreListTest() {
|
||||
values.add(Entry.YES_MANUAL)
|
||||
}
|
||||
check(values)
|
||||
assertThat(habit.scores[today].value, IsCloseTo.closeTo(1.0, 1e-3))
|
||||
assertCloseTo(1.0, habit.scores[today].value, 1e-3)
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -205,43 +205,43 @@ class YesNoScoreListTest : BaseScoreListTest() {
|
||||
habit.frequency = Frequency.DAILY
|
||||
for (i in 0..89) check(i)
|
||||
habit.recompute()
|
||||
assertThat(habit.scores[today].value, OrderingComparison.greaterThan(0.99))
|
||||
assertTrue(habit.scores[today].value > 0.99)
|
||||
|
||||
// Weekly habits should achieve at least 99% in 9 months
|
||||
habit = fixtures.createEmptyHabit()
|
||||
habit.frequency = Frequency.WEEKLY
|
||||
for (i in 0..38) check(7 * i)
|
||||
habit.recompute()
|
||||
assertThat(habit.scores[today].value, OrderingComparison.greaterThan(0.99))
|
||||
assertTrue(habit.scores[today].value > 0.99)
|
||||
|
||||
// Monthly habits should achieve at least 99% in 18 months
|
||||
habit.frequency = Frequency(1, 30)
|
||||
for (i in 0..17) check(30 * i)
|
||||
habit.recompute()
|
||||
assertThat(habit.scores[today].value, OrderingComparison.greaterThan(0.99))
|
||||
assertTrue(habit.scores[today].value > 0.99)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun test_recompute() {
|
||||
assertThat(habit.scores[today].value, IsCloseTo.closeTo(0.0, E))
|
||||
assertCloseTo(0.0, habit.scores[today].value, E)
|
||||
check(0, 2)
|
||||
assertThat(habit.scores[today].value, IsCloseTo.closeTo(0.101149, E))
|
||||
assertCloseTo(0.101149, habit.scores[today].value, E)
|
||||
habit.frequency = Frequency(1, 2)
|
||||
habit.recompute()
|
||||
assertThat(habit.scores[today].value, IsCloseTo.closeTo(0.054816, E))
|
||||
assertCloseTo(0.054816, habit.scores[today].value, E)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun test_addThenRemove() {
|
||||
val habit = fixtures.createEmptyHabit()
|
||||
habit.recompute()
|
||||
assertThat(habit.scores[today].value, IsCloseTo.closeTo(0.0, E))
|
||||
assertCloseTo(0.0, habit.scores[today].value, E)
|
||||
habit.originalEntries.add(Entry(today, Entry.YES_MANUAL))
|
||||
habit.recompute()
|
||||
assertThat(habit.scores[today].value, IsCloseTo.closeTo(0.051922, E))
|
||||
assertCloseTo(0.051922, habit.scores[today].value, E)
|
||||
habit.originalEntries.add(Entry(today, Entry.UNKNOWN))
|
||||
habit.recompute()
|
||||
assertThat(habit.scores[today].value, IsCloseTo.closeTo(0.0, E))
|
||||
assertCloseTo(0.0, habit.scores[today].value, E)
|
||||
}
|
||||
|
||||
private fun check(offset: Int) {
|
||||
@ -288,8 +288,7 @@ open class NumericalScoreListTest : BaseScoreListTest() {
|
||||
}
|
||||
|
||||
class NumericalAtLeastScoreListTest : NumericalScoreListTest() {
|
||||
@Before
|
||||
@Throws(Exception::class)
|
||||
@BeforeTest
|
||||
override fun setUp() {
|
||||
super.setUp()
|
||||
habit = fixtures.createEmptyNumericalHabit(NumericalHabitType.AT_LEAST)
|
||||
@ -336,12 +335,12 @@ class NumericalAtLeastScoreListTest : NumericalScoreListTest() {
|
||||
|
||||
@Test
|
||||
fun test_recompute() {
|
||||
assertThat(habit.scores[today].value, IsCloseTo.closeTo(0.0, E))
|
||||
assertCloseTo(0.0, habit.scores[today].value, E)
|
||||
addEntries(0, 2, 2000)
|
||||
assertThat(habit.scores[today].value, IsCloseTo.closeTo(0.101149, E))
|
||||
assertCloseTo(0.101149, habit.scores[today].value, E)
|
||||
habit.frequency = Frequency(1, 2)
|
||||
habit.recompute()
|
||||
assertThat(habit.scores[today].value, IsCloseTo.closeTo(0.072631, E))
|
||||
assertCloseTo(0.072631, habit.scores[today].value, E)
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -351,42 +350,41 @@ class NumericalAtLeastScoreListTest : NumericalScoreListTest() {
|
||||
habit.frequency = Frequency.DAILY
|
||||
for (i in 0..89) addEntry(i, 2000)
|
||||
habit.recompute()
|
||||
assertThat(habit.scores[today].value, OrderingComparison.greaterThan(0.99))
|
||||
assertTrue(habit.scores[today].value > 0.99)
|
||||
|
||||
// Weekly habits should achieve at least 99% in 9 months
|
||||
habit = fixtures.createEmptyNumericalHabit(NumericalHabitType.AT_LEAST)
|
||||
habit.frequency = Frequency.WEEKLY
|
||||
for (i in 0..38) addEntry(7 * i, 2000)
|
||||
habit.recompute()
|
||||
assertThat(habit.scores[today].value, OrderingComparison.greaterThan(0.99))
|
||||
assertTrue(habit.scores[today].value > 0.99)
|
||||
|
||||
// Monthly habits should achieve at least 99% in 18 months
|
||||
habit.frequency = Frequency(1, 30)
|
||||
for (i in 0..17) addEntry(30 * i, 2000)
|
||||
habit.recompute()
|
||||
assertThat(habit.scores[today].value, OrderingComparison.greaterThan(0.99))
|
||||
assertTrue(habit.scores[today].value > 0.99)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun shouldAchieveComparableScoreToProgress() {
|
||||
addEntries(0, 500, 1000)
|
||||
assertThat(habit.scores[today].value, IsCloseTo.closeTo(0.5, E))
|
||||
assertCloseTo(0.5, habit.scores[today].value, E)
|
||||
|
||||
addEntries(0, 500, 500)
|
||||
assertThat(habit.scores[today].value, IsCloseTo.closeTo(0.25, E))
|
||||
assertCloseTo(0.25, habit.scores[today].value, E)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun overeachievingIsntRelevant() {
|
||||
addEntry(0, 10000000)
|
||||
habit.recompute()
|
||||
assertThat(habit.scores[today].value, IsCloseTo.closeTo(0.051922, E))
|
||||
assertCloseTo(0.051922, habit.scores[today].value, E)
|
||||
}
|
||||
}
|
||||
|
||||
class NumericalAtLeastScoreListWithSkipTest : NumericalScoreListTest() {
|
||||
@Before
|
||||
@Throws(Exception::class)
|
||||
@BeforeTest
|
||||
override fun setUp() {
|
||||
super.setUp()
|
||||
habit = fixtures.createEmptyNumericalHabit(NumericalHabitType.AT_LEAST)
|
||||
@ -433,20 +431,19 @@ class NumericalAtLeastScoreListWithSkipTest : NumericalScoreListTest() {
|
||||
val initialScore = habit.scores[today].value
|
||||
|
||||
addEntries(500, 1000, SKIP)
|
||||
assertThat(habit.scores[today].value, IsCloseTo.closeTo(initialScore, E))
|
||||
assertCloseTo(initialScore, habit.scores[today].value, E)
|
||||
|
||||
addEntries(0, 300, 1000)
|
||||
addEntries(300, 500, SKIP)
|
||||
addEntries(500, 700, 1000)
|
||||
|
||||
// skipped days should be treated as if they never existed
|
||||
assertThat(habit.scores[today].value, IsCloseTo.closeTo(initialScore, E))
|
||||
assertCloseTo(initialScore, habit.scores[today].value, E)
|
||||
}
|
||||
}
|
||||
|
||||
class NumericalAtMostScoreListTest : NumericalScoreListTest() {
|
||||
@Before
|
||||
@Throws(Exception::class)
|
||||
@BeforeTest
|
||||
override fun setUp() {
|
||||
super.setUp()
|
||||
habit = fixtures.createEmptyNumericalHabit(NumericalHabitType.AT_MOST)
|
||||
@ -496,28 +493,28 @@ class NumericalAtMostScoreListTest : NumericalScoreListTest() {
|
||||
@Test
|
||||
fun test_recompute() {
|
||||
habit.recompute()
|
||||
assertThat(habit.scores[today].value, IsCloseTo.closeTo(1.0, E))
|
||||
assertCloseTo(1.0, habit.scores[today].value, E)
|
||||
addEntries(0, 2, 5000)
|
||||
assertThat(habit.scores[today].value, IsCloseTo.closeTo(0.898850, E))
|
||||
assertCloseTo(0.898850, habit.scores[today].value, E)
|
||||
habit.frequency = Frequency(1, 2)
|
||||
habit.recompute()
|
||||
assertThat(habit.scores[today].value, IsCloseTo.closeTo(0.927369, E))
|
||||
assertCloseTo(0.927369, habit.scores[today].value, E)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun shouldAchieveComparableScoreToProgress() {
|
||||
addEntries(0, 500, 3000)
|
||||
assertThat(habit.scores[today].value, IsCloseTo.closeTo(0.5, E))
|
||||
assertCloseTo(0.5, habit.scores[today].value, E)
|
||||
|
||||
addEntries(0, 500, 3500)
|
||||
assertThat(habit.scores[today].value, IsCloseTo.closeTo(0.25, E))
|
||||
assertCloseTo(0.25, habit.scores[today].value, E)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun undereachievingIsntRelevant() {
|
||||
addEntry(1, 10000000)
|
||||
habit.recompute()
|
||||
assertThat(habit.scores[today].value, IsCloseTo.closeTo(0.950773, E))
|
||||
assertCloseTo(0.950773, habit.scores[today].value, E)
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -526,10 +523,10 @@ class NumericalAtMostScoreListTest : NumericalScoreListTest() {
|
||||
|
||||
addEntry(1, 0)
|
||||
habit.recompute()
|
||||
assertThat(habit.scores[today].value, IsCloseTo.closeTo(0.948077, E))
|
||||
assertCloseTo(0.948077, habit.scores[today].value, E)
|
||||
|
||||
addEntry(1, 1000)
|
||||
habit.recompute()
|
||||
assertThat(habit.scores[today].value, IsCloseTo.closeTo(0.948077, E))
|
||||
assertCloseTo(0.948077, habit.scores[today].value, E)
|
||||
}
|
||||
}
|
||||
@ -18,16 +18,14 @@
|
||||
*/
|
||||
package org.isoron.uhabits.core.models
|
||||
|
||||
import org.hamcrest.MatcherAssert.assertThat
|
||||
import org.hamcrest.number.IsCloseTo.closeTo
|
||||
import org.isoron.uhabits.core.BaseUnitTest
|
||||
import org.isoron.uhabits.core.models.Score.Companion.compute
|
||||
import org.junit.Before
|
||||
import org.junit.Test
|
||||
import kotlin.test.BeforeTest
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertTrue
|
||||
|
||||
class ScoreTest : BaseUnitTest() {
|
||||
@Before
|
||||
@Throws(Exception::class)
|
||||
@BeforeTest
|
||||
override fun setUp() {
|
||||
super.setUp()
|
||||
}
|
||||
@ -36,29 +34,34 @@ class ScoreTest : BaseUnitTest() {
|
||||
fun test_compute_withDailyHabit() {
|
||||
var check = 1
|
||||
val freq = 1.0
|
||||
assertThat(compute(freq, 0.0, check.toDouble()), closeTo(0.051922, E))
|
||||
assertThat(compute(freq, 0.5, check.toDouble()), closeTo(0.525961, E))
|
||||
assertThat(compute(freq, 0.75, check.toDouble()), closeTo(0.762981, E))
|
||||
assertCloseTo(0.051922, compute(freq, 0.0, check.toDouble()), E)
|
||||
assertCloseTo(0.525961, compute(freq, 0.5, check.toDouble()), E)
|
||||
assertCloseTo(0.762981, compute(freq, 0.75, check.toDouble()), E)
|
||||
check = 0
|
||||
assertThat(compute(freq, 0.0, check.toDouble()), closeTo(0.0, E))
|
||||
assertThat(compute(freq, 0.5, check.toDouble()), closeTo(0.474039, E))
|
||||
assertThat(compute(freq, 0.75, check.toDouble()), closeTo(0.711058, E))
|
||||
assertCloseTo(0.0, compute(freq, 0.0, check.toDouble()), E)
|
||||
assertCloseTo(0.474039, compute(freq, 0.5, check.toDouble()), E)
|
||||
assertCloseTo(0.711058, compute(freq, 0.75, check.toDouble()), E)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun test_compute_withNonDailyHabit() {
|
||||
var check = 1
|
||||
val freq = 1 / 3.0
|
||||
assertThat(compute(freq, 0.0, check.toDouble()), closeTo(0.030314, E))
|
||||
assertThat(compute(freq, 0.5, check.toDouble()), closeTo(0.515157, E))
|
||||
assertThat(compute(freq, 0.75, check.toDouble()), closeTo(0.757578, E))
|
||||
assertCloseTo(0.030314, compute(freq, 0.0, check.toDouble()), E)
|
||||
assertCloseTo(0.515157, compute(freq, 0.5, check.toDouble()), E)
|
||||
assertCloseTo(0.757578, compute(freq, 0.75, check.toDouble()), E)
|
||||
check = 0
|
||||
assertThat(compute(freq, 0.0, check.toDouble()), closeTo(0.0, E))
|
||||
assertThat(compute(freq, 0.5, check.toDouble()), closeTo(0.484842, E))
|
||||
assertThat(compute(freq, 0.75, check.toDouble()), closeTo(0.727263, E))
|
||||
assertCloseTo(0.0, compute(freq, 0.0, check.toDouble()), E)
|
||||
assertCloseTo(0.484842, compute(freq, 0.5, check.toDouble()), E)
|
||||
assertCloseTo(0.727263, compute(freq, 0.75, check.toDouble()), E)
|
||||
}
|
||||
|
||||
companion object {
|
||||
private const val E = 1e-6
|
||||
|
||||
private fun assertCloseTo(expected: Double, actual: Double, epsilon: Double) {
|
||||
val diff = kotlin.math.abs(expected - actual)
|
||||
assertTrue(diff <= epsilon, "Expected $expected ± $epsilon, but got $actual (diff=$diff)")
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -18,19 +18,17 @@
|
||||
*/
|
||||
package org.isoron.uhabits.core.models
|
||||
|
||||
import org.hamcrest.CoreMatchers.equalTo
|
||||
import org.hamcrest.MatcherAssert.assertThat
|
||||
import org.isoron.platform.time.LocalDate
|
||||
import org.isoron.platform.time.getToday
|
||||
import org.isoron.uhabits.core.BaseUnitTest
|
||||
import org.junit.Test
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertEquals
|
||||
|
||||
class StreakListTest : BaseUnitTest() {
|
||||
private lateinit var habit: Habit
|
||||
private lateinit var streaks: StreakList
|
||||
private lateinit var today: LocalDate
|
||||
|
||||
@Throws(Exception::class)
|
||||
override fun setUp() {
|
||||
super.setUp()
|
||||
habit = fixtures.createLongHabit()
|
||||
@ -41,18 +39,17 @@ class StreakListTest : BaseUnitTest() {
|
||||
}
|
||||
|
||||
@Test
|
||||
@Throws(Exception::class)
|
||||
fun testGetBest() {
|
||||
var best = streaks.getBest(4)
|
||||
assertThat(best.size, equalTo(4))
|
||||
assertThat(best[0].length, equalTo(4))
|
||||
assertThat(best[1].length, equalTo(3))
|
||||
assertThat(best[2].length, equalTo(5))
|
||||
assertThat(best[3].length, equalTo(6))
|
||||
assertEquals(4, best.size)
|
||||
assertEquals(4, best[0].length)
|
||||
assertEquals(3, best[1].length)
|
||||
assertEquals(5, best[2].length)
|
||||
assertEquals(6, best[3].length)
|
||||
best = streaks.getBest(2)
|
||||
assertThat(best.size, equalTo(2))
|
||||
assertThat(best[0].length, equalTo(5))
|
||||
assertThat(best[1].length, equalTo(6))
|
||||
assertEquals(2, best.size)
|
||||
assertEquals(5, best[0].length)
|
||||
assertEquals(6, best[1].length)
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -62,7 +59,7 @@ class StreakListTest : BaseUnitTest() {
|
||||
habit.originalEntries.add(Entry(today.minus(5), Entry.NO))
|
||||
habit.recompute()
|
||||
val best = streaks.getBest(5)
|
||||
assertThat(best.size, equalTo(1))
|
||||
assertThat(best[0].length, equalTo(1))
|
||||
assertEquals(1, best.size)
|
||||
assertEquals(1, best[0].length)
|
||||
}
|
||||
}
|
||||
@ -18,10 +18,10 @@
|
||||
*/
|
||||
package org.isoron.uhabits.core.models
|
||||
|
||||
import org.hamcrest.CoreMatchers.equalTo
|
||||
import org.hamcrest.MatcherAssert.assertThat
|
||||
import org.isoron.uhabits.core.BaseUnitTest
|
||||
import org.junit.Test
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertContentEquals
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.test.assertFalse
|
||||
import kotlin.test.assertTrue
|
||||
|
||||
@ -31,11 +31,11 @@ class WeekdayListTest : BaseUnitTest() {
|
||||
val daysInt = 124
|
||||
val daysArray = booleanArrayOf(false, false, true, true, true, true, true)
|
||||
var list = WeekdayList(daysArray)
|
||||
assertThat(list.toArray(), equalTo(daysArray))
|
||||
assertThat(list.toInteger(), equalTo(daysInt))
|
||||
assertContentEquals(daysArray, list.toArray())
|
||||
assertEquals(daysInt, list.toInteger())
|
||||
list = WeekdayList(daysInt)
|
||||
assertThat(list.toArray(), equalTo(daysArray))
|
||||
assertThat(list.toInteger(), equalTo(daysInt))
|
||||
assertContentEquals(daysArray, list.toArray())
|
||||
assertEquals(daysInt, list.toInteger())
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -48,7 +48,7 @@ class WeekdayListTest : BaseUnitTest() {
|
||||
@Test
|
||||
fun testWeekdayList_IntConstructor_toString() {
|
||||
val string = WeekdayList(0).toString()
|
||||
assertThat(string, equalTo("{weekdays: [false,false,false,false,false,false,false]}"))
|
||||
assertEquals("{weekdays: [false,false,false,false,false,false,false]}", string)
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -56,6 +56,6 @@ class WeekdayListTest : BaseUnitTest() {
|
||||
val string = WeekdayList(
|
||||
booleanArrayOf(false, false, true, true, true, true, true)
|
||||
).toString()
|
||||
assertThat(string, equalTo("{weekdays: [false,false,true,true,true,true,true]}"))
|
||||
assertEquals("{weekdays: [false,false,true,true,true,true,true]}", string)
|
||||
}
|
||||
}
|
||||
@ -24,8 +24,8 @@ import org.isoron.uhabits.core.BaseUnitTest.Companion.buildMemoryDatabase
|
||||
import org.isoron.uhabits.core.database.EntryData
|
||||
import org.isoron.uhabits.core.models.Entry
|
||||
import org.isoron.uhabits.core.models.Entry.Companion.UNKNOWN
|
||||
import org.junit.Before
|
||||
import org.junit.Test
|
||||
import kotlin.test.BeforeTest
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertEquals
|
||||
|
||||
class SQLiteEntryListTest {
|
||||
@ -36,7 +36,7 @@ class SQLiteEntryListTest {
|
||||
private lateinit var entries: SQLiteEntryList
|
||||
private val today = LocalDate(2015, 1, 25)
|
||||
|
||||
@Before
|
||||
@BeforeTest
|
||||
fun setUp() {
|
||||
val habitList = factory.buildHabitList()
|
||||
val habit = factory.buildHabit()
|
||||
@ -18,15 +18,13 @@
|
||||
*/
|
||||
package org.isoron.uhabits.core.preferences
|
||||
|
||||
import org.hamcrest.CoreMatchers.equalTo
|
||||
import org.hamcrest.MatcherAssert.assertThat
|
||||
import org.isoron.platform.time.LocalDate
|
||||
import org.isoron.uhabits.core.BaseUnitTest
|
||||
import org.isoron.uhabits.core.models.HabitList
|
||||
import org.isoron.uhabits.core.ui.ThemeSwitcher
|
||||
import org.junit.Before
|
||||
import org.junit.Test
|
||||
import org.mockito.kotlin.mock
|
||||
import kotlin.test.BeforeTest
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.test.assertFalse
|
||||
import kotlin.test.assertNull
|
||||
import kotlin.test.assertTrue
|
||||
@ -34,11 +32,10 @@ import kotlin.test.assertTrue
|
||||
class PreferencesTest : BaseUnitTest() {
|
||||
private lateinit var prefs: Preferences
|
||||
|
||||
private var listener: Preferences.Listener = mock()
|
||||
private var listener: Preferences.Listener = object : Preferences.Listener {}
|
||||
private lateinit var storage: MemoryStorage
|
||||
|
||||
@Before
|
||||
@Throws(Exception::class)
|
||||
@BeforeTest
|
||||
override fun setUp() {
|
||||
super.setUp()
|
||||
storage = MemoryStorage()
|
||||
@ -47,69 +44,62 @@ class PreferencesTest : BaseUnitTest() {
|
||||
}
|
||||
|
||||
@Test
|
||||
@Throws(Exception::class)
|
||||
fun testClear() {
|
||||
prefs.setDefaultHabitColor(99)
|
||||
prefs.clear()
|
||||
assertThat(prefs.getDefaultHabitColor(0), equalTo(0))
|
||||
assertEquals(0, prefs.getDefaultHabitColor(0))
|
||||
}
|
||||
|
||||
@Test
|
||||
@Throws(Exception::class)
|
||||
fun testHabitColor() {
|
||||
assertThat(prefs.getDefaultHabitColor(999), equalTo(999))
|
||||
assertEquals(999, prefs.getDefaultHabitColor(999))
|
||||
prefs.setDefaultHabitColor(10)
|
||||
assertThat(prefs.getDefaultHabitColor(999), equalTo(10))
|
||||
assertEquals(10, prefs.getDefaultHabitColor(999))
|
||||
}
|
||||
|
||||
@Test
|
||||
@Throws(Exception::class)
|
||||
fun testDefaultOrder() {
|
||||
assertThat(prefs.defaultPrimaryOrder, equalTo(HabitList.Order.BY_POSITION))
|
||||
assertEquals(HabitList.Order.BY_POSITION, prefs.defaultPrimaryOrder)
|
||||
prefs.defaultPrimaryOrder = HabitList.Order.BY_SCORE_DESC
|
||||
assertThat(prefs.defaultPrimaryOrder, equalTo(HabitList.Order.BY_SCORE_DESC))
|
||||
assertEquals(HabitList.Order.BY_SCORE_DESC, prefs.defaultPrimaryOrder)
|
||||
storage.putString("pref_default_order", "BOGUS")
|
||||
assertThat(prefs.defaultPrimaryOrder, equalTo(HabitList.Order.BY_POSITION))
|
||||
assertThat(
|
||||
storage.getString("pref_default_order", ""),
|
||||
equalTo("BY_POSITION")
|
||||
assertEquals(HabitList.Order.BY_POSITION, prefs.defaultPrimaryOrder)
|
||||
assertEquals(
|
||||
"BY_POSITION",
|
||||
storage.getString("pref_default_order", "")
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
@Throws(Exception::class)
|
||||
fun testScoreCardSpinnerPosition() {
|
||||
assertThat(prefs.scoreCardSpinnerPosition, equalTo(1))
|
||||
assertEquals(1, prefs.scoreCardSpinnerPosition)
|
||||
prefs.scoreCardSpinnerPosition = 4
|
||||
assertThat(prefs.scoreCardSpinnerPosition, equalTo(4))
|
||||
assertEquals(4, prefs.scoreCardSpinnerPosition)
|
||||
storage.putInt("pref_score_view_interval", 9000)
|
||||
assertThat(prefs.scoreCardSpinnerPosition, equalTo(4))
|
||||
assertEquals(4, prefs.scoreCardSpinnerPosition)
|
||||
}
|
||||
|
||||
@Test
|
||||
@Throws(Exception::class)
|
||||
fun testLastHint() {
|
||||
assertThat(prefs.lastHintNumber, equalTo(-1))
|
||||
assertEquals(-1, prefs.lastHintNumber)
|
||||
assertNull(prefs.lastHintDate)
|
||||
val date = LocalDate(2015, 3, 15)
|
||||
prefs.updateLastHint(34, date)
|
||||
assertThat(prefs.lastHintNumber, equalTo(34))
|
||||
assertThat(prefs.lastHintDate, equalTo(date))
|
||||
assertEquals(34, prefs.lastHintNumber)
|
||||
assertEquals(date, prefs.lastHintDate)
|
||||
}
|
||||
|
||||
@Test
|
||||
@Throws(Exception::class)
|
||||
fun testTheme() {
|
||||
assertThat(prefs.theme, equalTo(ThemeSwitcher.THEME_AUTOMATIC))
|
||||
assertEquals(ThemeSwitcher.THEME_AUTOMATIC, prefs.theme)
|
||||
prefs.theme = ThemeSwitcher.THEME_DARK
|
||||
assertThat(prefs.theme, equalTo(ThemeSwitcher.THEME_DARK))
|
||||
assertEquals(ThemeSwitcher.THEME_DARK, prefs.theme)
|
||||
assertFalse(prefs.isPureBlackEnabled)
|
||||
prefs.isPureBlackEnabled = true
|
||||
assertTrue(prefs.isPureBlackEnabled)
|
||||
}
|
||||
|
||||
@Test
|
||||
@Throws(Exception::class)
|
||||
fun testNotifications() {
|
||||
assertFalse(prefs.shouldMakeNotificationsSticky())
|
||||
prefs.setNotificationsSticky(true)
|
||||
@ -117,21 +107,19 @@ class PreferencesTest : BaseUnitTest() {
|
||||
}
|
||||
|
||||
@Test
|
||||
@Throws(Exception::class)
|
||||
fun testAppVersionAndLaunch() {
|
||||
assertThat(prefs.lastAppVersion, equalTo(0))
|
||||
assertEquals(0, prefs.lastAppVersion)
|
||||
prefs.lastAppVersion = 23
|
||||
assertThat(prefs.lastAppVersion, equalTo(23))
|
||||
assertEquals(23, prefs.lastAppVersion)
|
||||
assertTrue(prefs.isFirstRun)
|
||||
prefs.isFirstRun = false
|
||||
assertFalse(prefs.isFirstRun)
|
||||
assertThat(prefs.launchCount, equalTo(0))
|
||||
assertEquals(0, prefs.launchCount)
|
||||
prefs.incrementLaunchCount()
|
||||
assertThat(prefs.launchCount, equalTo(1))
|
||||
assertEquals(1, prefs.launchCount)
|
||||
}
|
||||
|
||||
@Test
|
||||
@Throws(Exception::class)
|
||||
fun testCheckmarks() {
|
||||
assertFalse(prefs.isCheckmarkSequenceReversed)
|
||||
prefs.isCheckmarkSequenceReversed = true
|
||||
@ -142,7 +130,6 @@ class PreferencesTest : BaseUnitTest() {
|
||||
}
|
||||
|
||||
@Test
|
||||
@Throws(Exception::class)
|
||||
fun testDeveloper() {
|
||||
assertFalse(prefs.isDeveloper)
|
||||
prefs.isDeveloper = true
|
||||
@ -150,7 +137,6 @@ class PreferencesTest : BaseUnitTest() {
|
||||
}
|
||||
|
||||
@Test
|
||||
@Throws(Exception::class)
|
||||
fun testFiltering() {
|
||||
assertFalse(prefs.showArchived)
|
||||
assertTrue(prefs.showCompleted)
|
||||
@ -161,7 +147,6 @@ class PreferencesTest : BaseUnitTest() {
|
||||
}
|
||||
|
||||
@Test
|
||||
@Throws(Exception::class)
|
||||
fun testMidnightDelay() {
|
||||
assertFalse(prefs.isMidnightDelayEnabled)
|
||||
prefs.isMidnightDelayEnabled = true
|
||||
@ -20,14 +20,7 @@ package org.isoron.uhabits.core
|
||||
|
||||
import org.apache.commons.io.IOUtils
|
||||
import org.isoron.platform.io.JavaDatabaseOpener
|
||||
import org.isoron.platform.io.TestDatabaseHelper
|
||||
import org.isoron.platform.time.LocalDate
|
||||
import org.isoron.platform.time.setToday
|
||||
import org.isoron.uhabits.core.commands.CommandRunner
|
||||
import org.isoron.uhabits.core.models.HabitList
|
||||
import org.isoron.uhabits.core.models.ModelFactory
|
||||
import org.isoron.uhabits.core.models.memory.MemoryModelFactory
|
||||
import org.isoron.uhabits.core.tasks.SingleThreadTaskRunner
|
||||
import org.isoron.uhabits.core.test.HabitFixtures
|
||||
import org.junit.After
|
||||
import org.junit.Before
|
||||
@ -46,24 +39,14 @@ import java.util.GregorianCalendar
|
||||
import java.util.TimeZone
|
||||
|
||||
@RunWith(MockitoJUnitRunner::class)
|
||||
open class BaseUnitTest {
|
||||
protected open lateinit var habitList: HabitList
|
||||
protected lateinit var fixtures: HabitFixtures
|
||||
protected lateinit var modelFactory: ModelFactory
|
||||
protected lateinit var taskRunner: SingleThreadTaskRunner
|
||||
protected open lateinit var commandRunner: CommandRunner
|
||||
open class JvmBaseUnitTest : BaseUnitTest() {
|
||||
protected var databaseOpener: org.isoron.platform.io.DatabaseOpener = JavaDatabaseOpener()
|
||||
|
||||
@Before
|
||||
@Throws(Exception::class)
|
||||
open fun setUp() {
|
||||
setToday(LocalDate(2015, 1, 25))
|
||||
val memoryModelFactory = MemoryModelFactory()
|
||||
habitList = spy(memoryModelFactory.buildHabitList())
|
||||
fixtures = HabitFixtures(memoryModelFactory, habitList)
|
||||
modelFactory = memoryModelFactory
|
||||
taskRunner = SingleThreadTaskRunner()
|
||||
commandRunner = CommandRunner(taskRunner)
|
||||
override fun setUp() {
|
||||
super.setUp()
|
||||
habitList = spy(habitList)
|
||||
fixtures = HabitFixtures(modelFactory as MemoryModelFactory, habitList)
|
||||
}
|
||||
|
||||
@After
|
||||
@ -112,10 +95,4 @@ open class BaseUnitTest {
|
||||
IOUtils.copy(original, FileOutputStream(tmpDbFile))
|
||||
return databaseOpener.open(tmpDbFile.absolutePath)
|
||||
}
|
||||
|
||||
companion object {
|
||||
fun buildMemoryDatabase(): org.isoron.platform.io.Database {
|
||||
return TestDatabaseHelper.createEmptyDatabase()
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -27,11 +27,11 @@ import org.isoron.platform.io.JavaFileOpener
|
||||
import org.isoron.platform.io.migrateTo
|
||||
import org.isoron.platform.io.querySingle
|
||||
import org.isoron.platform.io.run
|
||||
import org.isoron.uhabits.core.BaseUnitTest
|
||||
import org.isoron.uhabits.core.JvmBaseUnitTest
|
||||
import org.junit.Test
|
||||
import org.junit.jupiter.api.Assertions.assertThrows
|
||||
|
||||
class Version22Test : BaseUnitTest() {
|
||||
class Version22Test : JvmBaseUnitTest() {
|
||||
private lateinit var db: Database
|
||||
|
||||
@Throws(Exception::class)
|
||||
|
||||
@ -26,10 +26,10 @@ import org.isoron.platform.io.Database
|
||||
import org.isoron.platform.io.JavaFileOpener
|
||||
import org.isoron.platform.io.migrateTo
|
||||
import org.isoron.platform.io.query
|
||||
import org.isoron.uhabits.core.BaseUnitTest
|
||||
import org.isoron.uhabits.core.JvmBaseUnitTest
|
||||
import org.junit.Test
|
||||
|
||||
class Version23Test : BaseUnitTest() {
|
||||
class Version23Test : JvmBaseUnitTest() {
|
||||
|
||||
private lateinit var db: Database
|
||||
|
||||
|
||||
@ -19,7 +19,7 @@
|
||||
package org.isoron.uhabits.core.io
|
||||
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import org.isoron.uhabits.core.BaseUnitTest
|
||||
import org.isoron.uhabits.core.JvmBaseUnitTest
|
||||
import org.isoron.uhabits.core.models.Habit
|
||||
import org.junit.Before
|
||||
import org.junit.Test
|
||||
@ -32,7 +32,7 @@ import java.util.zip.ZipInputStream
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.test.assertTrue
|
||||
|
||||
class HabitsCSVExporterTest : BaseUnitTest() {
|
||||
class HabitsCSVExporterTest : JvmBaseUnitTest() {
|
||||
private lateinit var baseDir: File
|
||||
|
||||
@Before
|
||||
|
||||
@ -24,7 +24,7 @@ import org.hamcrest.core.IsEqual.equalTo
|
||||
import org.isoron.platform.io.JavaFileOpener
|
||||
import org.isoron.platform.io.JavaUserFile
|
||||
import org.isoron.platform.time.LocalDate
|
||||
import org.isoron.uhabits.core.BaseUnitTest
|
||||
import org.isoron.uhabits.core.JvmBaseUnitTest
|
||||
import org.isoron.uhabits.core.models.Entry
|
||||
import org.isoron.uhabits.core.models.Frequency
|
||||
import org.isoron.uhabits.core.models.Habit
|
||||
@ -36,7 +36,7 @@ import java.io.IOException
|
||||
import kotlin.test.assertFalse
|
||||
import kotlin.test.assertTrue
|
||||
|
||||
class ImportTest : BaseUnitTest() {
|
||||
class ImportTest : JvmBaseUnitTest() {
|
||||
@Before
|
||||
@Throws(Exception::class)
|
||||
override fun setUp() {
|
||||
|
||||
@ -20,7 +20,7 @@ package org.isoron.uhabits.core.models.sqlite
|
||||
|
||||
import org.hamcrest.CoreMatchers.equalTo
|
||||
import org.hamcrest.MatcherAssert.assertThat
|
||||
import org.isoron.uhabits.core.BaseUnitTest
|
||||
import org.isoron.uhabits.core.JvmBaseUnitTest
|
||||
import org.isoron.uhabits.core.database.HabitRepository
|
||||
import org.isoron.uhabits.core.models.Habit
|
||||
import org.isoron.uhabits.core.models.HabitList
|
||||
@ -36,7 +36,7 @@ import org.mockito.kotlin.verify
|
||||
import java.util.ArrayList
|
||||
import kotlin.test.assertNull
|
||||
|
||||
class SQLiteHabitListTest : BaseUnitTest() {
|
||||
class SQLiteHabitListTest : JvmBaseUnitTest() {
|
||||
private lateinit var repository: HabitRepository
|
||||
private var listener: ModelObservable.Listener = mock()
|
||||
private lateinit var habitsArray: ArrayList<Habit>
|
||||
|
||||
@ -22,7 +22,7 @@ import org.isoron.platform.time.DateUtils
|
||||
import org.isoron.platform.time.DateUtils.removeTimezone
|
||||
import org.isoron.platform.time.DateUtils.setFixedLocalTime
|
||||
import org.isoron.platform.time.DateUtils.setFixedTimeZone
|
||||
import org.isoron.uhabits.core.BaseUnitTest
|
||||
import org.isoron.uhabits.core.JvmBaseUnitTest
|
||||
import org.isoron.uhabits.core.models.Habit
|
||||
import org.isoron.uhabits.core.models.Reminder
|
||||
import org.isoron.uhabits.core.models.WeekdayList
|
||||
@ -40,7 +40,7 @@ import org.mockito.kotlin.whenever
|
||||
import java.util.TimeZone
|
||||
|
||||
@RunWith(MockitoJUnitRunner::class)
|
||||
class ReminderSchedulerTest : BaseUnitTest() {
|
||||
class ReminderSchedulerTest : JvmBaseUnitTest() {
|
||||
private val habitId = 10L
|
||||
private lateinit var habit: Habit
|
||||
private lateinit var reminderScheduler: ReminderScheduler
|
||||
|
||||
@ -18,7 +18,7 @@
|
||||
*/
|
||||
package org.isoron.uhabits.core.tasks
|
||||
|
||||
import org.isoron.uhabits.core.BaseUnitTest
|
||||
import org.isoron.uhabits.core.JvmBaseUnitTest
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.junit.runners.JUnit4
|
||||
@ -26,7 +26,7 @@ import org.mockito.kotlin.inOrder
|
||||
import org.mockito.kotlin.mock
|
||||
|
||||
@RunWith(JUnit4::class)
|
||||
class SingleThreadTaskRunnerTest : BaseUnitTest() {
|
||||
class SingleThreadTaskRunnerTest : JvmBaseUnitTest() {
|
||||
private lateinit var runner: SingleThreadTaskRunner
|
||||
private var task: Task = mock()
|
||||
|
||||
|
||||
@ -21,7 +21,7 @@ package org.isoron.uhabits.core.ui.screens.habits.list
|
||||
import org.hamcrest.CoreMatchers.equalTo
|
||||
import org.hamcrest.MatcherAssert.assertThat
|
||||
import org.isoron.platform.time.LocalDate
|
||||
import org.isoron.uhabits.core.BaseUnitTest
|
||||
import org.isoron.uhabits.core.JvmBaseUnitTest
|
||||
import org.isoron.uhabits.core.commands.CreateRepetitionCommand
|
||||
import org.isoron.uhabits.core.commands.DeleteHabitsCommand
|
||||
import org.isoron.uhabits.core.models.Entry
|
||||
@ -31,7 +31,7 @@ import org.mockito.kotlin.reset
|
||||
import org.mockito.kotlin.verify
|
||||
import org.mockito.kotlin.verifyNoMoreInteractions
|
||||
|
||||
class HabitCardListCacheTest : BaseUnitTest() {
|
||||
class HabitCardListCacheTest : JvmBaseUnitTest() {
|
||||
private lateinit var cache: HabitCardListCache
|
||||
private lateinit var listener: HabitCardListCache.Listener
|
||||
var today = LocalDate(2015, 1, 25)
|
||||
|
||||
@ -22,7 +22,7 @@ import org.hamcrest.MatcherAssert.assertThat
|
||||
import org.hamcrest.Matchers.equalTo
|
||||
import org.isoron.platform.time.LocalDate
|
||||
import org.isoron.platform.time.getToday
|
||||
import org.isoron.uhabits.core.BaseUnitTest
|
||||
import org.isoron.uhabits.core.JvmBaseUnitTest
|
||||
import org.isoron.uhabits.core.preferences.Preferences
|
||||
import org.junit.Test
|
||||
import org.mockito.kotlin.mock
|
||||
@ -32,7 +32,7 @@ import kotlin.test.assertFalse
|
||||
import kotlin.test.assertNull
|
||||
import kotlin.test.assertTrue
|
||||
|
||||
class HintListTest : BaseUnitTest() {
|
||||
class HintListTest : JvmBaseUnitTest() {
|
||||
private lateinit var hintList: HintList
|
||||
private lateinit var hints: Array<String>
|
||||
|
||||
|
||||
@ -23,7 +23,7 @@ import org.hamcrest.MatcherAssert.assertThat
|
||||
import org.hamcrest.core.IsEqual.equalTo
|
||||
import org.isoron.platform.io.JavaUserFile
|
||||
import org.isoron.platform.time.getToday
|
||||
import org.isoron.uhabits.core.BaseUnitTest
|
||||
import org.isoron.uhabits.core.JvmBaseUnitTest
|
||||
import org.isoron.uhabits.core.models.Entry
|
||||
import org.isoron.uhabits.core.models.Habit
|
||||
import org.isoron.uhabits.core.preferences.Preferences
|
||||
@ -42,7 +42,7 @@ import java.nio.file.Files
|
||||
import kotlin.test.assertFalse
|
||||
import kotlin.test.assertTrue
|
||||
|
||||
class ListHabitsBehaviorTest : BaseUnitTest() {
|
||||
class ListHabitsBehaviorTest : JvmBaseUnitTest() {
|
||||
private val dirFinder: ListHabitsBehavior.DirFinder = mock()
|
||||
|
||||
private val prefs: Preferences = mock()
|
||||
|
||||
@ -20,7 +20,7 @@ package org.isoron.uhabits.core.ui.screens.habits.list
|
||||
|
||||
import org.hamcrest.MatcherAssert.assertThat
|
||||
import org.hamcrest.Matchers.equalTo
|
||||
import org.isoron.uhabits.core.BaseUnitTest
|
||||
import org.isoron.uhabits.core.JvmBaseUnitTest
|
||||
import org.isoron.uhabits.core.models.HabitList
|
||||
import org.isoron.uhabits.core.models.HabitMatcher
|
||||
import org.isoron.uhabits.core.preferences.Preferences
|
||||
@ -38,7 +38,7 @@ import org.mockito.kotlin.whenever
|
||||
import kotlin.test.assertFalse
|
||||
import kotlin.test.assertTrue
|
||||
|
||||
class ListHabitsMenuBehaviorTest : BaseUnitTest() {
|
||||
class ListHabitsMenuBehaviorTest : JvmBaseUnitTest() {
|
||||
private lateinit var behavior: ListHabitsMenuBehavior
|
||||
|
||||
private val screen: ListHabitsMenuBehavior.Screen = mock()
|
||||
|
||||
@ -20,7 +20,7 @@ package org.isoron.uhabits.core.ui.screens.habits.list
|
||||
|
||||
import org.hamcrest.MatcherAssert.assertThat
|
||||
import org.hamcrest.Matchers.equalTo
|
||||
import org.isoron.uhabits.core.BaseUnitTest
|
||||
import org.isoron.uhabits.core.JvmBaseUnitTest
|
||||
import org.isoron.uhabits.core.models.Habit
|
||||
import org.isoron.uhabits.core.models.PaletteColor
|
||||
import org.isoron.uhabits.core.ui.callbacks.OnColorPickedCallback
|
||||
@ -36,7 +36,7 @@ import kotlin.test.assertFalse
|
||||
import kotlin.test.assertNull
|
||||
import kotlin.test.assertTrue
|
||||
|
||||
class ListHabitsSelectionMenuBehaviorTest : BaseUnitTest() {
|
||||
class ListHabitsSelectionMenuBehaviorTest : JvmBaseUnitTest() {
|
||||
private val screen: ListHabitsSelectionMenuBehavior.Screen = mock()
|
||||
|
||||
private val adapter: ListHabitsSelectionMenuBehavior.Adapter = mock()
|
||||
|
||||
@ -22,7 +22,7 @@ import org.apache.commons.io.FileUtils
|
||||
import org.hamcrest.CoreMatchers.equalTo
|
||||
import org.hamcrest.MatcherAssert.assertThat
|
||||
import org.isoron.platform.io.JavaUserFile
|
||||
import org.isoron.uhabits.core.BaseUnitTest
|
||||
import org.isoron.uhabits.core.JvmBaseUnitTest
|
||||
import org.isoron.uhabits.core.models.Habit
|
||||
import org.junit.Test
|
||||
import org.mockito.kotlin.mock
|
||||
@ -30,7 +30,7 @@ import org.mockito.kotlin.verify
|
||||
import org.mockito.kotlin.whenever
|
||||
import java.nio.file.Files
|
||||
|
||||
class ShowHabitMenuPresenterTest : BaseUnitTest() {
|
||||
class ShowHabitMenuPresenterTest : JvmBaseUnitTest() {
|
||||
private lateinit var system: ShowHabitMenuPresenter.System
|
||||
private lateinit var screen: ShowHabitMenuPresenter.Screen
|
||||
private lateinit var habit: Habit
|
||||
|
||||
@ -20,7 +20,7 @@ package org.isoron.uhabits.core.ui.widgets
|
||||
|
||||
import org.isoron.platform.time.LocalDate
|
||||
import org.isoron.platform.time.getToday
|
||||
import org.isoron.uhabits.core.BaseUnitTest
|
||||
import org.isoron.uhabits.core.JvmBaseUnitTest
|
||||
import org.isoron.uhabits.core.commands.CreateRepetitionCommand
|
||||
import org.isoron.uhabits.core.models.Entry
|
||||
import org.isoron.uhabits.core.models.Entry.Companion.nextToggleValue
|
||||
@ -35,7 +35,7 @@ import org.mockito.kotlin.verify
|
||||
import org.mockito.kotlin.verifyNoInteractions
|
||||
import org.mockito.kotlin.whenever
|
||||
|
||||
class WidgetBehaviorTest : BaseUnitTest() {
|
||||
class WidgetBehaviorTest : JvmBaseUnitTest() {
|
||||
private lateinit var notificationTray: NotificationTray
|
||||
private lateinit var preferences: Preferences
|
||||
private lateinit var behavior: WidgetBehavior
|
||||
|
||||
@ -2,12 +2,12 @@ package org.isoron.uhabits.core.utils
|
||||
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import org.isoron.platform.io.JavaUserFile
|
||||
import org.isoron.uhabits.core.BaseUnitTest
|
||||
import org.isoron.uhabits.core.JvmBaseUnitTest
|
||||
import org.junit.Test
|
||||
import java.io.File
|
||||
import kotlin.test.assertTrue
|
||||
|
||||
class FileExtensionsTest : BaseUnitTest() {
|
||||
class FileExtensionsTest : JvmBaseUnitTest() {
|
||||
|
||||
@Test
|
||||
fun testIsSQLite3File() = runBlocking {
|
||||
|
||||
@ -4,7 +4,7 @@ import kotlinx.coroutines.asCoroutineDispatcher
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import kotlinx.coroutines.withContext
|
||||
import org.isoron.platform.time.DateUtils
|
||||
import org.isoron.uhabits.core.BaseUnitTest
|
||||
import org.isoron.uhabits.core.JvmBaseUnitTest
|
||||
import org.isoron.uhabits.core.io.StandardLogging
|
||||
import org.isoron.uhabits.core.preferences.Preferences
|
||||
import org.junit.After
|
||||
@ -17,7 +17,7 @@ import kotlin.coroutines.resume
|
||||
import kotlin.coroutines.suspendCoroutine
|
||||
import kotlin.test.assertEquals
|
||||
|
||||
class MidnightTimerTest : BaseUnitTest() {
|
||||
class MidnightTimerTest : JvmBaseUnitTest() {
|
||||
|
||||
@After
|
||||
override fun tearDown() {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user