UNPKG

ts-simple-ast

Version:

TypeScript compiler wrapper for AST navigation and code generation.

101 lines (99 loc) 5.66 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const chai_1 = require("chai"); const testHelpers_1 = require("./../testHelpers"); describe("ImplementsClauseableNode", () => { describe("getImplements", () => { it("should return an empty array when they don't exist", () => { const { firstChild } = testHelpers_1.getInfoFromText("class Identifier {}"); const implementsExpressions = firstChild.getImplements(); chai_1.expect(implementsExpressions.length).to.equal(0); }); it("should return an empty array when they don't exist, but an extends does", () => { const { firstChild } = testHelpers_1.getInfoFromText("class Identifier extends Base {}"); const implementsExpressions = firstChild.getImplements(); chai_1.expect(implementsExpressions.length).to.equal(0); }); it("should get all the implements expressions when they exist", () => { const { firstChild } = testHelpers_1.getInfoFromText("class Identifier extends Base implements IBase, IBase2 {}"); const implementsExpressions = firstChild.getImplements(); chai_1.expect(implementsExpressions.length).to.equal(2); }); }); describe("addImplements", () => { function doTest(code, implementsTextOrArray, expectedCode) { const { firstChild, sourceFile } = testHelpers_1.getInfoFromText(code); if (implementsTextOrArray instanceof Array) { const result = firstChild.addImplements(implementsTextOrArray); chai_1.expect(result.length).to.equal(implementsTextOrArray.length); } else { const result = firstChild.addImplements(implementsTextOrArray); chai_1.expect(result).to.not.be.instanceOf(Array).and.to.be.not.undefined; } chai_1.expect(sourceFile.getFullText()).to.equal(expectedCode); } it("should add an implements", () => { doTest(" class Identifier {} ", "Base", " class Identifier implements Base {} "); }); it("should add an implements when the brace is right beside the identifier", () => { doTest(" class Identifier{} ", "Base", " class Identifier implements Base {} "); }); it("should add an implements when an implements already exists", () => { doTest("class Identifier implements Base1 {}", "Base2", "class Identifier implements Base1, Base2 {}"); }); it("should add multiple implements", () => { doTest("class Identifier implements Base1 {}", ["Base2", "Base3"], "class Identifier implements Base1, Base2, Base3 {}"); }); it("should do nothing if an empty array", () => { doTest("class Identifier {}", [], "class Identifier {}"); }); it("should throw an error when providing invalid input", () => { const { firstChild, sourceFile } = testHelpers_1.getInfoFromText("class Identifier implements Base1 {}"); chai_1.expect(() => firstChild.addImplements("")).to.throw(); chai_1.expect(() => firstChild.addImplements(" ")).to.throw(); chai_1.expect(() => firstChild.addImplements(5)).to.throw(); }); }); describe("insertImplements", () => { function doTest(code, index, implementsTextOrArray, expectedCode) { const { firstChild, sourceFile } = testHelpers_1.getInfoFromText(code); if (implementsTextOrArray instanceof Array) { const result = firstChild.insertImplements(index, implementsTextOrArray); chai_1.expect(result.length).to.equal(implementsTextOrArray.length); } else { const result = firstChild.insertImplements(index, implementsTextOrArray); chai_1.expect(result).to.not.be.instanceOf(Array).and.to.be.not.undefined; } chai_1.expect(sourceFile.getFullText()).to.equal(expectedCode); } // mosts of the tests for this are already in addImplements it("should insert an implements at a position", () => { doTest("class Identifier implements Base, Base1 {}", 1, "Base2", "class Identifier implements Base, Base2, Base1 {}"); }); it("should insert multiple implements at a position", () => { doTest("class Identifier implements Base, Base1 {}", 1, ["Base2", "Base3"], "class Identifier implements Base, Base2, Base3, Base1 {}"); }); }); describe("fill", () => { function doTest(startingCode, structure, expectedCode) { const { firstChild, sourceFile } = testHelpers_1.getInfoFromText(startingCode); firstChild.fill(structure); chai_1.expect(firstChild.getText()).to.equal(expectedCode); } it("should modify when setting one", () => { doTest("class MyClass {}", { implements: ["Test"] }, "class MyClass implements Test {}"); }); it("should modify when setting two", () => { doTest("class MyClass {}", { implements: ["Test", "Test2"] }, "class MyClass implements Test, Test2 {}"); }); it("should not modify anything if the structure doesn't change anything", () => { doTest("class MyClass {}", {}, "class MyClass {}"); }); it("should not modify anything if the structure has an empty array", () => { doTest("class MyClass {}", { implements: [] }, "class MyClass {}"); }); }); }); //# sourceMappingURL=implementsClauseableNodeTests.js.map