UNPKG

ts-simple-ast

Version:

TypeScript compiler wrapper for AST navigation and code generation.

135 lines (133 loc) 7.49 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const chai_1 = require("chai"); const ts = require("typescript"); const compiler_1 = require("./../../../compiler"); const testHelpers_1 = require("./../testHelpers"); describe("DocumentationableNode", () => { describe("VariableStatement", () => { const { sourceFile } = testHelpers_1.getInfoFromText("/** Text */\nvar docedComment;\n/** First */\n/** Second */var multiCommented;\nvar nonDocedComment;"); const statements = sourceFile.getVariableStatements(); const docedStatement = statements[0]; const multiDocedStatement = statements[1]; const nonDocedStatement = statements[2]; describe("getDocumentationComment", () => { describe("documentationed node", () => { it("should get the comment", () => { chai_1.expect(docedStatement.getDocumentationComment()).to.equal("Text"); }); }); describe("multi-documentationed node", () => { it("should get the comment separated by newlines", () => { chai_1.expect(multiDocedStatement.getDocumentationComment()).to.equal("First\nSecond"); }); }); describe("not documentationed node", () => { it("should return undefined", () => { chai_1.expect(nonDocedStatement.getDocumentationComment()).to.be.undefined; }); }); }); describe("getDocumentationCommentNodes", () => { describe("documentationed node", () => { const nodes = docedStatement.getDocumentationCommentNodes(); it("should have the right number of nodes", () => { chai_1.expect(nodes.length).to.equal(1); }); it("should have the right node", () => { chai_1.expect(nodes[0].getText()).to.equal("/** Text */"); }); it("should have the right node kind", () => { chai_1.expect(nodes[0].getKind()).to.equal(ts.SyntaxKind.JSDocComment); }); }); describe("multi documentationed node", () => { it("should have the right number of nodes", () => { chai_1.expect(multiDocedStatement.getDocumentationCommentNodes().length).to.equal(2); }); }); describe("not documentationed node", () => { it("should not have any doc comment nodes", () => { chai_1.expect(nonDocedStatement.getDocumentationCommentNodes().length).to.equal(0); }); }); }); }); describe("FunctionDeclaration", () => { const { firstChild } = testHelpers_1.getInfoFromText("/**\n * Test.\n * @name - Test\n */\nfunction myFunction(name: string) {}"); const doc = firstChild.getDocumentationCommentNodes()[0]; it("should have the right node kind", () => { chai_1.expect(doc.getKind()).to.equal(ts.SyntaxKind.JSDocComment); }); }); describe("insertDocs", () => { function doTest(startCode, insertIndex, structures, expectedCode) { const { firstChild, sourceFile } = testHelpers_1.getInfoFromText(startCode); const result = firstChild.insertDocs(insertIndex, structures); chai_1.expect(result.length).to.equal(structures.length); chai_1.expect(sourceFile.getFullText()).to.equal(expectedCode); } it("should insert when none exist", () => { doTest("function identifier() {}", 0, [{ description: "Description" }], "/**\n * Description\n */\nfunction identifier() {}"); }); it("should insert at start when indentation is different", () => { doTest(" /**\n * Desc2\n */\n function identifier() {}", 0, [{ description: "Desc1" }], " /**\n * Desc1\n */\n /**\n * Desc2\n */\n function identifier() {}"); }); it("should insert multiple at end", () => { doTest("/**\n * Desc1\n */\nfunction identifier() {}", 1, [{ description: "Desc2" }, { description: "Desc3" }], "/**\n * Desc1\n */\n/**\n * Desc2\n */\n/**\n * Desc3\n */\nfunction identifier() {}"); }); it("should insert in the middle", () => { doTest("/**\n * Desc1\n */\n/**\n * Desc3\n */\nfunction identifier() {}", 1, [{ description: "Desc2" }], "/**\n * Desc1\n */\n/**\n * Desc2\n */\n/**\n * Desc3\n */\nfunction identifier() {}"); }); it("should do nothing if empty array", () => { doTest("function identifier() {}", 0, [], "function identifier() {}"); }); }); describe("insertDoc", () => { function doTest(startCode, index, structure, expectedCode) { const { firstChild, sourceFile } = testHelpers_1.getInfoFromText(startCode); const result = firstChild.insertDoc(index, structure); chai_1.expect(result).to.be.instanceOf(compiler_1.Node); chai_1.expect(sourceFile.getFullText()).to.equal(expectedCode); } it("should insert", () => { doTest("/**\n * Desc2\n */\nfunction identifier() {}", 0, { description: "Desc1" }, "/**\n * Desc1\n */\n/**\n * Desc2\n */\nfunction identifier() {}"); }); }); describe("addDocs", () => { function doTest(startCode, structures, expectedCode) { const { firstChild, sourceFile } = testHelpers_1.getInfoFromText(startCode); const result = firstChild.addDocs(structures); chai_1.expect(result.length).to.equal(structures.length); chai_1.expect(sourceFile.getFullText()).to.equal(expectedCode); } it("should add multiple at end", () => { doTest("/**\n * Desc1\n */\nfunction identifier() {}", [{ description: "Desc2" }, { description: "Desc3" }], "/**\n * Desc1\n */\n/**\n * Desc2\n */\n/**\n * Desc3\n */\nfunction identifier() {}"); }); }); describe("addDoc", () => { function doTest(startCode, structure, expectedCode) { const { firstChild, sourceFile } = testHelpers_1.getInfoFromText(startCode); const result = firstChild.addDoc(structure); chai_1.expect(result).to.be.instanceOf(compiler_1.Node); chai_1.expect(sourceFile.getFullText()).to.equal(expectedCode); } it("should add at the end", () => { doTest("/**\n * Desc1\n */\nfunction identifier() {}", { description: "Desc2" }, "/**\n * Desc1\n */\n/**\n * Desc2\n */\nfunction identifier() {}"); }); }); describe("fill", () => { function doTest(startingCode, structure, expectedCode) { const { firstChild, sourceFile } = testHelpers_1.getInfoFromText(startingCode); firstChild.fill(structure); chai_1.expect(sourceFile.getFullText()).to.equal(expectedCode); } it("should modify when setting", () => { doTest("class Identifier {}", { docs: [{ description: "Desc1" }, { description: "Desc2" }] }, "/**\n * Desc1\n */\n/**\n * Desc2\n */\nclass Identifier {}"); }); it("should not modify anything if the structure doesn't change anything", () => { doTest("class Identifier {}", {}, "class Identifier {}"); }); }); }); //# sourceMappingURL=documentationableNodeTests.js.map