UNPKG

ts-simple-ast

Version:

TypeScript compiler wrapper for AST navigation and code generation.

97 lines (95 loc) 5.16 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const chai_1 = require("chai"); const testHelpers_1 = require("./../testHelpers"); describe("ExtendsClauseableNode", () => { describe("getExtends", () => { function doTest(text, expectedLength) { const { firstChild } = testHelpers_1.getInfoFromText(text); const extendsExpressions = firstChild.getExtends(); chai_1.expect(extendsExpressions.length).to.equal(expectedLength); } it("should return an empty array when they don't exist", () => { doTest("export interface Identifier {}", 0); }); it("should get all the extends expressions when they exist", () => { doTest("export interface Identifier extends Base, Base2 {}", 2); }); }); describe("addExtends", () => { function doTest(code, extendsTextOrArray, expectedCode) { const { firstChild, sourceFile } = testHelpers_1.getInfoFromText(code); if (extendsTextOrArray instanceof Array) { const result = firstChild.addExtends(extendsTextOrArray); chai_1.expect(result.length).to.equal(extendsTextOrArray.length); } else { const result = firstChild.addExtends(extendsTextOrArray); 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 extends", () => { doTest(" interface Identifier {} ", "Base", " interface Identifier extends Base {} "); }); it("should add an extends when the brace is right beside the identifier", () => { doTest(" interface Identifier{} ", "Base", " interface Identifier extends Base {} "); }); it("should add an extends when an extends already exists", () => { doTest("interface Identifier extends Base1 {}", "Base2", "interface Identifier extends Base1, Base2 {}"); }); it("should add multiple extends", () => { doTest("interface Identifier {}", ["Base", "Base2"], "interface Identifier extends Base, Base2 {}"); }); it("should do nothing if an empty array", () => { doTest("interface Identifier {}", [], "interface Identifier {}"); }); it("should throw an error when providing invalid input", () => { const { firstChild, sourceFile } = testHelpers_1.getInfoFromText("interface Identifier extends Base1 {}"); chai_1.expect(() => firstChild.addExtends("")).to.throw(); chai_1.expect(() => firstChild.addExtends(" ")).to.throw(); chai_1.expect(() => firstChild.addExtends(5)).to.throw(); }); }); describe("insertExtends", () => { function doTest(code, index, extendsTextOrArray, expectedCode) { const { firstChild, sourceFile } = testHelpers_1.getInfoFromText(code); if (extendsTextOrArray instanceof Array) { const result = firstChild.insertExtends(index, extendsTextOrArray); chai_1.expect(result.length).to.equal(extendsTextOrArray.length); } else { const result = firstChild.insertExtends(index, extendsTextOrArray); 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 addExtends it("should insert an extends at a position", () => { doTest("interface Identifier extends Base, Base1 {}", 1, "Base2", "interface Identifier extends Base, Base2, Base1 {}"); }); it("should insert multiple extends at a position", () => { doTest("interface Identifier extends Base, Base1 {}", 1, ["Base2", "Base3"], "interface Identifier extends 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("interface MyClass {}", { extends: ["Test"] }, "interface MyClass extends Test {}"); }); it("should modify when setting two", () => { doTest("interface MyClass {}", { extends: ["Test", "Test2"] }, "interface MyClass extends Test, Test2 {}"); }); it("should not modify anything if the structure doesn't change anything", () => { doTest("interface MyClass {}", {}, "interface MyClass {}"); }); it("should not modify anything if the structure has an empty array", () => { doTest("interface MyClass {}", { extends: [] }, "interface MyClass {}"); }); }); }); //# sourceMappingURL=extendsClauseableNodeTests.js.map