ts-simple-ast
Version:
TypeScript compiler wrapper for AST navigation and code generation.
174 lines (172 loc) • 10.5 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const chai_1 = require("chai");
const compiler_1 = require("./../../../../compiler");
const testHelpers_1 = require("./../../testHelpers");
describe("StatementedNode", () => {
describe("insertVariableStatements", () => {
function doTest(startCode, index, structures, expectedText) {
const { sourceFile } = testHelpers_1.getInfoFromText(startCode);
const result = sourceFile.insertVariableStatements(index, structures);
chai_1.expect(sourceFile.getFullText()).to.equal(expectedText);
chai_1.expect(result.length).to.equal(structures.length);
}
it("should insert to an empty file", () => {
doTest("", 0, [{
isExported: true,
declarationType: compiler_1.VariableDeclarationType.Var,
declarations: [{
name: "Identifier",
initializer: `'test'`
}, {
name: "Identifier2",
type: "number",
initializer: "5"
}]
}], "export var Identifier = 'test', Identifier2: number = 5;\n");
});
it("should insert at the start with two new lines for a non-variable statement after", () => {
doTest("namespace Identifier2 {\n}\n", 0, [{ declarations: [{ name: "Identifier1" }] }], "let Identifier1;\n\nnamespace Identifier2 {\n}\n");
});
it("should insert at the start with one new line for a variable statement after", () => {
doTest("let Identifier2: string;\n", 0, [{ declarations: [{ name: "Identifier1", type: "string" }] }], "let Identifier1: string;\nlet Identifier2: string;\n");
});
it("should insert at the end of a file with two new lines for a non-variable statement before", () => {
doTest("namespace Identifier1 {\n}\n", 1, [{ declarations: [{ name: "Identifier2" }] }], "namespace Identifier1 {\n}\n\nlet Identifier2;\n");
});
it("should insert in the middle of children", () => {
doTest("namespace Identifier1 {\n}\n\nnamespace Identifier3 {\n}\n", 1, [{ declarations: [{ name: "Identifier2" }] }], "namespace Identifier1 {\n}\n\nlet Identifier2;\n\nnamespace Identifier3 {\n}\n");
});
it("should insert multiple", () => {
doTest("namespace Identifier1 {\n}\n", 1, [{ declarations: [{ name: "Identifier2" }] }, { declarations: [{ name: "Identifier3" }] }], "namespace Identifier1 {\n}\n\nlet Identifier2;\nlet Identifier3;\n");
});
it("should have the expected text adding to non-source file", () => {
const { sourceFile } = testHelpers_1.getInfoFromText("namespace Identifier {\n}\n");
const namespaceDec = sourceFile.getNamespaces()[0];
namespaceDec.insertVariableStatements(0, [{ declarations: [{ name: "Identifier" }] }]);
chai_1.expect(sourceFile.getFullText()).to.equal("namespace Identifier {\n let Identifier;\n}\n");
});
});
describe("insertVariableStatement", () => {
function doTest(startCode, index, structure, expectedText) {
const { sourceFile } = testHelpers_1.getInfoFromText(startCode);
const result = sourceFile.insertVariableStatement(index, structure);
chai_1.expect(sourceFile.getFullText()).to.equal(expectedText);
chai_1.expect(result).to.be.instanceOf(compiler_1.VariableStatement);
}
it("should insert", () => {
doTest("namespace Identifier2 {\n}\n", 0, { declarations: [{ name: "Identifier1" }] }, "let Identifier1;\n\nnamespace Identifier2 {\n}\n");
});
});
describe("addVariableStatements", () => {
function doTest(startCode, structures, expectedText) {
const { sourceFile } = testHelpers_1.getInfoFromText(startCode);
const result = sourceFile.addVariableStatements(structures);
chai_1.expect(sourceFile.getFullText()).to.equal(expectedText);
chai_1.expect(result.length).to.equal(structures.length);
}
it("should add multiple", () => {
doTest("namespace Identifier1 {\n}\n", [{ declarations: [{ name: "Identifier2" }] }, { declarations: [{ name: "Identifier3" }] }], "namespace Identifier1 {\n}\n\nlet Identifier2;\nlet Identifier3;\n");
});
});
describe("addVariableStatement", () => {
function doTest(startCode, structure, expectedText) {
const { sourceFile } = testHelpers_1.getInfoFromText(startCode);
const result = sourceFile.addVariableStatement(structure);
chai_1.expect(sourceFile.getFullText()).to.equal(expectedText);
chai_1.expect(result).to.be.instanceOf(compiler_1.VariableStatement);
}
it("should add one", () => {
doTest("namespace Identifier1 {\n}\n", { declarations: [{ name: "Identifier2" }] }, "namespace Identifier1 {\n}\n\nlet Identifier2;\n");
});
});
const { sourceFile: variablesSourceFile } = testHelpers_1.getInfoFromText("var Identifier1;\nvar Identifier2, Identifier3;");
describe("getVariableStatements", () => {
const statements = variablesSourceFile.getVariableStatements();
it("should have the expected number of statements", () => {
chai_1.expect(statements.length).to.equal(2);
});
it("should have correct type", () => {
chai_1.expect(statements[0]).to.be.instanceOf(compiler_1.VariableStatement);
});
});
describe("getVariableStatement", () => {
it("should get a variable statement when something matches", () => {
const statement = variablesSourceFile.getVariableStatement(s => s.getDeclarationList().getDeclarations().length === 2);
chai_1.expect(statement.getDeclarationList().getDeclarations()[0].getName()).to.equal("Identifier2");
});
it("should return undefined when nothing matches", () => {
const statement = variablesSourceFile.getVariableStatement(s => s.getDeclarationList().getDeclarations().length === 5);
chai_1.expect(statement).to.be.undefined;
});
});
describe("getVariableStatementOrThrow", () => {
it("should get a variable statement when something matches", () => {
const statement = variablesSourceFile.getVariableStatementOrThrow(s => s.getDeclarationList().getDeclarations().length === 2);
chai_1.expect(statement.getDeclarationList().getDeclarations()[0].getName()).to.equal("Identifier2");
});
it("should throw when nothing matches", () => {
chai_1.expect(() => variablesSourceFile.getVariableStatementOrThrow(s => s.getDeclarationList().getDeclarations().length === 5)).to.throw();
});
});
describe("getVariableDeclarationLists", () => {
const declarationLists = variablesSourceFile.getVariableDeclarationLists();
it("should have the expected number of variable declaration lists", () => {
chai_1.expect(declarationLists.length).to.equal(2);
});
it("should have correct type", () => {
chai_1.expect(declarationLists[0]).to.be.instanceOf(compiler_1.VariableDeclarationList);
});
});
describe("getVariableDeclarationList", () => {
it("should get a variable declaration list when something matches", () => {
const list = variablesSourceFile.getVariableDeclarationList(s => s.getDeclarations().length === 2);
chai_1.expect(list.getDeclarations()[0].getName()).to.equal("Identifier2");
});
it("should return undefined when nothing matches", () => {
const list = variablesSourceFile.getVariableDeclarationList(s => s.getDeclarations().length === 5);
chai_1.expect(list).to.be.undefined;
});
});
describe("getVariableDeclarationListOrThrow", () => {
it("should get a variable declaration list when something matches", () => {
const list = variablesSourceFile.getVariableDeclarationListOrThrow(s => s.getDeclarations().length === 2);
chai_1.expect(list.getDeclarations()[0].getName()).to.equal("Identifier2");
});
it("should throw when nothing matches", () => {
chai_1.expect(() => variablesSourceFile.getVariableDeclarationListOrThrow(s => s.getDeclarations().length === 5)).to.throw();
});
});
describe("getVariableDeclarations", () => {
const declarations = variablesSourceFile.getVariableDeclarations();
it("should have the expected number of variable declarations", () => {
chai_1.expect(declarations.length).to.equal(3);
});
it("should have correct type", () => {
chai_1.expect(declarations[0]).to.be.instanceOf(compiler_1.VariableDeclaration);
});
});
describe("getVariableDeclaration", () => {
it("should get a variable declaration by a name", () => {
chai_1.expect(variablesSourceFile.getVariableDeclaration("Identifier2").getName()).to.equal("Identifier2");
});
it("should get a variable declaration by a search function", () => {
chai_1.expect(variablesSourceFile.getVariableDeclaration(c => c.getName() === "Identifier1").getName()).to.equal("Identifier1");
});
it("should return undefined when the variable declaration doesn't exist", () => {
chai_1.expect(variablesSourceFile.getVariableDeclaration("asdf")).to.be.undefined;
});
});
describe("getVariableDeclarationOrThrow", () => {
it("should get a variable declaration by a name", () => {
chai_1.expect(variablesSourceFile.getVariableDeclarationOrThrow("Identifier2").getName()).to.equal("Identifier2");
});
it("should get a variableOrThrow declaration by a earch function", () => {
chai_1.expect(variablesSourceFile.getVariableDeclarationOrThrow(c => c.getName() === "Identifier1").getName()).to.equal("Identifier1");
});
it("should return undefined OrThrowwhen the variable declaration doesn't exist", () => {
chai_1.expect(() => variablesSourceFile.getVariableDeclarationOrThrow("asdf")).to.throw();
});
});
});
//# sourceMappingURL=variableTests.js.map