ts-simple-ast
Version:
TypeScript compiler wrapper for AST navigation and code generation.
109 lines (107 loc) • 5.86 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const chai_1 = require("chai");
const compiler_1 = require("./../../../compiler");
const testHelpers_1 = require("./../testHelpers");
describe("OverloadableNode", () => {
const functionCode = `function myFunction();function myFunction() {}`;
const { sourceFile: functionSourceFile } = testHelpers_1.getInfoFromText(functionCode);
const functions = functionSourceFile.getChildSyntaxListOrThrow().getChildren();
const constructorCode = `class MyClass { constructor();constructor();constructor() {} myMethod(): void;myMethod() {} }`;
const { firstChild: classChild } = testHelpers_1.getInfoFromText(constructorCode);
const constructors = classChild.getChildSyntaxListOrThrow().getChildren().filter(c => c instanceof compiler_1.ConstructorDeclaration);
describe("isImplementation", () => {
it("should not be an implementation when not one", () => {
chai_1.expect(functions[0].isImplementation()).to.be.false;
});
it("should be an implementation when is one", () => {
chai_1.expect(functions[1].isImplementation()).to.be.true;
});
});
describe("isOverload", () => {
it("should be an overload when is on", () => {
chai_1.expect(functions[0].isOverload()).to.be.true;
});
it("should not be an overload when not one", () => {
chai_1.expect(functions[1].isOverload()).to.be.false;
});
});
describe("getOverloads", () => {
describe("functions", () => {
it("should have the right number of overloads when asking an overload", () => {
const overloads = functions[0].getOverloads();
chai_1.expect(functions[0].isOverload()).to.be.true;
chai_1.expect(overloads.length).to.equal(1);
chai_1.expect(overloads[0]).to.equal(functions[0]);
});
it("should have the right number of overloads when asking an implementation", () => {
const overloads = functions[1].getOverloads();
chai_1.expect(functions[1].isImplementation()).to.be.true;
chai_1.expect(overloads.length).to.equal(1);
chai_1.expect(overloads[0]).to.equal(functions[0]);
});
});
describe("constructors", () => {
it("should have the right number of overloads when asking an overload", () => {
const overloads = constructors[0].getOverloads();
chai_1.expect(constructors[0].isOverload()).to.be.true;
chai_1.expect(overloads.length).to.equal(2);
chai_1.expect(overloads.map(o => o.isOverload())).to.deep.equal([true, true]);
});
it("should have the right number of overloads when asking an implementation", () => {
const overloads = constructors[2].getOverloads();
chai_1.expect(constructors[2].isImplementation()).to.be.true;
chai_1.expect(overloads.length).to.equal(2);
chai_1.expect(overloads.map(o => o.isOverload())).to.deep.equal([true, true]);
});
});
describe("ambient context", () => {
it("should return all the overloads in an ambient context", () => {
const code = `declare function myFunction(): void;declare function myFunction(): void;`;
const { firstChild } = testHelpers_1.getInfoFromText(code);
chai_1.expect(firstChild.getOverloads().length).to.equal(2);
});
});
});
describe("getImplementation", () => {
describe("functions", () => {
it("should get the implementation when asking an overload", () => {
const implementation = functions[0].getImplementation();
chai_1.expect(implementation).to.equal(functions[1]);
});
it("should have the right number of overloads when asking an implementation", () => {
const implementation = functions[1].getImplementation();
chai_1.expect(implementation).to.equal(functions[1]);
});
});
describe("constructors", () => {
it("should get the implementation when asking an overload", () => {
const implementation = constructors[0].getImplementation();
chai_1.expect(implementation).to.equal(constructors[2]);
});
it("should have the right number of overloads when asking an implementation", () => {
const implementation = constructors[2].getImplementation();
chai_1.expect(implementation).to.equal(constructors[2]);
});
});
describe("ambient context", () => {
it("should return undefined in an ambient context", () => {
const code = `declare function myFunction(): void;declare function myFunction(): void;`;
const { firstChild } = testHelpers_1.getInfoFromText(code);
chai_1.expect(firstChild.getImplementation()).to.be.undefined;
});
});
});
describe("getImplementationOrThrow", () => {
it("should get the implementation when asking an overload", () => {
const implementation = functions[0].getImplementationOrThrow();
chai_1.expect(implementation).to.equal(functions[1]);
});
it("should throw in an ambient context", () => {
const code = `declare function myFunction(): void;declare function myFunction(): void;`;
const { firstChild } = testHelpers_1.getInfoFromText(code);
chai_1.expect(() => firstChild.getImplementationOrThrow()).to.throw();
});
});
});
//# sourceMappingURL=overloadableNodeTests.js.map