Skip to content

Commit 777acac

Browse files
committed
Add tests for nested script invokes
1 parent b7b41f1 commit 777acac

File tree

12 files changed

+201
-7
lines changed

12 files changed

+201
-7
lines changed

core/src/test/java/org/mini2Dx/miniscript/core/AbstractGameScriptingEngineTest.java

Lines changed: 93 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -607,7 +607,70 @@ public boolean callOnGameThread() {
607607
Assert.assertEquals(false, gameFuture.isFutureSkipped());
608608
Assert.assertEquals(false, gameFuture.isScriptSkipped());
609609
}
610-
610+
611+
@Test
612+
public void testInvokeNestedEmbeddedScript() throws Exception {
613+
if(!scriptingEngine.isEmbeddedSynchronousScriptSupported()) {
614+
return;
615+
}
616+
617+
final int expectedScriptId = scriptingEngine.compileScript(getNestedInvokeWithinScriptFilepath(), getNestedInvokeWithScript());
618+
scriptingEngine.compileScript(getDefaultScriptFilepath(), getDefaultScript());
619+
scriptingEngine.compileScript(getInvokeWithinScriptFilepath(), getInvokeWithScript());
620+
scriptingEngine.invokeCompiledScript(expectedScriptId, scriptBindings, new ScriptInvocationListener() {
621+
622+
@Override
623+
public void onScriptSuccess(int scriptId, ScriptExecutionResult executionResult) {
624+
if(scriptId != expectedScriptId) {
625+
scriptResult.set(ScriptResult.INCORRECT_SCRIPT_ID);
626+
scriptExecuted.set(true);
627+
} else if(!checkExpectedNestedEmbeddedScriptResults(executionResult)) {
628+
scriptResult.set(ScriptResult.INCORRECT_VARIABLES);
629+
scriptExecuted.set(true);
630+
} else {
631+
scriptResult.set(ScriptResult.SUCCESS);
632+
scriptExecuted.set(true);
633+
}
634+
}
635+
636+
@Override
637+
public void onScriptSkipped(int scriptId) {
638+
scriptResult.set(ScriptResult.SKIPPED);
639+
scriptExecuted.set(true);
640+
}
641+
642+
@Override
643+
public void onScriptException(int scriptId, Exception e) {
644+
e.printStackTrace();
645+
scriptResult.set(ScriptResult.EXCEPTION);
646+
scriptExecuted.set(true);
647+
}
648+
649+
@Override
650+
public boolean callOnGameThread() {
651+
return true;
652+
}
653+
});
654+
final long timeout = 20000L;
655+
long timer = 0L;
656+
657+
while(!scriptExecuted.get() && timer < timeout) {
658+
long startTime = System.currentTimeMillis();
659+
scriptingEngine.update(1f);
660+
timer += System.currentTimeMillis() - startTime;
661+
}
662+
663+
if(timer >= timeout) {
664+
Assert.fail("Timed out after " + timeout + "ms wait for script");
665+
}
666+
667+
Assert.assertEquals(ScriptResult.SUCCESS, scriptResult.get());
668+
Assert.assertEquals(true, gameFuture.isUpdated());
669+
Assert.assertEquals(false, gameFuture.waitOccurred());
670+
Assert.assertEquals(false, gameFuture.isFutureSkipped());
671+
Assert.assertEquals(false, gameFuture.isScriptSkipped());
672+
}
673+
611674
protected abstract GameScriptingEngine createScriptingEngine();
612675

