UNPKG

ts-simple-ast

Version:

TypeScript compiler wrapper for AST navigation and code generation.

110 lines (108 loc) 5.38 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const chai_1 = require("chai"); const testHelpers_1 = require("./../testHelpers"); describe("ParameterDeclaration", () => { describe("isRestParameter", () => { function doTest(startCode, isRestParameter) { const { firstChild } = testHelpers_1.getInfoFromText(startCode); const firstParam = firstChild.getParameters()[0]; chai_1.expect(firstParam.isRestParameter()).to.be.equal(isRestParameter); } it("should be a rest parameter when is one", () => { doTest("function func(...param: string[]){}", true); }); it("should not be a rest parameter when not one", () => { doTest("function func(param: string[]){}", false); }); }); describe("setIsRestParameter", () => { function doTest(startCode, isRestParameter, expectedCode) { const { firstChild, sourceFile } = testHelpers_1.getInfoFromText(startCode); const firstParam = firstChild.getParameters()[0]; firstParam.setIsRestParameter(isRestParameter); chai_1.expect(sourceFile.getFullText()).to.be.equal(expectedCode); } it("should not change when not changing", () => { doTest("function func(...param: string[]){}", true, "function func(...param: string[]){}"); }); it("should set as rest parameter", () => { doTest("function func(param: string[]){}", true, "function func(...param: string[]){}"); }); it("should remove as rest parameter", () => { doTest("function func(...param: string[]){}", false, "function func(param: string[]){}"); }); }); describe("isParameterProperty", () => { function doTest(startCode, isParameterProperty) { const { firstChild } = testHelpers_1.getInfoFromText(startCode); const firstParam = firstChild.getParameters()[0]; chai_1.expect(firstParam.isParameterProperty()).to.be.equal(isParameterProperty); } it("should be parameter property when has a scope", () => { doTest("function func(public param: any){}", true); }); it("should be parameter property when is readonly", () => { doTest("function func(readonly param: any){}", true); }); it("should not be parameter property when not readonly or having a scope", () => { doTest("function func(param: any){}", false); }); }); describe("isOptional", () => { function doTest(startCode, isOptional) { const { firstChild } = testHelpers_1.getInfoFromText(startCode); const firstParam = firstChild.getParameters()[0]; chai_1.expect(firstParam.isOptional()).to.be.equal(isOptional); } it("should be a optional when optional", () => { doTest("function func(param?: string){}", true); }); it("should be a optional when has a default value", () => { doTest("function func(param = 4){}", true); }); it("should be optional when has a rest parameter", () => { doTest("function func(...param: string[]){}", true); }); it("should not be a optional otherwise", () => { doTest("function func(param: string){}", false); }); }); describe("fill", () => { function doTest(startCode, structure, expectedCode) { const { firstChild, sourceFile } = testHelpers_1.getInfoFromText(startCode); const firstParam = firstChild.getParameters()[0]; firstParam.fill(structure); chai_1.expect(sourceFile.getFullText()).to.be.equal(expectedCode); } it("should not modify when not changing", () => { doTest("function func(param: string) {}", {}, "function func(param: string) {}"); }); it("should modify when setting", () => { const structure = { isRestParameter: true }; doTest("function func(param: string) {}", structure, "function func(...param: string) {}"); }); }); describe("remove", () => { function doTest(code, nameToRemove, expectedCode) { const { firstChild, sourceFile } = testHelpers_1.getInfoFromText(code); firstChild.getParameters().find(p => p.getName() === nameToRemove).remove(); chai_1.expect(sourceFile.getFullText()).to.equal(expectedCode); } it("should remove when it's the only parameter", () => { doTest("function identifier(param) {}", "param", "function identifier() {}"); }); it("should remove when it's the first parameter", () => { doTest("function identifier(param: string, param2) {}", "param", "function identifier(param2) {}"); }); it("should remove when it's the last parameter", () => { doTest("function identifier(param: string, param2?: string = '') {}", "param2", "function identifier(param: string) {}"); }); it("should remove when it's the middle parameter", () => { doTest("function identifier(param, param2, param3) {}", "param2", "function identifier(param, param3) {}"); }); }); }); //# sourceMappingURL=parameterDeclarationTests.js.map