ts-simple-ast
Version:
TypeScript compiler wrapper for AST navigation and code generation.
175 lines (173 loc) • 9.93 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const chai_1 = require("chai");
const testHelpers_1 = require("./../testHelpers");
describe("Decorator", () => {
function getFirstClassDecorator(code) {
const result = testHelpers_1.getInfoFromText(code);
const firstDecorator = result.firstChild.getDecorators()[0];
return Object.assign({}, result, { firstDecorator });
}
describe("isDecoratorFactory", () => {
it("should not be a decorator factory when has no call expression", () => {
const { firstDecorator } = getFirstClassDecorator("@decorator\nclass Identifier {}");
chai_1.expect(firstDecorator.isDecoratorFactory()).to.equal(false);
});
it("should be a decorator factory when has call expression", () => {
const { firstDecorator } = getFirstClassDecorator("@decorator()\nclass Identifier {}");
chai_1.expect(firstDecorator.isDecoratorFactory()).to.equal(true);
});
it("should be a decorator factory when has call expression with parameters", () => {
const { firstDecorator } = getFirstClassDecorator("@decorator('str', 23)\nclass Identifier {}");
chai_1.expect(firstDecorator.isDecoratorFactory()).to.equal(true);
});
});
describe("getNameIdentifier", () => {
function doTest(text, expectedName) {
const { firstDecorator } = getFirstClassDecorator(text);
chai_1.expect(firstDecorator.getNameIdentifier().getText()).to.equal(expectedName);
}
it("should get the name identifier for a non-decorator factory", () => {
doTest("@decorator\nclass Identifier {}", "decorator");
});
it("should get the name identifier for a non-decorator factory decorator with a namespace", () => {
doTest("@namespaceTest.decorator\nclass Identifier {}", "decorator");
});
it("should get the name identifier for a decorator factory", () => {
doTest("@decorator()\nclass Identifier {}", "decorator");
});
it("should get the name identifier for a decorator factory decorator with a namespace", () => {
doTest("@namespaceTest.decorator()\nclass Identifier {}", "decorator");
});
});
describe("getName", () => {
function doTest(text, expectedName) {
const { firstDecorator } = getFirstClassDecorator(text);
chai_1.expect(firstDecorator.getName()).to.equal(expectedName);
}
it("should get the name for a non-decorator factory", () => {
doTest("@decorator\nclass Identifier {}", "decorator");
});
it("should get the name for a non-decorator factory decorator with a namespace", () => {
doTest("@namespaceTest.decorator\nclass Identifier {}", "decorator");
});
it("should get the name for a decorator factory", () => {
doTest("@decorator()\nclass Identifier {}", "decorator");
});
it("should get the name for a decorator factory decorator with a namespace", () => {
doTest("@namespaceTest.decorator()\nclass Identifier {}", "decorator");
});
});
describe("getFullName", () => {
function doTest(text, expectedName) {
const { firstDecorator } = getFirstClassDecorator(text);
chai_1.expect(firstDecorator.getFullName()).to.equal(expectedName);
}
it("should get the name for a non-decorator factory", () => {
doTest("@decorator\nclass Identifier {}", "decorator");
});
it("should get the name for a non-decorator factory decorator with a namespace", () => {
doTest("@namespaceTest.decorator\nclass Identifier {}", "namespaceTest.decorator");
});
it("should get the name for a decorator factory", () => {
doTest("@decorator()\nclass Identifier {}", "decorator");
});
it("should get the name for a decorator factory decorator with a namespace", () => {
doTest("@namespaceTest.decorator()\nclass Identifier {}", "namespaceTest.decorator");
});
});
describe("getCallExpression", () => {
it("should return undefined when not a decorator factory", () => {
const { firstDecorator } = getFirstClassDecorator("@decorator\nclass Identifier {}");
chai_1.expect(firstDecorator.getCallExpression()).to.be.undefined;
});
it("should get the compiler call expression when a decorator factory", () => {
const { firstDecorator } = getFirstClassDecorator("@decorator('str', 4)\nclass Identifier {}");
chai_1.expect(firstDecorator.getCallExpression().getArguments().length).to.equal(2);
});
});
describe("getArguments", () => {
it("should return an empty array when not a decorator factory", () => {
const { firstDecorator } = getFirstClassDecorator("@decorator\nclass Identifier {}");
chai_1.expect(firstDecorator.getArguments()).to.deep.equal([]);
});
it("should get the arguments when a decorator factory", () => {
const { firstDecorator } = getFirstClassDecorator("@decorator('str', 4)\nclass Identifier {}");
chai_1.expect(firstDecorator.getArguments().length).to.equal(2);
});
});
describe("getTypeArguments", () => {
it("should return an empty array when not a decorator factory", () => {
const { firstDecorator } = getFirstClassDecorator("@decorator\nclass Identifier {}");
chai_1.expect(firstDecorator.getTypeArguments()).to.deep.equal([]);
});
it("should get the type arguments when a decorator factory", () => {
const { firstDecorator } = getFirstClassDecorator("@decorator<number, string>()\nclass Identifier {}");
chai_1.expect(firstDecorator.getTypeArguments().length).to.equal(2);
});
});
describe("removeTypeArgument", () => {
function doRemoveTypeArgTest(code, argIndexToRemove, expectedText) {
const { firstChild, sourceFile } = testHelpers_1.getInfoFromText(code);
firstChild.getDecorators()[0].removeTypeArgument(argIndexToRemove);
chai_1.expect(sourceFile.getFullText()).to.equal(expectedText);
}
it("should throw when not a call expression", () => {
const { firstChild } = testHelpers_1.getInfoFromText("@decorator\nclass MyClass {}");
chai_1.expect(() => firstChild.getDecorators()[0].getCallExpression().removeTypeArgument(0)).to.throw();
});
it("should remove when the only type argument", () => {
doRemoveTypeArgTest("@decorator<MyClass>(arg1, arg2)\nclass MyClass {}", 0, "@decorator(arg1, arg2)\nclass MyClass {}");
});
});
describe("remove", () => {
describe("class decorators", () => {
function doTest(code, index, expectedText) {
const { firstChild, sourceFile } = testHelpers_1.getInfoFromText(code);
firstChild.getDecorators()[index].remove();
chai_1.expect(sourceFile.getFullText()).to.equal(expectedText);
}
it("should remove when it's the only class decorator", () => {
doTest("@decorator(2, 5, 3)\nclass T {}", 0, "class T {}");
});
it("should remove when it's the first class decorator on the same line", () => {
doTest("@dec1 @dec2 @dec3\nclass T {}", 0, "@dec2 @dec3\nclass T {}");
});
it("should remove when it's the middle class decorator on the same line", () => {
doTest("@dec1 @dec2 @dec3\nclass T {}", 1, "@dec1 @dec3\nclass T {}");
});
it("should remove when it's the last class decorator on the same line", () => {
doTest("@dec1 @dec2 @dec3\nclass T {}", 2, "@dec1 @dec2\nclass T {}");
});
it("should remove when it's the first class decorator on different lines", () => {
doTest("@dec1\n@dec2\n@dec3\nclass T {}", 0, "@dec2\n@dec3\nclass T {}");
});
it("should remove when it's the middle class decorator on different lines", () => {
doTest("@dec1\n@dec2\n@dec3\nclass T {}", 1, "@dec1\n@dec3\nclass T {}");
});
it("should remove when it's the last class decorator on different lines", () => {
doTest("@dec1\n@dec2\n@dec3\nclass T {}", 2, "@dec1\n@dec2\nclass T {}");
});
});
describe("parameter decorators", () => {
function doTest(code, index, expectedText) {
const { firstChild, sourceFile } = testHelpers_1.getInfoFromText(code);
firstChild.getInstanceMethods()[0].getParameters()[0].getDecorators()[index].remove();
chai_1.expect(sourceFile.getFullText()).to.equal(expectedText);
}
it("should remove when it's the only parameter decorator", () => {
doTest("class T { myMethod(@dec param) {} }", 0, "class T { myMethod(param) {} }");
});
it("should remove when it's the first parameter decorator", () => {
doTest("class T { myMethod(@dec1 @dec2 @dec3 param) {} }", 0, "class T { myMethod(@dec2 @dec3 param) {} }");
});
it("should remove when it's the middle parameter decorator", () => {
doTest("class T { myMethod(@dec1 @dec2 @dec3 param) {} }", 1, "class T { myMethod(@dec1 @dec3 param) {} }");
});
it("should remove when it's the last parameter decorator", () => {
doTest("class T { myMethod(@dec1 @dec2 @dec3 param) {} }", 2, "class T { myMethod(@dec1 @dec2 param) {} }");
});
});
});
});
//# sourceMappingURL=decoratorTests.js.map