Skip to content

Commit bd4ee02

Browse files
committed
Add PiktImageProcessingTest
1 parent 3c6e212 commit bd4ee02

File tree

7 files changed

+152
-5
lines changed

7 files changed

+152
-5
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package eu.iamgio.pikt.tests
2+
3+
import eu.iamgio.pikt.command.commands.imageprocessing.MaskCommand
4+
import eu.iamgio.pikt.image.rgbToHex
5+
import org.junit.jupiter.api.Test
6+
import java.awt.image.BufferedImage
7+
import kotlin.test.assertTrue
8+
9+
// These tests apply image processing filters to the same image (/imageprocessing-tests/source.png)
10+
// which generates the following pseudocode:
11+
// var str = "Pikt"
12+
// if true
13+
// print str
14+
15+
private const val EXPECTED_RESULT = "Pikt" // Expected result from all the programs.
16+
17+
class PiktImageProcessingTest {
18+
19+
private val launcher = PiktImageProcessingTestLauncher()
20+
21+
// Launches a Pikt program.
22+
private fun launch() = launcher.launch()
23+
24+
// Copies a resource from /imageprocessing-tests/ to the temp folder.
25+
private fun copy(name: String) = launcher.copy(name)
26+
27+
// Gets the file path from file name within the temp folder.
28+
private fun pathify(name: String) = launcher.pathify(name)
29+
30+
// Checks whether the transformed image outputs the correct result.
31+
private fun checkCorrectOutput() = assertTrue { launch().firstOrNull() == EXPECTED_RESULT }
32+
33+
// Latest generated image
34+
private val outImage: BufferedImage
35+
get() = launcher.getOutputImage()
36+
37+
// Applies a mask image (-mask=<path>)
38+
@Test
39+
fun mask() {
40+
copy("face-mask.png")
41+
MaskCommand().fire(pathify("face-mask.png"))
42+
checkCorrectOutput()
43+
44+
with(outImage) {
45+
assertTrue {
46+
getRGB(0, 0).rgbToHex() == "FFFFFF" // Top left corner, not in mask
47+
&& getRGB(3, 4).rgbToHex() == "FF0000" // Left eye
48+
&& getRGB(9, 4).rgbToHex() == "505050" // Right eye
49+
&& getRGB(6, 7).rgbToHex() == "FF65FF" // Mouth
50+
}
51+
}
52+
}
53+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package eu.iamgio.pikt.tests
2+
3+
import eu.iamgio.pikt.command.Command
4+
import eu.iamgio.pikt.command.commands.imageprocessing.ImageOutputCommand
5+
import java.awt.image.BufferedImage
6+
import java.io.File
7+
import java.io.FileInputStream
8+
import java.io.InputStream
9+
import java.nio.file.Files
10+
import java.nio.file.StandardCopyOption
11+
import javax.imageio.ImageIO
12+
13+
private const val TESTS_FOLDER = "/imageprocessing-tests"
14+
private const val SOURCE = "source.png"
15+
private const val OUT = "out.png"
16+
17+
fun Command.fire(args: String? = null) = this.action(args)
18+
19+
class PiktImageProcessingTestLauncher : PiktTestLauncher() {
20+
private val folder: File = File(tempDirectory, "imageprocessing")
21+
private val sourceFilePath: String
22+
get() = folder.absolutePath + File.separator + OUT
23+
24+
init {
25+
folder.mkdirs()
26+
println("Storing to: $folder")
27+
28+
System.setProperty("source", sourceFilePath)
29+
// Set output file
30+
ImageOutputCommand().fire(sourceFilePath)
31+
copy("source.png")
32+
}
33+
34+
fun copy(name: String) {
35+
Files.copy(
36+
PiktImageProcessingTest::class.java.getResourceAsStream("$TESTS_FOLDER/$name")!!,
37+
File(folder, name).toPath(),
38+
StandardCopyOption.REPLACE_EXISTING
39+
)
40+
}
41+
42+
fun getOutputImage(): BufferedImage = ImageIO.read(getImage(""))
43+
44+
fun launch() = launch("")
45+
46+
fun pathify(name: String) = folder.absolutePath + File.separator + name
47+
48+
override fun getImage(name: String): InputStream {
49+
return FileInputStream(sourceFilePath)
50+
}
51+
52+
override fun getColorScheme(colorSchemeName: String): InputStream {
53+
return PiktTest::class.java.getResourceAsStream("/schemes/$colorSchemeName.properties")!!
54+
}
55+
}

core/src/test/kotlin/eu/iamgio/pikt/tests/PiktTest.kt

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,22 @@
11
package eu.iamgio.pikt.tests
22

33
import org.junit.jupiter.api.Test
4+
import java.io.InputStream
45
import kotlin.test.assertTrue
56

7+
private class InternalTestLauncher : PiktTestLauncher() {
8+
override fun getImage(name: String): InputStream {
9+
return PiktTest::class.java.getResourceAsStream("/$name.png")!!
10+
}
11+
12+
override fun getColorScheme(colorSchemeName: String): InputStream {
13+
return PiktTest::class.java.getResourceAsStream("/schemes/$colorSchemeName.properties")!!
14+
}
15+
}
616

717
class PiktTest {
818

9-
private val launcher = PiktTestLauncher()
19+
private val launcher = InternalTestLauncher()
1020
private fun launch(name: String, scheme: String? = null) = launcher.launch(name, scheme)
1121

1222
@Test

core/src/test/kotlin/eu/iamgio/pikt/tests/PiktTestLauncher.kt

+17-4
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import eu.iamgio.pikt.properties.PiktProperties
99
import eu.iamgio.pikt.properties.PiktPropertiesRetriever
1010
import eu.iamgio.pikt.registerStatements
1111
import java.io.File
12+
import java.io.InputStream
1213
import javax.imageio.ImageIO
1314

1415

@@ -17,15 +18,27 @@ import javax.imageio.ImageIO
1718
*
1819
* @author Giorgio Garofalo
1920
*/
20-
class PiktTestLauncher {
21+
abstract class PiktTestLauncher {
2122

22-
private val tempDirectory = File(System.getProperty("java.io.tmpdir") + File.separator + "pikt-test")
23+
protected val tempDirectory = File(System.getProperty("java.io.tmpdir") + File.separator + "pikt-test")
2324

2425
init {
2526
tempDirectory.mkdir()
2627
registerStatements()
2728
}
2829

30+
/**
31+
* @param name name of the source image
32+
* @return source image as [InputStream]
33+
*/
34+
abstract fun getImage(name: String): InputStream
35+
36+
/**
37+
* @param colorSchemeName name of the color scheme
38+
* @return color scheme as [InputStream]
39+
*/
40+
abstract fun getColorScheme(colorSchemeName: String): InputStream
41+
2942
/**
3043
* Launches the interpreter and returns all the non-error messages.
3144
* @param name PNG image name without extension
@@ -53,14 +66,14 @@ class PiktTestLauncher {
5366
nativeCompilerPath = null,
5467
colors = ColorsPropertiesRetriever(libraries).also {
5568
if(colorSchemeName != null) {
56-
it.loadProperties(PiktTest::class.java.getResourceAsStream("/schemes/$colorSchemeName.properties")!!)
69+
it.loadProperties(getColorScheme(colorSchemeName))
5770
}
5871
}.retrieve()
5972
)
6073

6174
val lines = mutableListOf<String>()
6275

63-
val image = PiktImage(ImageIO.read(PiktTest::class.java.getResourceAsStream("/$name.png")), properties.colors)
76+
val image = PiktImage(ImageIO.read(getImage(name)), properties.colors)
6477

6578
val evaluator = Evaluator()
6679
evaluator.evaluate(image, properties.libraries)
Loading
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<component name="ProjectRunConfigurationManager">
2+
<configuration default="false" name="PiktImageProcessingTest" type="JUnit" factoryName="JUnit" nameIsGenerated="true">
3+
<module name="core" />
4+
<extension name="coverage">
5+
<pattern>
6+
<option name="PATTERN" value="eu.iamgio.pikt.tests.*" />
7+
<option name="ENABLED" value="true" />
8+
</pattern>
9+
</extension>
10+
<option name="PACKAGE_NAME" value="eu.iamgio.pikt.tests" />
11+
<option name="MAIN_CLASS_NAME" value="eu.iamgio.pikt.tests.PiktImageProcessingTest" />
12+
<option name="METHOD_NAME" value="" />
13+
<option name="TEST_OBJECT" value="class" />
14+
<option name="VM_PARAMETERS" value="-Djvmcompiler=SET_HERE -Dlib=../stdlib/target/stdlib.jar" />
15+
</configuration>
16+
</component>

0 commit comments

Comments
 (0)