Skip to content

Commit

Permalink
update test resource fetch
Browse files Browse the repository at this point in the history
  • Loading branch information
itboy87 committed Aug 23, 2024
1 parent f54b8ba commit d9345a1
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 57 deletions.
1 change: 1 addition & 0 deletions ksoup-test/module.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ repositories:
test-dependencies:
- ../ksoup
- $libs.korlibs.io
- $libs.kotlinx.io
- $libs.codepoints
- $libs.kotlinx.coroutines.test
- $libs.kotlinx.datetime
Expand Down
65 changes: 36 additions & 29 deletions ksoup-test/test/com/fleeksoft/ksoup/TestHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,61 +4,68 @@ import com.fleeksoft.ksoup.io.SourceReader
import com.fleeksoft.ksoup.ported.openSourceReader
import korlibs.io.compression.deflate.GZIP
import korlibs.io.compression.uncompress
import korlibs.io.file.VfsFile
import korlibs.io.file.fullName
import korlibs.io.file.readAsSyncStream
import korlibs.io.file.std.uniVfs
import korlibs.io.stream.readAll
import kotlinx.io.buffered
import kotlinx.io.files.Path
import kotlinx.io.files.SystemFileSystem
import kotlinx.io.readByteArray

object TestHelper {

suspend fun readGzipResource(file: String): SourceReader {
return readGzipFile(getResourceAbsolutePath(file).uniVfs)
suspend fun readGzipResource(resource: String): SourceReader {
return readGzipFile(resource)
}

suspend fun readResource(file: String): SourceReader {
if (file.endsWith(".gz") || file.endsWith(".z")) {
return readGzipResource(file)
suspend fun readResource(resource: String): SourceReader {
if (resource.endsWith(".gz") || resource.endsWith(".z")) {
return readGzipResource(resource)
}
return readFile(getResourceAbsolutePath(file).uniVfs)
return readFile(resource)
}

fun getResourceAbsolutePath(resourceName: String): String {
if (Platform.isWindows()) {
fun getResourceAbsolutePath(resourceName: String, absForWindows: Boolean = true): String {
if (Platform.isWindows() && !BuildConfig.isKotlinx && absForWindows) {
return "../../../../testResources/$resourceName"
} else if (Platform.isJsOrWasm()) {
return "https://raw.githubusercontent.com/fleeksoft/ksoup/release/ksoup-test/testResources/$resourceName"
}
return "${BuildConfig.PROJECT_ROOT}/ksoup-test/testResources/$resourceName"
}

suspend fun getFileAsString(file: VfsFile): String {
val bytes: ByteArray = if (file.fullName.endsWith(".gz")) {
readGzipFile(file).readAllBytes()
suspend fun readResourceAsString(resourceName: String): String {
val bytes: ByteArray = if (resourceName.endsWith(".gz")) {
readGzipFile(resourceName).readAllBytes()
} else {
readFile(file).readAllBytes()
readFile(resourceName).readAllBytes()
}
return bytes.decodeToString()
}

suspend fun resourceFilePathToStream(path: String): SourceReader {
val file = this.getResourceAbsolutePath(path).uniVfs
return pathToStream(file)
}

suspend fun pathToStream(file: VfsFile): SourceReader {
return if (file.fullName.endsWith(".gz") || file.fullName.endsWith(".z")) {
readGzipFile(file)
suspend fun resourceFilePathToStream(resource: String): SourceReader {
return if (resource.endsWith(".gz") || resource.endsWith(".z")) {
readGzipFile(resource)
} else {
readFile(file)
readFile(resource)
}
}

suspend fun readFile(file: VfsFile): SourceReader {
return file.readAll().openSourceReader()
private suspend fun readFile(resource: String): SourceReader {
val abs = getResourceAbsolutePath(resource, absForWindows = false)
val bytes = if (Platform.isJsOrWasm()) {
abs.uniVfs.readAll()
} else {
SystemFileSystem.source(Path(abs)).buffered().readByteArray()
}
return bytes.openSourceReader()
}

suspend fun readGzipFile(file: VfsFile): SourceReader {
return file.readAsSyncStream().readAll().uncompress(GZIP).openSourceReader()
private suspend fun readGzipFile(resource: String): SourceReader {
val abs = getResourceAbsolutePath(resource, absForWindows = false)
val bytes = if (Platform.isJsOrWasm()) {
abs.uniVfs.readAll()
} else {
SystemFileSystem.source(Path(abs)).buffered().readByteArray()
}
return bytes.uncompress(GZIP).openSourceReader()
}
}
12 changes: 5 additions & 7 deletions ksoup-test/test/com/fleeksoft/ksoup/helper/DataUtilTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import com.fleeksoft.ksoup.ported.io.Charsets
import com.fleeksoft.ksoup.ported.openSourceReader
import com.fleeksoft.ksoup.ported.toByteArray
import com.fleeksoft.ksoup.ported.toSourceFile
import korlibs.io.file.std.uniVfs
import kotlinx.coroutines.test.runTest
import kotlin.test.*

Expand Down Expand Up @@ -339,9 +338,10 @@ class DataUtilTest {
// kotlinx module not support gzip
return@runTest
}
val resourceFile = TestHelper.getResourceAbsolutePath("htmltests/large.html.gz")
val resourceName = "htmltests/large.html.gz"
val resourceFile = TestHelper.getResourceAbsolutePath(resourceName)
val inputFile = resourceFile.toSourceFile()
val input: String = TestHelper.getFileAsString(resourceFile.uniVfs)
val input: String = TestHelper.readResourceAsString(resourceName)

val expected = Ksoup.parse(input, "https://example.com")
val doc: Document = Ksoup.parseFile(inputFile, baseUri = "https://example.com", charsetName = null)
Expand All @@ -352,8 +352,7 @@ class DataUtilTest {

@Test
fun testStringVsSourceReaderParse() = runTest {
val resourceFile = TestHelper.getResourceAbsolutePath("htmltests/large.html.gz")
val input: String = TestHelper.getFileAsString(resourceFile.uniVfs)
val input: String = TestHelper.readResourceAsString("htmltests/large.html.gz")

val expected = Ksoup.parse(input, "https://example.com")
val doc: Document = Ksoup.parse(sourceReader = input.openSourceReader(), baseUri = "https://example.com", charsetName = null)
Expand All @@ -363,8 +362,7 @@ class DataUtilTest {

@Test
fun handlesUnlimitedRead() = runTest {
val inputFile: String = TestHelper.getResourceAbsolutePath("htmltests/large.html.gz")
val input: String = TestHelper.getFileAsString(inputFile.uniVfs)
val input: String = TestHelper.readResourceAsString("htmltests/large.html.gz")
val byteBuffer: ByteArray = DataUtil.readToByteBuffer(input.openSourceReader(), 0)
val read = byteBuffer.decodeToString()
assertEquals(input, read)
Expand Down
7 changes: 2 additions & 5 deletions ksoup-test/test/com/fleeksoft/ksoup/integration/ParseTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import com.fleeksoft.ksoup.Ksoup.parseFile
import com.fleeksoft.ksoup.nodes.Document
import com.fleeksoft.ksoup.parser.Parser
import com.fleeksoft.ksoup.ported.openSourceReader
import korlibs.io.file.std.uniVfs
import kotlinx.coroutines.test.runTest
import kotlin.test.*

Expand Down Expand Up @@ -130,8 +129,7 @@ class ParseTest {

@Test
fun testWikiExpandedFromString() = runTest {
val input = TestHelper.getResourceAbsolutePath("htmltests/xwiki-edit.html.gz")
val html = TestHelper.getFileAsString(input.uniVfs)
val html = TestHelper.readResourceAsString("htmltests/xwiki-edit.html.gz")
val doc = parse(html)
assertEquals("XWiki Jetty HSQLDB 12.1-SNAPSHOT", doc.select("#xwikiplatformversion").text())
val wantHtml =
Expand All @@ -141,8 +139,7 @@ class ParseTest {

@Test
fun testWikiFromString() = runTest {
val input = TestHelper.getResourceAbsolutePath("htmltests/xwiki-1324.html.gz")
val html = TestHelper.getFileAsString(input.uniVfs)
val html = TestHelper.readResourceAsString("htmltests/xwiki-1324.html.gz")
val doc = parse(html)
assertEquals("XWiki Jetty HSQLDB 12.1-SNAPSHOT", doc.select("#xwikiplatformversion").text())
val wantHtml =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package com.fleeksoft.ksoup.issues

import com.fleeksoft.ksoup.Ksoup
import com.fleeksoft.ksoup.TestHelper
import korlibs.io.file.std.uniVfs
import kotlinx.coroutines.test.runTest
import kotlin.test.Test
import kotlin.test.assertEquals
Expand All @@ -11,7 +10,7 @@ class GithubIssuesTests {
@Test
fun testIssue20DuplicateElements() = runTest {
// https://github.com/fleeksoft/ksoup/issues/20
Ksoup.parse(TestHelper.getFileAsString(TestHelper.getResourceAbsolutePath("htmltests/issue20.html.gz").uniVfs))
Ksoup.parse(TestHelper.readResourceAsString("htmltests/issue20.html.gz"))
// Ksoup.parseGetRequest("https://www.dm530w.org/")
.apply {
body().select("div[class=firs l]")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import com.fleeksoft.ksoup.ported.exception.UncheckedIOException
import com.fleeksoft.ksoup.ported.io.Charsets
import com.fleeksoft.ksoup.ported.io.StringReader
import com.fleeksoft.ksoup.ported.toReader
import korlibs.io.file.std.uniVfs
import korlibs.io.lang.substr
import kotlinx.coroutines.test.runTest
import kotlin.test.*
Expand Down Expand Up @@ -561,9 +560,7 @@ class CharacterReaderTest {

@Test
fun lineNumbersAgreeWithEditor() = runTest {
val content: String = TestHelper.getFileAsString(
TestHelper.getResourceAbsolutePath("htmltests/large.html.gz").uniVfs
)
val content: String = TestHelper.readResourceAsString("htmltests/large.html.gz")
val reader = CharacterReader(content)
reader.trackNewlines(true)
val scan = "<p>VESTIBULUM" // near the end of the file
Expand Down
12 changes: 5 additions & 7 deletions ksoup-test/test/com/fleeksoft/ksoup/parser/StreamParserTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import com.fleeksoft.ksoup.nodes.Node
import com.fleeksoft.ksoup.ported.io.Charsets
import com.fleeksoft.ksoup.ported.toReader
import com.fleeksoft.ksoup.select.Elements
import korlibs.io.file.std.uniVfs
import kotlinx.coroutines.test.runTest
import kotlin.test.*

Expand Down Expand Up @@ -293,12 +292,11 @@ class StreamParserTest {
}

@Test
fun canParseFileReader() = runTest() {
val file = TestHelper.getResourceAbsolutePath("htmltests/large.html.gz").uniVfs


val reader = TestHelper.readGzipFile(file).toReader()
val streamer: StreamParser = StreamParser(Parser.htmlParser()).parse(reader, file.absolutePath)
fun canParseFileReader() = runTest {
val resourceName = "htmltests/large.html.gz"
val file = TestHelper.getResourceAbsolutePath(resourceName)
val reader = TestHelper.readGzipResource(resourceName).toReader()
val streamer: StreamParser = StreamParser(Parser.htmlParser()).parse(reader, file)

var last: Element? = null
var e: Element?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.fleeksoft.ksoup

import com.fleeksoft.ksoup.nodes.Document
import korlibs.io.file.std.uniVfs
import kotlinx.coroutines.delay
import kotlinx.coroutines.test.runTest
import org.jsoup.Jsoup
import kotlin.system.measureTimeMillis
Expand All @@ -11,9 +12,10 @@ import kotlin.test.Test
class PerformanceComparisonTest {

@Test
@Ignore
// @Ignore
fun compareWithJsoup() = runTest {

delay(8000)
if (BuildConfig.isGithubActions) {
return@runTest
}
Expand Down
4 changes: 2 additions & 2 deletions sample/shared/module.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ product:
dependencies:
- $compose.foundation: exported
- $compose.material3: exported
- ../../ksoup-korlibs
- ../../ksoup-network-korlibs
- ../../ksoup
- ../../ksoup-network

dependencies@android:
# Compose integration with Android activities
Expand Down

0 comments on commit d9345a1

Please sign in to comment.