UNPKG

ts-simple-ast

Version:

TypeScript compiler wrapper for AST navigation and code generation.

110 lines (108 loc) 6.31 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const chai_1 = require("chai"); const errors = require("./../../../errors"); const testHelpers_1 = require("./../testHelpers"); describe("TextInsertableNode", () => { describe("replaceText", () => { describe("SourceFile", () => { function doTest(startCode, range, insertCode, expectedCode) { const { sourceFile } = testHelpers_1.getInfoFromText(startCode); sourceFile.replaceText(range, insertCode); chai_1.expect(sourceFile.getFullText()).to.equal(expectedCode); } function doWriterTest(startCode, range, insertCode, expectedCode) { const { sourceFile } = testHelpers_1.getInfoFromText(startCode); sourceFile.replaceText(range, writer => writer.write(insertCode)); chai_1.expect(sourceFile.getFullText()).to.equal(expectedCode); } it("should throw when specifying a position outside the lower bound", () => { const { sourceFile } = testHelpers_1.getInfoFromText(""); chai_1.expect(() => sourceFile.replaceText([-1, 0], "text;")).to.throw(errors.InvalidOperationError); }); it("should throw when specifying a position outside the upper bound", () => { const { sourceFile } = testHelpers_1.getInfoFromText(""); chai_1.expect(() => sourceFile.replaceText([1, 1], "text;")).to.throw(errors.InvalidOperationError); }); it("should throw when specifying an end position outside the upper bound", () => { const { sourceFile } = testHelpers_1.getInfoFromText(""); chai_1.expect(() => sourceFile.replaceText([0, 1], "text;")).to.throw(errors.InvalidOperationError); }); it("should throw when specifying a position greater than the end position", () => { const { sourceFile } = testHelpers_1.getInfoFromText(" "); chai_1.expect(() => sourceFile.replaceText([1, 0], "text;")).to.throw(errors.ArgumentError); }); it("should replace the text specified", () => { doTest("var t;", [4, 5], "u", "var u;"); }); it("should replace with a writer", () => { doWriterTest("var t;", [4, 5], "u", "var u;"); }); }); describe("ClassDeclaration", () => { function doTest(startCode, range, insertCode, expectedCode) { const { sourceFile, firstChild } = testHelpers_1.getInfoFromText(startCode); firstChild.replaceText(range, insertCode); chai_1.expect(sourceFile.getFullText()).to.equal(expectedCode); chai_1.expect(() => firstChild.getInstanceProperties()).to.not.throw(); } it("should throw when specifying a position outside the lower bound", () => { const { firstChild } = testHelpers_1.getInfoFromText("class MyClass {}"); chai_1.expect(() => firstChild.replaceText([14, 14], "prop;")).to.throw(errors.InvalidOperationError); }); it("should throw when specifying a position outside the upper bound", () => { const { firstChild } = testHelpers_1.getInfoFromText("class MyClass {}"); chai_1.expect(() => firstChild.replaceText([16, 16], "prop;")).to.throw(errors.InvalidOperationError); }); it("should insert when specifying the correct bounds", () => { doTest("class MyClass {}", [15, 15], "prop;", "class MyClass {prop;}"); }); it("should replace when replacing text", () => { doTest("class MyClass {myPreviousProp;}", [15, 30], " prop; ", "class MyClass { prop; }"); }); }); }); describe("insertText", () => { describe("SourceFile", () => { function doTest(startCode, pos, insertCode, expectedCode) { const { sourceFile } = testHelpers_1.getInfoFromText(startCode); sourceFile.insertText(pos, insertCode); chai_1.expect(sourceFile.getFullText()).to.equal(expectedCode); } function doWriterTest(startCode, pos, insertCode, expectedCode) { const { sourceFile } = testHelpers_1.getInfoFromText(startCode); sourceFile.insertText(pos, writer => writer.write(insertCode)); chai_1.expect(sourceFile.getFullText()).to.equal(expectedCode); } it("should insert text into an empty source file", () => { doTest("", 0, "class MyClass {}", "class MyClass {}"); }); it("should insert text into a source file at the beginning", () => { doTest("class MyClass {}", 0, "interface MyInterface {}\n\n", "interface MyInterface {}\n\nclass MyClass {}"); }); it("should insert text into a source file in the middle", () => { doTest("class MyClass {}", 15, "\n prop;\n", "class MyClass {\n prop;\n}"); }); it("should insert text into a source file at the end", () => { doTest("class MyClass {}", 16, "\n\ninterface MyInterface {}", "class MyClass {}\n\ninterface MyInterface {}"); }); it("should insert with a writer", () => { doWriterTest("class MyClass {}", 16, "\n\ninterface MyInterface {}", "class MyClass {}\n\ninterface MyInterface {}"); }); }); }); describe("removeText", () => { describe("SourceFile", () => { function doTest(startCode, pos, end, expectedCode) { const { sourceFile } = testHelpers_1.getInfoFromText(startCode); sourceFile.removeText(pos, end); chai_1.expect(sourceFile.getFullText()).to.equal(expectedCode); } // no need to do so many tests in here because they're already covered by replaceText it("should remove the specified range", () => { doTest("class MyClass {}", 7, 13, "class M {}"); }); }); }); }); //# sourceMappingURL=textInsertableNodeTests.js.map