Skip to content

Commit

Permalink
reading an empty file works for desktop
Browse files Browse the repository at this point in the history
  • Loading branch information
vextorspace committed Jul 10, 2024
1 parent 26cf217 commit d5ea239
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ actual class AppFile actual constructor(val fileName: String) {
return context?.deleteFile(fileName) ?: false
}

actual fun readText(): String? {
return context?.openFileInput(fileName)?.bufferedReader()?.use {
it.readText()
}
}

companion object {
var context: android.content.Context? = null
}
Expand Down
1 change: 1 addition & 0 deletions composeApp/src/commonMain/kotlin/resources/AppFile.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ expect class AppFile(fileName: String) {
fun exists(): Boolean
fun create(): Boolean
fun delete(): Boolean
fun readText(): String?
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ actual class AppFile actual constructor(fileName: String) {
}

actual fun delete(): Boolean {
if (!file.exists())
return false
return file.delete()
}

actual fun readText(): String? {
if (!file.exists())
return null
return file.readText()
}
}
44 changes: 44 additions & 0 deletions composeApp/src/desktopTest/kotlin/resources/ReadAppFileTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package resources

import io.kotest.matchers.nulls.shouldBeNull
import io.kotest.matchers.nulls.shouldNotBeNull
import io.kotest.matchers.string.shouldBeEmpty
import kotlin.test.AfterTest
import kotlin.test.BeforeTest
import kotlin.test.Test

class ReadAppFileTest {

val appFile = AppFile("appFile.json")

@BeforeTest
fun setup() {
if(appFile.exists()) {
appFile.delete()
}
}

@AfterTest
fun teardown() {
if(appFile.exists()) {
appFile.delete()
}
}

@Test
fun `Non-existent App File Reads null`() {
val appFile = AppFile("appFile.json")
appFile.readText()
.shouldBeNull()
}

@Test
fun `Reads Empty App File`() {
val appFile = AppFile("appFile.json")
appFile.create()

appFile.readText()
.shouldNotBeNull()
.shouldBeEmpty()
}
}
4 changes: 4 additions & 0 deletions composeApp/src/nativeMain/kotlin/resources/AppFile.native.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,8 @@ actual class AppFile actual constructor(fileName: String) {
actual fun delete(): Boolean {
TODO("Not yet implemented")
}

actual fun readText(): String? {
TODO("Not yet implemented")
}
}

0 comments on commit d5ea239

Please sign in to comment.