UNPKG

ts-simple-ast

Version:

TypeScript compiler wrapper for AST navigation and code generation.

62 lines (60 loc) 3.63 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const chai_1 = require("chai"); const testHelpers_1 = require("./../testHelpers"); describe("TypeArgumentedNode", () => { describe("getTypeArguments", () => { function doTest(code, expectedArgs) { const { firstChild } = testHelpers_1.getInfoFromText(code); const args = firstChild.getDecorators()[0].getCallExpression().getTypeArguments(); chai_1.expect(args.map(a => a.getText())).to.deep.equal(expectedArgs); } it("should get the type arguments when there are none", () => { doTest("@decorator()\nclass MyClass {}", []); }); it("should get the type arguments when they exist", () => { doTest("@decorator<string, number, {}>(arg1, arg2)\nclass MyClass {}", ["string", "number", "{}"]); }); }); describe("removeTypeArgument", () => { it("should throw when there are no current type arguments", () => { const { firstChild } = testHelpers_1.getInfoFromText("@decorator(arg1, arg2)\nclass MyClass {}"); chai_1.expect(() => firstChild.getDecorators()[0].getCallExpression().removeTypeArgument(0)).to.throw(); }); it("should throw when specifying an out of range index", () => { const { firstChild } = testHelpers_1.getInfoFromText("@decorator<MyClass>(arg1, arg2)\nclass MyClass {}"); chai_1.expect(() => firstChild.getDecorators()[0].getCallExpression().removeTypeArgument(1)).to.throw(); }); describe("index", () => { function doTest(code, argIndexToRemove, expectedText) { const { firstChild, sourceFile } = testHelpers_1.getInfoFromText(code); firstChild.getDecorators()[0].getCallExpression().removeTypeArgument(argIndexToRemove); chai_1.expect(sourceFile.getFullText()).to.equal(expectedText); } it("should remove when the only type argument", () => { doTest("@decorator<MyClass>(arg1, arg2)\nclass MyClass {}", 0, "@decorator(arg1, arg2)\nclass MyClass {}"); }); it("should remove the first type argument", () => { doTest("@decorator<string, number, {}>(arg1, arg2)\nclass MyClass {}", 0, "@decorator<number, {}>(arg1, arg2)\nclass MyClass {}"); }); it("should remove the middle type argument", () => { doTest("@decorator<string, number, {}>(arg1, arg2)\nclass MyClass {}", 1, "@decorator<string, {}>(arg1, arg2)\nclass MyClass {}"); }); it("should remove the last type argument", () => { doTest("@decorator<string, number, {}>(arg1, arg2)\nclass MyClass {}", 2, "@decorator<string, number>(arg1, arg2)\nclass MyClass {}"); }); }); describe("element", () => { function doTest(code, argIndexToRemove, expectedText) { const { firstChild, sourceFile } = testHelpers_1.getInfoFromText(code); const callExpr = firstChild.getDecorators()[0].getCallExpression(); callExpr.removeTypeArgument(callExpr.getTypeArguments()[argIndexToRemove]); chai_1.expect(sourceFile.getFullText()).to.equal(expectedText); } it("should remove the specified type argument", () => { doTest("@decorator<string, number, {}>(arg1, arg2)\nclass MyClass {}", 1, "@decorator<string, {}>(arg1, arg2)\nclass MyClass {}"); }); }); }); }); //# sourceMappingURL=typeArgumentedNodeTests.js.map