Skip to content

Commit

Permalink
Move tests to commonTest; convert some classes from Swift to Kotlin
Browse files Browse the repository at this point in the history
  • Loading branch information
iSoron committed Apr 14, 2019
1 parent d463bb5 commit fe4139e
Show file tree
Hide file tree
Showing 20 changed files with 247 additions and 247 deletions.
8 changes: 8 additions & 0 deletions core/src/commonTest/kotlin/org/isoron/DependencyResolver.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,22 @@ 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
fun exportCanvas(canvas: Canvas, filename: String)
}

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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class DatabaseTest {

@Test
fun testUsage() = asyncTest{
if (!DependencyResolver.supportsDatabaseTests) return@asyncTest
val db = DependencyResolver.getDatabase()

db.setVersion(0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,39 +17,35 @@
* 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)
private val d2 = LocalDate(2019, 4, 4)
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))
assertEquals(today.plus(42), LocalDate(2019, 5, 6))
}

@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))
assertEquals("Mar", fmt.shortMonthName(d1))
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))
Expand All @@ -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))
Expand All @@ -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)))
Expand All @@ -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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand Down
55 changes: 55 additions & 0 deletions core/src/iosMain/kotlin/org/isoron/platform/time/IosDates.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright (C) 2016-2019 Álinson Santos Xavier <[email protected]>
*
* 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())
}

}
67 changes: 67 additions & 0 deletions core/src/iosMain/kotlin/org/isoron/uhabits/IosLocaleHelper.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Copyright (C) 2016-2019 Álinson Santos Xavier <[email protected]>
*
* 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()
}
}
}
12 changes: 11 additions & 1 deletion core/src/iosTest/kotlin/org/isoron/DependencyResolver.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Loading

0 comments on commit fe4139e

Please sign in to comment.