ts-simple-ast
Version:
TypeScript compiler wrapper for AST navigation and code generation.
296 lines (294 loc) • 16.8 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const chai_1 = require("chai");
const compiler_1 = require("./../../../compiler");
const testHelpers_1 = require("./../testHelpers");
describe("InterfaceDeclaration", () => {
describe("insertConstructSignatures", () => {
function doTest(startCode, insertIndex, structures, expectedCode) {
const { firstChild } = testHelpers_1.getInfoFromText(startCode);
const result = firstChild.insertConstructSignatures(insertIndex, structures);
chai_1.expect(firstChild.getText()).to.equal(expectedCode);
chai_1.expect(result.length).to.equal(structures.length);
}
it("should insert when none exists", () => {
doTest("interface i {\n}", 0, [{}], "interface i {\n new();\n}");
});
it("should insert multiple into other", () => {
doTest("interface i {\n method1();\n method2();\n}", 1, [{ returnType: "string" }, {}], "interface i {\n method1();\n new(): string;\n new();\n method2();\n}");
});
});
describe("insertConstructSignature", () => {
function doTest(startCode, insertIndex, structure, expectedCode) {
const { firstChild } = testHelpers_1.getInfoFromText(startCode);
const result = firstChild.insertConstructSignature(insertIndex, structure);
chai_1.expect(firstChild.getText()).to.equal(expectedCode);
chai_1.expect(result).to.be.instanceOf(compiler_1.ConstructSignatureDeclaration);
}
it("should insert at index", () => {
doTest("interface i {\n method1();\n method2();\n}", 1, {}, "interface i {\n method1();\n new();\n method2();\n}");
});
});
describe("addConstructSignatures", () => {
function doTest(startCode, structures, expectedCode) {
const { firstChild } = testHelpers_1.getInfoFromText(startCode);
const result = firstChild.addConstructSignatures(structures);
chai_1.expect(firstChild.getText()).to.equal(expectedCode);
chai_1.expect(result.length).to.equal(structures.length);
}
it("should add multiple at end", () => {
doTest("interface i {\n method1();\n}", [{}, {}], "interface i {\n method1();\n new();\n new();\n}");
});
});
describe("addConstructSignature", () => {
function doTest(startCode, structure, expectedCode) {
const { firstChild } = testHelpers_1.getInfoFromText(startCode);
const result = firstChild.addConstructSignature(structure);
chai_1.expect(firstChild.getText()).to.equal(expectedCode);
chai_1.expect(result).to.be.instanceOf(compiler_1.ConstructSignatureDeclaration);
}
it("should add at end", () => {
doTest("interface i {\n method1();\n}", {}, "interface i {\n method1();\n new();\n}");
});
});
describe("getConstructSignatures", () => {
describe("none", () => {
it("should not have any", () => {
const { firstChild } = testHelpers_1.getInfoFromText("interface Identifier {\n}\n");
chai_1.expect(firstChild.getConstructSignatures().length).to.equal(0);
});
});
describe("has construct signatures", () => {
const { firstChild } = testHelpers_1.getInfoFromText("interface Identifier {\n prop: string;\n new(): string;\n method1():void;\n method2():string;\n}\n");
it("should get the right number of construct signatures", () => {
chai_1.expect(firstChild.getConstructSignatures().length).to.equal(1);
});
it("should get a construct signature of the right instance of", () => {
chai_1.expect(firstChild.getConstructSignatures()[0]).to.be.instanceOf(compiler_1.ConstructSignatureDeclaration);
});
});
});
describe("getConstructSignature", () => {
const { firstChild } = testHelpers_1.getInfoFromText("interface Identifier { new(): string; new(str: string): string; }");
it("should get the first that matches", () => {
chai_1.expect(firstChild.getConstructSignature(c => c.getParameters().length > 0)).to.equal(firstChild.getConstructSignatures()[1]);
});
it("should return undefined when none match", () => {
chai_1.expect(firstChild.getConstructSignature(c => c.getParameters().length > 5)).to.be.undefined;
});
});
describe("getConstructSignatureOrThrow", () => {
const { firstChild } = testHelpers_1.getInfoFromText("interface Identifier { new(): string; new(str: string): string; }");
it("should get the first that matches", () => {
chai_1.expect(firstChild.getConstructSignatureOrThrow(c => c.getParameters().length > 0)).to.equal(firstChild.getConstructSignatures()[1]);
});
it("should throw when none match", () => {
chai_1.expect(() => firstChild.getConstructSignatureOrThrow(c => c.getParameters().length > 5)).to.throw();
});
});
describe("insertMethods", () => {
function doTest(startCode, insertIndex, structures, expectedCode) {
const { firstChild } = testHelpers_1.getInfoFromText(startCode);
const result = firstChild.insertMethods(insertIndex, structures);
chai_1.expect(firstChild.getText()).to.equal(expectedCode);
chai_1.expect(result.length).to.equal(structures.length);
}
it("should insert when none exists", () => {
doTest("interface i {\n}", 0, [{ name: "method" }], "interface i {\n method();\n}");
});
it("should insert multiple into other methods", () => {
doTest("interface i {\n method1();\n method4();\n}", 1, [{ name: "method2", hasQuestionToken: true, returnType: "string" }, { name: "method3" }], "interface i {\n method1();\n method2?(): string;\n method3();\n method4();\n}");
});
});
describe("insertMethod", () => {
function doTest(startCode, insertIndex, structure, expectedCode) {
const { firstChild } = testHelpers_1.getInfoFromText(startCode);
const result = firstChild.insertMethod(insertIndex, structure);
chai_1.expect(firstChild.getText()).to.equal(expectedCode);
chai_1.expect(result).to.be.instanceOf(compiler_1.MethodSignature);
}
it("should insert at index", () => {
doTest("interface i {\n method1();\n method3();\n}", 1, { name: "method2" }, "interface i {\n method1();\n method2();\n method3();\n}");
});
});
describe("addMethods", () => {
function doTest(startCode, structures, expectedCode) {
const { firstChild } = testHelpers_1.getInfoFromText(startCode);
const result = firstChild.addMethods(structures);
chai_1.expect(firstChild.getText()).to.equal(expectedCode);
chai_1.expect(result.length).to.equal(structures.length);
}
it("should add multiple at end", () => {
doTest("interface i {\n method1();\n}", [{ name: "method2" }, { name: "method3" }], "interface i {\n method1();\n method2();\n method3();\n}");
});
});
describe("addMethod", () => {
function doTest(startCode, structure, expectedCode) {
const { firstChild } = testHelpers_1.getInfoFromText(startCode);
const result = firstChild.addMethod(structure);
chai_1.expect(firstChild.getText()).to.equal(expectedCode);
chai_1.expect(result).to.be.instanceOf(compiler_1.MethodSignature);
}
it("should add at end", () => {
doTest("interface i {\n method1();\n}", { name: "method2" }, "interface i {\n method1();\n method2();\n}");
});
});
describe("getMethod", () => {
const { firstChild } = testHelpers_1.getInfoFromText("interface Identifier { method1(); method2(); method3(); }");
it("should get the first that matches by name", () => {
chai_1.expect(firstChild.getMethod("method2").getName()).to.equal("method2");
});
it("should return the first that matches by a find function", () => {
chai_1.expect(firstChild.getMethod(m => m.getName() === "method3").getName()).to.equal("method3");
});
it("should return undefined when none match", () => {
chai_1.expect(firstChild.getMethod(m => m.getParameters().length > 5)).to.be.undefined;
});
});
describe("getMethodOrThrow", () => {
const { firstChild } = testHelpers_1.getInfoFromText("interface Identifier { method1(); method2(); method3(); }");
it("should get the first that matches by name", () => {
chai_1.expect(firstChild.getMethodOrThrow("method2").getName()).to.equal("method2");
});
it("should return the first that matches by a find function", () => {
chai_1.expect(firstChild.getMethodOrThrow(m => m.getName() === "method3").getName()).to.equal("method3");
});
it("should throw when none match", () => {
chai_1.expect(() => firstChild.getMethodOrThrow(m => m.getParameters().length > 5)).to.throw();
});
});
describe("getMethods", () => {
describe("no methods", () => {
it("should not have any methods", () => {
const { firstChild } = testHelpers_1.getInfoFromText("interface Identifier {\n}\n");
chai_1.expect(firstChild.getMethods().length).to.equal(0);
});
});
describe("has methods", () => {
const { firstChild } = testHelpers_1.getInfoFromText("interface Identifier {\n prop: string;\n method1():void;\n method2():string;\n}\n");
it("should get the right number of methods", () => {
chai_1.expect(firstChild.getMethods().length).to.equal(2);
});
it("should get a method of the right instance of", () => {
chai_1.expect(firstChild.getMethods()[0]).to.be.instanceOf(compiler_1.MethodSignature);
});
});
});
describe("insertProperties", () => {
function doTest(startCode, insertIndex, structures, expectedCode) {
const { firstChild } = testHelpers_1.getInfoFromText(startCode);
const result = firstChild.insertProperties(insertIndex, structures);
chai_1.expect(firstChild.getText()).to.equal(expectedCode);
chai_1.expect(result.length).to.equal(structures.length);
}
it("should insert when none exists", () => {
doTest("interface i {\n}", 0, [{ name: "prop" }], "interface i {\n prop;\n}");
});
it("should insert multiple into other properties", () => {
doTest("interface i {\n prop1;\n prop4;\n}", 1, [{ name: "prop2", hasQuestionToken: true, type: "string" }, { name: "prop3" }], "interface i {\n prop1;\n prop2?: string;\n prop3;\n prop4;\n}");
});
});
describe("insertProperty", () => {
function doTest(startCode, insertIndex, structure, expectedCode) {
const { firstChild } = testHelpers_1.getInfoFromText(startCode);
const result = firstChild.insertProperty(insertIndex, structure);
chai_1.expect(firstChild.getText()).to.equal(expectedCode);
chai_1.expect(result).to.be.instanceOf(compiler_1.PropertySignature);
}
it("should insert at index", () => {
doTest("interface i {\n prop1;\n prop3;\n}", 1, { name: "prop2" }, "interface i {\n prop1;\n prop2;\n prop3;\n}");
});
});
describe("addProperties", () => {
function doTest(startCode, structures, expectedCode) {
const { firstChild } = testHelpers_1.getInfoFromText(startCode);
const result = firstChild.addProperties(structures);
chai_1.expect(firstChild.getText()).to.equal(expectedCode);
chai_1.expect(result.length).to.equal(structures.length);
}
it("should add multiple at end", () => {
doTest("interface i {\n prop1;\n}", [{ name: "prop2" }, { name: "prop3" }], "interface i {\n prop1;\n prop2;\n prop3;\n}");
});
});
describe("addProperty", () => {
function doTest(startCode, structure, expectedCode) {
const { firstChild } = testHelpers_1.getInfoFromText(startCode);
const result = firstChild.addProperty(structure);
chai_1.expect(firstChild.getText()).to.equal(expectedCode);
chai_1.expect(result).to.be.instanceOf(compiler_1.PropertySignature);
}
it("should add at end", () => {
doTest("interface i {\n prop1;\n}", { name: "prop2" }, "interface i {\n prop1;\n prop2;\n}");
});
});
describe("getProperty", () => {
const { firstChild } = testHelpers_1.getInfoFromText("interface Identifier { prop1: string; prop2: number; prop3: Date; }");
it("should get the first that matches by name", () => {
chai_1.expect(firstChild.getProperty("prop2").getName()).to.equal("prop2");
});
it("should return the first that matches by a find function", () => {
chai_1.expect(firstChild.getProperty(p => p.getName() === "prop3").getName()).to.equal("prop3");
});
it("should return undefined when none match", () => {
chai_1.expect(firstChild.getProperty(p => p.getName() === "none")).to.be.undefined;
});
});
describe("getPropertyOrThrow", () => {
const { firstChild } = testHelpers_1.getInfoFromText("interface Identifier { prop1: string; prop2: number; prop3: Date; }");
it("should get the first that matches by name", () => {
chai_1.expect(firstChild.getPropertyOrThrow("prop2").getName()).to.equal("prop2");
});
it("should return the first that matches by a find function", () => {
chai_1.expect(firstChild.getPropertyOrThrow(p => p.getName() === "prop3").getName()).to.equal("prop3");
});
it("should throw when none match", () => {
chai_1.expect(() => firstChild.getPropertyOrThrow(p => p.getName() === "none")).to.throw();
});
});
describe("getProperties", () => {
describe("no properties", () => {
it("should not have any properties", () => {
const { firstChild } = testHelpers_1.getInfoFromText("interface Identifier {\n}\n");
chai_1.expect(firstChild.getProperties().length).to.equal(0);
});
});
describe("has properties", () => {
const { firstChild } = testHelpers_1.getInfoFromText("interface Identifier {\nprop: string;\nprop2: number;method1(): void;\n}\n");
it("should get the right number of properties", () => {
chai_1.expect(firstChild.getProperties().length).to.equal(2);
});
it("should get a property of the right instance of", () => {
chai_1.expect(firstChild.getProperties()[0]).to.be.instanceOf(compiler_1.PropertySignature);
});
});
});
describe("fill", () => {
function doTest(startingCode, structure, expectedCode) {
const { firstChild, sourceFile } = testHelpers_1.getInfoFromText(startingCode);
firstChild.fill(structure);
chai_1.expect(firstChild.getText()).to.equal(expectedCode);
}
it("should not modify anything if the structure doesn't change anything", () => {
doTest("interface Identifier {\n}", {}, "interface Identifier {\n}");
});
it("should modify when changed", () => {
const structure = {
constructSignatures: [{ returnType: "string" }],
properties: [{ name: "p" }],
methods: [{ name: "m" }]
};
doTest("interface Identifier {\n}", structure, "interface Identifier {\n new(): string;\n p;\n m();\n}");
});
});
describe("remove", () => {
function doTest(text, index, expectedText) {
const { sourceFile } = testHelpers_1.getInfoFromText(text);
sourceFile.getInterfaces()[index].remove();
chai_1.expect(sourceFile.getFullText()).to.equal(expectedText);
}
it("should remove the interface declaration", () => {
doTest("interface I {}\n\ninterface J {}\n\ninterface K {}", 1, "interface I {}\n\ninterface K {}");
});
});
});
//# sourceMappingURL=interfaceDeclarationTests.js.map