Unify async tests

pull/498/head
Alinson S. Xavier 7 years ago
parent 5ea19c9475
commit d463bb55d7

@ -23,7 +23,6 @@ enum class TextAlign {
LEFT, CENTER, RIGHT
}
enum class Font {
REGULAR,
BOLD,

@ -19,8 +19,6 @@
package org.isoron.platform.io
import org.isoron.uhabits.*
interface PreparedStatement {
fun step(): StepResult
fun finalize()

@ -35,5 +35,4 @@ class StandardLog : Log {
override fun debug(tag: String, msg: String) {
println("D/$tag $msg")
}
}

@ -47,8 +47,8 @@ class Backend(private val databaseName: String,
private val fileOpener: FileOpener,
private val localeHelper: LocaleHelper,
private val log: Log,
private val crCtx: CoroutineContext
) : CoroutineScope by BackendScope(crCtx, log) {
private val scope: CoroutineContext
) : CoroutineScope by BackendScope(scope, log) {
private lateinit var database: Database

@ -0,0 +1,26 @@
/*
* 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
/**
* Workaround until Kotlin adds support for testing suspend functions
* https://youtrack.jetbrains.com/issue/KT-22228
*/
expect fun asyncTest(block: suspend () -> Unit)

@ -0,0 +1,34 @@
/*
* 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
import org.isoron.platform.gui.*
import org.isoron.platform.io.*
interface CanvasHelper {
fun createCanvas(width: Int, height: Int): Canvas
fun exportCanvas(canvas: Canvas, filename: String)
}
expect object DependencyResolver {
suspend fun getFileOpener(): FileOpener
suspend fun getDatabase(): Database
fun getCanvasHelper(): CanvasHelper
}

@ -19,9 +19,14 @@
package org.isoron.platform.gui
class CanvasTest(val platform: Platform) {
import org.isoron.*
import kotlin.test.*
class CanvasTest {
@Test
fun run() {
val canvas = platform.createCanvas(500, 400)
val helper = DependencyResolver.getCanvasHelper()
val canvas = helper.createCanvas(500, 400)
canvas.setColor(Color(0x303030))
canvas.fillRect(0.0, 0.0, 500.0, 400.0)
@ -61,11 +66,6 @@ class CanvasTest(val platform: Platform) {
canvas.setFont(Font.FONT_AWESOME)
canvas.drawText(FontAwesome.CHECK, 250.0, 300.0)
platform.exportCanvas(canvas, "CanvasTest.png")
}
interface Platform {
fun createCanvas(width: Int, height: Int): Canvas
fun exportCanvas(canvas: Canvas, filename: String)
helper.exportCanvas(canvas, "CanvasTest.png")
}
}

@ -19,10 +19,15 @@
package org.isoron.platform.io
import org.isoron.*
import kotlin.test.*
class DatabaseTest(val db: Database) {
fun testUsage() {
class DatabaseTest {
@Test
fun testUsage() = asyncTest{
val db = DependencyResolver.getDatabase()
db.setVersion(0)
assertEquals(0, db.getVersion())

@ -22,8 +22,11 @@ package org.isoron.platform.io
import org.isoron.*
import kotlin.test.*
class FilesTest(val fileOpener: FileOpener) {
suspend fun testLines() {
class FilesTest() {
@Test
fun testLines() = asyncTest {
val fileOpener = DependencyResolver.getFileOpener()
assertFalse(fileOpener.openUserFile("non-existing-usr.txt").exists(),
"non-existing-usr.txt shouldn't exist")

@ -25,8 +25,11 @@ import org.isoron.platform.time.*
import kotlin.test.*
class CheckmarkRepositoryTest(val db: Database) {
fun testCRUD() {
class CheckmarkRepositoryTest() {
@Test
fun testCRUD() = asyncTest {
val db = DependencyResolver.getDatabase()
val habitA = 10
var checkmarksA = listOf(Checkmark(LocalDate(2019, 1, 15), 100),
Checkmark(LocalDate(2019, 1, 7), 500),

@ -21,11 +21,12 @@ package org.isoron.uhabits.models
import org.isoron.*
import org.isoron.platform.gui.*
import org.isoron.platform.io.*
import kotlin.test.*
class HabitRepositoryTest(val db: Database) {
fun testCRUD() {
class HabitRepositoryTest() {
@Test
fun testCRUD() = asyncTest{
val db = DependencyResolver.getDatabase()
val original0 = Habit(id = 0,
name = "Wake up early",
description = "Did you wake up before 6am?",

@ -19,11 +19,14 @@
package org.isoron.uhabits.models
import org.isoron.*
import org.isoron.platform.io.*
import kotlin.test.*
class PreferencesRepositoryTest(val db: Database) {
fun testUsage() {
class PreferencesRepositoryTest() {
@Test
fun testUsage() = asyncTest{
val db = DependencyResolver.getDatabase()
val prefs = PreferencesRepository(db)
assertEquals("default", prefs.getString("non_existing_key", "default"))
prefs.putString("ringtone_path", "/tmp")

@ -0,0 +1,31 @@
/*
* 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
import org.isoron.platform.io.*
actual object DependencyResolver {
actual suspend fun getFileOpener(): FileOpener = IosFileOpener()
// IosDatabase and IosCanvas are currently implemented in Swift, so we
// cannot test these classes here.
actual suspend fun getDatabase(): Database = TODO()
actual fun getCanvasHelper(): CanvasHelper = TODO()
}

@ -20,12 +20,9 @@
package org.isoron
import kotlinx.coroutines.*
import org.isoron.platform.io.*
import kotlin.test.*
class IosAsyncTests {
@Test
fun testFiles() = runBlocking {
FilesTest(IosFileOpener()).testLines()
}
}
/**
* Workaround until Kotlin adds support for testing suspend functions
* https://youtrack.jetbrains.com/issue/KT-22228
*/
actual fun asyncTest(block: suspend () -> Unit) = runBlocking { block() }

@ -17,14 +17,40 @@
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.isoron.platform
package org.isoron
import org.isoron.platform.gui.*
import org.isoron.platform.io.*
import org.isoron.uhabits.*
import org.w3c.dom.*
import kotlin.browser.*
import kotlin.test.*
class JsCanvasTest : CanvasTest.Platform {
actual object DependencyResolver {
var fileOpener: JsFileOpener? = null
actual suspend fun getFileOpener(): FileOpener {
if (fileOpener == null) {
val fs = JsFileStorage()
fs.init()
fileOpener = JsFileOpener(fs)
}
return fileOpener!!
}
actual suspend fun getDatabase(): Database {
val nativeDB = eval("new SQL.Database()")
val db = JsDatabase(nativeDB)
db.migrateTo(LOOP_DATABASE_VERSION, getFileOpener(), StandardLog())
return db
}
actual fun getCanvasHelper(): CanvasHelper {
return JsCanvasHelper()
}
}
class JsCanvasHelper : CanvasHelper {
override fun createCanvas(width: Int, height: Int): Canvas {
val canvasElement = document.getElementById("canvas") as HTMLCanvasElement
canvasElement.style.width = "${width}px"
@ -35,7 +61,4 @@ class JsCanvasTest : CanvasTest.Platform {
override fun exportCanvas(canvas: Canvas, filename: String) {
// do nothing
}
@Test
fun testDrawing() = CanvasTest(this).run()
}

@ -0,0 +1,28 @@
/*
* 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
import kotlinx.coroutines.*
/**
* Workaround until Kotlin adds support for testing suspend functions
* https://youtrack.jetbrains.com/issue/KT-22228
*/
actual fun asyncTest(block: suspend () -> Unit): dynamic = GlobalScope.promise { block() }

@ -1,70 +0,0 @@
/*
* 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
import kotlinx.coroutines.*
import org.isoron.platform.io.*
import org.isoron.uhabits.*
import org.isoron.uhabits.models.*
import kotlin.test.*
class JsAsyncTests {
var fs: JsFileStorage? = null
suspend fun getFileOpener(): FileOpener {
if (fs == null) {
fs = JsFileStorage()
fs?.init()
}
return JsFileOpener(fs!!)
}
suspend fun getDatabase(): Database {
val nativeDB = eval("new SQL.Database()")
val db = JsDatabase(nativeDB)
db.migrateTo(LOOP_DATABASE_VERSION, getFileOpener(), StandardLog())
return db
}
@Test
fun testFiles() = GlobalScope.promise {
FilesTest(getFileOpener()).testLines()
}
@Test
fun testDatabase() = GlobalScope.promise {
DatabaseTest(getDatabase()).testUsage()
}
@Test
fun testCheckmarkRepository() = GlobalScope.promise {
CheckmarkRepositoryTest(getDatabase()).testCRUD()
}
@Test
fun testHabitRepository() = GlobalScope.promise {
HabitRepositoryTest(getDatabase()).testCRUD()
}
@Test
fun testPreferencesRepository() = GlobalScope.promise {
PreferencesRepositoryTest(getDatabase()).testUsage()
}
}

@ -17,15 +17,34 @@
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.isoron.platform
package org.isoron
import org.isoron.platform.gui.*
import org.junit.*
import org.isoron.platform.io.*
import org.isoron.uhabits.*
import java.awt.image.*
import java.io.*
import javax.imageio.*
class JavaCanvasTest : CanvasTest.Platform {
actual object DependencyResolver {
actual suspend fun getFileOpener(): FileOpener = JavaFileOpener()
actual fun getCanvasHelper(): CanvasHelper = JavaCanvasHelper()
actual suspend fun getDatabase(): Database {
val log = StandardLog()
val fileOpener = JavaFileOpener()
val databaseOpener = JavaDatabaseOpener(log)
val dbFile = fileOpener.openUserFile("test.sqlite3")
if (dbFile.exists()) dbFile.delete()
val db = databaseOpener.open(dbFile)
db.migrateTo(LOOP_DATABASE_VERSION, fileOpener, log)
return db
}
}
class JavaCanvasHelper : CanvasHelper {
override fun createCanvas(width: Int, height: Int): Canvas {
val image = BufferedImage(width, height, BufferedImage.TYPE_INT_RGB)
return JavaCanvas(image, pixelScale = 1.0)
@ -35,7 +54,4 @@ class JavaCanvasTest : CanvasTest.Platform {
val javaCanvas = canvas as JavaCanvas
ImageIO.write(javaCanvas.image, "png", File("/tmp/$filename"))
}
@Test
fun testDrawing() = CanvasTest(this).run()
}

@ -20,47 +20,9 @@
package org.isoron
import kotlinx.coroutines.*
import org.isoron.platform.io.*
import org.isoron.uhabits.*
import org.isoron.uhabits.models.*
import org.junit.*
class JavaAsyncTests {
val log = StandardLog()
val fileOpener = JavaFileOpener()
val databaseOpener = JavaDatabaseOpener(log)
suspend fun getDatabase(): Database {
val dbFile = fileOpener.openUserFile("test.sqlite3")
if (dbFile.exists()) dbFile.delete()
val db = databaseOpener.open(dbFile)
db.migrateTo(LOOP_DATABASE_VERSION, fileOpener, log)
return db
}
@Test
fun testFiles() = runBlocking {
FilesTest(fileOpener).testLines()
}
@Test
fun testDatabase() = runBlocking {
DatabaseTest(getDatabase()).testUsage()
}
@Test
fun testCheckmarkRepository() = runBlocking {
CheckmarkRepositoryTest(getDatabase()).testCRUD()
}
@Test
fun testHabitRepository() = runBlocking {
HabitRepositoryTest(getDatabase()).testCRUD()
}
@Test
fun testPreferencesRepository() = runBlocking {
PreferencesRepositoryTest(getDatabase()).testUsage()
}
}
/**
* Workaround until Kotlin adds support for testing suspend functions
* https://youtrack.jetbrains.com/issue/KT-22228
*/
actual fun asyncTest(block: suspend () -> Unit) = runBlocking { block() }

@ -34,7 +34,7 @@ import UIKit
fileOpener: IosFileOpener(),
localeHelper: IosLocaleHelper(NSLocale.preferredLanguages),
log: log,
crCtx: UIDispatcher())
scope: UIDispatcher())
backend?.observable.addListener(listener: self)
backend?.doInit()

Loading…
Cancel
Save