ts-simple-ast
Version:
TypeScript compiler wrapper for AST navigation and code generation.
204 lines (202 loc) • 9.81 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const chai_1 = require("chai");
const compiler = require("./../../compiler");
const testHelpers_1 = require("./../compiler/testHelpers");
const getMixinStructureFuncs = require("./../../manipulation/getMixinStructureFunctions");
describe("fromAbstractableNode", () => {
function doTest(startingCode, expectedStructure) {
const { firstChild } = testHelpers_1.getInfoFromText(startingCode);
chai_1.expect(getMixinStructureFuncs.fromAbstractableNode(firstChild)).to.deep.equal(expectedStructure);
}
it("should get when abstract", () => {
doTest("abstract class MyClass {}", { isAbstract: true });
});
it("should get when not abstract", () => {
doTest("class MyClass {}", { isAbstract: false });
});
});
describe("fromAmbientableNode", () => {
function doTest(startingCode, expectedStructure) {
const { firstChild } = testHelpers_1.getInfoFromText(startingCode);
chai_1.expect(getMixinStructureFuncs.fromAmbientableNode(firstChild)).to.deep.equal(expectedStructure);
}
it("should get when ambient", () => {
doTest("declare class MyClass {}", { hasDeclareKeyword: true });
});
it("should get when not ambient", () => {
doTest("class MyClass {}", { hasDeclareKeyword: false });
});
});
describe("fromAsyncableNode", () => {
function doTest(startingCode, expectedStructure) {
const { firstChild } = testHelpers_1.getInfoFromText(startingCode);
chai_1.expect(getMixinStructureFuncs.fromAsyncableNode(firstChild)).to.deep.equal(expectedStructure);
}
it("should get when async", () => {
doTest("async function identifier() {}", { isAsync: true });
});
it("should get when not async", () => {
doTest("function identifier() {}", { isAsync: false });
});
});
describe("fromExportableNode", () => {
function doTest(startingCode, expectedStructure) {
const { firstChild } = testHelpers_1.getInfoFromText(startingCode);
chai_1.expect(getMixinStructureFuncs.fromExportableNode(firstChild)).to.deep.equal(expectedStructure);
}
it("should get when default export", () => {
doTest("export default function identifier() {}", { isDefaultExport: true, isExported: true });
});
it("should get as not default export if default exported on a different statement", () => {
doTest("function identifier() {}\nexport default identifier;", { isDefaultExport: false, isExported: false });
});
it("should get when an export", () => {
doTest("export function identifier() {}", { isDefaultExport: false, isExported: true });
});
it("should get when not exported", () => {
doTest("function identifier() {}", { isDefaultExport: false, isExported: false });
});
});
describe("fromGeneratorableNode", () => {
function doTest(startingCode, expectedStructure) {
const { firstChild } = testHelpers_1.getInfoFromText(startingCode);
chai_1.expect(getMixinStructureFuncs.fromGeneratorableNode(firstChild)).to.deep.equal(expectedStructure);
}
it("should get when is a generator", () => {
doTest("function* identifier() {}", { isGenerator: true });
});
it("should get when not a generator", () => {
doTest("function identifier() {}", { isGenerator: false });
});
});
describe("fromReturnTypedNode", () => {
function doTest(startingCode, expectedStructure) {
const { firstChild } = testHelpers_1.getInfoFromText(startingCode);
chai_1.expect(getMixinStructureFuncs.fromReturnTypedNode(firstChild)).to.deep.equal(expectedStructure);
}
it("should get when has a return type", () => {
doTest("function identifier(): string {}", { returnType: "string" });
});
it("should get when not has a return type", () => {
doTest("function identifier() {}", { returnType: undefined });
});
});
describe("fromStaticableNode", () => {
function doTest(startingCode, expectedStructure) {
const { firstChild } = testHelpers_1.getInfoFromText(startingCode);
chai_1.expect(getMixinStructureFuncs.fromStaticableNode(firstChild.getAllMembers()[0])).to.deep.equal(expectedStructure);
}
it("should get when is static", () => {
doTest("class Identifier { static method() {} }", { isStatic: true });
});
it("should get when not static", () => {
doTest("class Identifier { method() {} }", { isStatic: false });
});
});
describe("fromScopedNode", () => {
function doTest(startingCode, expectedStructure) {
const { firstChild } = testHelpers_1.getInfoFromText(startingCode);
chai_1.expect(getMixinStructureFuncs.fromScopedNode(firstChild.getAllMembers()[0])).to.deep.equal(expectedStructure);
}
it("should get when has scope", () => {
doTest("class Identifier { private method() {} }", { scope: compiler.Scope.Private });
});
it("should get when scope is not defined", () => {
doTest("class Identifier { method() {} }", { scope: undefined });
});
});
describe("fromScopeableNode", () => {
function doTest(startingCode, expectedStructure) {
const { firstChild } = testHelpers_1.getInfoFromText(startingCode);
chai_1.expect(getMixinStructureFuncs.fromScopeableNode(firstChild.getConstructors()[0].getParameters()[0])).to.deep.equal(expectedStructure);
}
it("should get when has scope", () => {
doTest("class Identifier { constructor(private param: string) {} }", { scope: compiler.Scope.Private });
});
it("should get when scope is not defined", () => {
doTest("class Identifier { constructor(param: string) {} }", { scope: undefined });
});
});
describe("fromExtendsClauseableNode", () => {
function doTest(startingCode, expectedStructure) {
const { firstChild } = testHelpers_1.getInfoFromText(startingCode);
chai_1.expect(getMixinStructureFuncs.fromExtendsClauseableNode(firstChild)).to.deep.equal(expectedStructure);
}
it("should get no extends", () => {
doTest("interface Identifier {}", { extends: undefined });
});
it("should get when has extends", () => {
doTest("interface Identifier extends First, Second<Third> { }", { extends: ["First", "Second<Third>"] });
});
});
describe("fromImplementsClauseableNode", () => {
function doTest(startingCode, expectedStructure) {
const { firstChild } = testHelpers_1.getInfoFromText(startingCode);
chai_1.expect(getMixinStructureFuncs.fromImplementsClauseableNode(firstChild)).to.deep.equal(expectedStructure);
}
it("should get no implements", () => {
doTest("class Identifier {}", { implements: undefined });
});
it("should get when has implements", () => {
doTest("class Identifier implements First, Second<Third> { }", { implements: ["First", "Second<Third>"] });
});
});
describe("fromQuestionTokenableNode", () => {
function doTest(startingCode, expectedStructure) {
const { firstChild } = testHelpers_1.getInfoFromText(startingCode);
chai_1.expect(getMixinStructureFuncs.fromQuestionTokenableNode(firstChild.getAllMembers()[0])).to.deep.equal(expectedStructure);
}
it("should get when has question token", () => {
doTest("class Identifier { prop?: string; }", { hasQuestionToken: true });
});
it("should get when not has question token", () => {
doTest("class Identifier { prop: string; }", { hasQuestionToken: false });
});
});
describe("fromReadonlyableNode", () => {
function doTest(startingCode, expectedStructure) {
const { firstChild } = testHelpers_1.getInfoFromText(startingCode);
chai_1.expect(getMixinStructureFuncs.fromReadonlyableNode(firstChild.getAllMembers()[0])).to.deep.equal(expectedStructure);
}
it("should get when is readonly", () => {
doTest("class Identifier { readonly prop: string; }", { isReadonly: true });
});
it("should get when not readonly", () => {
doTest("class Identifier { prop: string; }", { isReadonly: false });
});
});
describe("fromTypedNode", () => {
function doTest(startingCode, expectedStructure) {
const { firstChild } = testHelpers_1.getInfoFromText(startingCode);
chai_1.expect(getMixinStructureFuncs.fromTypedNode(firstChild.getAllMembers()[0])).to.deep.equal(expectedStructure);
}
it("should get when has a type", () => {
doTest("class Identifier { prop: string; }", { type: "string" });
});
it("should get when not has a type", () => {
doTest("class Identifier { prop; }", { type: undefined });
});
});
describe("fromInitializerExpressionableNode", () => {
function doTest(startingCode, expectedStructure) {
const { firstChild } = testHelpers_1.getInfoFromText(startingCode);
chai_1.expect(getMixinStructureFuncs.fromInitializerExpressionableNode(firstChild.getAllMembers()[0])).to.deep.equal(expectedStructure);
}
it("should get when has a an initailizer", () => {
doTest("class Identifier { prop = 'some value'; }", { initializer: "'some value'" });
});
it("should get when not has an initailizer", () => {
doTest("class Identifier { prop; }", { initializer: undefined });
});
});
describe("fromNamedNode", () => {
function doTest(startingCode, expectedStructure) {
const { firstChild } = testHelpers_1.getInfoFromText(startingCode);
chai_1.expect(getMixinStructureFuncs.fromNamedNode(firstChild)).to.deep.equal(expectedStructure);
}
it("should get the name", () => {
doTest("class Identifier { }", { name: "Identifier" });
});
});
//# sourceMappingURL=getMixinStructureFunctionsTests.js.map