613676
protected abstract InputStream getDefaultScriptInputStream();
@@ -619,9 +682,37 @@ public boolean callOnGameThread() {
619682
protected abstract String getInvokeWithScript();
620683

621684
protected abstract String getInvokeWithinScriptFilepath();
622-
685+
686+
protected abstract String getNestedInvokeWithScript();
687+
688+
protected abstract String getNestedInvokeWithinScriptFilepath();
689+
623690
protected abstract String getWaitForCompletionScript();
624691

692+
protected boolean checkExpectedNestedEmbeddedScriptResults(ScriptExecutionResult executionResult) {
693+
if(!executionResult.containsKey("booleanValue")) {
694+
System.err.println("booleanValue not present");
695+
return false;
696+
}
697+
if(!executionResult.containsKey("intValue")) {
698+
System.err.println("intValue not present");
699+
return false;
700+
}
701+
if(!"hello1234".equals(executionResult.get("stringValue"))) {
702+
System.err.println("Expected stringValue to be hello123 but was " + executionResult.get("stringValue"));
703+
return false;
704+
}
705+
if(!executionResult.containsKey("intValue2")) {
706+
System.err.println("intValue2 not present");
707+
return false;
708+
}
709+
if(executionResult.get("intValue2") instanceof Integer && ((Integer) executionResult.get("intValue2")) != 102) {
710+
System.err.println("Expected intValue2 to be 102 but was " + executionResult.get("intValue2"));
711+
return false;
712+
}
713+
return true;
714+
}
715+
625716
protected boolean checkExpectedEmbeddedScriptResults(ScriptExecutionResult executionResult) {
626717
if(!checkExpectedScriptResults(executionResult)) {
627718
return false;

groovy/src/test/java/org/mini2Dx/miniscript/groovy/GroovyGameScriptingEngineTest.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ protected String getInvokeWithScript() {
7070
} catch (URISyntaxException e) {
7171
e.printStackTrace();
7272
}
73-
Assert.fail("Could not read default script");
73+
Assert.fail("Could not read invokeWithinScript script");
7474
return null;
7575
}
7676

@@ -79,6 +79,24 @@ protected String getInvokeWithinScriptFilepath() {
7979
return "invokeWithinScript.groovy";
8080
}
8181

82+
@Override
83+
protected String getNestedInvokeWithScript() {
84+
try {
85+
return new String(Files.readAllBytes(Paths.get(this.getClass().getResource("/nestedInvokeWithinScript.groovy").toURI())));
86+
} catch (IOException e) {
87+
e.printStackTrace();
88+
} catch (URISyntaxException e) {
89+
e.printStackTrace();
90+
}
91+
Assert.fail("Could not read nestedInvokeWithinScript script");
92+
return null;
93+
}
94+
95+
@Override
96+
protected String getNestedInvokeWithinScriptFilepath() {
97+
return "nestedInvokeWithinScript.groovy";
98+
}
99+
82100
@Override
83101
protected InputStream getDefaultScriptInputStream() {
84102
return GroovyGameScriptingEngineTest.class.getResourceAsStream("/default.groovy");
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
scripts.invokeSync("invokeWithinScript.groovy")
2+
stringValue += "4";

kotlin/src/test/java/org/mini2Dx/miniscript/kotlin/KotlinGameScriptingEngineTest.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ protected String getInvokeWithScript() {
7171
} catch (URISyntaxException e) {
7272
e.printStackTrace();
7373
}
74-
Assert.fail("Could not read waitForCompletion script");
74+
Assert.fail("Could not read invokeWithinScript script");
7575
return null;
7676
}
7777

@@ -80,6 +80,24 @@ protected String getInvokeWithinScriptFilepath() {
8080
return "invokeWithinScript.kts";
8181
}
8282

83+
@Override
84+
protected String getNestedInvokeWithScript() {
85+
try {
86+
return new String(Files.readAllBytes(Paths.get(this.getClass().getResource("/nestedInvokeWithinScript.kts").toURI())));
87+
} catch (IOException e) {
88+
e.printStackTrace();
89+
} catch (URISyntaxException e) {
90+
e.printStackTrace();
91+
}
92+
Assert.fail("Could not read nestedInvokeWithinScript script");
93+
return null;
94+
}
95+
96+
@Override
97+
protected String getNestedInvokeWithinScriptFilepath() {
98+
return "nestedInvokeWithinScript.kts";
99+
}
100+
83101
@Override
84102
protected InputStream getDefaultScriptInputStream() {
85103
return KotlinGameScriptingEngineTest.class.getResourceAsStream("/default.kts");
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
val scripts = bindings["scripts"] as org.mini2Dx.miniscript.core.EmbeddedScriptInvoker
2+
scripts.invokeSync("invokeWithinScript.kts")
3+
val stringValue: String = bindings["stringValue"] as String + "4"

lua/src/test/java/org/mini2Dx/miniscript/lua/LuaGameScriptingEngineTest.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ protected String getInvokeWithScript() {
7070
} catch (URISyntaxException e) {
7171
e.printStackTrace();
7272
}
73-
Assert.fail("Could not read default script");
73+
Assert.fail("Could not read invokeWithinScript script");
7474
return null;
7575
}
7676

@@ -79,6 +79,24 @@ protected String getInvokeWithinScriptFilepath() {
7979
return "invokeWithinScript.lua";
8080
}
8181

82+
@Override
83+
protected String getNestedInvokeWithScript() {
84+
try {
85+
return new String(Files.readAllBytes(Paths.get(this.getClass().getResource("/nestedInvokeWithinScript.lua").toURI())));
86+
} catch (IOException e) {
87+
e.printStackTrace();
88+
} catch (URISyntaxException e) {
89+
e.printStackTrace();
90+
}
91+
Assert.fail("Could not read nestedInvokeWithinScript script");
92+
return null;
93+
}
94+
95+
@Override
96+
protected String getNestedInvokeWithinScriptFilepath() {
97+
return "nestedInvokeWithinScript.lua";
98+
}
99+
82100
@Override
83101
protected InputStream getDefaultScriptInputStream() {
84102
return LuaGameScriptingEngineTest.class.getResourceAsStream("/default.lua");

lua/src/test/java/LuaScriptExecutorPoolTest.java renamed to lua/src/test/java/org/mini2Dx/miniscript/lua/LuaScriptExecutorPoolTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
package org.mini2Dx.miniscript.lua;
2+
13
import org.junit.Assert;
24
import org.junit.Test;
35
import org.luaj.vm2.lib.jse.JsePlatform;
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
scripts:invokeSync("invokeWithinScript.lua")
2+
stringValue = stringValue .. "4"

python/src/test/java/org/mini2Dx/miniscript/python/PythonGameScriptingEngineTest.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ protected String getInvokeWithScript() {
7070
} catch (URISyntaxException e) {
7171
e.printStackTrace();
7272
}
73-
Assert.fail("Could not read default script");
73+
Assert.fail("Could not read invokeWithinScript script");
7474
return null;
7575
}
7676

@@ -79,6 +79,24 @@ protected String getInvokeWithinScriptFilepath() {
7979
return "invokeWithinScript.py";
8080
}
8181

82+
@Override
83+
protected String getNestedInvokeWithScript() {
84+
try {
85+
return new String(Files.readAllBytes(Paths.get(this.getClass().getResource("/nestedInvokeWithinScript.py").toURI())));
86+
} catch (IOException e) {
87+
e.printStackTrace();
88+
} catch (URISyntaxException e) {
89+
e.printStackTrace();
90+
}
91+
Assert.fail("Could not read nestedInvokeWithinScript script");
92+
return null;
93+
}
94+
95+
@Override
96+
protected String getNestedInvokeWithinScriptFilepath() {
97+
return "nestedInvokeWithinScript.py";
98+
}
99+
82100
@Override
83101
protected InputStream getDefaultScriptInputStream() {
84102
return PythonGameScriptingEngineTest.class.getResourceAsStream("/default.py");
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
scripts.invokeSync("invokeWithinScript.py")
2+
stringValue = stringValue + "4"

0 commit comments

Comments
 (0)