Skip to content

Commit

Permalink
Cleanup Tests with JUnit5 and move to different modules
Browse files Browse the repository at this point in the history
  • Loading branch information
farmboy0 committed Jul 11, 2021
1 parent 1a6de4a commit 9792fcb
Show file tree
Hide file tree
Showing 64 changed files with 2,357 additions and 1,808 deletions.
8 changes: 8 additions & 0 deletions luaj-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,12 @@
<name>luaj-core</name>
<description>Core code for LuaJ</description>

<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

</project>
Empty file removed luaj-core/src/test/java/.keep
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -21,40 +21,38 @@
******************************************************************************/
package org.luaj.vm2;

import java.io.ByteArrayInputStream;
import static org.junit.jupiter.api.Assertions.assertEquals;

import junit.framework.TestCase;
import java.io.ByteArrayInputStream;

import org.junit.jupiter.api.Test;
import org.luaj.vm2.Globals.BufferedStream;

public class BufferedStreamTest extends TestCase {

public BufferedStreamTest() {}
class BufferedStreamTest {

private BufferedStream NewBufferedStream(int buflen, String contents) {
return new BufferedStream(buflen, new ByteArrayInputStream(contents.getBytes()));
}

protected void setUp() throws Exception {
super.setUp();
}

public void testReadEmptyStream() throws java.io.IOException {
@Test
void testReadEmptyStream() throws java.io.IOException {
BufferedStream bs = NewBufferedStream(4, "");
assertEquals(-1, bs.read());
assertEquals(-1, bs.read(new byte[10]));
assertEquals(-1, bs.read(new byte[10], 0, 10));
}

public void testReadByte() throws java.io.IOException {
@Test
void testReadByte() throws java.io.IOException {
BufferedStream bs = NewBufferedStream(2, "abc");
assertEquals('a', bs.read());
assertEquals('b', bs.read());
assertEquals('c', bs.read());
assertEquals(-1, bs.read());
}

public void testReadByteArray() throws java.io.IOException {
@Test
void testReadByteArray() throws java.io.IOException {
byte[] array = new byte[3];
BufferedStream bs = NewBufferedStream(4, "abcdef");
assertEquals(3, bs.read(array));
Expand All @@ -66,7 +64,8 @@ public void testReadByteArray() throws java.io.IOException {
assertEquals(-1, bs.read());
}

public void testReadByteArrayOffsetLength() throws java.io.IOException {
@Test
void testReadByteArrayOffsetLength() throws java.io.IOException {
byte[] array = new byte[10];
BufferedStream bs = NewBufferedStream(8, "abcdefghijklmn");
assertEquals(4, bs.read(array, 0, 4));
Expand All @@ -78,7 +77,8 @@ public void testReadByteArrayOffsetLength() throws java.io.IOException {
assertEquals(-1, bs.read());
}

public void testMarkOffsetBeginningOfStream() throws java.io.IOException {
@Test
void testMarkOffsetBeginningOfStream() throws java.io.IOException {
byte[] array = new byte[4];
BufferedStream bs = NewBufferedStream(8, "abcdefghijkl");
assertEquals(true, bs.markSupported());
Expand All @@ -95,7 +95,8 @@ public void testMarkOffsetBeginningOfStream() throws java.io.IOException {
assertEquals(-1, bs.read());
}

public void testMarkOffsetMiddleOfStream() throws java.io.IOException {
@Test
void testMarkOffsetMiddleOfStream() throws java.io.IOException {
byte[] array = new byte[4];
BufferedStream bs = NewBufferedStream(8, "abcdefghijkl");
assertEquals(true, bs.markSupported());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,16 @@
******************************************************************************/
package org.luaj.vm2;

import java.io.Reader;
import java.io.StringReader;
import java.lang.reflect.InvocationTargetException;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.fail;

import junit.framework.TestCase;
import java.lang.reflect.InvocationTargetException;

import org.junit.jupiter.api.Test;
import org.luaj.vm2.TypeTest.MyData;
import org.luaj.vm2.compiler.LuaC;
import org.luaj.vm2.lib.ZeroArgFunction;

public class LuaOperationsTest extends TestCase {
class LuaOperationsTest {

private final int sampleint = 77;
private final long samplelong = 123400000000L;
Expand All @@ -57,6 +56,7 @@ public class LuaOperationsTest extends TestCase {
private final LuaTable table = LuaValue
.listOf(new LuaValue[] { LuaValue.valueOf("aaa"), LuaValue.valueOf("bbb") });
private final LuaValue somefunc = new ZeroArgFunction() {
@Override
public LuaValue call() { return NONE; }
};
private final LuaThread thread = new LuaThread(new Globals(), somefunc);
Expand Down Expand Up @@ -91,7 +91,8 @@ private void throwsLuaError(String methodName, Object obj, Object arg) {
}
}

public void testLen() {
@Test
void testLen() {
throwsLuaError("len", somenil);
throwsLuaError("len", sometrue);
throwsLuaError("len", somefalse);
Expand All @@ -111,7 +112,8 @@ public void testLen() {
throwsLuaError("len", userdatacls);
}

public void testLength() {
@Test
void testLength() {
throwsLuaError("length", somenil);
throwsLuaError("length", sometrue);
throwsLuaError("length", somefalse);
Expand All @@ -130,51 +132,4 @@ public void testLength() {
throwsLuaError("length", userdataobj);
throwsLuaError("length", userdatacls);
}

public Prototype createPrototype(String script, String name) {
try {
Globals globals = org.luaj.vm2.lib.jse.JsePlatform.standardGlobals();
Reader reader = new StringReader(script);
return globals.compilePrototype(reader, name);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
fail(e.toString());
return null;
}
}

public void testFunctionClosureThreadEnv() {

// set up suitable environments for execution
LuaValue aaa = LuaValue.valueOf("aaa");
LuaValue eee = LuaValue.valueOf("eee");
final Globals globals = org.luaj.vm2.lib.jse.JsePlatform.standardGlobals();
LuaTable newenv = LuaValue.tableOf(new LuaValue[] { LuaValue.valueOf("a"), LuaValue.valueOf("aaa"),
LuaValue.valueOf("b"), LuaValue.valueOf("bbb"), });
LuaTable mt = LuaValue.tableOf(new LuaValue[] { LuaValue.INDEX, globals });
newenv.setmetatable(mt);
globals.set("a", aaa);
newenv.set("a", eee);

// function tests
{
LuaFunction f = new ZeroArgFunction() {
public LuaValue call() { return globals.get("a"); }
};
assertEquals(aaa, f.call());
}

// closure tests
{
Prototype p = createPrototype("return a\n", "closuretester");
LuaClosure c = new LuaClosure(p, globals);

// Test that a clusure with a custom enviroment uses that environment.
assertEquals(aaa, c.call());
c = new LuaClosure(p, newenv);
assertEquals(newenv, c.upValues[0].getValue());
assertEquals(eee, c.call());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,18 @@
******************************************************************************/
package org.luaj.vm2;

import junit.framework.TestCase;
import static org.junit.jupiter.api.Assertions.assertEquals;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.luaj.vm2.TypeTest.MyData;
import org.luaj.vm2.lib.StringLib;
import org.luaj.vm2.lib.ThreeArgFunction;
import org.luaj.vm2.lib.TwoArgFunction;
import org.luaj.vm2.lib.ZeroArgFunction;

public class MetatableTest extends TestCase {
class MetatableTest {

private final String samplestring = "abcdef";
private final Object sampleobject = new Object();
Expand All @@ -38,20 +41,22 @@ public class MetatableTest extends TestCase {
private final LuaValue string = LuaValue.valueOf(samplestring);
private final LuaTable table = LuaValue.tableOf();
private final LuaFunction function = new ZeroArgFunction() {
@Override
public LuaValue call() { return NONE; }
};
private final LuaThread thread = new LuaThread(new Globals(), function);
private final LuaClosure closure = new LuaClosure(new Prototype(), new LuaTable());
private final LuaUserdata userdata = LuaValue.userdataOf(sampleobject);
private final LuaUserdata userdatamt = LuaValue.userdataOf(sampledata, table);

@BeforeEach
protected void setUp() throws Exception {
// needed for metatable ops to work on strings
new StringLib();
}

@AfterEach
protected void tearDown() throws Exception {
super.tearDown();
LuaBoolean.s_metatable = null;
LuaFunction.s_metatable = null;
LuaNil.s_metatable = null;
Expand All @@ -60,7 +65,8 @@ protected void tearDown() throws Exception {
LuaThread.s_metatable = null;
}

public void testGetMetatable() {
@Test
void testGetMetatable() {
assertEquals(null, LuaValue.NIL.getmetatable());
assertEquals(null, LuaValue.TRUE.getmetatable());
assertEquals(null, LuaValue.ONE.getmetatable());
Expand All @@ -73,7 +79,8 @@ public void testGetMetatable() {
assertEquals(table, userdatamt.getmetatable());
}

public void testSetMetatable() {
@Test
void testSetMetatable() {
LuaValue mt = LuaValue.tableOf();
assertEquals(null, table.getmetatable());
assertEquals(null, userdata.getmetatable());
Expand Down Expand Up @@ -127,7 +134,8 @@ public void testSetMetatable() {
assertEquals(mt, thread.getmetatable());
}

public void testMetatableIndex() {
@Test
void testMetatableIndex() {
assertEquals(table, table.setmetatable(null));
assertEquals(userdata, userdata.setmetatable(null));
assertEquals(userdatamt, userdatamt.setmetatable(null));
Expand Down Expand Up @@ -168,6 +176,7 @@ public void testMetatableIndex() {

// plain metatable
mt.set(LuaValue.INDEX, new TwoArgFunction() {
@Override
public LuaValue call(LuaValue arg1, LuaValue arg2) {
return LuaValue.valueOf(arg1.typename() + "[" + arg2.tojstring() + "]=xyz");
}
Expand All @@ -183,7 +192,8 @@ public LuaValue call(LuaValue arg1, LuaValue arg2) {
assertEquals("thread[1]=xyz", thread.get(1).tojstring());
}

public void testMetatableNewIndex() {
@Test
void testMetatableNewIndex() {
// empty metatable
LuaValue mt = LuaValue.tableOf();
assertEquals(table, table.setmetatable(mt));
Expand Down Expand Up @@ -218,6 +228,7 @@ public void testMetatableNewIndex() {

// metatable with function call
mt.set(LuaValue.NEWINDEX, new ThreeArgFunction() {
@Override
public LuaValue call(LuaValue arg1, LuaValue arg2, LuaValue arg3) {
fallback.rawset(arg2, LuaValue.valueOf("via-func-" + arg3));
return NONE;
Expand Down Expand Up @@ -266,7 +277,8 @@ private LuaValue makeTable(String key1, String val1, String key2, String val2) {
LuaValue.valueOf(val2), });
}

public void testRawsetMetatableSet() {
@Test
void testRawsetMetatableSet() {
// set up tables
LuaValue m = makeTable("aa", "aaa", "bb", "bbb");
m.set(LuaValue.INDEX, m);
Expand Down Expand Up @@ -356,7 +368,5 @@ public void testRawsetMetatableSet() {
checkTable(s, www, zzz, qqq, ddd, xxx, yyy, ttt, www, nil, qqq, ddd, xxx, nil, nil);
checkTable(t, aaa, zzz, ccc, sss, nil, yyy, ttt, nil, zzz, ccc, sss, nil, nil, nil);
checkTable(m, aaa, bbb, nil, nil, nil, yyy, ttt, aaa, bbb, nil, nil, nil, yyy, ttt);

}

}
Loading

0 comments on commit 9792fcb

Please sign in to comment.