Skip to content

Commit e4cb119

Browse files
committed
testing: add image transformation tests
1 parent 95b010d commit e4cb119

File tree

5 files changed

+61
-5
lines changed

5 files changed

+61
-5
lines changed

core/src/js/main/dev/gmitch215/kasciffy/api/ImageJs.kt

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,29 @@ class ImageJS internal constructor(
4646
*/
4747
fun toDataURL(): String = canvas.toDataURL("image/$extension")
4848

49+
/**
50+
* Converts this image to a [Blob].
51+
* @return A promise that resolves to the [Blob] representation of this image.
52+
*/
53+
fun toBlob(): Promise<Blob> = Promise { resolve, reject ->
54+
canvas.toBlob({ blob ->
55+
if (blob != null) {
56+
resolve(blob)
57+
} else {
58+
reject(Error("Failed to convert canvas to blob"))
59+
}
60+
})
61+
}
62+
4963
/**
5064
* Asynchronously Asciffies this piece of media.
5165
* @param map The ASCII map to use for asciffying.
5266
* @param downScale The downscale factor to use when asciffying. This is used to reduce the size of the media, if necessary.
53-
* If not specified, this is automatically calculated.
67+
* If not specified, this is automatically calculated. On JavaScript, it is recommended to use a value of 2 or higher compared to
68+
* on the JVM.
5469
* @return The asciffied version of this piece of media as a JavaScript promise.
5570
*/
56-
fun asciffy(map: String, downScale: Int? = null): Promise<ImageJS> = GlobalScope.promise {
71+
fun asciffy(map: String, downScale: Int? = 2): Promise<ImageJS> = GlobalScope.promise {
5772
val asciffied = asciffy0(this@ImageJS, map, downScale)
5873
val canvas = toCanvas(asciffied, spaced = true, fontName = "Monospace", fontSize = 12)
5974
return@promise ImageJS(name, extension, creationDateDouble = creationDateDouble, canvas = canvas)
@@ -144,7 +159,7 @@ fun Image(binary: ByteArray, type: String): Promise<ImageJS> {
144159
internal fun toCanvas(output: String, spaced: Boolean = true, fontName: String = "Monospace", fontSize: Int = 12): HTMLCanvasElement {
145160
val canvas = document.createElement("canvas") as HTMLCanvasElement
146161
val ctx = canvas.getContext("2d") as CanvasRenderingContext2D
147-
ctx.font = "$fontSize}px $fontName"
162+
ctx.font = "${fontSize}px $fontName"
148163

149164
val lines = output.split("\n")
150165
val lineHeight = fontSize * 1.2

core/src/js/test/dev/gmitch215/kasciffy/api/TestImageJs.kt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ import kotlinx.browser.window
44
import kotlinx.coroutines.await
55
import kotlinx.coroutines.test.runTest
66
import kotlin.test.Test
7+
import kotlin.test.assertEquals
78
import kotlin.test.assertFalse
9+
import kotlin.test.assertNotNull
810
import kotlin.test.assertTrue
911
import kotlin.time.Duration.Companion.minutes
1012

@@ -19,4 +21,16 @@ class TestImageJs {
1921
assertFalse { asciffy.isEmpty() }
2022
}
2123

24+
@Test
25+
fun testTransform() = runTest(timeout = 10.minutes) {
26+
val image = Image("${window.location.origin}/base/kotlin/gmitch215.png").await()
27+
assertTrue { image.toDataURL().isNotEmpty() }
28+
29+
val transformed = image.asciffy(TEN_NUMERIC).await()
30+
assertTrue { transformed.toDataURL().isNotEmpty() }
31+
assertEquals(image.name, transformed.name)
32+
assertEquals(image.extension, transformed.extension)
33+
assertEquals(image.creationDateDouble, transformed.creationDateDouble)
34+
}
35+
2236
}

core/src/jvm/main/dev/gmitch215/kasciffy/api/ImageJVM.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ class ImageJVM internal constructor(
5858
* If not specified, this is automatically calculated.
5959
* @return The asciffied version of this piece of media.
6060
*/
61-
suspend fun asciffy(map: String, downScale: Int?): Image {
61+
suspend fun asciffy(map: String, downScale: Int? = null): Image {
6262
val asciffied = toPNG(asciffy0(this, map, downScale))
6363
return ImageJVM(name, extension, creationDate, asciffied)
6464
}

core/src/jvm/test/dev/gmitch215/kasciffy/api/TestImageJVM.kt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ package dev.gmitch215.kasciffy.api
22

33
import kotlinx.coroutines.test.runTest
44
import org.junit.Test
5+
import kotlin.test.assertEquals
56
import kotlin.test.assertFalse
67
import kotlin.test.assertNotNull
8+
import kotlin.test.assertTrue
79

810
class TestImageJVM {
911

@@ -16,4 +18,15 @@ class TestImageJVM {
1618
assertFalse { asciffy.isEmpty() }
1719
}
1820

21+
@Test
22+
fun testTransform() = runTest {
23+
val image = Image(javaClass.getResourceAsStream("/gmitch215.png")!!)
24+
assertNotNull(image.bufferedImage)
25+
26+
val transformed = image.asciffy(TEN_NUMERIC, 1)
27+
assertEquals(image.name, transformed.name)
28+
assertEquals(image.extension, transformed.extension)
29+
assertEquals(image.creationDate, transformed.creationDate)
30+
}
31+
1932
}

core/src/native/test/dev/gmitch215/kasciffy/api/TestImageNative.kt

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ package dev.gmitch215.kasciffy.api
22

33
import kotlinx.coroutines.test.runTest
44
import kotlin.test.Test
5+
import kotlin.test.assertEquals
56
import kotlin.test.assertFalse
7+
import kotlin.test.assertNotNull
68
import kotlin.test.assertTrue
79

810
class TestImageNative {
@@ -16,7 +18,19 @@ class TestImageNative {
1618

1719
val asciffy = asciffy0(image, TEN_NUMERIC, 1)
1820
assertFalse { asciffy.isEmpty() }
19-
println(asciffy)
21+
}
22+
23+
@Test
24+
fun testTransformed() = runTest {
25+
val image = Image("src/common/test-resources/gmitch215.png")
26+
assertTrue { image.width > 0 }
27+
assertTrue { image.height > 0 }
28+
assertTrue { image.rgbValues.isNotEmpty() }
29+
30+
val transformed = image.asciffy(TEN_NUMERIC, 1)
31+
assertEquals(image.name, transformed.name)
32+
assertEquals(image.extension, transformed.extension)
33+
assertEquals(image.creationDate, transformed.creationDate)
2034
}
2135

2236
}

0 commit comments

Comments
 (0)