-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve test coverage for LuaJ bindings
- Loading branch information
Showing
10 changed files
with
210 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
example/src/test/java/party/iroiro/luajava/luaj/LuaJLibTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package party.iroiro.luajava.luaj; | ||
|
||
|
||
import org.junit.jupiter.api.Test; | ||
import party.iroiro.luajava.Lua; | ||
|
||
import java.io.*; | ||
import java.nio.file.Files; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
import static party.iroiro.luajava.Lua.LuaError.OK; | ||
|
||
public class LuaJLibTest { | ||
@Test | ||
public void testUnsupportedOperation() { | ||
try (Lua L = new LuaJ()) { | ||
L.openLibraries(); | ||
assertNotEquals(OK, L.run("return io.tmpfile()")); | ||
assertNotEquals(OK, L.run("return io.popen('/bin/ls')")); | ||
} | ||
} | ||
|
||
@Test | ||
public void testFileOperations() { | ||
try (Lua L = new LuaJ()) { | ||
L.openLibraries(); | ||
File file = File.createTempFile("luajava", ".txt"); | ||
assertFalse(file.getAbsolutePath().contains("'") || file.getAbsolutePath().endsWith("\\")); | ||
assertEquals(OK, L.run("f = io.open('" + file.getAbsolutePath() + "', 'w')")); | ||
assertEquals(OK, L.run("f:setvbuf('line')")); | ||
assertEquals(OK, L.run("f:write('Hello World')")); | ||
assertEquals(OK, L.run("io.flush(f)")); | ||
assertEquals(OK, L.run("io.close(f)")); | ||
try (BufferedReader r = new BufferedReader(new FileReader(file))) { | ||
assertEquals("Hello World", r.readLine()); | ||
} | ||
|
||
assertEquals(OK, L.run("f = io.open('" + file.getAbsolutePath() + "', 'a')")); | ||
assertEquals(OK, L.run("f:write('!!!')")); | ||
assertEquals(OK, L.run("io.close(f)")); | ||
try (BufferedReader r = new BufferedReader(new FileReader(file))) { | ||
assertEquals("Hello World!!!", r.readLine()); | ||
} | ||
|
||
assertEquals(OK, L.run("f = io.open('" + file.getAbsolutePath() + "', 'r')")); | ||
assertNotEquals(OK, L.run("f:seek('set', 2)")); | ||
assertEquals(OK, L.run("assert(not f:read('*n'))")); | ||
assertEquals(OK, L.run("assert('Hello World!!!' == f:read('*l'))")); | ||
assertEquals(OK, L.run("assert(not f:read('*l'))")); | ||
assertEquals(OK, L.run("io.close(f)")); | ||
|
||
assertEquals(OK, L.run("f = io.open('" + file.getAbsolutePath() + "', 'r')")); | ||
assertEquals(OK, L.run("for l in f:lines() do assert(l == 'Hello World!!!') end")); | ||
assertEquals(OK, L.run("io.close(f)")); | ||
|
||
assertEquals(OK, L.run("f = io.open('" + file.getAbsolutePath() + "', 'r')")); | ||
assertEquals(OK, L.run("assert(f:read('*a') == 'Hello World!!!')")); | ||
assertEquals(OK, L.run("io.close(f)")); | ||
|
||
assertEquals(OK, L.run("io.input()")); | ||
assertEquals(OK, L.run("io.output()")); | ||
assertEquals(OK, L.run("io.input('" + file.getAbsolutePath() + "')")); | ||
assertEquals(OK, L.run("io.output('" + file.getAbsolutePath() + "')")); | ||
|
||
Files.delete(file.toPath()); | ||
} catch (IOException e) { | ||
fail(e); | ||
} | ||
} | ||
} |
83 changes: 83 additions & 0 deletions
83
example/src/test/java/party/iroiro/luajava/luaj/LuaJTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package party.iroiro.luajava.luaj; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import party.iroiro.luajava.Lua; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
import static party.iroiro.luajava.Lua.LuaError.OK; | ||
import static party.iroiro.luajava.Lua.LuaType.TABLE; | ||
|
||
public class LuaJTest { | ||
@Test | ||
public void testLuaJGlobalNop() { | ||
try (Lua L = new LuaJ()) { | ||
L.getLuaNative().loadAsGlobal(); | ||
} | ||
} | ||
|
||
@Test | ||
public void testLuaJConcat() { | ||
try (Lua L = new LuaJ()) { | ||
L.createTable(0, 0); | ||
assertThrows(IllegalArgumentException.class, () -> L.concat(-1)); | ||
L.concat(1); | ||
assertEquals(TABLE, L.type(-1)); | ||
} | ||
} | ||
|
||
@Test | ||
public void testLuaJUnsupported() { | ||
try (Lua L = new LuaJ()) { | ||
LuaJNatives natives = (LuaJNatives) L.getLuaNative(); | ||
long p = L.getPointer(); | ||
assertThrows(UnsupportedOperationException.class, () -> natives.lua_topointer(p, 1)); | ||
assertThrows(UnsupportedOperationException.class, () -> natives.lua_tothread(p, 1)); | ||
assertThrows(UnsupportedOperationException.class, () -> natives.lua_touserdata(p, 1)); | ||
assertThrows(UnsupportedOperationException.class, () -> natives.lua_yield(p, 1)); | ||
assertThrows(UnsupportedOperationException.class, () -> natives.lua_gethookcount(p)); | ||
assertThrows(UnsupportedOperationException.class, () -> natives.lua_gethookmask(p)); | ||
|
||
natives.luaL_where(p, 0); | ||
assertEquals("", L.toString(-1)); | ||
} | ||
} | ||
|
||
@Test | ||
public void testLuaJTypes() { | ||
try (Lua L = new LuaJ()) { | ||
LuaJNatives natives = (LuaJNatives) L.getLuaNative(); | ||
L.push((J) -> 0); | ||
natives.lua_pushlightuserdata(L.getPointer(), 1000); | ||
assertEquals(1, natives.lua_islightuserdata(L.getPointer(), -1)); | ||
assertEquals(0, natives.lua_islightuserdata(L.getPointer(), -2)); | ||
|
||
assertEquals(1, natives.lua_iscfunction(L.getPointer(), -2)); | ||
assertEquals(0, natives.lua_iscfunction(L.getPointer(), -1)); | ||
|
||
natives.lua_newtable(L.getPointer()); | ||
assertEquals(TABLE, L.type(-1)); | ||
} | ||
} | ||
|
||
@Test | ||
public void testLuaJMisc() { | ||
try (Lua L = new LuaJ()) { | ||
LuaJNatives natives = (LuaJNatives) L.getLuaNative(); | ||
long p = L.getPointer(); | ||
assertEquals("BBB", natives.luaL_gsub(p, "AAA", "A", "B")); | ||
|
||
assertEquals(OK, L.run("local a = 'up'; return function() return a end")); | ||
assertEquals("", natives.lua_getupvalue(p, -1, 0)); | ||
assertEquals("up", L.toString(-1)); | ||
L.pop(1); | ||
L.push("down"); | ||
assertEquals("", natives.lua_setupvalue(p, -2, 0)); | ||
assertEquals(OK, L.pCall(0, 1)); | ||
assertEquals("down", L.toString(-1)); | ||
|
||
assertEquals(OK, L.run("local t = {}; setmetatable(t, { a = function() return 100 end }); return t")); | ||
assertEquals(1, natives.luaL_callmeta(p, -1, "a")); | ||
assertEquals(100, L.toNumber(-1)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
local functions = {} | ||
for i = 1, 10 do | ||
functions[#functions + 1] = coroutine.wrap(function(prefix) | ||
local i = 1 | ||
for i = 1, 5 do | ||
prefix = coroutine.yield(prefix .. i) | ||
end | ||
end) | ||
end | ||
|
||
function assertFunctions(prefix, yields) | ||
for _, f in ipairs(functions) do | ||
assert(f(prefix) == yields) | ||
end | ||
end | ||
|
||
assert("A", "A1") | ||
assert("B", "B2") | ||
assert("C", "C3") | ||
assert("D", "D4") | ||
assert("E", "E5") | ||
assert("F", nil) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters