Skip to content

Commit

Permalink
Continue converting tests to the Lua spec framework
Browse files Browse the repository at this point in the history
 - Remove Lua 5.2 bitwise test - this already exists at bit32_spec.

 - Port Lua 5.1 to 5.4's strings.lua. This has highlighted several
   issues with Cobalt here, which I'm leaving to fix in a separate
   commit. It's not entirely clear what the correct behaviour is in all
   cases.

 - Port a couple of our general "assert" tests.
  • Loading branch information
SquidDev committed Oct 29, 2023
1 parent 76369cf commit bfe1a9a
Show file tree
Hide file tree
Showing 18 changed files with 617 additions and 683 deletions.
5 changes: 4 additions & 1 deletion src/main/java/org/squiddev/cobalt/lib/FormatDesc.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public class FormatDesc {

private final int flags;
private final int width;
final int precision;
private final int precision;

final int conversion;

Expand Down Expand Up @@ -159,7 +159,10 @@ void checkFlags(int flags) throws LuaError {
}

void format(Buffer buf, byte c) {
int nSpaces = width > 1 ? width - 1 : 0;
if (!leftAdjust()) pad(buf, ' ', nSpaces);
buf.append(c);
if (leftAdjust()) pad(buf, ' ', nSpaces);
}

public void format(Buffer buf, long number) {
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/squiddev/cobalt/lib/StringFormat.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ static LuaString format(LuaState state, FormatState format) throws LuaError, Unw
desc.format(result, value.checkDouble());
}
case 'q' -> {
desc.checkFlags(0);
if (desc.length != 1) throw new LuaError("specifier '%q' cannot have modifiers");
addQuoted(result, format.arg, value);
}
case 's' -> {
Expand Down Expand Up @@ -139,7 +139,7 @@ private static void addQuoted(Buffer buf, int arg, LuaValue s) throws LuaError {
}
}
case TBOOLEAN, TNIL -> buf.append(s.toString());
default -> throw ErrorFactory.argError(arg, "value has no literal representation");
default -> throw ErrorFactory.argError(arg, "value has no literal form");
}
}

Expand Down
19 changes: 0 additions & 19 deletions src/test/java/org/squiddev/cobalt/AssertTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,20 +52,15 @@ public void tables(String name) throws IOException, CompileException, LuaError,
@ParameterizedTest(name = ParameterizedTest.ARGUMENTS_WITH_NAMES_PLACEHOLDER)
@ValueSource(strings = {
"debug",
"debug-coroutine-hook",
"debug-getinfo",
"debug-upvalue",
"gc",
"immutable",
"invalid-tailcall",
"lex-context",
"lex-number",
"load-error",
"no-unwind",
"setlist",
"string-compare",
"string-issues",
"string-format",
"time",
"traceback",
})
Expand Down Expand Up @@ -95,7 +90,6 @@ public void main(String name) throws IOException, CompileException, LuaError, In
"nextvar",
"pm",
"sort",
"strings",
"vararg",
"verybig",
})
Expand All @@ -113,19 +107,6 @@ public void lua51(String name) throws Exception {
helpers.runWithDump(name);
}

@ParameterizedTest(name = ParameterizedTest.ARGUMENTS_WITH_NAMES_PLACEHOLDER)
@ValueSource(strings = {
"bitwise",
"strings",
})
public void lua52(String name) throws Exception {
ScriptHelper helpers = new ScriptHelper("/assert/lua5.2/");
helpers.setup(x -> x.bytecodeFormat(LuaBytecodeFormat.instance()));
Bit32Lib.add(helpers.state, helpers.globals);

helpers.runWithDump(name);
}

@ParameterizedTest(name = ParameterizedTest.ARGUMENTS_WITH_NAMES_PLACEHOLDER)
@ValueSource(strings = {
"db",
Expand Down
16 changes: 9 additions & 7 deletions src/test/java/org/squiddev/cobalt/LuaSpecTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,13 @@ public LuaSpecTest() throws IOException, LuaError, CompileException {

env.rawset("fail", RegisteredFunction.of("fail", (state, arg) -> {
var frame = state.getCurrentThread().getDebugState().getFrame(2);
var values = frame.stack;
for (int i = 0; i < values.length; i++) {
var value = values[i];
var local = frame.closure.getPrototype().getLocalName(i + 1, frame.pc);
if (!value.isNil() || local != null) System.out.printf("% 2d => %s [%s]\n", i, values[i], local);
if (frame != null && frame.stack != null) {
var values = frame.stack;
for (int i = 0; i < values.length; i++) {
var value = values[i];
var local = frame.closure.getPrototype().getLocalName(i + 1, frame.pc);
if (!value.isNil() || local != null) System.out.printf("% 2d => %s [%s]\n", i, values[i], local);
}
}
throw new AssertionError(arg.checkString() + "\n" + DebugHelpers.traceback(state.getCurrentThread(), 0));
}).create());
Expand Down Expand Up @@ -127,7 +129,7 @@ private static DynamicNode makePending(DynamicNode node) {
public Stream<DynamicNode> getTests() throws IOException {
return Files.walk(ROOT)
.filter(x -> x.getFileName().toString().endsWith("_spec.lua"))
.map(path -> {
.flatMap(path -> {
LuaFunction function;
try (InputStream stream = Files.newInputStream(path)) {
function = LoadState.load(state, stream, "@" + path.getFileName(), env);
Expand All @@ -144,7 +146,7 @@ public Stream<DynamicNode> getTests() throws IOException {
this.nodes = null;
}

return DynamicContainer.dynamicContainer(path.getFileName().toString(), nodes);
return nodes.stream();
});
}
}
16 changes: 0 additions & 16 deletions src/test/resources/assert/debug-coroutine-hook.lua

This file was deleted.

9 changes: 0 additions & 9 deletions src/test/resources/assert/load-error.lua

This file was deleted.

176 changes: 0 additions & 176 deletions src/test/resources/assert/lua5.1/strings.lua

This file was deleted.

Loading

0 comments on commit bfe1a9a

Please sign in to comment.