UNPKG

ts-simple-ast

Version:

TypeScript compiler wrapper for AST navigation and code generation.

103 lines (101 loc) 5.38 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const chai_1 = require("chai"); const compiler_1 = require("./../../../compiler"); const testHelpers_1 = require("./../testHelpers"); describe("TypeParameteredNode", () => { describe("getTypeParameters", () => { const { sourceFile } = testHelpers_1.getInfoFromText("function noTypeParamsFunc() {}\n function typeParamsFunc<T, U>() {}"); const noTypeParamsFunc = sourceFile.getFunctions()[0]; const typeParamsFunc = sourceFile.getFunctions()[1]; describe("having no type parameters", () => { it("should return an empty array", () => { chai_1.expect(noTypeParamsFunc.getTypeParameters().length).to.equal(0); }); }); describe("having type parameters", () => { it("should get the correct number of type parameters", () => { chai_1.expect(typeParamsFunc.getTypeParameters().length).to.equal(2); }); it("should have the right instance of", () => { chai_1.expect(typeParamsFunc.getTypeParameters()[0]).to.be.instanceOf(compiler_1.TypeParameterDeclaration); }); }); }); describe("addTypeParameter", () => { function doTest(startCode, structure, expectedCode) { const { firstChild } = testHelpers_1.getInfoFromText(startCode); const result = firstChild.addTypeParameter(structure); chai_1.expect(firstChild.getText()).to.equal(expectedCode); chai_1.expect(result).to.be.instanceof(compiler_1.TypeParameterDeclaration); } it("should add when none exists", () => { doTest("function identifier() {}", { name: "T" }, "function identifier<T>() {}"); }); it("should add when one exists", () => { doTest("function identifier<T>() {}", { name: "U" }, "function identifier<T, U>() {}"); }); }); describe("addTypeParameters", () => { function doTest(startCode, structures, expectedCode) { const { firstChild } = testHelpers_1.getInfoFromText(startCode); const result = firstChild.addTypeParameters(structures); chai_1.expect(firstChild.getText()).to.equal(expectedCode); chai_1.expect(result.length).to.equal(structures.length); } it("should add multiple", () => { doTest("function identifier<T>() {}", [{ name: "U" }, { name: "V" }], "function identifier<T, U, V>() {}"); }); }); describe("insertTypeParameter", () => { function doTest(startCode, insertIndex, structure, expectedCode) { const { firstChild } = testHelpers_1.getInfoFromText(startCode); const result = firstChild.insertTypeParameter(insertIndex, structure); chai_1.expect(firstChild.getText()).to.equal(expectedCode); chai_1.expect(result).to.be.instanceof(compiler_1.TypeParameterDeclaration); } it("should insert when none exists", () => { doTest("function identifier() {}", 0, { name: "T" }, "function identifier<T>() {}"); }); it("should insert at the start", () => { doTest("function identifier<T>() {}", 0, { name: "U" }, "function identifier<U, T>() {}"); }); it("should insert at the end", () => { doTest("function identifier<T>() {}", 1, { name: "U" }, "function identifier<T, U>() {}"); }); it("should insert in the middle", () => { doTest("function identifier<T, U>() {}", 1, { name: "V" }, "function identifier<T, V, U>() {}"); }); it("should insert with constraint", () => { doTest("function identifier<T, U>() {}", 1, { name: "V", constraint: "string" }, "function identifier<T, V extends string, U>() {}"); }); }); describe("insertTypeParameters", () => { function doTest(startCode, insertIndex, structures, expectedCode) { const { firstChild } = testHelpers_1.getInfoFromText(startCode); const result = firstChild.insertTypeParameters(insertIndex, structures); chai_1.expect(firstChild.getText()).to.equal(expectedCode); chai_1.expect(result.length).to.equal(structures.length); } it("should insert multiple", () => { doTest("function identifier<V>() {}", 0, [{ name: "T" }, { name: "U" }], "function identifier<T, U, V>() {}"); }); it("should do nothing if empty array", () => { doTest("function identifier() {}", 0, [], "function identifier() {}"); }); }); 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", () => { doTest("type myAlias = string;", { typeParameters: [{ name: "T" }] }, "type myAlias<T> = string;"); }); it("should not modify anything if the structure doesn't change anything", () => { doTest("type myAlias = string;", {}, "type myAlias = string;"); }); }); }); //# sourceMappingURL=typeParameteredNodeTests.js.map