forked from TypeScriptToLua/TypeScriptToLua
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathluaTable.spec.ts
More file actions
125 lines (111 loc) · 4.97 KB
/
luaTable.spec.ts
File metadata and controls
125 lines (111 loc) · 4.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import * as TSTLErrors from "../../src/TSTLErrors";
import * as util from "../util";
const tableLibClass = `
/** @luaTable */
declare class Table<K extends {} = {}, V = any> {
length: number;
constructor(notAllowed?: boolean);
set(key?: K, value?: V): void;
get(key?: K): V;
other(): void;
}
declare let tbl: Table;
`;
const tableLibInterface = `
/** @luaTable */
declare interface Table<K extends {} = {}, V = any> {
length: number;
constructor(notAllowed?: boolean);
set(key?: K, value?: V): void;
get(key?: K): V;
other(): void;
}
declare let tbl: Table;
`;
test.each([tableLibClass])("LuaTables cannot be constructed with arguments", tableLib => {
expect(() => util.transpileString(tableLib + `const table = new Table(true);`)).toThrowExactError(
TSTLErrors.ForbiddenLuaTableUseException(
"No parameters are allowed when constructing a LuaTable object.",
util.nodeStub
)
);
});
test.each([tableLibClass, tableLibInterface])("LuaTable set() cannot be used in an expression position", tableLib => {
expect(() => util.transpileString(tableLib + `const exp = tbl.set("value", 5)`)).toThrowExactError(
TSTLErrors.ForbiddenLuaTableSetExpression(util.nodeStub)
);
});
test.each([tableLibClass, tableLibInterface])("LuaTables cannot have other methods", tableLib => {
expect(() => util.transpileString(tableLib + `tbl.other()`)).toThrowExactError(
TSTLErrors.ForbiddenLuaTableUseException("Unsupported method.", util.nodeStub)
);
});
test.each([tableLibClass, tableLibInterface])("LuaTables cannot have other methods", tableLib => {
expect(() => util.transpileString(tableLib + `let x = tbl.other()`)).toThrowExactError(
TSTLErrors.ForbiddenLuaTableUseException("Unsupported method.", util.nodeStub)
);
});
test.each([tableLibClass])("LuaTable new", tableLib => {
const content = tableLib + `tbl = new Table();`;
expect(util.transpileString(content)).toEqual("tbl = {}");
});
test.each([tableLibClass])("LuaTable length", tableLib => {
const content = tableLib + `tbl = new Table();\nreturn tbl.length;`;
const lua = util.transpileString(content);
expect(util.executeLua(lua)).toEqual(0);
});
test.each([tableLibClass, tableLibInterface])("Cannot set LuaTable length", tableLib => {
expect(() => util.transpileString(tableLib + `tbl.length = 2;`)).toThrowExactError(
TSTLErrors.ForbiddenLuaTableUseException("A LuaTable object's length cannot be re-assigned.", util.nodeStub)
);
});
test.each([tableLibClass, tableLibInterface])("Forbidden LuaTable use", tableLib => {
test.each([
[`tbl.get()`, "One parameter is required for get()."],
[`tbl.get("field", "field2")`, "One parameter is required for get()."],
[`tbl.set()`, "Two parameters are required for set()."],
[`tbl.set("field")`, "Two parameters are required for set()."],
[`tbl.set("field", 0, 1)`, "Two parameters are required for set()."],
[`tbl.set("field", ...[0, 1])`, "Arguments cannot be spread."],
])("Forbidden LuaTable use (%p)", (invalidCode, errorDescription) => {
expect(() => util.transpileString(tableLib + invalidCode)).toThrowExactError(
TSTLErrors.ForbiddenLuaTableUseException(errorDescription, util.nodeStub)
);
});
});
test.each([tableLibClass])("Cannot extend LuaTable class", tableLib => {
test.each([`class Ext extends Table {}`, `const c = class Ext extends Table {}`])(
"Cannot extend LuaTable class (%p)",
code => {
expect(() => util.transpileString(tableLib + code)).toThrowExactError(
TSTLErrors.InvalidExtendsLuaTable(util.nodeStub)
);
}
);
});
test.each([
`/** @luaTable */ class Table {}`,
`/** @luaTable */ export class Table {}`,
`/** @luaTable */ const c = class Table {}`,
])("LuaTable classes must be ambient (%p)", code => {
expect(() => util.transpileString(code)).toThrowExactError(
TSTLErrors.ForbiddenLuaTableNonDeclaration(util.nodeStub)
);
});
test.each([tableLibClass])("Cannot extend LuaTable class", tableLib => {
test.each([`tbl instanceof Table`])("Cannot use instanceof on a LuaTable class (%p)", code => {
expect(() => util.transpileString(tableLib + code)).toThrowExactError(
TSTLErrors.InvalidInstanceOfLuaTable(util.nodeStub)
);
});
});
test.each([tableLibClass])("LuaTable functional tests", tableLib => {
test.each<[string, any]>([
[`const t = new Table(); t.set("field", "value"); return t.get("field");`, "value"],
[`const t = new Table(); t.set("field", 0); return t.get("field");`, 0],
[`const t = new Table(); t.set(1, true); return t.length`, 1],
[`const t = new Table(); t.set(t.length + 1, true); t.set(t.length + 1, true); return t.length`, 2],
])("LuaTable test (%p)", (code, expectedReturnValue) => {
expect(util.transpileAndExecute(code, undefined, undefined, tableLib)).toBe(expectedReturnValue);
});
});