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
153 lines (137 loc) · 6.1 KB
/
luaTable.spec.ts
File metadata and controls
153 lines (137 loc) · 6.1 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import * as ts from "typescript";
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 a LuaTable call expression",
tableLib => {
expect(() => util.transpileString(tableLib + `const exp = tbl.set("value", 5)`)).toThrowExactError(
TSTLErrors.UnsupportedProperty("LuaTable", "set", util.nodeStub)
);
}
);
test.each([tableLibClass, tableLibInterface])("LuaTables cannot have other members", tableLib => {
expect(() => util.transpileString(tableLib + `tbl.other()`)).toThrowExactError(
TSTLErrors.UnsupportedProperty("LuaTable", "other", util.nodeStub)
);
});
test.each([tableLibClass, tableLibInterface])("LuaTables cannot have other members", tableLib => {
expect(() => util.transpileString(tableLib + `let x = tbl.other()`)).toThrowExactError(
TSTLErrors.UnsupportedProperty("LuaTable", "other", 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, tableLibInterface])("Cannot use ElementAccessExpression on a LuaTable", tableLib => {
test.each([`tbl["get"]("field")`, `tbl["set"]("field")`, `tbl["length"]`])(
"Cannot use ElementAccessExpression on a LuaTable (%p)",
code => {
expect(() => util.transpileString(tableLib + code)).toThrowExactError(
TSTLErrors.UnsupportedKind(
"LuaTable access expression",
ts.SyntaxKind.ElementAccessExpression,
util.nodeStub
)
);
}
);
});
test.each([tableLibClass, tableLibInterface])("Cannot isolate LuaTable methods", tableLib => {
test.each([`set`, `get`])("Cannot isolate LuaTable method (%p)", propertyName => {
expect(() => util.transpileString(`${tableLib} let property = tbl.${propertyName}`)).toThrowExactError(
TSTLErrors.UnsupportedProperty("LuaTable", propertyName, 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],
[`const k = "k"; const t = { data: new Table() }; t.data.set(k, 3); return t.data.get(k);`, 3],
])("LuaTable test (%p)", (code, expectedReturnValue) => {
expect(util.transpileAndExecute(code, undefined, undefined, tableLib)).toBe(expectedReturnValue);
});
});