Skip to content

Commit 9548b33

Browse files
committed
Simplify exception assertions for Android tests
1 parent 3982e58 commit 9548b33

File tree

2 files changed

+24
-17
lines changed

2 files changed

+24
-17
lines changed

example/suite/src/main/java/party/iroiro/luajava/LuaScriptSuite.java

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,20 @@
1919
import static party.iroiro.luajava.Lua.LuaError.RUNTIME;
2020

2121
public class LuaScriptSuite<T extends AbstractLua> {
22-
private static final String LUA_ASSERT_THROWS = "function assertThrows(message, fun, ...)\n" +
23-
" ok, msg = pcall(fun, ...)\n" +
24-
" assert(not ok, debug.traceback('No error while expecting \"' .. message .. '\"'))\n" +
25-
" assert(type(msg) == 'string', debug.traceback('Expecting error message on top of the stack'))\n" +
26-
" assert(string.find(msg, message) ~= nil, debug.traceback('Expecting \"' .. message .. '\": Received \"' .. msg .. '\"'))\n" +
27-
"end";
22+
private static final String LUA_ASSERT_THROWS = "\n" +
23+
"function assertThrows(message, fun, ...)\n" +
24+
" ok, msg = pcall(fun, ...)\n" +
25+
" messages = type(message) == 'string' and { message } or message\n" +
26+
" message = '\"' .. table.concat(messages, ', ') .. '\"'\n" +
27+
" assert(not ok, debug.traceback('No error while expecting ' .. message))\n" +
28+
" assert(type(msg) == 'string', debug.traceback('Expecting error message on top of the stack'))\n" +
29+
" for _, m in ipairs(messages) do\n" +
30+
" if string.find(msg, m) ~= nil then\n" +
31+
" return\n" +
32+
" end\n" +
33+
" end\n" +
34+
" assert(false, debug.traceback('Expecting ' .. message .. ': Received \"' .. msg .. '\"'))\n" +
35+
"end";
2836
private final T L;
2937
private final LuaTestConsumer<String> logger;
3038

@@ -43,6 +51,7 @@ public LuaScriptSuite(T L, LuaTestConsumer<String> logger) {
4351
public static void addAssertThrows(Lua L) {
4452
L.openLibrary("string");
4553
L.openLibrary("debug");
54+
L.openLibrary("table");
4655
assertEquals(OK, L.run(LUA_ASSERT_THROWS));
4756
L.push(DefaultProxyTest.isDefaultAvailable() && !(isLuaJ(L)));
4857
L.setGlobal("JAVA8");

example/suite/src/main/resources/suite/proxyTest.lua

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,13 @@ assertThrows('java.lang.IncompatibleClassChangeError',
5050

5151
-- java.* libraries: runs fine on Android
5252
iter = java.proxy('java.util.Iterator', iterImpl)
53-
expects = 'party.iroiro.luajava.LuaException: method not implemented: '
54-
if JAVA8 then
55-
expects = 'java.lang.UnsupportedOperationException'
56-
end
57-
if LUAJ then
58-
expects = 'invokespecial not available'
59-
end
60-
assertThrows(ANDROID and "UnsupportedOperationException" or expects, iter.remove, iter)
53+
expects = {
54+
'java.lang.UnsupportedOperationException',
55+
'java.lang.IncompatibleClassChangeError',
56+
'method not implemented',
57+
'invokespecial not available',
58+
}
59+
assertThrows(expects, iter.remove, iter)
6160
res = {}
6261
if JAVA8 then
6362
iter:forEachRemaining(function(this, e) res[e] = true end)
@@ -78,7 +77,7 @@ B = java.proxy('party.iroiro.luajava.suite.B', 'party.iroiro.luajava.DefaultProx
7877
if JAVA8 and not ANDROID then
7978
return java.method(B, 'party.iroiro.luajava.suite.B:b')()
8079
else
81-
assertThrows(LUAJ and expects or 'java.lang.IncompatibleClassChangeError', java.method(B, 'party.iroiro.luajava.suite.B:b'))
80+
assertThrows(expects, java.method(B, 'party.iroiro.luajava.suite.B:b'))
8281
return 3
8382
end
8483
end
@@ -114,8 +113,7 @@ obj = java.proxy('party.iroiro.luajava.DefaultProxyTest.D', {
114113
if JAVA8 and not ANDROID then
115114
assert(java.method(iter, 'party.iroiro.luajava.DefaultProxyTest.D:noReturn')() == nil)
116115
else
117-
assertThrows(LUAJ and expects or 'java.lang.IncompatibleClassChangeError',
118-
java.method(iter, 'party.iroiro.luajava.DefaultProxyTest.D:noReturn'))
116+
assertThrows(expects, java.method(iter, 'party.iroiro.luajava.DefaultProxyTest.D:noReturn'))
119117
end
120118
end
121119
})

0 commit comments

Comments
 (0)