Skip to content

Commit

Permalink
refactor to feedTextResource
Browse files Browse the repository at this point in the history
  • Loading branch information
yujinyan committed Jan 11, 2021
1 parent d1e6ef6 commit 98f8618
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/main/kotlin/me/yujinyan/facktory/text/FileReaders.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package me.yujinyan.facktory.text

import java.io.File

internal fun readTextsUnderResourcePath(path: String) = sequence {
internal fun readTextResource(path: String) = sequence {
val resource = javaClass.getResource(path)
?: throw IllegalArgumentException("No resource found under $path")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class MarkovGenerator(private val n: Int) {
}
}

public fun feedResourcesText(path: String): Unit = readTextsUnderResourcePath(path)
public fun feedTextResource(path: String): Unit = readTextResource(path)
.forEach { feed(it) }

public fun generate(length: Int): String = buildString {
Expand Down
10 changes: 5 additions & 5 deletions src/test/kotlin/me/yujinyan/facktory/FileReaderTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,25 @@ import io.kotest.assertions.throwables.shouldThrow
import io.kotest.matchers.sequences.shouldContain
import io.kotest.matchers.sequences.shouldHaveSize
import io.kotest.matchers.string.shouldContain
import me.yujinyan.facktory.text.readTextsUnderResourcePath
import me.yujinyan.facktory.text.readTextResource
import org.junit.jupiter.api.Test

class FileReaderTest {
@Test
fun `can read path`() {
readTextsUnderResourcePath("/factory/text/en")
readTextResource("/factory/text/en")
.shouldContain("Hello World!")
}

@Test
fun `can read path with trailing slash`() {
readTextsUnderResourcePath("/factory/text/en/")
readTextResource("/factory/text/en/")
.shouldContain("Hello World!")
}

@Test
fun `can read file`() {
readTextsUnderResourcePath("/factory/text/en/Hello.txt")
readTextResource("/factory/text/en/Hello.txt")
.apply {
shouldHaveSize(1)
shouldContain("Hello World!")
Expand All @@ -32,7 +32,7 @@ class FileReaderTest {
@Test
fun `throws exception if provided non-existent path`() {
shouldThrow<IllegalArgumentException> {
readTextsUnderResourcePath("/does/not/exist").first()
readTextResource("/does/not/exist").first()
}.message shouldContain "/does/not/exist"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class MarkovGeneratorTest {
@Test
fun `can read text from resources`() {
val generator = MarkovGenerator(3).apply {
feedResourcesText("/factory/text/cn")
feedTextResource("/factory/text/cn")
}
repeat(3) {
generator.generate(60).also { println(it) }
Expand Down

0 comments on commit 98f8618

Please sign in to comment.