ts-simple-ast
Version:
TypeScript compiler wrapper for AST navigation and code generation.
76 lines (74 loc) • 4.21 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const chai_1 = require("chai");
const testHelpers_1 = require("./../testHelpers");
describe("QuestionTokenableNode", () => {
function getInfoWithFirstPropertyFromText(text) {
const result = testHelpers_1.getInfoFromText(text);
return Object.assign({}, result, { firstProperty: result.firstChild.getInstanceProperties()[0] });
}
describe("hasQuestionToken", () => {
it("should have a question token when has one", () => {
const { firstProperty } = getInfoWithFirstPropertyFromText("class MyClass {\nprop?: string;}\n");
chai_1.expect(firstProperty.hasQuestionToken()).to.be.true;
});
it("should not have a question token when not has one", () => {
const { firstProperty } = getInfoWithFirstPropertyFromText("class MyClass {\nprop: string;}\n");
chai_1.expect(firstProperty.hasQuestionToken()).to.be.false;
});
});
describe("getQuestionTokenNode", () => {
it("should be get the question token node", () => {
const { firstProperty } = getInfoWithFirstPropertyFromText("class MyClass {\nprop?: string;}\n");
chai_1.expect(firstProperty.getQuestionTokenNode().getText()).to.equal("?");
});
it("should be undefined when not optional", () => {
const { firstProperty } = getInfoWithFirstPropertyFromText("class MyClass {\nprop: string;}\n");
chai_1.expect(firstProperty.getQuestionTokenNode()).to.be.undefined;
});
});
describe("getQuestionTokenNodeOrThrow", () => {
it("should be get the question token node", () => {
const { firstProperty } = getInfoWithFirstPropertyFromText("class MyClass {\nprop?: string;}\n");
chai_1.expect(firstProperty.getQuestionTokenNodeOrThrow().getText()).to.equal("?");
});
it("should throw when not optional", () => {
const { firstProperty } = getInfoWithFirstPropertyFromText("class MyClass {\nprop: string;}\n");
chai_1.expect(() => firstProperty.getQuestionTokenNodeOrThrow()).to.throw();
});
});
describe("setIsOptional", () => {
it("should be set as optional when not optional", () => {
const { firstProperty, sourceFile } = getInfoWithFirstPropertyFromText("class MyClass { prop: string; }");
firstProperty.setIsOptional(true);
chai_1.expect(sourceFile.getFullText()).to.be.equal("class MyClass { prop?: string; }");
});
it("should be set as not optional when optional", () => {
const { firstProperty, sourceFile } = getInfoWithFirstPropertyFromText("class MyClass { prop?: string; }");
firstProperty.setIsOptional(false);
chai_1.expect(sourceFile.getFullText()).to.be.equal("class MyClass { prop: string; }");
});
it("should do nothing when setting to same value", () => {
const { firstProperty, sourceFile } = getInfoWithFirstPropertyFromText("class MyClass { prop: string; }");
firstProperty.setIsOptional(false);
chai_1.expect(sourceFile.getFullText()).to.be.equal("class MyClass { prop: string; }");
});
});
describe("fill", () => {
function doTest(startCode, structure, expectedCode) {
const { firstProperty, sourceFile } = getInfoWithFirstPropertyFromText(startCode);
firstProperty.fill(structure);
chai_1.expect(sourceFile.getText()).to.equal(expectedCode);
}
it("should not modify when not set and structure empty", () => {
doTest("class Identifier { prop: string; }", {}, "class Identifier { prop: string; }");
});
it("should not modify when set and structure empty", () => {
doTest("class Identifier { prop?: string; }", {}, "class Identifier { prop?: string; }");
});
it("should modify when setting true", () => {
doTest("class Identifier { prop: string; }", { hasQuestionToken: true }, "class Identifier { prop?: string; }");
});
});
});
//# sourceMappingURL=questionTokenableNodeTests.js.map