Skip to content

Commit

Permalink
Simplify exception assertions for Android tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gudzpoz committed Apr 12, 2024
1 parent 3982e58 commit 9548b33
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,20 @@
import static party.iroiro.luajava.Lua.LuaError.RUNTIME;

public class LuaScriptSuite<T extends AbstractLua> {
private static final String LUA_ASSERT_THROWS = "function assertThrows(message, fun, ...)\n" +
" ok, msg = pcall(fun, ...)\n" +
" assert(not ok, debug.traceback('No error while expecting \"' .. message .. '\"'))\n" +
" assert(type(msg) == 'string', debug.traceback('Expecting error message on top of the stack'))\n" +
" assert(string.find(msg, message) ~= nil, debug.traceback('Expecting \"' .. message .. '\": Received \"' .. msg .. '\"'))\n" +
"end";
private static final String LUA_ASSERT_THROWS = "\n" +
"function assertThrows(message, fun, ...)\n" +
" ok, msg = pcall(fun, ...)\n" +
" messages = type(message) == 'string' and { message } or message\n" +
" message = '\"' .. table.concat(messages, ', ') .. '\"'\n" +
" assert(not ok, debug.traceback('No error while expecting ' .. message))\n" +
" assert(type(msg) == 'string', debug.traceback('Expecting error message on top of the stack'))\n" +
" for _, m in ipairs(messages) do\n" +
" if string.find(msg, m) ~= nil then\n" +
" return\n" +
" end\n" +
" end\n" +
" assert(false, debug.traceback('Expecting ' .. message .. ': Received \"' .. msg .. '\"'))\n" +
"end";
private final T L;
private final LuaTestConsumer<String> logger;

Expand All @@ -43,6 +51,7 @@ public LuaScriptSuite(T L, LuaTestConsumer<String> logger) {
public static void addAssertThrows(Lua L) {
L.openLibrary("string");
L.openLibrary("debug");
L.openLibrary("table");
assertEquals(OK, L.run(LUA_ASSERT_THROWS));
L.push(DefaultProxyTest.isDefaultAvailable() && !(isLuaJ(L)));
L.setGlobal("JAVA8");
Expand Down
20 changes: 9 additions & 11 deletions example/suite/src/main/resources/suite/proxyTest.lua
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,13 @@ assertThrows('java.lang.IncompatibleClassChangeError',

-- java.* libraries: runs fine on Android
iter = java.proxy('java.util.Iterator', iterImpl)
expects = 'party.iroiro.luajava.LuaException: method not implemented: '
if JAVA8 then
expects = 'java.lang.UnsupportedOperationException'
end
if LUAJ then
expects = 'invokespecial not available'
end
assertThrows(ANDROID and "UnsupportedOperationException" or expects, iter.remove, iter)
expects = {
'java.lang.UnsupportedOperationException',
'java.lang.IncompatibleClassChangeError',
'method not implemented',
'invokespecial not available',
}
assertThrows(expects, iter.remove, iter)
res = {}
if JAVA8 then
iter:forEachRemaining(function(this, e) res[e] = true end)
Expand All @@ -78,7 +77,7 @@ B = java.proxy('party.iroiro.luajava.suite.B', 'party.iroiro.luajava.DefaultProx
if JAVA8 and not ANDROID then
return java.method(B, 'party.iroiro.luajava.suite.B:b')()
else
assertThrows(LUAJ and expects or 'java.lang.IncompatibleClassChangeError', java.method(B, 'party.iroiro.luajava.suite.B:b'))
assertThrows(expects, java.method(B, 'party.iroiro.luajava.suite.B:b'))
return 3
end
end
Expand Down Expand Up @@ -114,8 +113,7 @@ obj = java.proxy('party.iroiro.luajava.DefaultProxyTest.D', {
if JAVA8 and not ANDROID then
assert(java.method(iter, 'party.iroiro.luajava.DefaultProxyTest.D:noReturn')() == nil)
else
assertThrows(LUAJ and expects or 'java.lang.IncompatibleClassChangeError',
java.method(iter, 'party.iroiro.luajava.DefaultProxyTest.D:noReturn'))
assertThrows(expects, java.method(iter, 'party.iroiro.luajava.DefaultProxyTest.D:noReturn'))
end
end
})
Expand Down

0 comments on commit 9548b33

Please sign in to comment.