ts-simple-ast
Version:
TypeScript compiler wrapper for AST navigation and code generation.
653 lines (644 loc) • 36.8 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("ClassDeclaration", () => {
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("class Identifier {\n}", {}, "class Identifier {\n}");
});
it("should modify when changed", () => {
const structure = {
extends: "Other",
ctor: {},
properties: [{ name: "p" }],
methods: [{ name: "m" }]
};
doTest("class Identifier {\n}", structure, "class Identifier extends Other {\n constructor() {\n }\n\n p;\n\n m() {\n }\n}");
});
});
describe("getExtends", () => {
it("should return undefined when no extends clause exists", () => {
const { firstChild } = testHelpers_1.getInfoFromText("class Identifier { }");
chai_1.expect(firstChild.getExtends()).to.be.undefined;
});
it("should return a heritage clause when an extends clause exists", () => {
const { firstChild } = testHelpers_1.getInfoFromText("class Identifier extends Base { }");
chai_1.expect(firstChild.getExtends()).to.be.instanceOf(compiler_1.ExpressionWithTypeArguments);
});
});
describe("setExtends", () => {
it("should set an extends", () => {
const { firstChild, sourceFile } = testHelpers_1.getInfoFromText(" class Identifier {} ");
firstChild.setExtends("Base");
chai_1.expect(sourceFile.getFullText()).to.equal(" class Identifier extends Base {} ");
});
it("should set an extends when an implements exists", () => {
const { firstChild, sourceFile } = testHelpers_1.getInfoFromText("class Identifier implements IBase {}");
firstChild.setExtends("Base");
chai_1.expect(sourceFile.getFullText()).to.equal("class Identifier extends Base implements IBase {}");
});
it("should set an extends when the brace is right beside the identifier", () => {
const { firstChild, sourceFile } = testHelpers_1.getInfoFromText(" class Identifier{} ");
firstChild.setExtends("Base");
chai_1.expect(sourceFile.getFullText()).to.equal(" class Identifier extends Base {} ");
});
it("should set an extends when an extends already exists", () => {
const { firstChild, sourceFile } = testHelpers_1.getInfoFromText("class Identifier extends Base1 {}");
firstChild.setExtends("Base2");
chai_1.expect(sourceFile.getFullText()).to.equal("class Identifier extends Base2 {}");
});
it("should throw an error when providing invalid input", () => {
const { firstChild, sourceFile } = testHelpers_1.getInfoFromText("class Identifier extends Base1 {}");
chai_1.expect(() => firstChild.setExtends("")).to.throw();
chai_1.expect(() => firstChild.setExtends(" ")).to.throw();
chai_1.expect(() => firstChild.setExtends(5)).to.throw();
});
});
describe("insertConstructor", () => {
function doTest(startCode, insertIndex, structure, expectedCode) {
const { firstChild } = testHelpers_1.getInfoFromText(startCode);
const result = firstChild.insertConstructor(insertIndex, structure);
chai_1.expect(firstChild.getText()).to.equal(expectedCode);
chai_1.expect(result).to.be.instanceOf(compiler_1.ConstructorDeclaration);
}
it("should insert when none exists", () => {
doTest("class c {\n}", 0, {}, "class c {\n constructor() {\n }\n}");
});
it("should remove the previous when one exists", () => {
doTest("class c {\n constructor() {\n }\n}", 0, { scope: compiler_1.Scope.Private }, "class c {\n private constructor() {\n }\n}");
});
it("should insert multiple into other members", () => {
doTest("class c {\n prop1;\n prop2;\n}", 1, {}, "class c {\n prop1;\n\n constructor() {\n }\n\n prop2;\n}");
});
});
describe("addConstructor", () => {
function doTest(startCode, structure, expectedCode) {
const { firstChild } = testHelpers_1.getInfoFromText(startCode);
const result = firstChild.addConstructor(structure);
chai_1.expect(firstChild.getText()).to.equal(expectedCode);
chai_1.expect(result).to.be.instanceOf(compiler_1.ConstructorDeclaration);
}
it("should add at the end", () => {
doTest("class c {\n prop1;\n}", {}, "class c {\n prop1;\n\n constructor() {\n }\n}");
});
});
describe("getConstructors", () => {
it("should return undefined when no constructor exists", () => {
const { firstChild } = testHelpers_1.getInfoFromText("class Identifier { }");
chai_1.expect(firstChild.getConstructors().length).to.equal(0);
});
it("should return the constructor when it exists", () => {
const { firstChild } = testHelpers_1.getInfoFromText("class Identifier { constructor() { } }");
chai_1.expect(firstChild.getConstructors()[0].getText()).to.equal("constructor() { }");
});
it("should return the implementation constructor if not in an ambient context", () => {
const { firstChild } = testHelpers_1.getInfoFromText("class Identifier { constructor(str: string);constructor(str: any) { } }");
const constructors = firstChild.getConstructors();
chai_1.expect(constructors.length).to.equal(1);
chai_1.expect(constructors[0].getText()).to.equal("constructor(str: any) { }");
});
it("should return both constructors in an ambient context", () => {
const { firstChild } = testHelpers_1.getInfoFromText("declare class Identifier { constructor(str: string);constructor(str: any);}");
const constructors = firstChild.getConstructors();
chai_1.expect(constructors.length).to.equal(2);
chai_1.expect(constructors[0].getText()).to.equal("constructor(str: string);");
chai_1.expect(constructors[1].getText()).to.equal("constructor(str: any);");
});
});
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("class c {\n}", 0, [{ name: "prop" }], "class c {\n prop;\n}");
});
it("should insert multiple into other properties", () => {
doTest("class c {\n prop1;\n prop4;\n}", 1, [{ name: "prop2", hasQuestionToken: true, type: "string" }, { name: "prop3" }], "class c {\n prop1;\n prop2?: string;\n prop3;\n prop4;\n}");
});
it("should add an extra newline if inserting before non-property", () => {
doTest("class c {\n myMethod() {}\n}", 0, [{ name: "prop" }], "class c {\n prop;\n\n myMethod() {}\n}");
});
it("should add an extra newline if inserting ater non-property", () => {
doTest("class c {\n myMethod() {}\n}", 1, [{ name: "prop" }], "class c {\n myMethod() {}\n\n prop;\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.PropertyDeclaration);
}
it("should insert at index", () => {
doTest("class c {\n prop1;\n prop3;\n}", 1, { name: "prop2" }, "class c {\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("class c {\n prop1;\n}", [{ name: "prop2" }, { name: "prop3" }], "class c {\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.PropertyDeclaration);
}
it("should add at end", () => {
doTest("class c {\n prop1;\n}", { name: "prop2" }, "class c {\n prop1;\n prop2;\n}");
});
});
describe("getInstanceProperties", () => {
describe("no properties", () => {
it("should not have any properties", () => {
const { firstChild } = testHelpers_1.getInfoFromText("class Identifier {\n}\n");
chai_1.expect(firstChild.getInstanceProperties().length).to.equal(0);
});
});
describe("has properties", () => {
const code = "class Identifier {\nstatic prop2: string;\nstatic method() {}\n" +
"constructor(param: string, public param2: string, readonly param3: string) {}\n" +
"instanceProp: string;\nprop2: number;method1() {}\n" +
"get prop(): string {return '';}\nset prop(val: string) {}\n}\n";
const { firstChild } = testHelpers_1.getInfoFromText(code);
it("should get the right number of properties", () => {
chai_1.expect(firstChild.getInstanceProperties().length).to.equal(6);
});
it("should get a property of the right instance of for parameter with a scope", () => {
chai_1.expect(firstChild.getInstanceProperties()[0]).to.be.instanceOf(compiler_1.ParameterDeclaration);
chai_1.expect(firstChild.getInstanceProperties()[0].getName()).to.equal("param2");
});
it("should get a property of the right instance of for parameter with readonly keyword", () => {
chai_1.expect(firstChild.getInstanceProperties()[1]).to.be.instanceOf(compiler_1.ParameterDeclaration);
chai_1.expect(firstChild.getInstanceProperties()[1].getName()).to.equal("param3");
});
it("should get a property of the right instance of", () => {
chai_1.expect(firstChild.getInstanceProperties()[2]).to.be.instanceOf(compiler_1.PropertyDeclaration);
});
it("should get a property of the right instance of for the get accessor", () => {
chai_1.expect(firstChild.getInstanceProperties()[4]).to.be.instanceOf(compiler_1.GetAccessorDeclaration);
});
it("should get a property of the right instance of for the set accessor", () => {
chai_1.expect(firstChild.getInstanceProperties()[5]).to.be.instanceOf(compiler_1.SetAccessorDeclaration);
});
});
});
describe("getInstanceProperty", () => {
const code = "class Identifier {\nstatic prop2: string;\nstatic method() {}\n" +
"constructor(param: string, public param2: string, readonly param3: string) {}\n" +
"instanceProp: string;\nprop2: number;method1() {}\n" +
"get prop(): string {return '';}\nset prop(val: string) {}\n}\n";
const { firstChild } = testHelpers_1.getInfoFromText(code);
it("should get a property by name", () => {
const prop = firstChild.getInstanceProperty("prop2");
chai_1.expect(prop.getName()).to.equal("prop2");
chai_1.expect(prop.isStatic()).to.equal(false);
});
it("should get a property by function", () => {
const prop = firstChild.getInstanceProperty(p => p.getName() === "prop2");
chai_1.expect(prop.getName()).to.equal("prop2");
chai_1.expect(prop.isStatic()).to.equal(false);
});
});
describe("getInstancePropertyOrThrow", () => {
const code = "class Identifier {\nstatic prop2: string;\nstatic method() {}\n" +
"constructor(param: string, public param2: string, readonly param3: string) {}\n" +
"instanceProp: string;\nprop2: number;method1() {}\n" +
"get prop(): string {return '';}\nset prop(val: string) {}\n}\n";
const { firstChild } = testHelpers_1.getInfoFromText(code);
it("should get a property by name", () => {
const prop = firstChild.getInstancePropertyOrThrow("prop2");
chai_1.expect(prop.getName()).to.equal("prop2");
chai_1.expect(prop.isStatic()).to.equal(false);
});
it("should get a property by function", () => {
const prop = firstChild.getInstancePropertyOrThrow(p => p.getName() === "prop2");
chai_1.expect(prop.getName()).to.equal("prop2");
chai_1.expect(prop.isStatic()).to.equal(false);
});
it("should throw when not found", () => {
chai_1.expect(() => firstChild.getInstancePropertyOrThrow(p => p.getName() === "prop9")).to.throw();
});
});
describe("getStaticProperty", () => {
const code = "class Identifier {\nstatic prop2: string;\nstatic method() {}\n" +
"constructor(param: string, public param2: string, readonly param3: string) {}\n" +
"instanceProp: string;\nprop2: number;method1() {}\n" +
"get prop(): string {return '';}\nset prop(val: string) {}\n}\n";
const { firstChild } = testHelpers_1.getInfoFromText(code);
it("should get a property by name", () => {
const prop = firstChild.getStaticProperty("prop2");
chai_1.expect(prop.getName()).to.equal("prop2");
chai_1.expect(prop.isStatic()).to.equal(true);
});
it("should get a property by function", () => {
const prop = firstChild.getStaticProperty(p => p.getName() === "prop2");
chai_1.expect(prop.getName()).to.equal("prop2");
chai_1.expect(prop.isStatic()).to.equal(true);
});
});
describe("getStaticPropertyOrThrow", () => {
const code = "class Identifier {\nstatic prop2: string;\nstatic method() {}\n" +
"constructor(param: string, public param2: string, readonly param3: string) {}\n" +
"instanceProp: string;\nprop2: number;method1() {}\n" +
"get prop(): string {return '';}\nset prop(val: string) {}\n}\n";
const { firstChild } = testHelpers_1.getInfoFromText(code);
it("should get a property by name", () => {
const prop = firstChild.getStaticPropertyOrThrow("prop2");
chai_1.expect(prop.getName()).to.equal("prop2");
chai_1.expect(prop.isStatic()).to.equal(true);
});
it("should get a property by function", () => {
const prop = firstChild.getStaticPropertyOrThrow(p => p.getName() === "prop2");
chai_1.expect(prop.getName()).to.equal("prop2");
chai_1.expect(prop.isStatic()).to.equal(true);
});
it("should throw when not found", () => {
chai_1.expect(() => firstChild.getStaticPropertyOrThrow(p => p.getName() === "prop9")).to.throw();
});
});
describe("getStaticProperties", () => {
describe("no static properties", () => {
it("should not have any properties", () => {
const { firstChild } = testHelpers_1.getInfoFromText("class Identifier {\n}\n");
chai_1.expect(firstChild.getStaticProperties().length).to.equal(0);
});
});
describe("has static properties", () => {
const code = "class Identifier {\nconstructor(public p: string) {}\nstatic prop2: string;\nstatic method() {}\nprop: string;\nprop2: number;method1() {}\n" +
"\nstatic get prop(): string { return ''; }\nstatic set prop(val: string) {}\n}";
const { firstChild } = testHelpers_1.getInfoFromText(code);
it("should get the right number of static properties", () => {
chai_1.expect(firstChild.getStaticProperties().length).to.equal(3);
});
it("should get a property of the right instance of", () => {
chai_1.expect(firstChild.getStaticProperties()[0]).to.be.instanceOf(compiler_1.PropertyDeclaration);
});
it("should get a property of the right instance of for the get accessor", () => {
chai_1.expect(firstChild.getStaticProperties()[1]).to.be.instanceOf(compiler_1.GetAccessorDeclaration);
});
it("should get a property of the right instance of for the set accessor", () => {
chai_1.expect(firstChild.getStaticProperties()[2]).to.be.instanceOf(compiler_1.SetAccessorDeclaration);
});
});
});
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("class c {\n}", 0, [{ name: "myMethod" }], "class c {\n myMethod() {\n }\n}");
});
it("should insert multiple into other methods", () => {
doTest("class c {\n m1() {\n }\n\n m4() {\n }\n}", 1, [{ isStatic: true, name: "m2", returnType: "string" }, { name: "m3" }], "class c {\n m1() {\n }\n\n static m2(): string {\n }\n\n m3() {\n }\n\n m4() {\n }\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.MethodDeclaration);
}
it("should insert", () => {
doTest("class c {\n m1() {\n }\n\n m3() {\n }\n}", 1, { name: "m2" }, "class c {\n m1() {\n }\n\n m2() {\n }\n\n m3() {\n }\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", () => {
doTest("class c {\n m1() {\n }\n}", [{ name: "m2" }, { name: "m3" }], "class c {\n m1() {\n }\n\n m2() {\n }\n\n m3() {\n }\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.MethodDeclaration);
}
it("should insert", () => {
doTest("class c {\n m1() {\n }\n}", { name: "m2" }, "class c {\n m1() {\n }\n\n m2() {\n }\n}");
});
});
describe("getInstanceMethods", () => {
describe("no methods", () => {
it("should not have any methods", () => {
const { firstChild } = testHelpers_1.getInfoFromText("class Identifier {\n}\n");
chai_1.expect(firstChild.getInstanceMethods().length).to.equal(0);
});
});
describe("has methods", () => {
const { firstChild } = testHelpers_1.getInfoFromText(`class Identifier {
static prop2: string;
static method() {}
prop: string;
method1() {}
method2() {}
abstract method3(): void;
}\n`);
it("should get the right number of methods", () => {
chai_1.expect(firstChild.getInstanceMethods().length).to.equal(3);
});
it("should get a method of the right instance of", () => {
chai_1.expect(firstChild.getInstanceMethods()[0]).to.be.instanceOf(compiler_1.MethodDeclaration);
});
});
});
describe("getInstanceMethod", () => {
const code = "class Identifier {\nstatic prop2: string;\nstatic method() {}\n" +
"constructor(param: string, public param2: string, readonly param3: string) {}\n" +
"instanceProp: string;\nprop2: number;method() {}\n" +
"get prop(): string {return '';}\nset prop(val: string) {}\n}\n";
const { firstChild } = testHelpers_1.getInfoFromText(code);
it("should get a method by name", () => {
const method = firstChild.getInstanceMethod("method");
chai_1.expect(method.getName()).to.equal("method");
chai_1.expect(method.isStatic()).to.equal(false);
});
it("should get a method by function", () => {
const method = firstChild.getInstanceMethod(m => m.getName() === "method");
chai_1.expect(method.getName()).to.equal("method");
chai_1.expect(method.isStatic()).to.equal(false);
});
});
describe("getInstanceMethodOrThrow", () => {
const code = "class Identifier {\nstatic prop2: string;\nstatic method() {}\n" +
"constructor(param: string, public param2: string, readonly param3: string) {}\n" +
"instanceProp: string;\nprop2: number;method() {}\n" +
"get prop(): string {return '';}\nset prop(val: string) {}\n}\n";
const { firstChild } = testHelpers_1.getInfoFromText(code);
it("should get a method by name", () => {
const method = firstChild.getInstanceMethodOrThrow("method");
chai_1.expect(method.getName()).to.equal("method");
chai_1.expect(method.isStatic()).to.equal(false);
});
it("should get a method by function", () => {
const method = firstChild.getInstanceMethodOrThrow(m => m.getName() === "method");
chai_1.expect(method.getName()).to.equal("method");
chai_1.expect(method.isStatic()).to.equal(false);
});
it("should throw when not found", () => {
chai_1.expect(() => firstChild.getInstanceMethodOrThrow(m => m.getName() === "method9")).to.throw();
});
});
describe("getStaticMethods", () => {
describe("no static methods", () => {
it("should not have any static methods", () => {
const { firstChild } = testHelpers_1.getInfoFromText("class Identifier {\n}\n");
chai_1.expect(firstChild.getStaticMethods().length).to.equal(0);
});
});
describe("has static methods", () => {
const { firstChild } = testHelpers_1.getInfoFromText("class Identifier {\nstatic prop2: string;\nstatic method() {}\nprop: string;\nmethod1() {}\nmethod2() {}\n}\n");
it("should get the right number of static methods", () => {
chai_1.expect(firstChild.getStaticMethods().length).to.equal(1);
});
it("should get a method of the right instance of", () => {
chai_1.expect(firstChild.getStaticMethods()[0]).to.be.instanceOf(compiler_1.MethodDeclaration);
});
});
});
describe("getStaticMethod", () => {
const code = "class Identifier {\nstatic prop2: string;\nstatic method() {}\n" +
"constructor(param: string, public param2: string, readonly param3: string) {}\n" +
"instanceProp: string;\nprop2: number;method() {}\n" +
"get prop(): string {return '';}\nset prop(val: string) {}\n}\n";
const { firstChild } = testHelpers_1.getInfoFromText(code);
it("should get a method by name", () => {
const method = firstChild.getStaticMethod("method");
chai_1.expect(method.getName()).to.equal("method");
chai_1.expect(method.isStatic()).to.equal(true);
});
it("should get a property by function", () => {
const method = firstChild.getStaticMethod(m => m.getName() === "method");
chai_1.expect(method.getName()).to.equal("method");
chai_1.expect(method.isStatic()).to.equal(true);
});
});
describe("getStaticMethodOrThrow", () => {
const code = "class Identifier {\nstatic prop2: string;\nstatic method() {}\n" +
"constructor(param: string, public param2: string, readonly param3: string) {}\n" +
"instanceProp: string;\nprop2: number;method() {}\n" +
"get prop(): string {return '';}\nset prop(val: string) {}\n}\n";
const { firstChild } = testHelpers_1.getInfoFromText(code);
it("should get a method by name", () => {
const method = firstChild.getStaticMethodOrThrow("method");
chai_1.expect(method.getName()).to.equal("method");
chai_1.expect(method.isStatic()).to.equal(true);
});
it("should get a property by function", () => {
const method = firstChild.getStaticMethodOrThrow(m => m.getName() === "method");
chai_1.expect(method.getName()).to.equal("method");
chai_1.expect(method.isStatic()).to.equal(true);
});
it("should throw when not found", () => {
chai_1.expect(() => firstChild.getStaticMethodOrThrow(m => m.getName() === "method9")).to.throw();
});
});
describe("getInstanceMembers", () => {
const { firstChild } = testHelpers_1.getInfoFromText("class Identifier {\nconstructor(public p: string) {}\nstatic prop2: string;\nstatic method() {}\nprop: string;\n" +
"prop2: number;method1() {}\n}\n");
it("should get the right number of instance members", () => {
chai_1.expect(firstChild.getInstanceMembers().length).to.equal(4);
});
});
describe("getInstanceMember", () => {
const code = "class Identifier {\nstatic prop2: string;\nstatic method() {}\n" +
"constructor(param: string, public param2: string, readonly param3: string) {}\n" +
"instanceProp: string;\nprop2: number;method() {}\n" +
"get prop(): string {return '';}\nset prop(val: string) {}\n}\n";
const { firstChild } = testHelpers_1.getInfoFromText(code);
it("should get a method by name", () => {
const method = firstChild.getInstanceMember("method");
chai_1.expect(method.getName()).to.equal("method");
chai_1.expect(method.isStatic()).to.equal(false);
});
it("should get a method by function", () => {
const method = firstChild.getInstanceMember(m => m.getName() === "method");
chai_1.expect(method.getName()).to.equal("method");
chai_1.expect(method.isStatic()).to.equal(false);
});
});
describe("getInstanceMemberOrThrow", () => {
const code = "class Identifier {\nstatic prop2: string;\nstatic method() {}\n" +
"constructor(param: string, public param2: string, readonly param3: string) {}\n" +
"instanceProp: string;\nprop2: number;method() {}\n" +
"get prop(): string {return '';}\nset prop(val: string) {}\n}\n";
const { firstChild } = testHelpers_1.getInfoFromText(code);
it("should get a method by name", () => {
const method = firstChild.getInstanceMemberOrThrow("method");
chai_1.expect(method.getName()).to.equal("method");
chai_1.expect(method.isStatic()).to.equal(false);
});
it("should get a method by function", () => {
const method = firstChild.getInstanceMemberOrThrow(m => m.getName() === "method");
chai_1.expect(method.getName()).to.equal("method");
chai_1.expect(method.isStatic()).to.equal(false);
});
it("should throw when not found", () => {
chai_1.expect(() => firstChild.getInstanceMemberOrThrow(m => m.getName() === "method9")).to.throw();
});
});
describe("getStaticMembers", () => {
const { firstChild } = testHelpers_1.getInfoFromText("class Identifier {\nconstructor(public p: string) {}\nstatic prop2: string;\nstatic method() {}\nprop: string;\n" +
"prop2: number;method1() {}\n}\n");
it("should get the right number of static members", () => {
chai_1.expect(firstChild.getStaticMembers().length).to.equal(2);
});
});
describe("getStaticMember", () => {
const code = "class Identifier {\nstatic prop2: string;\nstatic method() {}\n" +
"constructor(param: string, public param2: string, readonly param3: string) {}\n" +
"instanceProp: string;\nprop2: number;method() {}\n" +
"get prop(): string {return '';}\nset prop(val: string) {}\n}\n";
const { firstChild } = testHelpers_1.getInfoFromText(code);
it("should get a method by name", () => {
const method = firstChild.getStaticMember("method");
chai_1.expect(method.getName()).to.equal("method");
chai_1.expect(method.isStatic()).to.equal(true);
});
it("should get a property by function", () => {
const method = firstChild.getStaticMember(m => m.getName() === "method");
chai_1.expect(method.getName()).to.equal("method");
chai_1.expect(method.isStatic()).to.equal(true);
});
});
describe("getStaticMemberOrThrow", () => {
const code = "class Identifier {\nstatic prop2: string;\nstatic method() {}\n" +
"constructor(param: string, public param2: string, readonly param3: string) {}\n" +
"instanceProp: string;\nprop2: number;method() {}\n" +
"get prop(): string {return '';}\nset prop(val: string) {}\n}\n";
const { firstChild } = testHelpers_1.getInfoFromText(code);
it("should get a method by name", () => {
const method = firstChild.getStaticMemberOrThrow("method");
chai_1.expect(method.getName()).to.equal("method");
chai_1.expect(method.isStatic()).to.equal(true);
});
it("should get a property by function", () => {
const method = firstChild.getStaticMemberOrThrow(m => m.getName() === "method");
chai_1.expect(method.getName()).to.equal("method");
chai_1.expect(method.isStatic()).to.equal(true);
});
it("should throw when not found", () => {
chai_1.expect(() => firstChild.getStaticMemberOrThrow(m => m.getName() === "method9")).to.throw();
});
});
describe("getAllMembers", () => {
it("should get the right number of instance, static, and constructor members in a non-ambient context", () => {
const code = "class Identifier {\nconstructor();constructor() {}\nstatic prop2: string;\nstatic method();static method() {}\nabstract abstractMethod(): void;\n" +
"prop: string;\nprop2: number;method1(str);method1() {}\n}\n";
const { firstChild } = testHelpers_1.getInfoFromText(code);
chai_1.expect(firstChild.getAllMembers().length).to.equal(7);
});
it("should get the right number of instance, static, and constructor members in an ambient context", () => {
const code = "declare class Identifier {\nconstructor();constructor();\nstatic prop2: string;\nstatic method();static method();\n" +
"prop: string;\nprop2: number;method1(str);method1();\n}\n";
const { firstChild } = testHelpers_1.getInfoFromText(code);
chai_1.expect(firstChild.getAllMembers().length).to.equal(9);
});
});
describe("inserting members", () => {
it("should insert methods and properties in the correct location when constructor parameters and overload signatures exist", () => {
const { firstChild, sourceFile } = testHelpers_1.getInfoFromText(`
class c {
constructor();
constructor(public param: string) {}
myMethod(): void;
myMethod(): void {
}
}
`);
firstChild.insertProperty(1, { name: "prop2" });
firstChild.insertMethod(1, { name: "method" });
firstChild.insertProperty(0, { name: "prop1" });
chai_1.expect(sourceFile.getFullText()).to.equal(`
class c {
prop1;
constructor();
constructor(public param: string) {}
method() {
}
prop2;
myMethod(): void;
myMethod(): void {
}
}
`);
});
it("should insert a constructor in the correct location when constructor parameters and overload signatures exist", () => {
const { firstChild, sourceFile } = testHelpers_1.getInfoFromText(`
class c {
constructor();
constructor(public param: string) {}
myMethod(): void;
myMethod(): void {
}
}
`);
firstChild.insertConstructor(1, {}); // todo: should this be 2? A little iffy because it removes the previous constructor before inserting
chai_1.expect(sourceFile.getFullText()).to.equal(`
class c {
myMethod(): void;
myMethod(): void {
}
constructor() {
}
}
`);
});
});
describe("getDerivedClasses", () => {
function doTest(text, className, expectedNames) {
const { sourceFile } = testHelpers_1.getInfoFromText(text);
const classes = sourceFile.getClass(className).getDerivedClasses();
chai_1.expect(classes.map(c => c.getName())).to.deep.equal(expectedNames);
}
it("should get the class descendants", () => {
doTest("class Base {} class Child1 extends Base {} class Child2 extends Base {} class Grandchild1 extends Child1 {} class GreatGrandChild1 extends Grandchild1 {}", "Base", ["Child1", "Child2", "Grandchild1", "GreatGrandChild1"]);
});
it("should not blow up for a circular references", () => {
doTest("class Base extends GreatGrandChild1 {} class Child1 extends Base {} class Child2 extends Base {} class Grandchild1 extends Child1 {} " +
"class GreatGrandChild1 extends Grandchild1 {}", "Base", ["Child1", "Child2", "Grandchild1", "GreatGrandChild1"]);
});
it("should get the class descendants when there are none", () => {
doTest("class Base {} class Child1 extends Base {} class Child2 extends Base {} class Grandchild1 extends Child1 {}", "Grandchild1", []);
});
});
describe("remove", () => {
function doTest(text, index, expectedText) {
const { sourceFile } = testHelpers_1.getInfoFromText(text);
sourceFile.getClasses()[index].remove();
chai_1.expect(sourceFile.getFullText()).to.equal(expectedText);
}
it("should remove the class declaration", () => {
doTest("class I {}\n\nclass J {}\n\nclass K {}", 1, "class I {}\n\nclass K {}");
});
});
});
//# sourceMappingURL=classDeclarationTests.js.map