UNPKG

ts-simple-ast

Version:

TypeScript compiler wrapper for AST navigation and code generation.

98 lines (96 loc) 5.59 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const chai_1 = require("chai"); const compiler_1 = require("./../../../../compiler"); const testHelpers_1 = require("./../../testHelpers"); describe("StatementedNode", () => { 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