Skip to content

Commit 2a8ff2f

Browse files
committed
Use toThrowExactError on TSTLErrors in tests
1 parent 628069c commit 2a8ff2f

17 files changed

+87
-83
lines changed

src/LuaTransformer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3051,7 +3051,7 @@ export class LuaTransformer {
30513051
if (classDecorators.has(DecoratorKind.CustomConstructor)) {
30523052
const customDecorator = classDecorators.get(DecoratorKind.CustomConstructor);
30533053
if (!customDecorator.args[0]) {
3054-
throw TSTLErrors.InvalidDecoratorArgumentNumber("!CustomConstructor", 0, 1, node);
3054+
throw TSTLErrors.InvalidDecoratorArgumentNumber("@customConstructor", 0, 1, node);
30553055
}
30563056
return tstl.createCallExpression(
30573057
tstl.createIdentifier(customDecorator.args[0]),

test/unit/assignments.spec.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import * as ts from "typescript";
12
import { TranspileError } from "../../src/TranspileError";
23
import { TSTLErrors } from "../../src/TSTLErrors";
34
import * as util from "../util";
@@ -473,8 +474,8 @@ test.each([
473474
});
474475

475476
test("Ellipsis binding pattern", () => {
476-
expect(() => util.transpileString("let [a,b,...c] = [1,2,3];")).toThrow(
477-
"Ellipsis destruction is not allowed.",
477+
expect(() => util.transpileString("let [a,b,...c] = [1,2,3];")).toThrowExactError(
478+
TSTLErrors.ForbiddenEllipsisDestruction(util.nodeStub),
478479
);
479480
});
480481

@@ -578,9 +579,8 @@ test("TupleReturn in expression", () => {
578579
test.each(["and", "local", "nil", "not", "or", "repeat", "then", "until"])(
579580
"Keyword identifier error (%p)",
580581
identifier => {
581-
expect(() => util.transpileString(`const ${identifier} = 3;`)).toThrowWithMessage(
582-
TranspileError,
583-
`Cannot use Lua keyword ${identifier} as identifier.`,
582+
expect(() => util.transpileString(`const ${identifier} = 3;`)).toThrowExactError(
583+
TSTLErrors.KeywordIdentifier(ts.createIdentifier(identifier)),
584584
);
585585
},
586586
);

test/unit/class.spec.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as ts from "typescript";
22
import * as util from "../util";
33
import { TranspileError } from "../../src/TranspileError";
4+
import { TSTLErrors } from "../../src/TSTLErrors";
45

56
test("ClassFieldInitializer", () => {
67
const result = util.transpileAndExecute(
@@ -500,9 +501,9 @@ test("methodDefaultParameters", () => {
500501
test("Class without name error", () => {
501502
const transformer = util.makeTestTransformer();
502503

503-
expect(() => transformer.transformClassDeclaration({} as ts.ClassDeclaration)).toThrow(
504-
"Class declarations must have a name.",
505-
);
504+
expect(() =>
505+
transformer.transformClassDeclaration({} as ts.ClassDeclaration),
506+
).toThrowExactError(TSTLErrors.MissingClassName(util.nodeStub));
506507
});
507508

508509
test("CallSuperMethodNoArgs", () => {
@@ -713,9 +714,8 @@ test.each(["extension", "metaExtension"])("Class extends extension (%p)", extens
713714
/** @${extensionType} **/
714715
class B extends A {}
715716
class C extends B {}`;
716-
expect(() => util.transpileString(code)).toThrowWithMessage(
717-
TranspileError,
718-
"Cannot extend classes with decorator '@extension' or '@metaExtension'.",
717+
expect(() => util.transpileString(code)).toThrowExactError(
718+
TSTLErrors.InvalidExtendsExtension(util.nodeStub),
719719
);
720720
});
721721

@@ -724,9 +724,8 @@ test.each(["extension", "metaExtension"])("Class construct extension (%p)", exte
724724
/** @${extensionType} **/
725725
class B extends A {}
726726
const b = new B();`;
727-
expect(() => util.transpileString(code)).toThrowWithMessage(
728-
TranspileError,
729-
"Cannot construct classes with decorator '@extension' or '@metaExtension'.",
727+
expect(() => util.transpileString(code)).toThrowExactError(
728+
TSTLErrors.InvalidNewExpressionOnExtension(util.nodeStub),
730729
);
731730
});
732731

test/unit/conditionals.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { TranspileError } from "../../src/TranspileError";
22
import { LuaTarget } from "../../src/CompilerOptions";
33
import * as util from "../util";
4+
import { TSTLErrors } from "../../src/TSTLErrors";
45

56
test.each([{ inp: 0, expected: 0 }, { inp: 1, expected: 1 }])("if (%p)", ({ inp, expected }) => {
67
const result = util.transpileAndExecute(
@@ -374,8 +375,7 @@ test("switch dead code after return", () => {
374375
test("switch not allowed in 5.1", () => {
375376
expect(() =>
376377
util.transpileString(`switch ("abc") {}`, { luaTarget: LuaTarget.Lua51 }),
377-
).toThrowWithMessage(
378-
TranspileError,
379-
"Switch statements is/are not supported for target Lua 5.1.",
378+
).toThrowExactError(
379+
TSTLErrors.UnsupportedForTarget("Switch statements", LuaTarget.Lua51, util.nodeStub),
380380
);
381381
});

test/unit/decoratorCustomConstructor.spec.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as util from "../util";
22

33
import { TranspileError } from "../../src/TranspileError";
4+
import { TSTLErrors } from "../../src/TSTLErrors";
45

56
test("CustomCreate", () => {
67
const luaHeader = `function Point2DCreate(x, y)
@@ -39,5 +40,7 @@ test("IncorrectUsage", () => {
3940
return new Point2D(1, 2).x;
4041
`,
4142
);
42-
}).toThrowWithMessage(TranspileError, "!CustomConstructor expects 1 argument(s) but got 0.");
43+
}).toThrowExactError(
44+
TSTLErrors.InvalidDecoratorArgumentNumber("@customConstructor", 0, 1, util.nodeStub),
45+
);
4346
});

test/unit/decoratorMetaExtension.spec.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,5 @@ test("DontAllowInstantiation", () => {
5252
const e = new Ext();
5353
`,
5454
);
55-
}).toThrowWithMessage(
56-
TranspileError,
57-
"Cannot construct classes with decorator '@extension' or '@metaExtension'.",
58-
);
55+
}).toThrowExactError(TSTLErrors.InvalidNewExpressionOnExtension(util.nodeStub));
5956
});

test/unit/enum.spec.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as util from "../util";
22

33
import { TranspileError } from "../../src/TranspileError";
4+
import { TSTLErrors } from "../../src/TSTLErrors";
45

56
test("Declare const enum", () => {
67
const testCode = `
@@ -64,11 +65,7 @@ test("Invalid heterogeneous enum", () => {
6465
c,
6566
}`,
6667
);
67-
}).toThrowWithMessage(
68-
TranspileError,
69-
"Invalid heterogeneous enum. Enums should either specify no " +
70-
"member values, or specify values (of the same type) for all members.",
71-
);
68+
}).toThrowExactError(TSTLErrors.HeterogeneousEnum(util.nodeStub));
7269
});
7370

7471
test("String literal name in enum", () => {

test/unit/error.spec.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as util from "../util";
22

33
import { TranspileError } from "../../src/TranspileError";
4+
import { TSTLErrors } from "../../src/TSTLErrors";
45

56
test("throwString", () => {
67
const lua = util.transpileString(`throw "Some Error"`);
@@ -10,7 +11,7 @@ test("throwString", () => {
1011
test("throwError", () => {
1112
expect(() => {
1213
const lua = util.transpileString(`throw Error("Some Error")`);
13-
}).toThrowWithMessage(TranspileError, "Invalid throw expression, only strings can be thrown.");
14+
}).toThrowExactError(TSTLErrors.InvalidThrowExpression(util.nodeStub));
1415
});
1516

1617
test.each([{ i: 0, expected: "A" }, { i: 1, expected: "B" }, { i: 2, expected: "C" }])(

test/unit/expressions.spec.ts

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,13 @@ test("Unknown unary postfix error", () => {
455455

456456
expect(() =>
457457
transformer.transformPostfixUnaryExpression(mockExpression as ts.PostfixUnaryExpression),
458-
).toThrowWithMessage(TranspileError, "Unsupported unary postfix operator kind: AsteriskToken");
458+
).toThrowExactError(
459+
TSTLErrors.UnsupportedKind(
460+
"unary postfix operator",
461+
ts.SyntaxKind.AsteriskToken,
462+
util.nodeStub,
463+
),
464+
);
459465
});
460466

461467
test("Unknown unary postfix error", () => {
@@ -468,26 +474,34 @@ test("Unknown unary postfix error", () => {
468474

469475
expect(() =>
470476
transformer.transformPrefixUnaryExpression(mockExpression as ts.PrefixUnaryExpression),
471-
).toThrowWithMessage(TranspileError, "Unsupported unary prefix operator kind: AsteriskToken");
477+
).toThrowExactError(
478+
TSTLErrors.UnsupportedKind(
479+
"unary prefix operator",
480+
ts.SyntaxKind.AsteriskToken,
481+
util.nodeStub,
482+
),
483+
);
472484
});
473485

474486
test("Incompatible fromCodePoint expression error", () => {
475487
const transformer = util.makeTestTransformer(LuaTarget.LuaJIT);
476488

477489
const identifier = ts.createIdentifier("fromCodePoint");
478-
expect(() => transformer.transformStringExpression(identifier)).toThrowWithMessage(
479-
TranspileError,
480-
"string property fromCodePoint is/are not supported for target Lua jit.",
490+
expect(() => transformer.transformStringExpression(identifier)).toThrowExactError(
491+
TSTLErrors.UnsupportedForTarget(
492+
"string property fromCodePoint",
493+
LuaTarget.LuaJIT,
494+
util.nodeStub,
495+
),
481496
);
482497
});
483498

484499
test("Unknown string expression error", () => {
485500
const transformer = util.makeTestTransformer(LuaTarget.LuaJIT);
486501

487502
const identifier = ts.createIdentifier("abcd");
488-
expect(() => transformer.transformStringExpression(identifier)).toThrowWithMessage(
489-
TranspileError,
490-
"string property abcd is/are not supported for target Lua jit.",
503+
expect(() => transformer.transformStringExpression(identifier)).toThrowExactError(
504+
TSTLErrors.UnsupportedForTarget("string property abcd", LuaTarget.LuaJIT, util.nodeStub),
491505
);
492506
});
493507

@@ -506,15 +520,15 @@ test("Unsupported array function error", () => {
506520

507521
expect(() =>
508522
transformer.transformArrayCallExpression(mockNode as ts.CallExpression),
509-
).toThrowWithMessage(TranspileError, "Unsupported property on array: unknownFunction");
523+
).toThrowExactError(TSTLErrors.UnsupportedProperty("array", "unknownFunction", util.nodeStub));
510524
});
511525

512526
test("Unsupported math property error", () => {
513527
const transformer = util.makeTestTransformer();
514528

515529
expect(() =>
516530
transformer.transformMathExpression(ts.createIdentifier("unknownProperty")),
517-
).toThrowWithMessage(TranspileError, "Unsupported property on math: unknownProperty");
531+
).toThrowExactError(TSTLErrors.UnsupportedProperty("math", "unknownProperty", util.nodeStub));
518532
});
519533

520534
test("Unsupported object literal element error", () => {
@@ -531,5 +545,11 @@ test("Unsupported object literal element error", () => {
531545

532546
expect(() =>
533547
transformer.transformObjectLiteral(mockObject as ts.ObjectLiteralExpression),
534-
).toThrowWithMessage(TranspileError, "Unsupported object literal element kind: FalseKeyword");
548+
).toThrowExactError(
549+
TSTLErrors.UnsupportedKind(
550+
"object literal element",
551+
ts.SyntaxKind.FalseKeyword,
552+
util.nodeStub,
553+
),
554+
);
535555
});

test/unit/functions.spec.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as ts from "typescript";
22
import * as util from "../util";
3+
import { TSTLErrors } from "../../src/TSTLErrors";
34

45
test("Arrow Function Expression", () => {
56
const result = util.transpileAndExecute(`let add = (a, b) => a+b; return add(1,2);`);
@@ -195,9 +196,9 @@ test("Invalid property access call transpilation", () => {
195196
expression: ts.createLiteral("abc"),
196197
};
197198

198-
expect(() => transformer.transformPropertyCall(mockObject as ts.CallExpression)).toThrow(
199-
"Tried to transpile a non-property call as property call.",
200-
);
199+
expect(() =>
200+
transformer.transformPropertyCall(mockObject as ts.CallExpression),
201+
).toThrowExactError(TSTLErrors.InvalidPropertyCall(util.nodeStub));
201202
});
202203

203204
test("Function dead code after return", () => {

0 commit comments

Comments
 (0)