mirror of
https://github.com/iSoron/uhabits.git
synced 2025-12-06 09:08:52 -06:00
Move tests to commonTest; convert some classes from Swift to Kotlin
This commit is contained in:
@@ -21,6 +21,11 @@ package org.isoron
|
||||
|
||||
import org.isoron.platform.gui.*
|
||||
import org.isoron.platform.io.*
|
||||
import org.isoron.platform.time.*
|
||||
|
||||
enum class Locale {
|
||||
US, JAPAN
|
||||
}
|
||||
|
||||
interface CanvasHelper {
|
||||
fun createCanvas(width: Int, height: Int): Canvas
|
||||
@@ -28,7 +33,10 @@ interface CanvasHelper {
|
||||
}
|
||||
|
||||
expect object DependencyResolver {
|
||||
val supportsDatabaseTests: Boolean
|
||||
val supportsCanvasTests: Boolean
|
||||
suspend fun getFileOpener(): FileOpener
|
||||
suspend fun getDatabase(): Database
|
||||
fun getCanvasHelper(): CanvasHelper
|
||||
fun getDateFormatter(locale: Locale): LocalDateFormatter
|
||||
}
|
||||
@@ -25,6 +25,7 @@ import kotlin.test.*
|
||||
class CanvasTest {
|
||||
@Test
|
||||
fun run() {
|
||||
if (!DependencyResolver.supportsCanvasTests) return
|
||||
val helper = DependencyResolver.getCanvasHelper()
|
||||
val canvas = helper.createCanvas(500, 400)
|
||||
|
||||
|
||||
@@ -26,6 +26,7 @@ class DatabaseTest {
|
||||
|
||||
@Test
|
||||
fun testUsage() = asyncTest{
|
||||
if (!DependencyResolver.supportsDatabaseTests) return@asyncTest
|
||||
val db = DependencyResolver.getDatabase()
|
||||
|
||||
db.setVersion(0)
|
||||
|
||||
@@ -17,14 +17,10 @@
|
||||
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package org.isoron.platform
|
||||
|
||||
import junit.framework.TestCase.*
|
||||
import org.isoron.platform.time.*
|
||||
import org.junit.*
|
||||
import java.util.*
|
||||
import java.util.Calendar.*
|
||||
package org.isoron.platform.time
|
||||
|
||||
import org.isoron.*
|
||||
import kotlin.test.*
|
||||
|
||||
class DatesTest {
|
||||
private val d1 = LocalDate(2019, 3, 25)
|
||||
@@ -32,7 +28,7 @@ class DatesTest {
|
||||
private val d3 = LocalDate(2019, 5, 12)
|
||||
|
||||
@Test
|
||||
fun plusMinusDays() {
|
||||
fun testPlusMinusDays() {
|
||||
val today = LocalDate(2019, 3, 25)
|
||||
assertEquals(today.minus(28), LocalDate(2019, 2, 25))
|
||||
assertEquals(today.plus(7), LocalDate(2019, 4, 1))
|
||||
@@ -40,8 +36,8 @@ class DatesTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
fun shortMonthName() {
|
||||
var fmt = JavaLocalDateFormatter(Locale.US)
|
||||
fun testFormatter() {
|
||||
var fmt = DependencyResolver.getDateFormatter(Locale.US)
|
||||
assertEquals("Mon", fmt.shortWeekdayName(d1))
|
||||
assertEquals("Thu", fmt.shortWeekdayName(d2))
|
||||
assertEquals("Sun", fmt.shortWeekdayName(d3))
|
||||
@@ -49,7 +45,7 @@ class DatesTest {
|
||||
assertEquals("Apr", fmt.shortMonthName(d2))
|
||||
assertEquals("May", fmt.shortMonthName(d3))
|
||||
|
||||
fmt = JavaLocalDateFormatter(Locale.JAPAN)
|
||||
fmt = DependencyResolver.getDateFormatter(Locale.JAPAN)
|
||||
assertEquals("月", fmt.shortWeekdayName(d1))
|
||||
assertEquals("木", fmt.shortWeekdayName(d2))
|
||||
assertEquals("日", fmt.shortWeekdayName(d3))
|
||||
@@ -59,13 +55,7 @@ class DatesTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
fun weekDay() {
|
||||
assertEquals(DayOfWeek.SUNDAY, LocalDate(2015, 1, 25).dayOfWeek)
|
||||
assertEquals(DayOfWeek.MONDAY, LocalDate(2017, 7, 3).dayOfWeek)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun timestamps() {
|
||||
fun testTimestamps() {
|
||||
val timestamps = listOf(Timestamp(1555977600000),
|
||||
Timestamp(968716800000),
|
||||
Timestamp(946684800000))
|
||||
@@ -77,7 +67,7 @@ class DatesTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
fun isOlderThan() {
|
||||
fun testIsOlderThan() {
|
||||
val ref = LocalDate(2010, 10, 5)
|
||||
assertTrue(ref.isOlderThan(LocalDate(2010, 10, 10)))
|
||||
assertTrue(ref.isOlderThan(LocalDate(2010, 11, 4)))
|
||||
@@ -104,39 +94,49 @@ class DatesTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
fun gregorianCalendarConversion() {
|
||||
fun check(cal: GregorianCalendar, daysSince2000: Int) {
|
||||
val year = cal.get(YEAR)
|
||||
val month = cal.get(MONTH) + 1
|
||||
val day = cal.get(DAY_OF_MONTH)
|
||||
val weekday = cal.get(DAY_OF_WEEK)
|
||||
val date = LocalDate(year, month, day)
|
||||
val millisSince1970 = cal.timeInMillis
|
||||
val msg = "date=$year-$month-$day offset=$daysSince2000"
|
||||
|
||||
assertEquals(msg, daysSince2000, date.daysSince2000)
|
||||
assertEquals(msg, year, date.year)
|
||||
assertEquals(msg, month, date.month)
|
||||
assertEquals(msg, day, date.day)
|
||||
assertEquals(msg, weekday, date.dayOfWeek.index + 1)
|
||||
assertEquals(msg, millisSince1970, date.timestamp.millisSince1970)
|
||||
assertEquals(msg, date, date.timestamp.localDate)
|
||||
fun testGregorianCalendarConversion() {
|
||||
fun check(daysSince2000: Int,
|
||||
expectedYear: Int,
|
||||
expectedMonth: Int,
|
||||
expectedDay: Int,
|
||||
expectedWeekday: Int) {
|
||||
val date = LocalDate(daysSince2000)
|
||||
assertEquals(expectedYear, date.year)
|
||||
assertEquals(expectedMonth, date.month)
|
||||
assertEquals(expectedDay, date.day)
|
||||
assertEquals(expectedWeekday, date.dayOfWeek.index)
|
||||
assertEquals(date, date.timestamp.localDate)
|
||||
}
|
||||
|
||||
val cal = GregorianCalendar()
|
||||
cal.timeZone = TimeZone.getTimeZone("GMT")
|
||||
cal.set(MILLISECOND, 0)
|
||||
cal.set(SECOND, 0)
|
||||
cal.set(MINUTE, 0)
|
||||
cal.set(HOUR_OF_DAY, 0)
|
||||
cal.set(DAY_OF_MONTH, 1)
|
||||
cal.set(MONTH, 0)
|
||||
cal.set(YEAR, 2000)
|
||||
|
||||
// Check all dates from year 2000 until 2400
|
||||
for(offset in 0..146097) {
|
||||
check(cal, offset)
|
||||
cal.add(DAY_OF_YEAR, 1)
|
||||
}
|
||||
check(0, 2000, 1, 1, 6)
|
||||
check(626, 2001, 9, 18, 2)
|
||||
check(915, 2002, 7, 4, 4)
|
||||
check(2759, 2007, 7, 22, 0)
|
||||
check(2791, 2007, 8, 23, 4)
|
||||
check(6524, 2017, 11, 11, 6)
|
||||
check(7517, 2020, 7, 31, 5)
|
||||
check(10031, 2027, 6, 19, 6)
|
||||
check(13091, 2035, 11, 4, 0)
|
||||
check(14849, 2040, 8, 27, 1)
|
||||
check(17330, 2047, 6, 13, 4)
|
||||
check(20566, 2056, 4, 22, 6)
|
||||
check(23617, 2064, 8, 29, 5)
|
||||
check(27743, 2075, 12, 16, 1)
|
||||
check(31742, 2086, 11, 27, 3)
|
||||
check(36659, 2100, 5, 15, 6)
|
||||
check(39224, 2107, 5, 24, 2)
|
||||
check(39896, 2109, 3, 26, 2)
|
||||
check(40819, 2111, 10, 5, 1)
|
||||
check(43983, 2120, 6, 3, 1)
|
||||
check(46893, 2128, 5, 22, 6)
|
||||
check(51013, 2139, 9, 2, 3)
|
||||
check(55542, 2152, 1, 26, 3)
|
||||
check(58817, 2161, 1, 13, 2)
|
||||
check(63769, 2174, 8, 5, 5)
|
||||
check(64893, 2177, 9, 2, 2)
|
||||
check(66840, 2183, 1, 1, 3)
|
||||
check(68011, 2186, 3, 17, 5)
|
||||
check(70060, 2191, 10, 26, 3)
|
||||
check(70733, 2193, 8, 29, 4)
|
||||
}
|
||||
}
|
||||
@@ -28,6 +28,7 @@ import kotlin.test.*
|
||||
class CheckmarkRepositoryTest() {
|
||||
@Test
|
||||
fun testCRUD() = asyncTest {
|
||||
if (!DependencyResolver.supportsDatabaseTests) return@asyncTest
|
||||
val db = DependencyResolver.getDatabase()
|
||||
|
||||
val habitA = 10
|
||||
|
||||
@@ -26,6 +26,7 @@ import kotlin.test.*
|
||||
class HabitRepositoryTest() {
|
||||
@Test
|
||||
fun testCRUD() = asyncTest{
|
||||
if (!DependencyResolver.supportsDatabaseTests) return@asyncTest
|
||||
val db = DependencyResolver.getDatabase()
|
||||
val original0 = Habit(id = 0,
|
||||
name = "Wake up early",
|
||||
|
||||
@@ -26,6 +26,7 @@ import kotlin.test.*
|
||||
class PreferencesRepositoryTest() {
|
||||
@Test
|
||||
fun testUsage() = asyncTest{
|
||||
if (!DependencyResolver.supportsDatabaseTests) return@asyncTest
|
||||
val db = DependencyResolver.getDatabase()
|
||||
val prefs = PreferencesRepository(db)
|
||||
assertEquals("default", prefs.getString("non_existing_key", "default"))
|
||||
|
||||
55
core/src/iosMain/kotlin/org/isoron/platform/time/IosDates.kt
Normal file
55
core/src/iosMain/kotlin/org/isoron/platform/time/IosDates.kt
Normal file
@@ -0,0 +1,55 @@
|
||||
/*
|
||||
* Copyright (C) 2016-2019 Álinson Santos Xavier <isoron@gmail.com>
|
||||
*
|
||||
* 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.platform.time
|
||||
|
||||
import platform.Foundation.*
|
||||
|
||||
fun LocalDate.toNsDate(): NSDate {
|
||||
val calendar = NSCalendar.calendarWithIdentifier(NSCalendarIdentifierGregorian)!!
|
||||
val dc = NSDateComponents()
|
||||
dc.year = year.toLong()
|
||||
dc.month = month.toLong()
|
||||
dc.day = day.toLong()
|
||||
dc.hour = 13
|
||||
dc.minute = 0
|
||||
return calendar.dateFromComponents(dc)!!
|
||||
}
|
||||
|
||||
class IosLocalDateFormatter(val locale: String) : LocalDateFormatter {
|
||||
|
||||
constructor() : this(NSLocale.preferredLanguages[0] as String)
|
||||
|
||||
private val fmt = NSDateFormatter()
|
||||
|
||||
init {
|
||||
fmt.setLocale(NSLocale.localeWithLocaleIdentifier(locale))
|
||||
}
|
||||
|
||||
override fun shortWeekdayName(date: LocalDate): String {
|
||||
fmt.dateFormat = "EEE"
|
||||
return fmt.stringFromDate(date.toNsDate())
|
||||
}
|
||||
|
||||
override fun shortMonthName(date: LocalDate): String {
|
||||
fmt.dateFormat = "MMM"
|
||||
return fmt.stringFromDate(date.toNsDate())
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
/*
|
||||
* Copyright (C) 2016-2019 Álinson Santos Xavier <isoron@gmail.com>
|
||||
*
|
||||
* 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/>.
|
||||
*/
|
||||
|
||||
@file:Suppress("UNCHECKED_CAST")
|
||||
|
||||
package org.isoron.uhabits
|
||||
|
||||
import org.isoron.platform.io.*
|
||||
import org.isoron.uhabits.i18n.*
|
||||
import platform.Foundation.*
|
||||
|
||||
class IosLocaleHelper(private val log: Log) : LocaleHelper {
|
||||
override fun getStringsForCurrentLocale(): Strings {
|
||||
val pref = NSLocale.preferredLanguages as List<String>
|
||||
val lang = if (pref.isEmpty()) "en-US" else pref[0]
|
||||
log.info("IosLocaleHelper", lang)
|
||||
return when {
|
||||
lang.startsWith("ar") -> StringsArabic()
|
||||
lang.startsWith("ca") -> StringsCatalan()
|
||||
lang.startsWith("cs") -> StringsCzech()
|
||||
lang.startsWith("da") -> StringsDanish()
|
||||
lang.startsWith("de") -> StringsGerman()
|
||||
lang.startsWith("el") -> StringsGreek()
|
||||
lang.startsWith("es") -> StringsSpanish()
|
||||
lang.startsWith("fi") -> StringsFinnish()
|
||||
lang.startsWith("fr") -> StringsFrench()
|
||||
lang.startsWith("he") -> StringsHebrew()
|
||||
lang.startsWith("hi") -> StringsHindi()
|
||||
lang.startsWith("hr") -> StringsCroatian()
|
||||
lang.startsWith("hu") -> StringsHungarian()
|
||||
lang.startsWith("id") -> StringsIndonesian()
|
||||
lang.startsWith("it") -> StringsItalian()
|
||||
lang.startsWith("ja") -> StringsJapanese()
|
||||
lang.startsWith("ko") -> StringsKorean()
|
||||
lang.startsWith("nb") -> StringsNorwegian()
|
||||
lang.startsWith("nl") -> StringsDutch()
|
||||
lang.startsWith("pl") -> StringsPolish()
|
||||
lang.startsWith("pt-BR") -> StringsPortugueseBR()
|
||||
lang.startsWith("pt") -> StringsPortuguesePT()
|
||||
lang.startsWith("ro") -> StringsRomanian()
|
||||
lang.startsWith("ru") -> StringsRussian()
|
||||
lang.startsWith("sk") -> StringsSlovak()
|
||||
lang.startsWith("sv") -> StringsSwedish()
|
||||
lang.startsWith("tr") -> StringsTurkish()
|
||||
lang.startsWith("vi") -> StringsVietnamese()
|
||||
lang.startsWith("zh-Hans") -> StringsChineseCN()
|
||||
lang.startsWith("zh-Hant") -> StringsChineseTW()
|
||||
else -> Strings()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -20,12 +20,22 @@
|
||||
package org.isoron
|
||||
|
||||
import org.isoron.platform.io.*
|
||||
import org.isoron.platform.time.*
|
||||
|
||||
actual object DependencyResolver {
|
||||
actual suspend fun getFileOpener(): FileOpener = IosFileOpener()
|
||||
|
||||
actual fun getDateFormatter(locale: Locale): LocalDateFormatter {
|
||||
return when(locale) {
|
||||
Locale.US -> IosLocalDateFormatter("en-US")
|
||||
Locale.JAPAN -> IosLocalDateFormatter("ja-JP")
|
||||
}
|
||||
}
|
||||
|
||||
// IosDatabase and IosCanvas are currently implemented in Swift, so we
|
||||
// cannot test these classes here.
|
||||
// cannot test these classes here. The tests will be skipped.
|
||||
actual suspend fun getDatabase(): Database = TODO()
|
||||
actual fun getCanvasHelper(): CanvasHelper = TODO()
|
||||
actual val supportsDatabaseTests = false
|
||||
actual val supportsCanvasTests = false
|
||||
}
|
||||
38
core/src/jsMain/kotlin/org/isoron/platform/time/JsDates.kt
Normal file
38
core/src/jsMain/kotlin/org/isoron/platform/time/JsDates.kt
Normal file
@@ -0,0 +1,38 @@
|
||||
/*
|
||||
* Copyright (C) 2016-2019 Álinson Santos Xavier <isoron@gmail.com>
|
||||
*
|
||||
* 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.platform.time
|
||||
|
||||
import kotlin.js.*
|
||||
|
||||
fun LocalDate.toJsDate(): Date {
|
||||
return Date(year, month - 1, day)
|
||||
}
|
||||
|
||||
class JsDateFormatter(private val locale: String) : LocalDateFormatter {
|
||||
override fun shortWeekdayName(date: LocalDate): String {
|
||||
val options = dateLocaleOptions { weekday = "short" }
|
||||
return date.toJsDate().toLocaleString(locale, options)
|
||||
}
|
||||
|
||||
override fun shortMonthName(date: LocalDate): String {
|
||||
val options = dateLocaleOptions { month = "short" }
|
||||
return date.toJsDate().toLocaleString(locale, options)
|
||||
}
|
||||
}
|
||||
@@ -21,12 +21,14 @@ package org.isoron
|
||||
|
||||
import org.isoron.platform.gui.*
|
||||
import org.isoron.platform.io.*
|
||||
import org.isoron.platform.time.*
|
||||
import org.isoron.uhabits.*
|
||||
import org.w3c.dom.*
|
||||
import kotlin.browser.*
|
||||
|
||||
actual object DependencyResolver {
|
||||
|
||||
actual val supportsDatabaseTests = true
|
||||
actual val supportsCanvasTests = true
|
||||
var fileOpener: JsFileOpener? = null
|
||||
|
||||
actual suspend fun getFileOpener(): FileOpener {
|
||||
@@ -48,6 +50,13 @@ actual object DependencyResolver {
|
||||
actual fun getCanvasHelper(): CanvasHelper {
|
||||
return JsCanvasHelper()
|
||||
}
|
||||
|
||||
actual fun getDateFormatter(locale: Locale): LocalDateFormatter {
|
||||
return when(locale) {
|
||||
Locale.US -> JsDateFormatter("en-US")
|
||||
Locale.JAPAN -> JsDateFormatter("ja-JP")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class JsCanvasHelper : CanvasHelper {
|
||||
|
||||
@@ -22,7 +22,6 @@ package org.isoron.platform.time
|
||||
import java.util.*
|
||||
import java.util.Calendar.*
|
||||
|
||||
|
||||
fun LocalDate.toGregorianCalendar(): GregorianCalendar {
|
||||
val cal = GregorianCalendar()
|
||||
cal.timeZone = TimeZone.getTimeZone("GMT")
|
||||
@@ -36,12 +35,6 @@ fun LocalDate.toGregorianCalendar(): GregorianCalendar {
|
||||
return cal
|
||||
}
|
||||
|
||||
fun GregorianCalendar.toLocalDate(): LocalDate {
|
||||
return LocalDate(this.get(YEAR),
|
||||
this.get(MONTH) + 1,
|
||||
this.get(DAY_OF_MONTH))
|
||||
}
|
||||
|
||||
class JavaLocalDateFormatter(private val locale: Locale) : LocalDateFormatter {
|
||||
override fun shortMonthName(date: LocalDate): String {
|
||||
val cal = date.toGregorianCalendar()
|
||||
|
||||
@@ -21,12 +21,16 @@ package org.isoron
|
||||
|
||||
import org.isoron.platform.gui.*
|
||||
import org.isoron.platform.io.*
|
||||
import org.isoron.platform.time.*
|
||||
import org.isoron.uhabits.*
|
||||
import java.awt.image.*
|
||||
import java.io.*
|
||||
import javax.imageio.*
|
||||
|
||||
actual object DependencyResolver {
|
||||
actual val supportsDatabaseTests = true
|
||||
actual val supportsCanvasTests = true
|
||||
|
||||
actual suspend fun getFileOpener(): FileOpener = JavaFileOpener()
|
||||
actual fun getCanvasHelper(): CanvasHelper = JavaCanvasHelper()
|
||||
|
||||
@@ -42,6 +46,12 @@ actual object DependencyResolver {
|
||||
return db
|
||||
}
|
||||
|
||||
actual fun getDateFormatter(locale: Locale): LocalDateFormatter {
|
||||
return when(locale) {
|
||||
Locale.US -> JavaLocalDateFormatter(java.util.Locale.US)
|
||||
Locale.JAPAN -> JavaLocalDateFormatter(java.util.Locale.JAPAN)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class JavaCanvasHelper : CanvasHelper {
|
||||
|
||||
Reference in New Issue
Block a user