Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
"jest": "^26.0.1",
"jest-circus": "^25.1.0",
"lua-types": "^2.8.0",
"lua-wasm-bindings": "^0.1.4",
"lua-wasm-bindings": "^0.2.1",
"prettier": "^2.0.5",
"ts-jest": "^26.3.0",
"ts-node": "^8.6.2"
Expand Down
9 changes: 7 additions & 2 deletions test/unit/builtins/math.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ util.testEachVersion("Math.atan2", () => util.testExpression`Math.atan2(4, 5)`,
[tstl.LuaTarget.Lua54]: builder => builder.tap(expectMathAtan2),
});

test("Math.atan2(4, 5)", () => {
util.testExpression`Math.atan2(4, 5)`.expectToMatchJsResult();
util.testEachVersion("Math.atan2(4, 5)", () => util.testExpression`Math.atan2(4, 5)`, {
[tstl.LuaTarget.Universal]: builder => builder.expectToMatchJsResult(),
[tstl.LuaTarget.LuaJIT]: false,
[tstl.LuaTarget.Lua51]: builder => builder.expectToMatchJsResult(),
[tstl.LuaTarget.Lua52]: builder => builder.expectToMatchJsResult(),
[tstl.LuaTarget.Lua53]: builder => builder.expectToMatchJsResult(),
[tstl.LuaTarget.Lua54]: builder => builder.expectToMatchJsResult(),
});
3 changes: 0 additions & 3 deletions test/unit/conditionals.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -368,9 +368,6 @@ test.each([
{ input: "true ? false : true", options: { luaTarget: tstl.LuaTarget.Lua51 } },
{ input: "false ? false : true", options: { luaTarget: tstl.LuaTarget.Lua51 } },
{ input: "true ? undefined : true", options: { luaTarget: tstl.LuaTarget.Lua51 } },
{ input: "true ? false : true", options: { luaTarget: tstl.LuaTarget.LuaJIT } },
{ input: "false ? false : true", options: { luaTarget: tstl.LuaTarget.LuaJIT } },
{ input: "true ? undefined : true", options: { luaTarget: tstl.LuaTarget.LuaJIT } },
])("Ternary operator (%p)", ({ input, options }) => {
util.testFunction`
const literalValue = "literal";
Expand Down
16 changes: 8 additions & 8 deletions test/unit/spread.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,23 +73,23 @@ describe("in function call", () => {
return foo(...array);
`,
{
[tstl.LuaTarget.Universal]: builder => builder.tap(expectLualibUnpack),
[tstl.LuaTarget.Universal]: builder => builder.tap(expectLualibUnpack).expectToMatchJsResult(),
[tstl.LuaTarget.LuaJIT]: builder => builder.tap(expectUnpack),
[tstl.LuaTarget.Lua51]: builder => builder.tap(expectUnpack),
[tstl.LuaTarget.Lua52]: builder => builder.tap(expectTableUnpack),
[tstl.LuaTarget.Lua53]: builder => builder.tap(expectTableUnpack),
[tstl.LuaTarget.Lua51]: builder => builder.tap(expectUnpack).expectToMatchJsResult(),
[tstl.LuaTarget.Lua52]: builder => builder.tap(expectTableUnpack).expectToMatchJsResult(),
[tstl.LuaTarget.Lua53]: builder => builder.tap(expectTableUnpack).expectToMatchJsResult(),
[tstl.LuaTarget.Lua54]: builder => builder.tap(expectTableUnpack).expectToMatchJsResult(),
}
);
});

describe("in array literal", () => {
util.testEachVersion(undefined, () => util.testExpression`[...[0, 1, 2]]`, {
[tstl.LuaTarget.Universal]: builder => builder.tap(expectLualibUnpack),
[tstl.LuaTarget.Universal]: builder => builder.tap(expectLualibUnpack).expectToMatchJsResult(),
[tstl.LuaTarget.LuaJIT]: builder => builder.tap(expectUnpack),
[tstl.LuaTarget.Lua51]: builder => builder.tap(expectUnpack),
[tstl.LuaTarget.Lua52]: builder => builder.tap(expectTableUnpack),
[tstl.LuaTarget.Lua53]: builder => builder.tap(expectTableUnpack),
[tstl.LuaTarget.Lua51]: builder => builder.tap(expectUnpack).expectToMatchJsResult(),
[tstl.LuaTarget.Lua52]: builder => builder.tap(expectTableUnpack).expectToMatchJsResult(),
[tstl.LuaTarget.Lua53]: builder => builder.tap(expectTableUnpack).expectToMatchJsResult(),
[tstl.LuaTarget.Lua54]: builder => builder.tap(expectTableUnpack).expectToMatchJsResult(),
});

Expand Down
26 changes: 24 additions & 2 deletions test/util.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/* eslint-disable jest/no-standalone-expect */
import * as nativeAssert from "assert";
import { lauxlib, lua, lualib } from "lua-wasm-bindings/dist/lua.54";
import { LUA_OK } from "lua-wasm-bindings/dist/lua";
import { LauxLib, Lua, LuaLib, LUA_OK } from "lua-wasm-bindings/dist/lua";
import * as fs from "fs";
import { stringify } from "javascript-stringify";
import * as path from "path";
Expand All @@ -17,6 +16,27 @@ const luaLib = fs.readFileSync(path.resolve(__dirname, "../dist/lualib/lualib_bu
// Using `test` directly makes eslint-plugin-jest consider this file as a test
const defineTest = test;

function getLuaBindingsForVersion(target: tstl.LuaTarget): { lauxlib: LauxLib; lua: Lua; lualib: LuaLib } {
if (target === tstl.LuaTarget.Lua51) {
const { lauxlib, lua, lualib } = require("lua-wasm-bindings/dist/lua.51");
return { lauxlib, lua, lualib };
}
if (target === tstl.LuaTarget.Lua52) {
const { lauxlib, lua, lualib } = require("lua-wasm-bindings/dist/lua.52");
return { lauxlib, lua, lualib };
}
if (target === tstl.LuaTarget.Lua53) {
const { lauxlib, lua, lualib } = require("lua-wasm-bindings/dist/lua.53");
return { lauxlib, lua, lualib };
}
if (target === tstl.LuaTarget.LuaJIT) {
throw Error("Can't use executeLua() or expectToMatchJsResult() wit LuaJIT as target!");
}

const { lauxlib, lua, lualib } = require("lua-wasm-bindings/dist/lua.54");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if target is LuaJIT?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mhh we can use 5.1 or 5.2 for JIT. But not sure that will help much. Most of our version specific tests are snapshot tests or test for exceptions. So no Lua code is actually executed in those.

Copy link
Member

@Perryvw Perryvw Mar 6, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would be okay with simply erroring for this case with a message we cannot run JIT in tests.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But we can run JIT tests as long as they don't use any JIT specific functions or features that are only available since 5.2.
Not sure however what the point is of testing non JIT functionality with JIT as target. So I guess erroring is fine?

Throwing for JIT breaks the following 3 test cases:
https://github.com/TypeScriptToLua/TypeScriptToLua/blob/master/test/unit/conditionals.spec.ts#L371
Any idea why we test that for JIT specifically?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added the throw in a351162

return { lauxlib, lua, lualib };
}

export function assert(value: any, message?: string | Error): asserts value {
nativeAssert(value, message);
}
Expand Down Expand Up @@ -339,6 +359,8 @@ export abstract class TestBuilder {
// Main file
const mainFile = this.getMainLuaCodeChunk();

const { lauxlib, lua, lualib } = getLuaBindingsForVersion(this.options.luaTarget ?? tstl.LuaTarget.Lua54);

const L = lauxlib.luaL_newstate();
lualib.luaL_openlibs(L);

Expand Down