-
Notifications
You must be signed in to change notification settings - Fork 189
/
classTests.ts
211 lines (178 loc) · 8.49 KB
/
classTests.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import { nameof } from "@ts-morph/common";
import { expect } from "chai";
import { ClassDeclaration, FunctionDeclaration, Node, StatementedNode } from "../../../../../compiler";
import { ClassDeclarationStructure } from "../../../../../structures";
import { getInfoFromText, OptionalKindAndTrivia } from "../../../testHelpers";
describe("StatementedNode", () => {
describe(nameof<StatementedNode>("insertClasses"), () => {
function doTest(startCode: string, index: number, structures: OptionalKindAndTrivia<ClassDeclarationStructure>[], expectedText: string) {
const { sourceFile } = getInfoFromText(startCode);
const result = sourceFile.insertClasses(index, structures);
expect(sourceFile.getFullText()).to.equal(expectedText);
expect(result.length).to.equal(structures.length);
}
it("should insert to an empty file", () => {
doTest("", 0, [{
name: "Identifier",
}], "class Identifier {\n}\n");
});
it("should insert without a name", () => {
doTest("", 0, [{ isDefaultExport: true }], "export default class {\n}\n");
});
it("should insert at the start of a file", () => {
doTest("enum Enum {\n}\n", 0, [{ name: "Identifier" }], "class Identifier {\n}\n\nenum Enum {\n}\n");
});
it("should insert at the end of a file", () => {
doTest("enum Enum {\n}\n", 1, [{ name: "Identifier" }], "enum Enum {\n}\n\nclass Identifier {\n}\n");
});
it("should insert in the middle of children", () => {
doTest(
"class Identifier1 {\n}\n\nclass Identifier3 {\n}\n",
1,
[{ name: "Identifier2" }],
"class Identifier1 {\n}\n\nclass Identifier2 {\n}\n\nclass Identifier3 {\n}\n",
);
});
it("should insert multiple", () => {
doTest(
"class Identifier1 {\n}\n",
1,
[{ name: "Identifier2" }, { name: "Identifier3" }],
"class Identifier1 {\n}\n\nclass Identifier2 {\n}\n\nclass Identifier3 {\n}\n",
);
});
it("should have the expected text adding to non-source file", () => {
const { sourceFile } = getInfoFromText("namespace Namespace {\n}\n");
const namespaceDec = sourceFile.getModules()[0];
namespaceDec.insertClasses(0, [{
name: "Identifier",
}]);
expect(sourceFile.getFullText()).to.equal("namespace Namespace {\n class Identifier {\n }\n}\n");
});
it("should insert everything in a structure", () => {
const structure: OptionalKindAndTrivia<MakeRequired<ClassDeclarationStructure>> = {
name: "C",
ctors: [{}, {}],
staticBlocks: [{}],
decorators: [{ name: "D" }],
docs: [{ description: "Test" }],
extends: "Base",
implements: ["IBase", "IBase2"],
hasDeclareKeyword: false,
isAbstract: true,
isDefaultExport: true,
isExported: true,
typeParameters: [{ name: "T" }],
properties: [{ name: "p", type: writer => writer.write("number") }],
methods: [{ name: "m" }],
getAccessors: [{ name: "g" }, { name: "s" }, { name: "g2" }, { name: "g3" }],
setAccessors: [{ name: "s" }, { name: "g" }, { name: "s2" }],
};
const expectedText = "/** Test */\n@D\nexport default abstract class C<T> extends Base implements IBase, IBase2 {\n"
+ " p: number;\n\n"
+ " static {\n }\n\n"
+ " constructor() {\n }\n\n"
+ " constructor() {\n }\n\n"
+ " get g() {\n }\n\n set g() {\n }\n\n get s() {\n }\n\n set s() {\n }\n\n"
+ " get g2() {\n }\n\n get g3() {\n }\n\n set s2() {\n }\n\n"
+ " m() {\n }\n"
+ "}\n";
doTest("", 0, [structure], expectedText);
});
it("should insert an ambient method into a class when inserting a class into an ambient module", () => {
const { sourceFile } = getInfoFromText("declare module Namespace {\n}\n");
const namespaceDec = sourceFile.getModules()[0];
namespaceDec.insertClasses(0, [{
name: "Identifier",
methods: [{ name: "myMethod" }],
}]);
expect(sourceFile.getFullText()).to.equal("declare module Namespace {\n class Identifier {\n myMethod();\n }\n}\n");
});
function doFunctionTest(startText: string, endText: string) {
const { firstChild, sourceFile } = getInfoFromText<FunctionDeclaration>(startText);
firstChild.insertClasses(0, [{ name: "C" }]);
expect(sourceFile.getFullText()).to.equal(endText);
}
it("should insert into a function with no body", () => {
doFunctionTest("function test();", "function test() {\n class C {\n }\n}");
});
it("should insert into a function with a body", () => {
doFunctionTest("function test() {\n}", "function test() {\n class C {\n }\n}");
});
});
describe(nameof<StatementedNode>("insertClass"), () => {
function doTest(startCode: string, index: number, structure: OptionalKindAndTrivia<ClassDeclarationStructure>, expectedText: string) {
const { sourceFile } = getInfoFromText(startCode);
const result = sourceFile.insertClass(index, structure);
expect(sourceFile.getFullText()).to.equal(expectedText);
expect(result).to.be.instanceOf(ClassDeclaration);
}
it("should insert", () => {
doTest("class Identifier2 {\n}\n", 0, { name: "Identifier1" }, "class Identifier1 {\n}\n\nclass Identifier2 {\n}\n");
});
});
describe(nameof<StatementedNode>("addClasses"), () => {
function doTest(startCode: string, structures: OptionalKindAndTrivia<ClassDeclarationStructure>[], expectedText: string) {
const { sourceFile } = getInfoFromText(startCode);
const result = sourceFile.addClasses(structures);
expect(sourceFile.getFullText()).to.equal(expectedText);
expect(result.length).to.equal(structures.length);
}
it("should add multiple", () => {
doTest(
"class Identifier1 {\n}\n",
[{ name: "Identifier2" }, { name: "Identifier3" }],
"class Identifier1 {\n}\n\nclass Identifier2 {\n}\n\nclass Identifier3 {\n}\n",
);
});
});
describe(nameof<StatementedNode>("addClass"), () => {
function doTest(startCode: string, structure: OptionalKindAndTrivia<ClassDeclarationStructure>, expectedText: string) {
const { sourceFile } = getInfoFromText(startCode);
const result = sourceFile.addClass(structure);
expect(sourceFile.getFullText()).to.equal(expectedText);
expect(result).to.be.instanceOf(ClassDeclaration);
}
it("should add one", () => {
doTest("class Identifier1 {\n}\n", { name: "Identifier2" }, "class Identifier1 {\n}\n\nclass Identifier2 {\n}\n");
});
});
describe(nameof<StatementedNode>("getClasses"), () => {
const { sourceFile } = getInfoFromText("class Identifier1 {}\nclass Identifier2 { prop: string; }");
const classes = sourceFile.getClasses();
it("should have the expected number of classes", () => {
expect(classes.length).to.equal(2);
});
it("should have correct type", () => {
expect(classes[0]).to.be.instanceOf(ClassDeclaration);
});
it("should not throw when getting from an empty body", () => {
const { firstChild } = getInfoFromText<StatementedNode & Node>("function test();");
expect(firstChild.getClasses()).to.deep.equal([]);
});
});
describe(nameof<StatementedNode>("getClass"), () => {
const { sourceFile } = getInfoFromText("class Identifier1 {}\nclass Identifier2 { prop: string; }");
it("should get a class by a name", () => {
expect(sourceFile.getClass("Identifier2")!.getName()).to.equal("Identifier2");
});
it("should get a class by a search function", () => {
expect(sourceFile.getClass(c => c.getName() === "Identifier1")!.getName()).to.equal("Identifier1");
});
it("should return undefined when the class doesn't exist", () => {
expect(sourceFile.getClass("asdf")).to.be.undefined;
});
});
describe(nameof<StatementedNode>("getClassOrThrow"), () => {
const { sourceFile } = getInfoFromText("class Identifier1 {}\nclass Identifier2 { prop: string; }");
it("should get a class by a name", () => {
expect(sourceFile.getClassOrThrow("Identifier2").getName()).to.equal("Identifier2");
});
it("should get a class by a search function", () => {
expect(sourceFile.getClassOrThrow(c => c.getName() === "Identifier1").getName()).to.equal("Identifier1");
});
it("should throw when the class doesn't exist", () => {
expect(() => sourceFile.getClassOrThrow("asdf")).to.throw();
});
});
});