ts-simple-ast
Version:
TypeScript compiler wrapper for AST navigation and code generation.
115 lines (113 loc) • 7.55 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const chai_1 = require("chai");
const compiler_1 = require("./../../../compiler");
const testHelpers_1 = require("./../testHelpers");
describe("ConstructorDeclaration", () => {
describe("insertOverloads", () => {
function doTest(startCode, index, structures, expectedCode) {
const { firstChild, sourceFile } = testHelpers_1.getInfoFromText(startCode);
const methodDeclaration = firstChild.getConstructors()[0];
const result = methodDeclaration.insertOverloads(index, structures);
chai_1.expect(result.length).to.equal(structures.length);
chai_1.expect(sourceFile.getFullText()).to.equal(expectedCode);
}
it("should insert when no other overloads exist", () => {
doTest("class Identifier {\n constructor() {}\n }", 0, [{ parameters: [{ name: "param" }] }, {}], "class Identifier {\n constructor(param);\n constructor();\n constructor() {}\n }");
});
it("should copy over the scope keyword", () => {
doTest("class Identifier {\n protected constructor(p) {}\n }", 0, [{ parameters: [{ name: "param" }] }, {}], "class Identifier {\n protected constructor(param);\n protected constructor();\n protected constructor(p) {}\n }");
});
it("should be able to insert at start when another overload exists", () => {
doTest("class Identifier {\n constructor();\n constructor() {}\n }", 0, [{ parameters: [{ name: "param" }] }], "class Identifier {\n constructor(param);\n constructor();\n constructor() {}\n }");
});
it("should be able to insert at end when another overload exists", () => {
doTest("class Identifier {\n constructor();\n constructor() {}\n }", 1, [{ parameters: [{ name: "param" }] }], "class Identifier {\n constructor();\n constructor(param);\n constructor() {}\n }");
});
it("should be able to insert in the middle when other overloads exists", () => {
doTest("class Identifier {\n constructor();\n constructor();\n constructor() {}\n }", 1, [{ parameters: [{ name: "param" }] }], "class Identifier {\n constructor();\n constructor(param);\n constructor();\n constructor() {}\n }");
});
});
describe("insertOverload", () => {
function doTest(startCode, index, structure, expectedCode) {
const { firstChild, sourceFile } = testHelpers_1.getInfoFromText(startCode);
const methodDeclaration = firstChild.getAllMembers()[0];
const result = methodDeclaration.insertOverload(index, structure);
chai_1.expect(result).to.be.instanceof(compiler_1.ConstructorDeclaration);
chai_1.expect(sourceFile.getFullText()).to.equal(expectedCode);
}
it("should be able to insert in the middle when other overloads exists", () => {
doTest("class Identifier {\n constructor();\n constructor();\n constructor() {}\n }", 1, { parameters: [{ name: "param" }] }, "class Identifier {\n constructor();\n constructor(param);\n constructor();\n constructor() {}\n }");
});
});
describe("addOverloads", () => {
function doTest(startCode, structures, expectedCode) {
const { firstChild, sourceFile } = testHelpers_1.getInfoFromText(startCode);
const methodDeclaration = firstChild.getAllMembers()[0];
const result = methodDeclaration.addOverloads(structures);
chai_1.expect(result.length).to.equal(structures.length);
chai_1.expect(sourceFile.getFullText()).to.equal(expectedCode);
}
it("should be able to add multiple", () => {
doTest("class Identifier {\n constructor();\n constructor() {}\n }", [{ parameters: [{ name: "param" }] }, { parameters: [{ name: "param2" }] }], "class Identifier {\n constructor();\n constructor(param);\n constructor(param2);\n constructor() {}\n }");
});
});
describe("addOverload", () => {
function doTest(startCode, structure, expectedCode) {
const { firstChild, sourceFile } = testHelpers_1.getInfoFromText(startCode);
const methodDeclaration = firstChild.getAllMembers()[0];
const result = methodDeclaration.addOverload(structure);
chai_1.expect(result).to.be.instanceof(compiler_1.ConstructorDeclaration);
chai_1.expect(sourceFile.getFullText()).to.equal(expectedCode);
}
it("should be able to add an overload", () => {
doTest("class Identifier {\n constructor();\n constructor() {}\n }", { parameters: [{ name: "param" }] }, "class Identifier {\n constructor();\n constructor(param);\n constructor() {}\n }");
});
});
describe("remove", () => {
function doTest(startCode, expectedCode) {
const { sourceFile, firstChild } = testHelpers_1.getInfoFromText(startCode);
firstChild.getConstructors()[0].remove();
chai_1.expect(sourceFile.getFullText()).to.equal(expectedCode);
}
it("should remove when only member", () => {
doTest("class MyClass {\n constructor() {\n }\n}", "class MyClass {\n}");
});
it("should remove when between other members", () => {
doTest("class MyClass {\n prop: string;\n\n constructor() {\n }\n\n m() {\n }\n}", "class MyClass {\n prop: string;\n\n m() {\n }\n}");
});
it("should remove when at start", () => {
doTest("class MyClass {\n constructor() {\n }\n\n prop: string;\n}", "class MyClass {\n prop: string;\n}");
});
it("should remove when at end", () => {
doTest("class MyClass {\n prop: string;\n\n constructor() {\n }\n}", "class MyClass {\n prop: string;\n}");
});
it("should remove when there are overloads", () => {
doTest("class MyClass {\n constructor();constructor() {\n }\n}", "class MyClass {\n}");
});
it("should only remove the overload when calling remove on the overload", () => {
const startCode = "class MyClass {\n constructor();\n constructor() {\n }\n}";
const { sourceFile, firstChild } = testHelpers_1.getInfoFromText(startCode);
firstChild.getConstructors()[0].getOverloads()[0].remove();
chai_1.expect(sourceFile.getFullText()).to.equal("class MyClass {\n constructor() {\n }\n}");
});
});
describe("fill", () => {
function doTest(startingCode, structure, expectedCode) {
const { firstChild, sourceFile } = testHelpers_1.getInfoFromText(startingCode);
const ctor = firstChild.getConstructors()[0];
ctor.fill(structure);
chai_1.expect(sourceFile.getText()).to.equal(expectedCode);
}
it("should not modify anything if the structure doesn't change anything", () => {
doTest("class identifier {\n constructor() {}\n}", {}, "class identifier {\n constructor() {}\n}");
});
it("should modify when changed", () => {
const structure = {
overloads: [{ parameters: [{ name: "param" }] }]
};
doTest("class identifier {\n constructor() {}\n}", structure, "class identifier {\n constructor(param);\n constructor() {}\n}");
});
});
});
//# sourceMappingURL=constructorDeclarationTests.js.map