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
141 lines (124 loc) · 5.41 KB
/
luaTable.spec.ts
File metadata and controls
141 lines (124 loc) · 5.41 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
import {
luaTableCannotBeAccessedDynamically,
luaTableCannotBeExtended,
luaTableForbiddenUsage,
luaTableMustBeAmbient,
unsupportedProperty,
luaTableInvalidInstanceOf,
} from "../../../src/transformation/utils/diagnostics";
import * as util from "../../util";
const tableLibClass = `
/** @luaTable */
declare class Table<K extends {} = {}, V = any> {
length: number;
constructor(notAllowed?: any);
set(key?: K, value?: V, notAllowed?: any): void;
get(key?: K, notAllowed?: any): V;
other(): void;
}
declare let tbl: Table;
`;
const tableLibInterface = `
/** @luaTable */
declare interface Table<K extends {} = {}, V = any> {
length: number;
set(key?: K, value?: V, notAllowed?: any): void;
get(key?: K, notAllowed?: any): V;
other(): void;
}
/** @luaTable */
declare const Table: new <K extends {} = {}, V = any>(notAllowed?: any) => Table<K, V>;
declare let tbl: Table;
`;
test.each([tableLibClass])("LuaTables cannot be constructed with arguments", tableLib => {
util.testModule(tableLib + `const table = new Table(true);`).expectDiagnosticsToMatchSnapshot([
luaTableForbiddenUsage.code,
]);
});
test.each([tableLibClass, tableLibInterface])(
"LuaTable set() cannot be used in a LuaTable call expression",
tableLib => {
util.testModule(tableLib + `const exp = tbl.set("value", 5)`).expectDiagnosticsToMatchSnapshot([
unsupportedProperty.code,
]);
}
);
test.each([tableLibClass, tableLibInterface])("LuaTables cannot have other members", tableLib => {
util.testModule(tableLib + `tbl.other()`).expectDiagnosticsToMatchSnapshot([unsupportedProperty.code]);
});
test.each([tableLibClass, tableLibInterface])("LuaTables cannot have other members", tableLib => {
util.testModule(tableLib + `let x = tbl.other()`).expectDiagnosticsToMatchSnapshot([unsupportedProperty.code]);
});
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 => {
util.testModule(tableLib + `tbl.length = 2;`).expectDiagnosticsToMatchSnapshot([luaTableForbiddenUsage.code]);
});
test.each([tableLibClass, tableLibInterface])("Forbidden LuaTable use", tableLib => {
test.each([
"tbl.get()",
'tbl.get("field", "field2")',
"tbl.set()",
'tbl.set("field")',
'tbl.set("field", 0, 1)',
'tbl.set(...(["field", 0] as const))',
'tbl.set("field", ...([0] as const))',
])("Forbidden LuaTable use (%p)", invalidCode => {
util.testModule(tableLib + invalidCode).expectDiagnosticsToMatchSnapshot([luaTableForbiddenUsage.code]);
});
});
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 => {
util.testModule(tableLib + code).expectDiagnosticsToMatchSnapshot([luaTableCannotBeExtended.code]);
}
);
});
test.each([
`/** @luaTable */ class Table {}`,
`/** @luaTable */ export class Table {}`,
`/** @luaTable */ const c = class Table {}`,
])("LuaTable classes must be ambient (%p)", code => {
util.testModule(code).expectDiagnosticsToMatchSnapshot([luaTableMustBeAmbient.code]);
});
test.each([tableLibClass])("Cannot extend LuaTable class", tableLib => {
test.each([`tbl instanceof Table`])("Cannot use instanceof on a LuaTable class (%p)", code => {
util.testModule(tableLib + code).expectDiagnosticsToMatchSnapshot([luaTableInvalidInstanceOf.code]);
});
});
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 => {
util.testModule(tableLib + code).expectDiagnosticsToMatchSnapshot([
luaTableCannotBeAccessedDynamically.code,
]);
}
);
});
test.each([tableLibClass, tableLibInterface])("Cannot isolate LuaTable methods", tableLib => {
test.each([`set`, `get`])("Cannot isolate LuaTable method (%p)", propertyName => {
util.testModule(`${tableLib} let property = tbl.${propertyName}`).expectDiagnosticsToMatchSnapshot([
unsupportedProperty.code,
]);
});
});
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);
});
});