UNPKG

ts-simple-ast

Version:

TypeScript compiler wrapper for AST navigation and code generation.

131 lines (129 loc) 6.51 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const ts = require("typescript"); const chai_1 = require("chai"); const testHelpers_1 = require("./../testHelpers"); describe("ArrayLiteralExpression", () => { function getArrayLiteralExpression(text) { const opts = testHelpers_1.getInfoFromText(text); const declaration = opts.firstChild.getDeclarationList().getDeclarations()[0]; return Object.assign({ arrayLiteralExpression: declaration.getFirstChildByKindOrThrow(ts.SyntaxKind.ArrayLiteralExpression) }, opts); } describe("getElements", () => { function doTest(text, elementTexts) { const { arrayLiteralExpression } = getArrayLiteralExpression(text); chai_1.expect(arrayLiteralExpression.getElements().map(e => e.getText())).to.deep.equal(elementTexts); } it("should get the elements when there are none", () => { doTest("var t = []", []); }); it("should get the elements when there are some", () => { doTest("var t = [5, 3, 'test']", ["5", "3", "'test'"]); }); }); describe("insertElements", () => { function doTest(text, index, elementTexts, expectedText) { const { arrayLiteralExpression, sourceFile } = getArrayLiteralExpression(text); const result = arrayLiteralExpression.insertElements(index, elementTexts); chai_1.expect(result.map(r => r.getText())).to.deep.equal(elementTexts); chai_1.expect(sourceFile.getFullText()).to.equal(expectedText); } it("should insert into an empty array", () => { doTest("var t = []", 0, ["5", "3", "'test'"], "var t = [5, 3, 'test']"); }); it("should insert in at the start of the array", () => { doTest("var t = [3, 4]", 0, ["1", "2"], "var t = [1, 2, 3, 4]"); }); it("should insert in the middle of the array", () => { doTest("var t = [1, 4]", 1, ["2", "3"], "var t = [1, 2, 3, 4]"); }); it("should insert in at the end of the array", () => { doTest("var t = [1, 2]", 2, ["3", "4"], "var t = [1, 2, 3, 4]"); }); }); describe("insertElement", () => { function doTest(text, index, elementText, expectedText) { const { arrayLiteralExpression, sourceFile } = getArrayLiteralExpression(text); const result = arrayLiteralExpression.insertElement(index, elementText); chai_1.expect(result.getText()).to.equal(elementText); chai_1.expect(sourceFile.getFullText()).to.equal(expectedText); } it("should insert into an empty array", () => { doTest("var t = []", 0, "1", "var t = [1]"); }); it("should insert in at the start of the array", () => { doTest("var t = [2, 3]", 0, "1", "var t = [1, 2, 3]"); }); it("should insert in the middle of the array", () => { doTest("var t = [1, 3]", 1, "2", "var t = [1, 2, 3]"); }); it("should insert in at the end of the array", () => { doTest("var t = [1, 2]", 2, "3", "var t = [1, 2, 3]"); }); }); describe("addElements", () => { function doTest(text, elementTexts, expectedText) { const { arrayLiteralExpression, sourceFile } = getArrayLiteralExpression(text); const result = arrayLiteralExpression.addElements(elementTexts); chai_1.expect(result.map(r => r.getText())).to.deep.equal(elementTexts); chai_1.expect(sourceFile.getFullText()).to.equal(expectedText); } it("should add into an empty array", () => { doTest("var t = []", ["1", "2", "3"], "var t = [1, 2, 3]"); }); it("should add at the end of the array", () => { doTest("var t = [1, 2]", ["3", "4"], "var t = [1, 2, 3, 4]"); }); }); describe("addElement", () => { function doTest(text, elementText, expectedText) { const { arrayLiteralExpression, sourceFile } = getArrayLiteralExpression(text); const result = arrayLiteralExpression.addElement(elementText); chai_1.expect(result.getText()).to.equal(elementText); chai_1.expect(sourceFile.getFullText()).to.equal(expectedText); } it("should add into an empty array", () => { doTest("var t = []", "1", "var t = [1]"); }); it("should add at the end of the array", () => { doTest("var t = [1, 2]", "3", "var t = [1, 2, 3]"); }); }); describe("removeElement", () => { it("should throw when none exist", () => { const { arrayLiteralExpression, sourceFile } = getArrayLiteralExpression("var t = []"); chai_1.expect(() => arrayLiteralExpression.removeElement(0)).to.throw(); }); it("should throw when specifying an invalid index", () => { const { arrayLiteralExpression, sourceFile } = getArrayLiteralExpression("var t = [1]"); chai_1.expect(() => arrayLiteralExpression.removeElement(1)).to.throw(); }); describe("index", () => { function doTest(text, index, expectedText) { const { arrayLiteralExpression, sourceFile } = getArrayLiteralExpression(text); arrayLiteralExpression.removeElement(index); chai_1.expect(sourceFile.getFullText()).to.equal(expectedText); } it("should remove at the start", () => { doTest("var t = [1, 2, 3]", 0, "var t = [2, 3]"); }); it("should remove in the middle", () => { doTest("var t = [1, 2, 3]", 1, "var t = [1, 3]"); }); it("should remove at the end", () => { doTest("var t = [1, 2, 3]", 2, "var t = [1, 2]"); }); }); describe("element", () => { function doTest(text, index, expectedText) { const { arrayLiteralExpression, sourceFile } = getArrayLiteralExpression(text); arrayLiteralExpression.removeElement(arrayLiteralExpression.getElements()[index]); chai_1.expect(sourceFile.getFullText()).to.equal(expectedText); } it("should remove by element", () => { doTest("var t = [1, 2, 3]", 1, "var t = [1, 3]"); }); }); }); }); //# sourceMappingURL=arrayLiteralExpressionTests.js.map