Add luajava exceptino handling, array and unit test logic from 1.0 branch
This commit is contained in:
@@ -26,13 +26,14 @@ import junit.framework.TestSuite;
|
||||
|
||||
import org.luaj.vm2.WeakTableTest.WeakKeyTableTest;
|
||||
import org.luaj.vm2.WeakTableTest.WeakKeyValueTableTest;
|
||||
import org.luaj.vm2.lib.jse.LuaJavaCoercionTest;
|
||||
|
||||
public class AllTests {
|
||||
|
||||
public static Test suite() {
|
||||
TestSuite suite = new TestSuite("All Tests for Luaj-vm2");
|
||||
|
||||
// table tests
|
||||
// vm tests
|
||||
TestSuite vm = new TestSuite("VM Tests");
|
||||
vm.addTestSuite(TypeTest.class);
|
||||
vm.addTestSuite(UnaryBinaryOperatorsTest.class);
|
||||
@@ -50,6 +51,11 @@ public class AllTests {
|
||||
table.addTestSuite(WeakKeyValueTableTest.class);
|
||||
suite.addTest(table);
|
||||
|
||||
// library tests
|
||||
TestSuite lib = new TestSuite("Library Tests");
|
||||
lib.addTestSuite(LuaJavaCoercionTest.class);
|
||||
suite.addTest(lib);
|
||||
|
||||
// compatiblity tests
|
||||
suite.addTest(CompatibiltyTest.suite());
|
||||
|
||||
|
||||
254
test/junit/org/luaj/vm2/lib/jse/LuaJavaCoercionTest.java
Normal file
254
test/junit/org/luaj/vm2/lib/jse/LuaJavaCoercionTest.java
Normal file
@@ -0,0 +1,254 @@
|
||||
package org.luaj.vm2.lib.jse;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.luaj.vm2.LuaError;
|
||||
import org.luaj.vm2.LuaInteger;
|
||||
import org.luaj.vm2.LuaString;
|
||||
import org.luaj.vm2.LuaTable;
|
||||
import org.luaj.vm2.LuaUserdata;
|
||||
import org.luaj.vm2.LuaValue;
|
||||
import org.luaj.vm2.Varargs;
|
||||
import org.luaj.vm2.compiler.LuaC;
|
||||
import org.luaj.vm2.lib.JsePlatform;
|
||||
|
||||
public class LuaJavaCoercionTest extends TestCase {
|
||||
|
||||
private static LuaValue _G;
|
||||
private static LuaValue ZERO = LuaValue.ZERO;
|
||||
private static LuaValue ONE = LuaValue.ONE;
|
||||
private static LuaValue TWO = LuaValue.valueOf(2);
|
||||
private static LuaValue THREE = LuaValue.valueOf(3);
|
||||
private static LuaString LENGTH = LuaString.valueOf("length");
|
||||
|
||||
protected void setUp() throws Exception {
|
||||
super.setUp();
|
||||
_G = JsePlatform.standardGlobals();
|
||||
LuaC.install();
|
||||
}
|
||||
|
||||
public void testJavaIntToLuaInt() {
|
||||
Integer i = Integer.valueOf(777);
|
||||
LuaValue v = CoerceJavaToLua.coerce(i);
|
||||
assertEquals( LuaInteger.class, v.getClass() );
|
||||
assertEquals( 777, v.toint() );
|
||||
}
|
||||
|
||||
public void testLuaIntToJavaInt() {
|
||||
LuaInteger i = LuaInteger.valueOf(777);
|
||||
Object o = CoerceLuaToJava.coerceArg(i, int.class);
|
||||
assertEquals( Integer.class, o.getClass() );
|
||||
assertEquals( 777, ((Number)o).intValue() );
|
||||
o = CoerceLuaToJava.coerceArg(i, Integer.class);
|
||||
assertEquals( Integer.class, o.getClass() );
|
||||
assertEquals( new Integer(777), o );
|
||||
}
|
||||
|
||||
public void testJavaStringToLuaString() {
|
||||
String s = new String("777");
|
||||
LuaValue v = CoerceJavaToLua.coerce(s);
|
||||
assertEquals( LuaString.class, v.getClass() );
|
||||
assertEquals( "777", v.toString() );
|
||||
}
|
||||
|
||||
public void testLuaStringToJavaString() {
|
||||
LuaString s = LuaValue.valueOf("777");
|
||||
Object o = CoerceLuaToJava.coerceArg(s, String.class);
|
||||
assertEquals( String.class, o.getClass() );
|
||||
assertEquals( "777", o );
|
||||
}
|
||||
|
||||
public void testJavaIntArrayToLuaTable() {
|
||||
int[] i = { 222, 333 };
|
||||
LuaValue v = CoerceJavaToLua.coerce(i);
|
||||
assertEquals( LuaUserdata.class, v.getClass() );
|
||||
assertNotNull( v.getmetatable() );
|
||||
assertEquals( LuaInteger.valueOf(222), v.get(ONE) );
|
||||
assertEquals( LuaInteger.valueOf(333), v.get(TWO) );
|
||||
assertEquals( TWO, v.get(LENGTH));
|
||||
assertEquals( LuaValue.NIL, v.get(THREE) );
|
||||
assertEquals( LuaValue.NIL, v.get(ZERO) );
|
||||
v.set(ONE, LuaInteger.valueOf(444));
|
||||
v.set(TWO, LuaInteger.valueOf(555));
|
||||
assertEquals( 444, i[0] );
|
||||
assertEquals( 555, i[1] );
|
||||
assertEquals( LuaInteger.valueOf(444), v.get(ONE) );
|
||||
assertEquals( LuaInteger.valueOf(555), v.get(TWO) );
|
||||
try {
|
||||
v.set(ZERO, LuaInteger.valueOf(777));
|
||||
fail( "array bound exception not thrown" );
|
||||
} catch ( LuaError lee ) {
|
||||
// expected
|
||||
}
|
||||
try {
|
||||
v.set(THREE, LuaInteger.valueOf(777));
|
||||
fail( "array bound exception not thrown" );
|
||||
} catch ( LuaError lee ) {
|
||||
// expected
|
||||
}
|
||||
}
|
||||
|
||||
public void testLuaTableToJavaIntArray() {
|
||||
LuaTable t = new LuaTable();
|
||||
t.set(1, LuaInteger.valueOf(222) );
|
||||
t.set(2, LuaInteger.valueOf(333) );
|
||||
int[] i = null;
|
||||
Object o = CoerceLuaToJava.coerceArg(t, int[].class);
|
||||
assertEquals( int[].class, o.getClass() );
|
||||
i = (int[]) o;
|
||||
assertEquals( 2, i.length );
|
||||
assertEquals( 222, i[0] );
|
||||
assertEquals( 333, i[1] );
|
||||
}
|
||||
|
||||
|
||||
public void testArrayParamScoring() {
|
||||
int a = 5;
|
||||
int[] b = { 44, 66 };
|
||||
int[][] c = { { 11, 22 }, { 33, 44 } };
|
||||
LuaValue la = LuaInteger.valueOf(a);
|
||||
LuaTable tb = new LuaTable();
|
||||
LuaTable tc = new LuaTable();
|
||||
LuaValue va = CoerceJavaToLua.coerce(a);
|
||||
LuaValue vb = CoerceJavaToLua.coerce(b);
|
||||
LuaValue vc = CoerceJavaToLua.coerce(c);
|
||||
tc.set( ONE, new LuaTable() );
|
||||
|
||||
int saa = CoerceLuaToJava.scoreParamTypes( new LuaValue[] { la }, new Class[] { int.class } );
|
||||
int sab = CoerceLuaToJava.scoreParamTypes( new LuaValue[] { la }, new Class[] { int[].class } );
|
||||
int sac = CoerceLuaToJava.scoreParamTypes( new LuaValue[] { la }, new Class[] { int[][].class } );
|
||||
assertTrue( saa < sab );
|
||||
assertTrue( saa < sac );
|
||||
int sba = CoerceLuaToJava.scoreParamTypes( new LuaValue[] { tb }, new Class[] { int.class } );
|
||||
int sbb = CoerceLuaToJava.scoreParamTypes( new LuaValue[] { tb }, new Class[] { int[].class } );
|
||||
int sbc = CoerceLuaToJava.scoreParamTypes( new LuaValue[] { tb }, new Class[] { int[][].class } );
|
||||
assertTrue( sbb < sba );
|
||||
assertTrue( sbb < sbc );
|
||||
int sca = CoerceLuaToJava.scoreParamTypes( new LuaValue[] { tc }, new Class[] { int.class } );
|
||||
int scb = CoerceLuaToJava.scoreParamTypes( new LuaValue[] { tc }, new Class[] { int[].class } );
|
||||
int scc = CoerceLuaToJava.scoreParamTypes( new LuaValue[] { tc }, new Class[] { int[][].class } );
|
||||
assertTrue( scc < sca );
|
||||
assertTrue( scc < scb );
|
||||
|
||||
int vaa = CoerceLuaToJava.scoreParamTypes( new LuaValue[] { va }, new Class[] { int.class } );
|
||||
int vab = CoerceLuaToJava.scoreParamTypes( new LuaValue[] { va }, new Class[] { int[].class } );
|
||||
int vac = CoerceLuaToJava.scoreParamTypes( new LuaValue[] { va }, new Class[] { int[][].class } );
|
||||
assertTrue( vaa < vab );
|
||||
assertTrue( vaa < vac );
|
||||
int vba = CoerceLuaToJava.scoreParamTypes( new LuaValue[] { vb }, new Class[] { int.class } );
|
||||
int vbb = CoerceLuaToJava.scoreParamTypes( new LuaValue[] { vb }, new Class[] { int[].class } );
|
||||
int vbc = CoerceLuaToJava.scoreParamTypes( new LuaValue[] { vb }, new Class[] { int[][].class } );
|
||||
assertTrue( vbb < vba );
|
||||
assertTrue( vbb < vbc );
|
||||
int vca = CoerceLuaToJava.scoreParamTypes( new LuaValue[] { vc }, new Class[] { int.class } );
|
||||
int vcb = CoerceLuaToJava.scoreParamTypes( new LuaValue[] { vc }, new Class[] { int[].class } );
|
||||
int vcc = CoerceLuaToJava.scoreParamTypes( new LuaValue[] { vc }, new Class[] { int[][].class } );
|
||||
assertTrue( vcc < vca );
|
||||
assertTrue( vcc < vcb );
|
||||
}
|
||||
|
||||
public static class SampleClass {
|
||||
public String sample() { return "void-args"; }
|
||||
public String sample(int a) { return "int-args "+a; }
|
||||
public String sample(int[] a) { return "int-array-args "+a[0]+","+a[1]; }
|
||||
public String sample(int[][] a) { return "int-array-array-args "+a[0][0]+","+a[0][1]+","+a[1][0]+","+a[1][1]; }
|
||||
}
|
||||
|
||||
private static final LuaString SAMPLE = LuaString.valueOf("sample");
|
||||
|
||||
public void testIntArrayParameterMatching() {
|
||||
LuaValue v = CoerceJavaToLua.coerce(new SampleClass());
|
||||
|
||||
// get sample field, call with no arguments
|
||||
LuaValue method = v.get(SAMPLE);
|
||||
LuaValue result = method.call(v);
|
||||
assertEquals( "void-args", result.toString() );
|
||||
|
||||
// get sample field, call with one arguments
|
||||
LuaValue arg = CoerceJavaToLua.coerce(new Integer(123));
|
||||
method.call(v,arg);
|
||||
result = method.call(v);
|
||||
assertEquals( "int-args 123", result.toString() );
|
||||
|
||||
// get sample field, call with array argument
|
||||
arg = CoerceJavaToLua.coerce(new int[]{345,678});
|
||||
method.call(v,arg);
|
||||
result = method.call(v);
|
||||
assertEquals( "int-array-args 345,678", result.toString() );
|
||||
|
||||
// get sample field, call with two-d array argument
|
||||
arg = CoerceJavaToLua.coerce(new int[][]{{22,33},{44,55}});
|
||||
method.call(v,arg);
|
||||
result = method.call(v);
|
||||
assertEquals( "int-array-array-args 22,33,44,55", result.toString() );
|
||||
}
|
||||
|
||||
public static final class SomeException extends RuntimeException {
|
||||
public SomeException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
|
||||
public static final class SomeClass {
|
||||
public static void someMethod() {
|
||||
throw new SomeException( "this is some message" );
|
||||
}
|
||||
}
|
||||
|
||||
public void testExceptionMessage() {
|
||||
String script = "return pcall( luajava.bindClass( \""+SomeClass.class.getName()+"\").someMethod )";
|
||||
Varargs vresult = _G.get("loadstring").call(LuaValue.valueOf(script)).invoke(LuaValue.NONE);
|
||||
LuaValue message = vresult.arg1();
|
||||
LuaValue status = vresult.arg(2);
|
||||
assertEquals( LuaValue.FALSE, status );
|
||||
assertEquals( "lua error: "+SomeException.class.getName()+": this is some message", message.toString() );
|
||||
}
|
||||
|
||||
public void testLuaErrorCause() {
|
||||
String script = "luajava.bindClass( \""+SomeClass.class.getName()+"\").someMethod()";
|
||||
LuaValue chunk = _G.get("loadstring").call(LuaValue.valueOf(script));
|
||||
try {
|
||||
chunk.invoke(LuaValue.NONE);
|
||||
fail( "call should not have succeeded" );
|
||||
} catch ( LuaError lee ) {
|
||||
Throwable c = lee.getCause();
|
||||
assertEquals( SomeException.class, c.getClass() );
|
||||
}
|
||||
}
|
||||
|
||||
public interface VarArgsInterface {
|
||||
public String varargsMethod( String a, String ... v );
|
||||
public String arrayargsMethod( String a, String[] v );
|
||||
}
|
||||
|
||||
public void testVarArgsProxy() {
|
||||
String script = "return luajava.createProxy( \""+VarArgsInterface.class.getName()+"\", \n"+
|
||||
"{\n" +
|
||||
" varargsMethod = function(a,...)\n" +
|
||||
" return table.concat({a,...},'-')\n" +
|
||||
" end,\n" +
|
||||
" arrayargsMethod = function(a,array)\n" +
|
||||
" return tostring(a)..(array and \n" +
|
||||
" ('-'..tostring(array.length)\n" +
|
||||
" ..'-'..tostring(array[1])\n" +
|
||||
" ..'-'..tostring(array[2])\n" +
|
||||
" ) or '-nil')\n" +
|
||||
" end,\n" +
|
||||
"} )\n";
|
||||
Varargs chunk = _G.get("loadstring").call(LuaValue.valueOf(script));
|
||||
if ( ! chunk.arg1().toboolean() )
|
||||
fail( chunk.arg(2).toString() );
|
||||
LuaValue result = chunk.arg1().call();
|
||||
Object u = result.touserdata();
|
||||
VarArgsInterface v = (VarArgsInterface) u;
|
||||
assertEquals( "foo", v.varargsMethod("foo") );
|
||||
assertEquals( "foo-bar", v.varargsMethod("foo", "bar") );
|
||||
assertEquals( "foo-bar-etc", v.varargsMethod("foo", "bar", "etc") );
|
||||
assertEquals( "foo-0-nil-nil", v.arrayargsMethod("foo", new String[0]) );
|
||||
assertEquals( "foo-1-bar-nil", v.arrayargsMethod("foo", new String[] {"bar"}) );
|
||||
assertEquals( "foo-2-bar-etc", v.arrayargsMethod("foo", new String[] {"bar","etc"}) );
|
||||
assertEquals( "foo-3-bar-etc", v.arrayargsMethod("foo", new String[] {"bar","etc","etc"}) );
|
||||
assertEquals( "foo-nil", v.arrayargsMethod("foo", null) );
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user