Refactor loading of scripts during testing, remove use of "arg" from error test script.
This commit is contained in:
@@ -33,7 +33,7 @@ import org.luaj.vm2.luajc.LuaJC;
|
|||||||
*/
|
*/
|
||||||
public class CompatibiltyTest extends TestSuite {
|
public class CompatibiltyTest extends TestSuite {
|
||||||
|
|
||||||
private static final String dir = "test/lua";
|
private static final String dir = "";
|
||||||
|
|
||||||
abstract protected static class CompatibiltyTestSuite extends ScriptDrivenTest {
|
abstract protected static class CompatibiltyTestSuite extends ScriptDrivenTest {
|
||||||
LuaValue savedStringMetatable;
|
LuaValue savedStringMetatable;
|
||||||
|
|||||||
@@ -36,7 +36,7 @@ import org.luaj.vm2.lib.BaseLib;
|
|||||||
*/
|
*/
|
||||||
public class ErrorsTest extends ScriptDrivenTest {
|
public class ErrorsTest extends ScriptDrivenTest {
|
||||||
|
|
||||||
private static final String dir = "test/lua/errors";
|
private static final String dir = "errors/";
|
||||||
|
|
||||||
public ErrorsTest() {
|
public ErrorsTest() {
|
||||||
super(ScriptDrivenTest.PlatformType.JSE, dir);
|
super(ScriptDrivenTest.PlatformType.JSE, dir);
|
||||||
|
|||||||
@@ -34,10 +34,11 @@ import java.net.URL;
|
|||||||
import junit.framework.TestCase;
|
import junit.framework.TestCase;
|
||||||
|
|
||||||
import org.luaj.vm2.lib.BaseLib;
|
import org.luaj.vm2.lib.BaseLib;
|
||||||
|
import org.luaj.vm2.lib.ResourceFinder;
|
||||||
import org.luaj.vm2.luajc.LuaJC;
|
import org.luaj.vm2.luajc.LuaJC;
|
||||||
|
|
||||||
abstract
|
abstract
|
||||||
public class ScriptDrivenTest extends TestCase {
|
public class ScriptDrivenTest extends TestCase implements ResourceFinder {
|
||||||
public static final boolean nocompile = "true".equals(System.getProperty("nocompile"));
|
public static final boolean nocompile = "true".equals(System.getProperty("nocompile"));
|
||||||
|
|
||||||
public enum PlatformType {
|
public enum PlatformType {
|
||||||
@@ -45,12 +46,15 @@ public class ScriptDrivenTest extends TestCase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private final PlatformType platform;
|
private final PlatformType platform;
|
||||||
private final String basedir;
|
private final String subdir;
|
||||||
private LuaTable _G;
|
private LuaTable _G;
|
||||||
|
|
||||||
protected ScriptDrivenTest( PlatformType platform, String directory ) {
|
static final String zipdir = "test/lua/";
|
||||||
|
static final String zipfile = "luaj3.0-tests.zip";
|
||||||
|
|
||||||
|
protected ScriptDrivenTest( PlatformType platform, String subdir ) {
|
||||||
this.platform = platform;
|
this.platform = platform;
|
||||||
this.basedir = directory;
|
this.subdir = subdir;
|
||||||
initGlobals();
|
initGlobals();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -71,8 +75,73 @@ public class ScriptDrivenTest extends TestCase {
|
|||||||
protected void setUp() throws Exception {
|
protected void setUp() throws Exception {
|
||||||
super.setUp();
|
super.setUp();
|
||||||
initGlobals();
|
initGlobals();
|
||||||
|
BaseLib.FINDER = this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ResourceFinder implementation.
|
||||||
|
public InputStream findResource(String filename) {
|
||||||
|
InputStream is = findInPlainFile(filename);
|
||||||
|
if (is != null) return is;
|
||||||
|
is = findInPlainFileAsResource("",filename);
|
||||||
|
if (is != null) return is;
|
||||||
|
is = findInPlainFileAsResource("/",filename);
|
||||||
|
if (is != null) return is;
|
||||||
|
is = findInZipFileAsPlainFile(filename);
|
||||||
|
if (is != null) return is;
|
||||||
|
is = findInZipFileAsResource("",filename);
|
||||||
|
if (is != null) return is;
|
||||||
|
is = findInZipFileAsResource("/",filename);
|
||||||
|
return is;
|
||||||
|
}
|
||||||
|
|
||||||
|
private InputStream findInPlainFileAsResource(String prefix, String filename) {
|
||||||
|
return getClass().getResourceAsStream(prefix + subdir + filename);
|
||||||
|
}
|
||||||
|
|
||||||
|
private InputStream findInPlainFile(String filename) {
|
||||||
|
try {
|
||||||
|
File f = new File(zipdir+subdir+filename);
|
||||||
|
if (f.exists())
|
||||||
|
return new FileInputStream(f);
|
||||||
|
} catch ( IOException ioe ) {
|
||||||
|
ioe.printStackTrace();
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private InputStream findInZipFileAsPlainFile(String filename) {
|
||||||
|
URL zip;
|
||||||
|
File file = new File(zipdir+zipfile);
|
||||||
|
try {
|
||||||
|
if ( file.exists() ) {
|
||||||
|
zip = file.toURI().toURL();
|
||||||
|
String path = "jar:"+zip.toExternalForm()+ "!/"+subdir+filename;
|
||||||
|
URL url = new URL(path);
|
||||||
|
return url.openStream();
|
||||||
|
}
|
||||||
|
} catch (MalformedURLException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
} catch (IOException ioe) {
|
||||||
|
ioe.printStackTrace();
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private InputStream findInZipFileAsResource(String prefix, String filename) {
|
||||||
|
URL zip = null;
|
||||||
|
zip = getClass().getResource(zipfile);
|
||||||
|
if ( zip != null )
|
||||||
|
try {
|
||||||
|
String path = "jar:"+zip.toExternalForm()+ "!/"+subdir+filename;
|
||||||
|
URL url = new URL(path);
|
||||||
|
return url.openStream();
|
||||||
|
} catch (IOException ioe) {
|
||||||
|
ioe.printStackTrace();
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
// */
|
// */
|
||||||
protected void runTest(String testName) {
|
protected void runTest(String testName) {
|
||||||
try {
|
try {
|
||||||
@@ -106,11 +175,9 @@ public class ScriptDrivenTest extends TestCase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected LuaValue loadScript(String name, LuaTable _G) throws IOException {
|
protected LuaValue loadScript(String name, LuaTable _G) throws IOException {
|
||||||
File file = new File(basedir+"/"+name+".lua");
|
InputStream script = this.findResource(name+".lua");
|
||||||
if ( !file.exists() )
|
if ( script == null )
|
||||||
fail("Could not load script for test case: " + name);
|
fail("Could not load script for test case: " + name);
|
||||||
|
|
||||||
InputStream script=null;
|
|
||||||
try {
|
try {
|
||||||
// Use "stdin" instead of resource name so that output matches
|
// Use "stdin" instead of resource name so that output matches
|
||||||
// standard Lua.
|
// standard Lua.
|
||||||
@@ -120,84 +187,38 @@ public class ScriptDrivenTest extends TestCase {
|
|||||||
LuaValue c = (LuaValue) Class.forName(name).newInstance();
|
LuaValue c = (LuaValue) Class.forName(name).newInstance();
|
||||||
return c;
|
return c;
|
||||||
} else {
|
} else {
|
||||||
script = new FileInputStream(file);
|
|
||||||
return LuaJC.getInstance().load( script, name, _G);
|
return LuaJC.getInstance().load( script, name, _G);
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
script = new FileInputStream(file);
|
|
||||||
return LoadState.load(script, "=stdin", "bt", _G);
|
return LoadState.load(script, "=stdin", "bt", _G);
|
||||||
}
|
}
|
||||||
} catch ( Exception e ) {
|
} catch ( Exception e ) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
throw new IOException( e.toString() );
|
throw new IOException( e.toString() );
|
||||||
} finally {
|
} finally {
|
||||||
if ( script != null )
|
script.close();
|
||||||
script.close();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private String getExpectedOutput(final String name) throws IOException,
|
private String getExpectedOutput(final String name) throws IOException,
|
||||||
InterruptedException {
|
InterruptedException {
|
||||||
String expectedOutput = loadFromFile(name);
|
InputStream output = this.findResource(name+".out");
|
||||||
|
if (output != null)
|
||||||
|
try {
|
||||||
|
return readString(output);
|
||||||
|
} finally {
|
||||||
|
output.close();
|
||||||
|
}
|
||||||
|
String expectedOutput = executeLuaProcess(name);
|
||||||
if (expectedOutput == null)
|
if (expectedOutput == null)
|
||||||
expectedOutput = loadFromZipfile(name);
|
throw new IOException("Failed to get comparison output or run process for "+name);
|
||||||
if (expectedOutput == null)
|
|
||||||
expectedOutput = executeLuaProcess(name);
|
|
||||||
if (expectedOutput == null)
|
|
||||||
throw new IOException("Failed to get comparison output for "+name);
|
|
||||||
return expectedOutput;
|
return expectedOutput;
|
||||||
}
|
}
|
||||||
|
|
||||||
private String loadFromFile(String name) throws IOException {
|
|
||||||
String expectedOutputName = basedir+"/"+name+"-expected.out";
|
|
||||||
File file = new File( expectedOutputName );
|
|
||||||
if ( file.exists() ) {
|
|
||||||
InputStream is = new FileInputStream(file);
|
|
||||||
try {
|
|
||||||
return readString(is);
|
|
||||||
} finally {
|
|
||||||
is.close();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
private String loadFromZipfile(String name) throws IOException {
|
|
||||||
String zipfile = "luaj3.0-tests.zip";
|
|
||||||
try {
|
|
||||||
URL zip = null;
|
|
||||||
zip = getClass().getResource(zipfile);
|
|
||||||
if ( zip == null ) {
|
|
||||||
File file = new File(basedir+"/"+zipfile);
|
|
||||||
try {
|
|
||||||
if ( file.exists() )
|
|
||||||
zip = file.toURI().toURL();
|
|
||||||
} catch (MalformedURLException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if ( zip == null )
|
|
||||||
return null;
|
|
||||||
String jar = "jar:" + zip.toExternalForm()+ "!/";
|
|
||||||
String path = jar + name + ".out";
|
|
||||||
URL url = new URL(path);
|
|
||||||
InputStream is = url.openStream();
|
|
||||||
try {
|
|
||||||
return readString(is);
|
|
||||||
} finally {
|
|
||||||
is.close();
|
|
||||||
}
|
|
||||||
} catch (Exception e) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
private String executeLuaProcess(String name) throws IOException, InterruptedException {
|
private String executeLuaProcess(String name) throws IOException, InterruptedException {
|
||||||
File sourcefile = new File(basedir+"/"+name+".lua");
|
InputStream script = findResource(name+".lua");
|
||||||
if ( !sourcefile.exists() )
|
if ( script == null )
|
||||||
throw new IOException("Failed to find source file "+sourcefile);
|
throw new IOException("Failed to find source file "+script);
|
||||||
InputStream script = new FileInputStream(sourcefile);
|
|
||||||
try {
|
try {
|
||||||
String luaCommand = System.getProperty("LUA_COMMAND");
|
String luaCommand = System.getProperty("LUA_COMMAND");
|
||||||
if ( luaCommand == null )
|
if ( luaCommand == null )
|
||||||
|
|||||||
@@ -114,6 +114,7 @@ local function split(t)
|
|||||||
end
|
end
|
||||||
|
|
||||||
local function expand(argsets, typesets, ...)
|
local function expand(argsets, typesets, ...)
|
||||||
|
local arg = {...} ; arg.n = #arg
|
||||||
local n = typesets and #typesets or 0
|
local n = typesets and #typesets or 0
|
||||||
if n <= 0 then
|
if n <= 0 then
|
||||||
table.insert(argsets,arg)
|
table.insert(argsets,arg)
|
||||||
@@ -155,7 +156,7 @@ local function subbanner(name)
|
|||||||
end
|
end
|
||||||
|
|
||||||
local function pack(s,...)
|
local function pack(s,...)
|
||||||
return s,arg
|
return s,{...}
|
||||||
end
|
end
|
||||||
|
|
||||||
-- check that all combinations of arguments pass
|
-- check that all combinations of arguments pass
|
||||||
|
|||||||
Reference in New Issue
Block a user