Changes made for the following:
1) Platform.java stays as abstract class with, but default implementation is removed. Stays in org.luaj.vm package
2) org.luaj.platform package is created to hold concrete Platform implementations
3) Platform.newLuaState() method is introduced to instantiate the LuaState. Other constructors are privatized.
4) Following Platform implementations are created:
J2sePlatform
J2meMidp20Cldc11Platform
J2meMidp10Cldc10Platform
5) All clients of luaj-vm are changed to include startup code that looks something like this:
Platform.setInstance( new J2meMidp20Cldc11Platform() );
LuaState state = Platform.getInstance().newLuaState();
This commit is contained in:
11
src/test/java/org/luaj/TestPlatform.java
Normal file
11
src/test/java/org/luaj/TestPlatform.java
Normal file
@@ -0,0 +1,11 @@
|
||||
package org.luaj;
|
||||
|
||||
import java.io.InputStream;
|
||||
|
||||
import org.luaj.platform.J2sePlatform;
|
||||
|
||||
public class TestPlatform extends J2sePlatform {
|
||||
public InputStream openFile(String fileName) {
|
||||
return getClass().getResourceAsStream("/" + fileName);
|
||||
}
|
||||
}
|
||||
@@ -9,84 +9,89 @@ import java.net.URL;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.luaj.TestPlatform;
|
||||
import org.luaj.debug.Print;
|
||||
import org.luaj.vm.LPrototype;
|
||||
import org.luaj.vm.LoadState;
|
||||
import org.luaj.vm.LuaState;
|
||||
import org.luaj.vm.Platform;
|
||||
|
||||
abstract public class AbstractUnitTests extends TestCase {
|
||||
|
||||
abstract
|
||||
public class AbstractUnitTests extends TestCase {
|
||||
|
||||
private final String zipfile;
|
||||
private final String dir;
|
||||
|
||||
public AbstractUnitTests(String zipfile, String dir) {
|
||||
this.zipfile = zipfile;
|
||||
this.dir = dir;
|
||||
}
|
||||
private final String zipfile;
|
||||
private final String dir;
|
||||
|
||||
protected void doTest( String file ) {
|
||||
try {
|
||||
// load source from jar
|
||||
String path = "jar:file:" + zipfile + "!/" + dir + "/" + file;
|
||||
byte[] lua = bytesFromJar( path );
|
||||
|
||||
// compile in memory
|
||||
InputStream is = new ByteArrayInputStream( lua );
|
||||
LPrototype p = LuaC.compile(is, dir+"/"+file);
|
||||
String actual = protoToString( p );
|
||||
|
||||
// load expected value from jar
|
||||
byte[] luac = bytesFromJar( path + "c" );
|
||||
LPrototype e = loadFromBytes( luac, file );
|
||||
String expected = protoToString( e );
|
||||
public AbstractUnitTests(String zipfile, String dir) {
|
||||
this.zipfile = zipfile;
|
||||
this.dir = dir;
|
||||
}
|
||||
|
||||
// compare results
|
||||
assertEquals( expected, actual );
|
||||
|
||||
// dump into memory
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
DumpState.dump(p, baos, false);
|
||||
byte[] dumped = baos.toByteArray();
|
||||
|
||||
// re-undump
|
||||
LPrototype p2 = loadFromBytes( dumped, file );
|
||||
String actual2 = protoToString( p2 );
|
||||
|
||||
// compare again
|
||||
assertEquals( actual, actual2 );
|
||||
|
||||
} catch (IOException e) {
|
||||
fail( e.toString() );
|
||||
}
|
||||
}
|
||||
|
||||
protected byte[] bytesFromJar(String path) throws IOException {
|
||||
URL url = new URL(path);
|
||||
InputStream is = url.openStream();
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
byte[] buffer = new byte[2048];
|
||||
int n;
|
||||
while ( (n = is.read(buffer)) >= 0 )
|
||||
baos.write( buffer, 0, n );
|
||||
is.close();
|
||||
return baos.toByteArray();
|
||||
}
|
||||
|
||||
protected LPrototype loadFromBytes(byte[] bytes, String script) throws IOException {
|
||||
LuaState state = new LuaState();
|
||||
InputStream is = new ByteArrayInputStream( bytes );
|
||||
return LoadState.undump(state, is, script);
|
||||
}
|
||||
|
||||
protected String protoToString(LPrototype p) {
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
PrintStream ps = new PrintStream( baos );
|
||||
Print.ps = ps;
|
||||
new Print().printFunction(p, true);
|
||||
return baos.toString();
|
||||
}
|
||||
|
||||
protected void setUp() throws Exception {
|
||||
super.setUp();
|
||||
Platform.setInstance(new TestPlatform());
|
||||
}
|
||||
|
||||
protected void doTest(String file) {
|
||||
try {
|
||||
// load source from jar
|
||||
String path = "jar:file:" + zipfile + "!/" + dir + "/" + file;
|
||||
byte[] lua = bytesFromJar(path);
|
||||
|
||||
// compile in memory
|
||||
InputStream is = new ByteArrayInputStream(lua);
|
||||
LPrototype p = LuaC.compile(is, dir + "/" + file);
|
||||
String actual = protoToString(p);
|
||||
|
||||
// load expected value from jar
|
||||
byte[] luac = bytesFromJar(path + "c");
|
||||
LPrototype e = loadFromBytes(luac, file);
|
||||
String expected = protoToString(e);
|
||||
|
||||
// compare results
|
||||
assertEquals(expected, actual);
|
||||
|
||||
// dump into memory
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
DumpState.dump(p, baos, false);
|
||||
byte[] dumped = baos.toByteArray();
|
||||
|
||||
// re-undump
|
||||
LPrototype p2 = loadFromBytes(dumped, file);
|
||||
String actual2 = protoToString(p2);
|
||||
|
||||
// compare again
|
||||
assertEquals(actual, actual2);
|
||||
|
||||
} catch (IOException e) {
|
||||
fail(e.toString());
|
||||
}
|
||||
}
|
||||
|
||||
protected byte[] bytesFromJar(String path) throws IOException {
|
||||
URL url = new URL(path);
|
||||
InputStream is = url.openStream();
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
byte[] buffer = new byte[2048];
|
||||
int n;
|
||||
while ((n = is.read(buffer)) >= 0)
|
||||
baos.write(buffer, 0, n);
|
||||
is.close();
|
||||
return baos.toByteArray();
|
||||
}
|
||||
|
||||
protected LPrototype loadFromBytes(byte[] bytes, String script)
|
||||
throws IOException {
|
||||
LuaState state = Platform.newLuaState();
|
||||
InputStream is = new ByteArrayInputStream(bytes);
|
||||
return LoadState.undump(state, is, script);
|
||||
}
|
||||
|
||||
protected String protoToString(LPrototype p) {
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
PrintStream ps = new PrintStream(baos);
|
||||
Print.ps = ps;
|
||||
new Print().printFunction(p, true);
|
||||
return baos.toString();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,29 +1,14 @@
|
||||
package org.luaj.compiler;
|
||||
|
||||
import org.luaj.debug.j2se.J2sePlatform;
|
||||
import org.luaj.vm.LDouble;
|
||||
import org.luaj.vm.LNumber;
|
||||
import org.luaj.vm.Platform;
|
||||
|
||||
|
||||
public class CompilerUnitTests extends AbstractUnitTests {
|
||||
|
||||
static {
|
||||
// override platform to test with standard debug features.
|
||||
Platform.setInstance( new J2sePlatform() {
|
||||
public LNumber mathPow(double lhs, double rhs) {
|
||||
double d = Math.pow(lhs, rhs);
|
||||
return LDouble.valueOf(d);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public CompilerUnitTests() {
|
||||
super( "src/test/compile/lua5.1-tests.zip",
|
||||
"lua5.1-tests" );
|
||||
}
|
||||
|
||||
public void testAll() { doTest("all.lua"); }
|
||||
public CompilerUnitTests() {
|
||||
super("src/test/compile/lua5.1-tests.zip", "lua5.1-tests");
|
||||
}
|
||||
|
||||
public void testAll() { doTest("all.lua"); }
|
||||
public void testApi() { doTest("api.lua"); }
|
||||
public void testAttrib() { doTest("attrib.lua"); }
|
||||
public void testBig() { doTest("big.lua"); }
|
||||
|
||||
@@ -5,15 +5,22 @@ import java.io.InputStream;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.luaj.TestPlatform;
|
||||
import org.luaj.debug.Print;
|
||||
import org.luaj.lib.BaseLib;
|
||||
import org.luaj.vm.LClosure;
|
||||
import org.luaj.vm.LPrototype;
|
||||
import org.luaj.vm.LValue;
|
||||
import org.luaj.vm.LuaState;
|
||||
import org.luaj.vm.Platform;
|
||||
|
||||
public class SimpleTests extends TestCase {
|
||||
|
||||
protected void setUp() throws Exception {
|
||||
super.setUp();
|
||||
Platform.setInstance(new TestPlatform());
|
||||
}
|
||||
|
||||
private void doTest( String script ) {
|
||||
try {
|
||||
InputStream is = new ByteArrayInputStream( script.getBytes("UTF8") );
|
||||
@@ -22,7 +29,7 @@ public class SimpleTests extends TestCase {
|
||||
Print.printCode( p );
|
||||
|
||||
// try running the code!
|
||||
LuaState state = new LuaState();
|
||||
LuaState state = Platform.newLuaState();
|
||||
BaseLib.install( state._G );
|
||||
LClosure c = new LClosure( state, p );
|
||||
state.doCall( c, new LValue[0] );
|
||||
|
||||
@@ -24,21 +24,33 @@ package org.luaj.debug;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
import org.luaj.debug.DebugLuaState;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.luaj.TestPlatform;
|
||||
import org.luaj.compiler.LuaC;
|
||||
import org.luaj.vm.DebugNetSupport;
|
||||
import org.luaj.vm.LClosure;
|
||||
import org.luaj.vm.LPrototype;
|
||||
import org.luaj.vm.LValue;
|
||||
import org.luaj.vm.LoadState;
|
||||
import org.luaj.vm.LPrototype;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
import org.luaj.vm.LuaState;
|
||||
import org.luaj.vm.Platform;
|
||||
|
||||
public class DebugStackStateTest extends TestCase {
|
||||
|
||||
public void testDebugStackState() throws InterruptedException, IOException {
|
||||
String script = "/test6.luac";
|
||||
String script = "/test6.lua";
|
||||
|
||||
// set up the vm
|
||||
final DebugLuaState state = new DebugLuaState();
|
||||
System.setProperty(Platform.PROPERTY_LUAJ_DEBUG, "true");
|
||||
Platform.setInstance(new TestPlatform() {
|
||||
public DebugNetSupport getDebugSupport() throws IOException {
|
||||
return null;
|
||||
}
|
||||
});
|
||||
|
||||
final DebugLuaState state = (DebugLuaState) Platform.newLuaState();
|
||||
LuaC.install();
|
||||
InputStream is = getClass().getResourceAsStream( script );
|
||||
LPrototype p = LoadState.undump(state, is, script);
|
||||
|
||||
|
||||
@@ -21,20 +21,13 @@
|
||||
******************************************************************************/
|
||||
package org.luaj.debug.j2se;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.Reader;
|
||||
import java.net.URL;
|
||||
import java.util.Properties;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.luaj.debug.DebugLuaState;
|
||||
import org.luaj.TestPlatform;
|
||||
import org.luaj.debug.j2se.StandardLuaJVM.ParseException;
|
||||
import org.luaj.debug.net.j2se.DebugSupportImpl;
|
||||
import org.luaj.vm.DebugNetSupport;
|
||||
import org.luaj.vm.LuaState;
|
||||
import org.luaj.vm.Platform;
|
||||
|
||||
/**
|
||||
@@ -44,33 +37,8 @@ public class LuaJVMTest extends TestCase {
|
||||
protected void setUp() throws Exception {
|
||||
super.setUp();
|
||||
|
||||
System.setProperty(DebugLuaState.PROPERTY_LUAJ_DEBUG_PORT, "1999");
|
||||
|
||||
Platform.setInstance(new Platform() {
|
||||
public Reader createReader(InputStream inputStream) {
|
||||
return new InputStreamReader(inputStream);
|
||||
}
|
||||
|
||||
public InputStream openFile(String fileName) {
|
||||
return getClass().getResourceAsStream("/" + fileName);
|
||||
}
|
||||
|
||||
/**
|
||||
* Assumes J2SE platform, return the corresponding system property
|
||||
*/
|
||||
public String getProperty(String propertyName) {
|
||||
return System.getProperty(propertyName);
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides a J2SE DebugSupport instance.
|
||||
*/
|
||||
public DebugNetSupport getDebugSupport() throws IOException {
|
||||
int port = getDebugPort();
|
||||
DebugSupportImpl debugSupport = new DebugSupportImpl(port);
|
||||
return debugSupport;
|
||||
}
|
||||
});
|
||||
System.setProperty(Platform.PROPERTY_LUAJ_DEBUG_PORT, "1999");
|
||||
Platform.setInstance(new TestPlatform());
|
||||
}
|
||||
|
||||
public void testCommandLineParse() {
|
||||
@@ -244,10 +212,10 @@ public class LuaJVMTest extends TestCase {
|
||||
|
||||
public void testRun() {
|
||||
Properties props = System.getProperties();
|
||||
props.remove(LuaState.PROPERTY_LUAJ_DEBUG);
|
||||
props.remove(DebugLuaState.PROPERTY_LUAJ_DEBUG_HOST);
|
||||
props.remove(DebugLuaState.PROPERTY_LUAJ_DEBUG_PORT);
|
||||
props.remove(DebugLuaState.PROPERTY_LUAJ_DEBUG_SUSPEND_AT_START);
|
||||
props.remove(Platform.PROPERTY_LUAJ_DEBUG);
|
||||
props.remove(Platform.PROPERTY_LUAJ_DEBUG_HOST);
|
||||
props.remove(Platform.PROPERTY_LUAJ_DEBUG_PORT);
|
||||
props.remove(Platform.PROPERTY_LUAJ_DEBUG_SUSPEND_AT_START);
|
||||
System.setProperties(props);
|
||||
|
||||
String[] tests = new String[] { "autoload", "boolean", "calls",
|
||||
@@ -261,8 +229,8 @@ public class LuaJVMTest extends TestCase {
|
||||
|
||||
public void testDebugRun() {
|
||||
Properties props = System.getProperties();
|
||||
props.setProperty(LuaState.PROPERTY_LUAJ_DEBUG, "true");
|
||||
props.setProperty(DebugLuaState.PROPERTY_LUAJ_DEBUG_PORT, "1999");
|
||||
props.setProperty(Platform.PROPERTY_LUAJ_DEBUG, "true");
|
||||
props.setProperty(Platform.PROPERTY_LUAJ_DEBUG_PORT, "1999");
|
||||
System.setProperties(props);
|
||||
|
||||
String[] tests = new String[] { "boolean", "calls",
|
||||
|
||||
@@ -6,301 +6,302 @@ import java.io.OutputStream;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.luaj.TestPlatform;
|
||||
import org.luaj.compiler.LuaC;
|
||||
import org.luaj.debug.DebugLuaState;
|
||||
import org.luaj.lib.BaseLib;
|
||||
import org.luaj.lib.j2se.LuajavaLib;
|
||||
|
||||
|
||||
public class LuaJTest extends TestCase {
|
||||
|
||||
|
||||
public void testTest1() throws IOException, InterruptedException {
|
||||
runTest( "test1" );
|
||||
}
|
||||
protected void setUp() throws Exception {
|
||||
super.setUp();
|
||||
Platform.setInstance(new TestPlatform());
|
||||
}
|
||||
|
||||
public void testTest2() throws IOException, InterruptedException {
|
||||
runTest( "test2" );
|
||||
}
|
||||
public void testTest1() throws IOException, InterruptedException {
|
||||
runTest( "test1" );
|
||||
}
|
||||
|
||||
public void testTest3() throws IOException, InterruptedException {
|
||||
runTest( "test3" );
|
||||
}
|
||||
public void testTest2() throws IOException, InterruptedException {
|
||||
runTest( "test2" );
|
||||
}
|
||||
|
||||
public void testTest4() throws IOException, InterruptedException {
|
||||
runTest( "test4" );
|
||||
}
|
||||
public void testTest3() throws IOException, InterruptedException {
|
||||
runTest( "test3" );
|
||||
}
|
||||
|
||||
public void testTest5() throws IOException, InterruptedException {
|
||||
runTest( "test5" );
|
||||
}
|
||||
public void testTest4() throws IOException, InterruptedException {
|
||||
runTest( "test4" );
|
||||
}
|
||||
|
||||
public void testTest6() throws IOException, InterruptedException {
|
||||
runTest( "test6" );
|
||||
}
|
||||
public void testTest5() throws IOException, InterruptedException {
|
||||
runTest( "test5" );
|
||||
}
|
||||
|
||||
public void testTest7() throws IOException, InterruptedException {
|
||||
runTest( "test7" );
|
||||
}
|
||||
public void testTest6() throws IOException, InterruptedException {
|
||||
runTest( "test6" );
|
||||
}
|
||||
|
||||
public void testTest8() throws IOException, InterruptedException {
|
||||
runTest( "test8" );
|
||||
}
|
||||
public void testTest7() throws IOException, InterruptedException {
|
||||
runTest( "test7" );
|
||||
}
|
||||
|
||||
public void testArgtypes() throws IOException, InterruptedException {
|
||||
runTest( "argtypes" );
|
||||
}
|
||||
public void testTest8() throws IOException, InterruptedException {
|
||||
runTest( "test8" );
|
||||
}
|
||||
|
||||
public void testAutoload() throws IOException, InterruptedException {
|
||||
runTest( "autoload" );
|
||||
}
|
||||
public void testArgtypes() throws IOException, InterruptedException {
|
||||
runTest( "argtypes" );
|
||||
}
|
||||
|
||||
public void testBaseLib() throws IOException, InterruptedException {
|
||||
runTest( "baselib" );
|
||||
}
|
||||
|
||||
public void testBoolean() throws IOException, InterruptedException {
|
||||
runTest( "boolean" );
|
||||
}
|
||||
public void testAutoload() throws IOException, InterruptedException {
|
||||
runTest( "autoload" );
|
||||
}
|
||||
|
||||
public void testCalls() throws IOException, InterruptedException {
|
||||
runTest( "calls" );
|
||||
}
|
||||
public void testBaseLib() throws IOException, InterruptedException {
|
||||
runTest( "baselib" );
|
||||
}
|
||||
|
||||
public void testBoolean() throws IOException, InterruptedException {
|
||||
runTest( "boolean" );
|
||||
}
|
||||
|
||||
public void testCoercions() throws IOException, InterruptedException {
|
||||
runTest( "coercions" );
|
||||
}
|
||||
|
||||
public void testCoroutines() throws IOException, InterruptedException {
|
||||
runTest( "coroutines" );
|
||||
}
|
||||
|
||||
public void testCompare() throws IOException, InterruptedException {
|
||||
runTest( "compare" );
|
||||
}
|
||||
public void testCalls() throws IOException, InterruptedException {
|
||||
runTest( "calls" );
|
||||
}
|
||||
|
||||
public void testErrors() throws IOException, InterruptedException {
|
||||
runTest( "errors" );
|
||||
}
|
||||
public void testCoercions() throws IOException, InterruptedException {
|
||||
runTest( "coercions" );
|
||||
}
|
||||
|
||||
public void testCoroutines() throws IOException, InterruptedException {
|
||||
runTest( "coroutines" );
|
||||
}
|
||||
|
||||
public void testCompare() throws IOException, InterruptedException {
|
||||
runTest( "compare" );
|
||||
}
|
||||
|
||||
public void testLoops() throws IOException, InterruptedException {
|
||||
runTest( "loops" );
|
||||
}
|
||||
public void testErrors() throws IOException, InterruptedException {
|
||||
runTest( "errors" );
|
||||
}
|
||||
|
||||
public void testMathLib() throws IOException, InterruptedException {
|
||||
runTest( "mathlib" );
|
||||
}
|
||||
public void testLoops() throws IOException, InterruptedException {
|
||||
runTest( "loops" );
|
||||
}
|
||||
|
||||
public void testMetatables() throws IOException, InterruptedException {
|
||||
runTest( "metatables" );
|
||||
}
|
||||
public void testMathLib() throws IOException, InterruptedException {
|
||||
runTest( "mathlib" );
|
||||
}
|
||||
|
||||
public void testModule() throws IOException, InterruptedException {
|
||||
runTest( "module" );
|
||||
}
|
||||
public void testMetatables() throws IOException, InterruptedException {
|
||||
runTest( "metatables" );
|
||||
}
|
||||
|
||||
public void testNext() throws IOException, InterruptedException {
|
||||
runTest( "next" );
|
||||
}
|
||||
public void testModule() throws IOException, InterruptedException {
|
||||
runTest( "module" );
|
||||
}
|
||||
|
||||
public void testPcalls() throws IOException, InterruptedException {
|
||||
runTest( "pcalls" );
|
||||
}
|
||||
|
||||
public void testRequire() throws IOException, InterruptedException {
|
||||
runTest( "require" );
|
||||
}
|
||||
|
||||
public void testSelect() throws IOException, InterruptedException {
|
||||
runTest( "select" );
|
||||
}
|
||||
public void testNext() throws IOException, InterruptedException {
|
||||
runTest( "next" );
|
||||
}
|
||||
|
||||
public void testSetfenv() throws IOException, InterruptedException {
|
||||
runTest( "setfenv" );
|
||||
}
|
||||
public void testPcalls() throws IOException, InterruptedException {
|
||||
runTest( "pcalls" );
|
||||
}
|
||||
|
||||
public void testRequire() throws IOException, InterruptedException {
|
||||
runTest( "require" );
|
||||
}
|
||||
|
||||
public void testSelect() throws IOException, InterruptedException {
|
||||
runTest( "select" );
|
||||
}
|
||||
|
||||
public void testSetlist() throws IOException, InterruptedException {
|
||||
runTest( "setlist" );
|
||||
}
|
||||
|
||||
public void testSimpleMetatables() throws IOException, InterruptedException {
|
||||
runTest( "simplemetatables" );
|
||||
}
|
||||
|
||||
public void testStrLib() throws IOException, InterruptedException {
|
||||
runTest( "strlib" );
|
||||
}
|
||||
|
||||
public void testTable() throws IOException, InterruptedException {
|
||||
runTest( "table" );
|
||||
}
|
||||
public void testSetfenv() throws IOException, InterruptedException {
|
||||
runTest( "setfenv" );
|
||||
}
|
||||
|
||||
public void testType() throws IOException, InterruptedException {
|
||||
runTest( "type" );
|
||||
}
|
||||
|
||||
public void testUpvalues() throws IOException, InterruptedException {
|
||||
runTest( "upvalues" );
|
||||
}
|
||||
|
||||
public void testUpvalues2() throws IOException, InterruptedException {
|
||||
runTest( "upvalues2" );
|
||||
}
|
||||
public void testSetlist() throws IOException, InterruptedException {
|
||||
runTest( "setlist" );
|
||||
}
|
||||
|
||||
public void testSimpleMetatables() throws IOException, InterruptedException {
|
||||
runTest( "simplemetatables" );
|
||||
}
|
||||
|
||||
public void testStrLib() throws IOException, InterruptedException {
|
||||
runTest( "strlib" );
|
||||
}
|
||||
|
||||
public void testTable() throws IOException, InterruptedException {
|
||||
runTest( "table" );
|
||||
}
|
||||
|
||||
public void testType() throws IOException, InterruptedException {
|
||||
runTest( "type" );
|
||||
}
|
||||
|
||||
public void testUpvalues() throws IOException, InterruptedException {
|
||||
runTest( "upvalues" );
|
||||
}
|
||||
|
||||
public void testUpvalues2() throws IOException, InterruptedException {
|
||||
runTest( "upvalues2" );
|
||||
}
|
||||
//*/
|
||||
private void runTest( String testName ) throws IOException, InterruptedException {
|
||||
private void runTest( String testName ) throws IOException, InterruptedException {
|
||||
|
||||
// new lua state
|
||||
LuaState state = LuaState.newState();
|
||||
|
||||
// add standard bindings
|
||||
state.installStandardLibs();
|
||||
|
||||
// add luajava
|
||||
LuajavaLib.install( state._G );
|
||||
LuaC.install();
|
||||
|
||||
// load the file
|
||||
LPrototype p = loadScriptResource( state, testName );
|
||||
|
||||
// Replace System.out with a ByteArrayOutputStream
|
||||
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
||||
BaseLib.redirectOutput( outputStream );
|
||||
try {
|
||||
// create closure and execute
|
||||
LClosure c = new LClosure( p, state._G );
|
||||
state.doCall(c, new LValue[0]);
|
||||
|
||||
final String actualOutput = new String( outputStream.toByteArray() );
|
||||
final String expectedOutput = getExpectedOutput( testName );
|
||||
|
||||
assertEquals( expectedOutput, actualOutput );
|
||||
} finally {
|
||||
BaseLib.restoreStandardOutput();
|
||||
outputStream.close();
|
||||
}
|
||||
}
|
||||
|
||||
private LPrototype loadScriptResource( LuaState state, String name ) throws IOException {
|
||||
InputStream script = getClass().getResourceAsStream( "/"+name+".luac" );
|
||||
if ( script == null ) {
|
||||
script = getClass().getResourceAsStream( "/"+name+".lua" );
|
||||
if ( script == null ) {
|
||||
fail( "Could not load script for test case: "+name );
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
return LoadState.undump(state, script, name);
|
||||
} finally {
|
||||
script.close();
|
||||
}
|
||||
}
|
||||
|
||||
private String getExpectedOutput( final String testName ) throws IOException, InterruptedException {
|
||||
String expectedOutputName = "/" + testName + "-expected.out";
|
||||
InputStream is = getClass().getResourceAsStream( expectedOutputName );
|
||||
if ( is != null ) {
|
||||
try {
|
||||
return readString( is );
|
||||
} finally {
|
||||
is.close();
|
||||
}
|
||||
} else {
|
||||
InputStream script;
|
||||
// script = getClass().getResourceAsStream( "/" + testName + ".luac" );
|
||||
// if ( script == null ) {
|
||||
script = getClass().getResourceAsStream( "/" + testName + ".lua" );
|
||||
if ( script == null ) {
|
||||
fail( "Could not find script for test case: "+testName );
|
||||
}
|
||||
// }
|
||||
try {
|
||||
return collectProcessOutput( new String[] { "lua", "-" }, script );
|
||||
} finally {
|
||||
script.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private String collectProcessOutput( String[] cmd, final InputStream input ) throws IOException, InterruptedException {
|
||||
Runtime r = Runtime.getRuntime();
|
||||
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
final Process p = r.exec( cmd );
|
||||
try {
|
||||
// start a thread to write the given input to the subprocess.
|
||||
Thread inputCopier = (new Thread() {
|
||||
public void run() {
|
||||
try {
|
||||
OutputStream processStdIn = p.getOutputStream();
|
||||
try {
|
||||
copy( input, processStdIn );
|
||||
} finally {
|
||||
processStdIn.close();
|
||||
}
|
||||
} catch ( IOException e ) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
});
|
||||
inputCopier.start();
|
||||
|
||||
// start another thread to read output from the subprocess.
|
||||
Thread outputCopier = (new Thread() {
|
||||
public void run() {
|
||||
try {
|
||||
InputStream processStdOut = p.getInputStream();
|
||||
try {
|
||||
copy( processStdOut, baos );
|
||||
} finally {
|
||||
processStdOut.close();
|
||||
}
|
||||
} catch ( IOException ioe ) {
|
||||
ioe.printStackTrace();
|
||||
}
|
||||
}
|
||||
});
|
||||
outputCopier.start();
|
||||
|
||||
// start another thread to read output from the subprocess.
|
||||
Thread errorCopier = (new Thread() {
|
||||
public void run() {
|
||||
try {
|
||||
InputStream processError = p.getErrorStream();
|
||||
try {
|
||||
copy( processError, System.err );
|
||||
} finally {
|
||||
processError.close();
|
||||
}
|
||||
} catch ( IOException ioe ) {
|
||||
ioe.printStackTrace();
|
||||
}
|
||||
}
|
||||
});
|
||||
errorCopier.start();
|
||||
|
||||
p.waitFor();
|
||||
inputCopier.join();
|
||||
outputCopier.join();
|
||||
errorCopier.join();
|
||||
|
||||
return new String( baos.toByteArray() );
|
||||
|
||||
} finally {
|
||||
p.destroy();
|
||||
}
|
||||
}
|
||||
|
||||
private String readString( InputStream is ) throws IOException {
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
copy( is, baos );
|
||||
return new String( baos.toByteArray() );
|
||||
}
|
||||
|
||||
private void copy( InputStream is, OutputStream os ) throws IOException {
|
||||
byte[] buf = new byte[ 1024 ];
|
||||
int r;
|
||||
while ( ( r = is.read( buf ) ) >= 0 ) {
|
||||
os.write( buf, 0, r );
|
||||
}
|
||||
}
|
||||
// new lua state
|
||||
LuaState state = Platform.newLuaState();
|
||||
|
||||
// install the compiler
|
||||
LuaC.install();
|
||||
|
||||
// load the file
|
||||
LPrototype p = loadScriptResource( state, testName );
|
||||
|
||||
// Replace System.out with a ByteArrayOutputStream
|
||||
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
||||
BaseLib.redirectOutput( outputStream );
|
||||
try {
|
||||
// create closure and execute
|
||||
LClosure c = new LClosure( p, state._G );
|
||||
state.doCall(c, new LValue[0]);
|
||||
|
||||
final String actualOutput = new String( outputStream.toByteArray() );
|
||||
final String expectedOutput = getExpectedOutput( testName );
|
||||
|
||||
assertEquals( expectedOutput, actualOutput );
|
||||
} finally {
|
||||
BaseLib.restoreStandardOutput();
|
||||
outputStream.close();
|
||||
}
|
||||
}
|
||||
|
||||
private LPrototype loadScriptResource( LuaState state, String name ) throws IOException {
|
||||
InputStream script = getClass().getResourceAsStream( "/"+name+".luac" );
|
||||
if ( script == null ) {
|
||||
script = getClass().getResourceAsStream( "/"+name+".lua" );
|
||||
if ( script == null ) {
|
||||
fail( "Could not load script for test case: "+name );
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
return LoadState.undump(state, script, name);
|
||||
} finally {
|
||||
script.close();
|
||||
}
|
||||
}
|
||||
|
||||
private String getExpectedOutput( final String testName ) throws IOException, InterruptedException {
|
||||
String expectedOutputName = "/" + testName + "-expected.out";
|
||||
InputStream is = getClass().getResourceAsStream( expectedOutputName );
|
||||
if ( is != null ) {
|
||||
try {
|
||||
return readString( is );
|
||||
} finally {
|
||||
is.close();
|
||||
}
|
||||
} else {
|
||||
InputStream script;
|
||||
// script = getClass().getResourceAsStream( "/" + testName + ".luac" );
|
||||
// if ( script == null ) {
|
||||
script = getClass().getResourceAsStream( "/" + testName + ".lua" );
|
||||
if ( script == null ) {
|
||||
fail( "Could not find script for test case: "+testName );
|
||||
}
|
||||
// }
|
||||
try {
|
||||
return collectProcessOutput( new String[] { "lua", "-" }, script );
|
||||
} finally {
|
||||
script.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private String collectProcessOutput( String[] cmd, final InputStream input ) throws IOException, InterruptedException {
|
||||
Runtime r = Runtime.getRuntime();
|
||||
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
final Process p = r.exec( cmd );
|
||||
try {
|
||||
// start a thread to write the given input to the subprocess.
|
||||
Thread inputCopier = (new Thread() {
|
||||
public void run() {
|
||||
try {
|
||||
OutputStream processStdIn = p.getOutputStream();
|
||||
try {
|
||||
copy( input, processStdIn );
|
||||
} finally {
|
||||
processStdIn.close();
|
||||
}
|
||||
} catch ( IOException e ) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
});
|
||||
inputCopier.start();
|
||||
|
||||
// start another thread to read output from the subprocess.
|
||||
Thread outputCopier = (new Thread() {
|
||||
public void run() {
|
||||
try {
|
||||
InputStream processStdOut = p.getInputStream();
|
||||
try {
|
||||
copy( processStdOut, baos );
|
||||
} finally {
|
||||
processStdOut.close();
|
||||
}
|
||||
} catch ( IOException ioe ) {
|
||||
ioe.printStackTrace();
|
||||
}
|
||||
}
|
||||
});
|
||||
outputCopier.start();
|
||||
|
||||
// start another thread to read output from the subprocess.
|
||||
Thread errorCopier = (new Thread() {
|
||||
public void run() {
|
||||
try {
|
||||
InputStream processError = p.getErrorStream();
|
||||
try {
|
||||
copy( processError, System.err );
|
||||
} finally {
|
||||
processError.close();
|
||||
}
|
||||
} catch ( IOException ioe ) {
|
||||
ioe.printStackTrace();
|
||||
}
|
||||
}
|
||||
});
|
||||
errorCopier.start();
|
||||
|
||||
p.waitFor();
|
||||
inputCopier.join();
|
||||
outputCopier.join();
|
||||
errorCopier.join();
|
||||
|
||||
return new String( baos.toByteArray() );
|
||||
|
||||
} finally {
|
||||
p.destroy();
|
||||
}
|
||||
}
|
||||
|
||||
private String readString( InputStream is ) throws IOException {
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
copy( is, baos );
|
||||
return new String( baos.toByteArray() );
|
||||
}
|
||||
|
||||
private void copy( InputStream is, OutputStream os ) throws IOException {
|
||||
byte[] buf = new byte[ 1024 ];
|
||||
int r;
|
||||
while ( ( r = is.read( buf ) ) >= 0 ) {
|
||||
os.write( buf, 0, r );
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -13,7 +13,7 @@ import junit.framework.Test;
|
||||
import junit.framework.TestCase;
|
||||
import junit.framework.TestSuite;
|
||||
|
||||
import org.luaj.debug.DebugLuaState;
|
||||
import org.luaj.TestPlatform;
|
||||
import org.luaj.lib.BaseLib;
|
||||
|
||||
public class StandardTest extends TestCase {
|
||||
@@ -65,10 +65,8 @@ public class StandardTest extends TestCase {
|
||||
}
|
||||
|
||||
public void runTest() {
|
||||
LuaState state = new DebugLuaState();
|
||||
|
||||
// add standard bindings
|
||||
state.installStandardLibs();
|
||||
Platform.setInstance(new TestPlatform());
|
||||
LuaState state = Platform.newLuaState();
|
||||
|
||||
// hack: it's unpleasant when the test cases fail to terminate;
|
||||
// unfortunately, there is a test in the standard suite that
|
||||
|
||||
Reference in New Issue
Block a user