Debian Codegraph @

clang-tools-extra/unittests/clang-doc/MDGeneratorTest.cpp

raw
1 //===-- clang-doc/MDGeneratorTest.cpp -------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "ClangDocTest.h"
10 #include "Generators.h"
11 #include "Representation.h"
12 #include "gtest/gtest.h"
13
14 namespace clang {
15 namespace doc {
16
17 static std::unique_ptr<Generator> getMDGenerator() {
18 auto G = doc::findGeneratorByName("md");
19 if (!G)
20 return nullptr;
21 return std::move(G.get());
22 }
23
24 class MDGeneratorTest : public ClangDocContextTest {};
25
26 TEST_F(MDGeneratorTest, emitNamespaceMD) {
27 NamespaceInfo I;
28 I.Name = "Namespace";
29 I.Namespace.emplace_back(EmptySID, "A", InfoType::IT_namespace);
30
31 I.Children.Namespaces.emplace_back(EmptySID, "ChildNamespace",
32 InfoType::IT_namespace);
33 I.Children.Records.emplace_back(EmptySID, "ChildStruct", InfoType::IT_record);
34 I.Children.Functions.emplace_back();
35 I.Children.Functions.back().Name = "OneFunction";
36 I.Children.Functions.back().Access = AccessSpecifier::AS_none;
37 I.Children.Enums.emplace_back();
38 I.Children.Enums.back().Name = "OneEnum";
39
40 auto G = getMDGenerator();
41 assert(G);
42 std::string Buffer;
43 llvm::raw_string_ostream Actual(Buffer);
44 auto Err = G->generateDocForInfo(&I, Actual, getClangDocContext());
45 assert(!Err);
46 std::string Expected = R"raw(# namespace Namespace
47
48
49
50 ## Namespaces
51
52 * [ChildNamespace](../ChildNamespace/index.md)
53
54
55 ## Records
56
57 * [ChildStruct](../ChildStruct.md)
58
59
60 ## Functions
61
62 ### OneFunction
63
64 * OneFunction()*
65
66
67
68 ## Enums
69
70 | enum OneEnum |
71
72 --
73
74
75
76
77
78 )raw";
79 EXPECT_EQ(Expected, Actual.str());
80 }
81
82 TEST_F(MDGeneratorTest, emitRecordMD) {
83 RecordInfo I;
84 I.Name = "r";
85 I.Namespace.emplace_back(EmptySID, "A", InfoType::IT_namespace);
86
87 I.DefLoc = Location(10, 10, "test.cpp");
88 I.Loc.emplace_back(12, 12, "test.cpp");
89
90 I.Members.emplace_back(TypeInfo("int"), "X", AccessSpecifier::AS_private);
91 I.TagType = TagTypeKind::Class;
92 I.Parents.emplace_back(EmptySID, "F", InfoType::IT_record);
93 I.VirtualParents.emplace_back(EmptySID, "G", InfoType::IT_record);
94
95 I.Children.Records.emplace_back(EmptySID, "ChildStruct", InfoType::IT_record);
96 I.Children.Functions.emplace_back();
97 I.Children.Functions.back().Name = "OneFunction";
98 I.Children.Enums.emplace_back();
99 I.Children.Enums.back().Name = "OneEnum";
100
101 auto G = getMDGenerator();
102 assert(G);
103 std::string Buffer;
104 llvm::raw_string_ostream Actual(Buffer);
105 auto Err = G->generateDocForInfo(&I, Actual, getClangDocContext());
106 assert(!Err);
107 std::string Expected = R"raw(# class r
108
109 *Defined at test.cpp#10*
110
111 Inherits from F, G
112
113
114
115 ## Members
116
117 private int X
118
119
120
121 ## Records
122
123 ChildStruct
124
125
126
127 ## Functions
128
129 ### OneFunction
130
131 *public OneFunction()*
132
133
134
135 ## Enums
136
137 | enum OneEnum |
138
139 --
140
141
142
143
144
145 )raw";
146 EXPECT_EQ(Expected, Actual.str());
147 }
148
149 TEST_F(MDGeneratorTest, emitFunctionMD) {
150 FunctionInfo I;
151 I.Name = "f";
152 I.Namespace.emplace_back(EmptySID, "A", InfoType::IT_namespace);
153
154 I.DefLoc = Location(10, 10, "test.cpp");
155 I.Loc.emplace_back(12, 12, "test.cpp");
156
157 I.Access = AccessSpecifier::AS_none;
158
159 I.ReturnType = TypeInfo("void");
160 I.Params.emplace_back(TypeInfo("int"), "P");
161 I.IsMethod = true;
162 I.Parent = Reference(EmptySID, "Parent", InfoType::IT_record);
163
164 auto G = getMDGenerator();
165 assert(G);
166 std::string Buffer;
167 llvm::raw_string_ostream Actual(Buffer);
168 auto Err = G->generateDocForInfo(&I, Actual, getClangDocContext());
169 assert(!Err);
170 std::string Expected = R"raw(### f
171
172 *void f(int P)*
173
174 *Defined at test.cpp#10*
175
176 )raw";
177
178 EXPECT_EQ(Expected, Actual.str());
179 }
180
181 TEST_F(MDGeneratorTest, emitEnumMD) {
182 EnumInfo I;
183 I.Name = "e";
184 I.Namespace.emplace_back(EmptySID, "A", InfoType::IT_namespace);
185
186 I.DefLoc = Location(10, 10, "test.cpp");
187 I.Loc.emplace_back(12, 12, "test.cpp");
188
189 I.Members.emplace_back("X");
190 I.Scoped = true;
191
192 auto G = getMDGenerator();
193 assert(G);
194 std::string Buffer;
195 llvm::raw_string_ostream Actual(Buffer);
196 auto Err = G->generateDocForInfo(&I, Actual, getClangDocContext());
197 assert(!Err);
198 std::string Expected = R"raw(| enum class e |
199
200 --
201
202 | X |
203
204
205 *Defined at test.cpp#10*
206
207 )raw";
208
209 EXPECT_EQ(Expected, Actual.str());
210 }
211
212 TEST_F(MDGeneratorTest, emitCommentMD) {
213 FunctionInfo I;
214 I.Name = "f";
215
216 I.DefLoc = Location(10, 10, "test.cpp");
217 I.ReturnType = TypeInfo("void");
218 I.Params.emplace_back(TypeInfo("int"), "I");
219 I.Params.emplace_back(TypeInfo("int"), "J");
220 I.Access = AccessSpecifier::AS_none;
221
222 CommentInfo Top;
223 Top.Kind = CommentKind::CK_FullComment;
224
225 Top.Children.emplace_back(std::make_unique<CommentInfo>());
226 CommentInfo *BlankLine = Top.Children.back().get();
227 BlankLine->Kind = CommentKind::CK_ParagraphComment;
228 BlankLine->Children.emplace_back(std::make_unique<CommentInfo>());
229 BlankLine->Children.back()->Kind = CommentKind::CK_TextComment;
230
231 Top.Children.emplace_back(std::make_unique<CommentInfo>());
232 CommentInfo *Brief = Top.Children.back().get();
233 Brief->Kind = CommentKind::CK_ParagraphComment;
234 Brief->Children.emplace_back(std::make_unique<CommentInfo>());
235 Brief->Children.back()->Kind = CommentKind::CK_TextComment;
236 Brief->Children.back()->Name = "ParagraphComment";
237 Brief->Children.back()->Text = " Brief description.";
238
239 Top.Children.emplace_back(std::make_unique<CommentInfo>());
240 CommentInfo *Extended = Top.Children.back().get();
241 Extended->Kind = CommentKind::CK_ParagraphComment;
242 Extended->Children.emplace_back(std::make_unique<CommentInfo>());
243 Extended->Children.back()->Kind = CommentKind::CK_TextComment;
244 Extended->Children.back()->Text = " Extended description that";
245 Extended->Children.emplace_back(std::make_unique<CommentInfo>());
246 Extended->Children.back()->Kind = CommentKind::CK_TextComment;
247 Extended->Children.back()->Text = " continues onto the next line.";
248
249 Top.Children.emplace_back(std::make_unique<CommentInfo>());
250 CommentInfo *HTML = Top.Children.back().get();
251 HTML->Kind = CommentKind::CK_ParagraphComment;
252 HTML->Children.emplace_back(std::make_unique<CommentInfo>());
253 HTML->Children.back()->Kind = CommentKind::CK_TextComment;
254 HTML->Children.emplace_back(std::make_unique<CommentInfo>());
255 HTML->Children.back()->Kind = CommentKind::CK_HTMLStartTagComment;
256 HTML->Children.back()->Name = "ul";
257 HTML->Children.back()->AttrKeys.emplace_back("class");
258 HTML->Children.back()->AttrValues.emplace_back("test");
259 HTML->Children.emplace_back(std::make_unique<CommentInfo>());
260 HTML->Children.back()->Kind = CommentKind::CK_HTMLStartTagComment;
261 HTML->Children.back()->Name = "li";
262 HTML->Children.emplace_back(std::make_unique<CommentInfo>());
263 HTML->Children.back()->Kind = CommentKind::CK_TextComment;
264 HTML->Children.back()->Text = " Testing.";
265 HTML->Children.emplace_back(std::make_unique<CommentInfo>());
266 HTML->Children.back()->Kind = CommentKind::CK_HTMLEndTagComment;
267 HTML->Children.back()->Name = "ul";
268 HTML->Children.back()->SelfClosing = true;
269
270 Top.Children.emplace_back(std::make_unique<CommentInfo>());
271 CommentInfo *Verbatim = Top.Children.back().get();
272 Verbatim->Kind = CommentKind::CK_VerbatimBlockComment;
273 Verbatim->Name = "verbatim";
274 Verbatim->CloseName = "endverbatim";
275 Verbatim->Children.emplace_back(std::make_unique<CommentInfo>());
276 Verbatim->Children.back()->Kind = CommentKind::CK_VerbatimBlockLineComment;
277 Verbatim->Children.back()->Text = " The description continues.";
278
279 Top.Children.emplace_back(std::make_unique<CommentInfo>());
280 CommentInfo *ParamOut = Top.Children.back().get();
281 ParamOut->Kind = CommentKind::CK_ParamCommandComment;
282 ParamOut->Direction = "[out]";
283 ParamOut->ParamName = "I";
284 ParamOut->Explicit = true;
285 ParamOut->Children.emplace_back(std::make_unique<CommentInfo>());
286 ParamOut->Children.back()->Kind = CommentKind::CK_ParagraphComment;
287 ParamOut->Children.back()->Children.emplace_back(
288 std::make_unique<CommentInfo>());
289 ParamOut->Children.back()->Children.back()->Kind =
290 CommentKind::CK_TextComment;
291 ParamOut->Children.back()->Children.emplace_back(
292 std::make_unique<CommentInfo>());
293 ParamOut->Children.back()->Children.back()->Kind =
294 CommentKind::CK_TextComment;
295 ParamOut->Children.back()->Children.back()->Text = " is a parameter.";
296
297 Top.Children.emplace_back(std::make_unique<CommentInfo>());
298 CommentInfo *ParamIn = Top.Children.back().get();
299 ParamIn->Kind = CommentKind::CK_ParamCommandComment;
300 ParamIn->Direction = "[in]";
301 ParamIn->ParamName = "J";
302 ParamIn->Children.emplace_back(std::make_unique<CommentInfo>());
303 ParamIn->Children.back()->Kind = CommentKind::CK_ParagraphComment;
304 ParamIn->Children.back()->Children.emplace_back(
305 std::make_unique<CommentInfo>());
306 ParamIn->Children.back()->Children.back()->Kind = CommentKind::CK_TextComment;
307 ParamIn->Children.back()->Children.back()->Text = " is a parameter.";
308 ParamIn->Children.back()->Children.emplace_back(
309 std::make_unique<CommentInfo>());
310 ParamIn->Children.back()->Children.back()->Kind = CommentKind::CK_TextComment;
311
312 Top.Children.emplace_back(std::make_unique<CommentInfo>());
313 CommentInfo *Return = Top.Children.back().get();
314 Return->Kind = CommentKind::CK_BlockCommandComment;
315 Return->Name = "return";
316 Return->Explicit = true;
317 Return->Children.emplace_back(std::make_unique<CommentInfo>());
318 Return->Children.back()->Kind = CommentKind::CK_ParagraphComment;
319 Return->Children.back()->Children.emplace_back(
320 std::make_unique<CommentInfo>());
321 Return->Children.back()->Children.back()->Kind = CommentKind::CK_TextComment;
322 Return->Children.back()->Children.back()->Text = "void";
323
324 I.Description.emplace_back(std::move(Top));
325
326 auto G = getMDGenerator();
327 assert(G);
328 std::string Buffer;
329 llvm::raw_string_ostream Actual(Buffer);
330 auto Err = G->generateDocForInfo(&I, Actual, getClangDocContext());
331 assert(!Err);
332 std::string Expected =
333 R"raw(### f
334
335 *void f(int I, int J)*
336
337 *Defined at test.cpp#10*
338
339
340
341 Brief description.
342
343 Extended description that continues onto the next line.
344
345 <ul "class=test">
346
347 <li>
348
349 Testing.</ul>
350
351
352
353 The description continues.
354
355 **I** [out] is a parameter.
356
357 **J** is a parameter.
358
359 **return**void
360
361 )raw";
362
363 EXPECT_EQ(Expected, Actual.str());
364 }
365
366 } // namespace doc
367 } // namespace clang