Skip to content

Commit 9eab650

Browse files
committed
Some random tests + cleanup of tshelper
1 parent 428c47c commit 9eab650

File tree

3 files changed

+54
-19
lines changed

3 files changed

+54
-19
lines changed

src/TSHelper.ts

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export class TSHelper {
2727

2828
// Breaks down a mask into all flag names.
2929
public static enumNames(mask, haystack) {
30-
const result = [mask];
30+
const result = [];
3131
for (const name in haystack) {
3232
if ((mask & haystack[name]) !== 0 && mask >= haystack[name]) {
3333
result.push(name);
@@ -110,21 +110,6 @@ export class TSHelper {
110110
return false;
111111
}
112112

113-
// Depth-First-Search up the inheritance tree for the name of the symbol containing the member
114-
public static findMemberHolder(type: ts.Type, memberName: ts.__String, typeChecker: ts.TypeChecker): string {
115-
if (type.symbol.members.has(memberName) || (type.symbol.exports && type.symbol.exports.has(memberName))) {
116-
while (this.isExtensionClass(type, typeChecker)) {
117-
type = typeChecker.getBaseTypes(type as ts.InterfaceType)[0];
118-
}
119-
return type.symbol.name;
120-
} else {
121-
for (const parent of typeChecker.getBaseTypes(type as ts.InterfaceType)) {
122-
const parentMember = this.findMemberHolder(parent, memberName, typeChecker);
123-
if (parentMember) { return parentMember; }
124-
}
125-
}
126-
}
127-
128113
// Search up until finding a node satisfying the callback
129114
public static findFirstNodeAbove<T extends ts.Node>(node: ts.Node, callback: (n: ts.Node) => n is T): T {
130115
let current = node;

test/unit/loops.spec.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,9 @@ export class LuaLoopTests {
9898
}
9999

100100
@TestCase([0, 1, 2, 3], [1, 2, 3, 4], "let i = 0; i < arrTest.length; i++")
101+
@TestCase([0, 1, 2, 3], [1, 2, 3, 4], "let i = 0; i <= arrTest.length - 1; i++")
102+
@TestCase([0, 1, 2, 3], [1, 2, 3, 4], "let i = 0; arrTest.length > i; i++")
103+
@TestCase([0, 1, 2, 3], [1, 2, 3, 4], "let i = 0; arrTest.length - 1 >= i; i++")
101104
@TestCase([0, 1, 2, 3], [1, 1, 3, 3], "let i = 0; i < arrTest.length; i += 2")
102105
@TestCase([0, 1, 2, 3], [1, 2, 3, 4], "let i = arrTest.length - 1; i <= 0; i--")
103106
@TestCase([0, 1, 2, 3], [0, 2, 2, 4], "let i = arrTest.length - 1; i <= 0; i -= 2")
@@ -135,18 +138,26 @@ export class LuaLoopTests {
135138
}).toThrowError(Error, "Unsupported for-loop increment step: BinaryExpression")
136139
}
137140

141+
@TestCase("let i = 0; i + 3; i++")
142+
@TestCase("let i = 0; 3 + i; i++")
143+
@TestCase("let i = 0; i - 3; i++")
144+
@TestCase("let i = 0; i * 3; i++")
145+
@TestCase("let i = 0; i / 3; i++")
146+
@TestCase("let i = 0; i &= 3; i++")
147+
@TestCase("let i = 0; i < 3; !i")
148+
@TestCase("let i = 0; i < 3; i as string")
138149
@Test("forconditionThrow")
139-
public forconditionThrow(inp: number[], expected: number[], header: string) {
150+
public forconditionThrow(header: string) {
140151
// Transpile & Assert
141152
Expect(() => {
142153
let lua = util.transpileString(
143-
`for (let i = arrTest.length - 1; i; i-- {
154+
`for (${header}) {
144155
}`
145156
);
146157

147158
// Execute
148159
let result = util.executeLua(lua);
149-
}).toThrowError(Error, "Unsupported for-loop condition type: Identifier")
160+
}).toThrow();
150161
}
151162

152163
@TestCase({ ['test1']: 0, ['test2']: 1, ['test3']: 2 }, { ['test1']: 1, ['test2']: 2, ['test3']: 3 })

test/unit/tshelper.spec.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import * as ts from "typescript";
2+
3+
import { Expect, FocusTest, IgnoreTest, Test, TestCase } from "alsatian";
4+
import { TSHelper as tsEx } from "../../src/TSHelper";
5+
6+
enum TestEnum {
7+
testA = 1,
8+
testB = 2,
9+
testC = 4,
10+
}
11+
12+
export class TSHelperTests {
13+
14+
@TestCase(TestEnum.testA, "testA")
15+
@TestCase(-1, "unknown")
16+
@TestCase(TestEnum.testA | TestEnum.testB, "unknown")
17+
@Test("EnumName")
18+
public testEnumName(inp, expected) {
19+
const result = tsEx.enumName(inp, TestEnum);
20+
21+
Expect(result).toEqual(expected);
22+
}
23+
24+
@TestCase(TestEnum.testA, ["testA"])
25+
@TestCase(-1, [])
26+
@TestCase(TestEnum.testA | TestEnum.testC, ["testA", "testC"])
27+
@TestCase(TestEnum.testA | TestEnum.testB | TestEnum.testC, ["testA", "testB", "testC"])
28+
@Test("EnumNames")
29+
public testEnumNames(inp, expected) {
30+
const result = tsEx.enumNames(inp, TestEnum);
31+
32+
Expect(result).toEqual(expected);
33+
}
34+
35+
@Test("IsFileModuleNull")
36+
public isFileModuleNull() {
37+
Expect(tsEx.isFileModule(null)).toEqual(false);
38+
}
39+
}

0 commit comments

Comments
 (0)