Skip to content

Commit d5e48e4

Browse files
committed
Add test for importing and using Processing libraries
Introduces a helper to create temporary Processing libraries and implements a test that imports and uses a custom library in a Processing sketch. This enhances test coverage for library integration in the Processing Gradle plugin.
1 parent 6b873ce commit d5e48e4

1 file changed

Lines changed: 91 additions & 1 deletion

File tree

‎java/gradle/src/test/kotlin/ProcessingPluginTest.kt‎

Lines changed: 91 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,62 @@ class ProcessingPluginTest{
4343
)
4444
}
4545

46+
data class TemporaryProcessingLibraryResult(
47+
val buildResult: BuildResult,
48+
val libraryFolder: File
49+
)
50+
51+
fun createTemporaryProcessingLibrary(name: String): TemporaryProcessingLibraryResult{
52+
val directory = TemporaryFolder()
53+
directory.create()
54+
val libraryFolder = directory.newFolder("libraries",name)
55+
directory.newFile("libraries/$name/build.gradle.kts").writeText("""
56+
plugins {
57+
java
58+
}
59+
tasks.jar{
60+
destinationDirectory.set(file("library"))
61+
}
62+
""".trimIndent())
63+
val srcDirectory = directory.newFolder("libraries", name,"src", "main", "java")
64+
directory.newFile("libraries/$name/src/main/java/Example.java").writeText("""
65+
package testing.example;
66+
67+
public class Example {
68+
public void exampleMethod() {
69+
System.out.println("Hello from Example library");
70+
}
71+
}
72+
""".trimIndent())
73+
directory.newFile("libraries/$name/settings.gradle.kts")
74+
directory.newFile("libraries/$name/library.properties").writeText("""
75+
name=$name
76+
author=Test Author
77+
version=1.0.0
78+
sentence=An example library
79+
paragraph=This is a longer description of the example library.
80+
category=Examples
81+
url=http://example.com
82+
""".trimIndent())
83+
84+
if(isDebuggerAttached()){
85+
openFolderInFinder(libraryFolder)
86+
}
87+
88+
val buildResult = GradleRunner.create()
89+
.withProjectDir(libraryFolder)
90+
.withArguments("jar")
91+
.withPluginClasspath()
92+
.withDebug(true)
93+
.build()
94+
95+
96+
return TemporaryProcessingLibraryResult(
97+
buildResult,
98+
libraryFolder
99+
)
100+
}
101+
46102
@Test
47103
fun testSinglePDE(){
48104
val (buildResult, sketchFolder, classLoader) = createTemporaryProcessingSketch("build"){ sketchFolder ->
@@ -177,7 +233,41 @@ class ProcessingPluginTest{
177233

178234
@Test
179235
fun testImportingLibrary(){
180-
// TODO: Implement a test that imports a Processing library and uses it in the sketch
236+
val libraryResult = createTemporaryProcessingLibrary("ExampleLibrary")
237+
val (buildResult, sketchFolder, classLoader) = createTemporaryProcessingSketch("build") { sketchFolder ->
238+
sketchFolder.resolve("sketch.pde").writeText("""
239+
import testing.example.*;
240+
241+
Example example;
242+
243+
void setup(){
244+
size(100, 100);
245+
example = new Example();
246+
example.exampleMethod();
247+
}
248+
249+
void draw(){
250+
println("Hello World");
251+
}
252+
""".trimIndent())
253+
sketchFolder.resolve("gradle.properties").writeText(""")
254+
processing.sketchbook = ${libraryResult.libraryFolder.parentFile.parentFile.absolutePath}
255+
""".trimIndent())
256+
}
257+
258+
val sketchClass = classLoader.loadClass("sketch")
259+
260+
assert(sketchClass != null) {
261+
"Class sketch not found"
262+
}
263+
264+
assert(sketchClass?.methods?.find { method -> method.name == "setup" } != null) {
265+
"Method setup not found in class sketch"
266+
}
267+
268+
assert(sketchClass?.methods?.find { method -> method.name == "draw" } != null) {
269+
"Method draw not found in class sketch"
270+
}
181271
}
182272

183273
fun isDebuggerAttached(): Boolean {

0 commit comments

Comments
 (0)