/
LuaScriptUtilTest.java
79 lines (61 loc) · 2.69 KB
/
LuaScriptUtilTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package nl.weeaboo.vn.impl.script.lua;
import java.io.IOException;
import java.util.Arrays;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import nl.weeaboo.filesystem.FilePath;
import nl.weeaboo.lua2.LuaException;
import nl.weeaboo.vn.core.IContext;
import nl.weeaboo.vn.gdx.HeadlessGdx;
import nl.weeaboo.vn.impl.core.TestEnvironment;
import nl.weeaboo.vn.script.IScriptThread;
import nl.weeaboo.vn.script.ScriptException;
public class LuaScriptUtilTest {
private TestEnvironment env;
private IContext mainContext;
@Before
public void init() throws ScriptException {
HeadlessGdx.init();
env = TestEnvironment.newInstance();
env.getScriptEnv().initEnv();
mainContext = env.getContextManager().createContext();
}
@After
public void deinit() {
env.destroy();
}
@Test
public void isLvnFile() {
Assert.assertFalse(LuaScriptUtil.isLvnFile("test.lua"));
Assert.assertTrue(LuaScriptUtil.isLvnFile("test.lvn"));
Assert.assertFalse(LuaScriptUtil.isLvnFile("test.lvn.txt"));
}
@Test
public void toScriptException() {
LuaException original = LuaException.wrap("message", new RuntimeException("cause"));
ScriptException converted = LuaScriptUtil.toScriptException("newMessage", original);
// The conversion doesn't wrap the entire exception in another layer!
Assert.assertEquals("newMessage: message", converted.getMessage());
Assert.assertSame(original.getCause(), converted.getCause());
}
/** Test for loadScript/callFunction/eval functions */
@Test
public void loadCallEval() throws IOException, ScriptException {
final IScriptThread mainThread = mainContext.getScriptContext().getMainThread();
// Load script with some test functions
LuaScriptUtil.loadScript(mainContext, FilePath.of("script-util-test"));
// Call test function
LuaScriptUtil.callFunction(mainContext, "x", 1, 2, 3);
// Check that parameters were passed and the function was executed
LuaTestUtil.assertGlobal("test", 1 + 2 + 3);
// Main thread is paused in the yield call on line 15
Assert.assertEquals(Arrays.asList("script-util-test.lvn:15"), mainThread.getStackTrace());
// Run some arbitrary code
LuaScriptUtil.eval(mainContext, "test = 42");
LuaTestUtil.assertGlobal("test", 42);
// After eval completes, the thread is still stuck in the same position as before
Assert.assertEquals(Arrays.asList("script-util-test.lvn:15"), mainThread.getStackTrace());
}
}