UNPKG

ts-simple-ast

Version:

TypeScript compiler wrapper for AST navigation and code generation.

85 lines (83 loc) 4.48 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const chai_1 = require("chai"); const testHelpers_1 = require("./../testHelpers"); describe("GeneratorableNode", () => { const { sourceFile: mainSourceFile } = testHelpers_1.getInfoFromText("function* Identifier() {}\nfunction Identifier2() {}"); const generatorFunc = mainSourceFile.getFunctions()[0]; const func = mainSourceFile.getFunctions()[1]; describe("isGenerator", () => { it("should be a generator when so", () => { chai_1.expect(generatorFunc.isGenerator()).to.be.true; }); it("should not be generator when not so", () => { chai_1.expect(func.isGenerator()).to.be.false; }); }); describe("getAsteriskToken", () => { it("should have an asterisk token when a generator", () => { chai_1.expect(generatorFunc.getAsteriskToken().getText()).to.equal("*"); }); it("should not have a async keyword when not async", () => { chai_1.expect(func.getAsteriskToken()).to.be.undefined; }); }); describe("getAsteriskTokenOrThrow", () => { it("should have an asterisk token when a generator", () => { chai_1.expect(generatorFunc.getAsteriskTokenOrThrow().getText()).to.equal("*"); }); it("should not have a async keyword when not async", () => { chai_1.expect(() => func.getAsteriskTokenOrThrow()).to.throw(); }); }); describe("setIsGenerator", () => { describe("Functions", () => { it("should set as generator when not a generator", () => { const { firstChild, sourceFile } = testHelpers_1.getInfoFromText("function Identifier() {}"); firstChild.setIsGenerator(true); chai_1.expect(sourceFile.getText()).to.equal("function* Identifier() {}"); }); it("should set as not a generator when a generator", () => { const { firstChild, sourceFile } = testHelpers_1.getInfoFromText("function* Identifier() {}"); firstChild.setIsGenerator(false); chai_1.expect(sourceFile.getText()).to.equal("function Identifier() {}"); }); it("should not change the generator when already that value", () => { const { firstChild, sourceFile } = testHelpers_1.getInfoFromText("function* Identifier() {}"); firstChild.setIsGenerator(true); chai_1.expect(sourceFile.getText()).to.equal("function* Identifier() {}"); }); }); describe("Methods", () => { it("should set as generator when not a generator", () => { const { firstChild, sourceFile } = testHelpers_1.getInfoFromText("class Identifier { public identifier() { } }"); const method = firstChild.getInstanceMethods()[0]; method.setIsGenerator(true); chai_1.expect(sourceFile.getText()).to.equal("class Identifier { public *identifier() { } }"); }); it("should set as not a generator when a generator", () => { const { firstChild, sourceFile } = testHelpers_1.getInfoFromText("class Identifier { public *identifier() { } }"); const method = firstChild.getInstanceMethods()[0]; method.setIsGenerator(false); chai_1.expect(sourceFile.getText()).to.equal("class Identifier { public identifier() { } }"); }); }); }); describe("fill", () => { function doTest(startCode, structure, expectedCode) { const { firstChild, sourceFile } = testHelpers_1.getInfoFromText(startCode); firstChild.fill(structure); chai_1.expect(firstChild.getText()).to.equal(expectedCode); } it("should not modify anything if the structure doesn't change anything", () => { doTest("function myFunction() {}", {}, "function myFunction() {}"); }); it("should not modify anything if the structure doesn't change anything and the node has everything set", () => { doTest("function* myFunction() {}", {}, "function* myFunction() {}"); }); it("should modify when setting as async", () => { doTest("function myFunction() {}", { isGenerator: true }, "function* myFunction() {}"); }); }); }); //# sourceMappingURL=generatorableNodeTests.js.map