1 //===-- clang-doc/YAMLGeneratorTest.cpp
2 //------------------------------------===//
4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
8 //===----------------------------------------------------------------------===//
10 #include "ClangDocTest.h"
11 #include "Generators.h"
12 #include "Representation.h"
13 #include "gtest/gtest.h"
18 std::unique_ptr
<Generator
> getYAMLGenerator() {
19 auto G
= doc::findGeneratorByName("yaml");
22 return std::move(G
.get());
25 TEST(YAMLGeneratorTest
, emitNamespaceYAML
) {
29 I
.Namespace
.emplace_back(EmptySID
, "A", InfoType::IT_namespace
);
31 I
.Children
.Namespaces
.emplace_back(
32 EmptySID
, "ChildNamespace", InfoType::IT_namespace
,
33 "path::to::A::Namespace::ChildNamespace", "path/to/A/Namespace");
34 I
.Children
.Records
.emplace_back(EmptySID
, "ChildStruct", InfoType::IT_record
,
35 "path::to::A::Namespace::ChildStruct",
36 "path/to/A/Namespace");
37 I
.Children
.Functions
.emplace_back();
38 I
.Children
.Functions
.back().Name
= "OneFunction";
39 I
.Children
.Functions
.back().Access
= AccessSpecifier::AS_none
;
40 I
.Children
.Enums
.emplace_back();
41 I
.Children
.Enums
.back().Name
= "OneEnum";
43 auto G
= getYAMLGenerator();
46 llvm::raw_string_ostream
Actual(Buffer
);
47 auto Err
= G
->generateDocForInfo(&I
, Actual
, ClangDocContext());
49 std::string Expected
=
51 USR: '0000000000000000000000000000000000000000'
60 Name: 'ChildNamespace'
61 QualName: 'path::to::A::Namespace::ChildNamespace'
62 Path: 'path/to/A/Namespace'
66 QualName: 'path::to::A::Namespace::ChildStruct'
67 Path: 'path/to/A/Namespace'
69 - USR: '0000000000000000000000000000000000000000'
73 - USR: '0000000000000000000000000000000000000000'
77 EXPECT_EQ(Expected
, Actual
.str());
80 TEST(YAMLGeneratorTest
, emitRecordYAML
) {
85 I
.Namespace
.emplace_back(EmptySID
, "A", InfoType::IT_namespace
);
87 I
.DefLoc
= Location(10, llvm::SmallString
<16>{"test.cpp"});
88 I
.Loc
.emplace_back(12, llvm::SmallString
<16>{"test.cpp"});
90 I
.Members
.emplace_back(TypeInfo("int"), "X", AccessSpecifier::AS_private
);
92 // Member documentation.
93 CommentInfo TopComment
;
94 TopComment
.Kind
= "FullComment";
95 TopComment
.Children
.emplace_back(std::make_unique
<CommentInfo
>());
96 CommentInfo
*Brief
= TopComment
.Children
.back().get();
97 Brief
->Kind
= "ParagraphComment";
98 Brief
->Children
.emplace_back(std::make_unique
<CommentInfo
>());
99 Brief
->Children
.back()->Kind
= "TextComment";
100 Brief
->Children
.back()->Name
= "ParagraphComment";
101 Brief
->Children
.back()->Text
= "Value of the thing.";
102 I
.Members
.back().Description
.push_back(std::move(TopComment
));
104 I
.TagType
= TagTypeKind::Class
;
105 I
.Bases
.emplace_back(EmptySID
, "F", "path/to/F", true,
106 AccessSpecifier::AS_public
, true);
107 I
.Bases
.back().Children
.Functions
.emplace_back();
108 I
.Bases
.back().Children
.Functions
.back().Name
= "InheritedFunctionOne";
109 I
.Bases
.back().Members
.emplace_back(TypeInfo("int"), "N",
110 AccessSpecifier::AS_private
);
111 // F is in the global namespace
112 I
.Parents
.emplace_back(EmptySID
, "F", InfoType::IT_record
, "");
113 I
.VirtualParents
.emplace_back(EmptySID
, "G", InfoType::IT_record
,
114 "path::to::G::G", "path/to/G");
116 I
.Children
.Records
.emplace_back(EmptySID
, "ChildStruct", InfoType::IT_record
,
117 "path::to::A::r::ChildStruct", "path/to/A/r");
118 I
.Children
.Functions
.emplace_back();
119 I
.Children
.Functions
.back().Name
= "OneFunction";
120 I
.Children
.Enums
.emplace_back();
121 I
.Children
.Enums
.back().Name
= "OneEnum";
123 auto G
= getYAMLGenerator();
126 llvm::raw_string_ostream
Actual(Buffer
);
127 auto Err
= G
->generateDocForInfo(&I
, Actual
, ClangDocContext());
129 std::string Expected
=
131 USR: '0000000000000000000000000000000000000000'
153 - Kind: 'FullComment'
155 - Kind: 'ParagraphComment'
157 - Kind: 'TextComment'
158 Text: 'Value of the thing.'
159 Name: 'ParagraphComment'
161 - USR: '0000000000000000000000000000000000000000'
172 - USR: '0000000000000000000000000000000000000000'
173 Name: 'InheritedFunctionOne'
185 QualName: 'path::to::G::G'
190 QualName: 'path::to::A::r::ChildStruct'
193 - USR: '0000000000000000000000000000000000000000'
198 - USR: '0000000000000000000000000000000000000000'
202 EXPECT_EQ(Expected
, Actual
.str());
205 TEST(YAMLGeneratorTest
, emitFunctionYAML
) {
208 I
.Namespace
.emplace_back(EmptySID
, "A", InfoType::IT_namespace
);
210 I
.DefLoc
= Location(10, llvm::SmallString
<16>{"test.cpp"});
211 I
.Loc
.emplace_back(12, llvm::SmallString
<16>{"test.cpp"});
213 I
.Access
= AccessSpecifier::AS_none
;
215 I
.ReturnType
= TypeInfo(Reference(EmptySID
, "void", InfoType::IT_default
));
216 I
.Params
.emplace_back(TypeInfo("int"), "P");
217 I
.Params
.emplace_back(TypeInfo("double"), "D");
218 I
.Params
.back().DefaultValue
= "2.0 * M_PI";
220 I
.Parent
= Reference(EmptySID
, "Parent", InfoType::IT_record
);
222 auto G
= getYAMLGenerator();
225 llvm::raw_string_ostream
Actual(Buffer
);
226 auto Err
= G
->generateDocForInfo(&I
, Actual
, ClangDocContext());
228 std::string Expected
=
230 USR: '0000000000000000000000000000000000000000'
256 DefaultValue: '2.0 * M_PI'
263 EXPECT_EQ(Expected
, Actual
.str());
266 // Tests the equivalent of:
270 TEST(YAMLGeneratorTest
, emitSimpleEnumYAML
) {
273 I
.Namespace
.emplace_back(EmptySID
, "A", InfoType::IT_namespace
);
275 I
.DefLoc
= Location(10, llvm::SmallString
<16>{"test.cpp"});
276 I
.Loc
.emplace_back(12, llvm::SmallString
<16>{"test.cpp"});
278 I
.Members
.emplace_back("X");
281 auto G
= getYAMLGenerator();
284 llvm::raw_string_ostream
Actual(Buffer
);
285 auto Err
= G
->generateDocForInfo(&I
, Actual
, ClangDocContext());
287 std::string Expected
=
289 USR: '0000000000000000000000000000000000000000'
306 EXPECT_EQ(Expected
, Actual
.str());
309 // Tests the equivalent of:
310 // enum class e : short { X = FOO_BAR + 2 };
311 TEST(YAMLGeneratorTest
, enumTypedScopedEnumYAML
) {
315 I
.Members
.emplace_back("X", "-9876", "FOO_BAR + 2");
317 I
.BaseType
= TypeInfo("short");
319 auto G
= getYAMLGenerator();
322 llvm::raw_string_ostream
Actual(Buffer
);
323 auto Err
= G
->generateDocForInfo(&I
, Actual
, ClangDocContext());
325 std::string Expected
=
327 USR: '0000000000000000000000000000000000000000'
340 EXPECT_EQ(Expected
, Actual
.str());
343 TEST(YAMLGeneratorTest
, enumTypedefYAML
) {
346 I
.Underlying
= TypeInfo("int");
349 auto G
= getYAMLGenerator();
352 llvm::raw_string_ostream
Actual(Buffer
);
353 auto Err
= G
->generateDocForInfo(&I
, Actual
, ClangDocContext());
355 std::string Expected
=
357 USR: '0000000000000000000000000000000000000000'
365 EXPECT_EQ(Expected
, Actual
.str());
368 TEST(YAMLGeneratorTest
, emitCommentYAML
) {
371 I
.DefLoc
= Location(10, llvm::SmallString
<16>{"test.cpp"});
372 I
.ReturnType
= TypeInfo("void");
373 I
.Params
.emplace_back(TypeInfo("int"), "I");
374 I
.Params
.emplace_back(TypeInfo("int"), "J");
375 I
.Access
= AccessSpecifier::AS_none
;
378 Top
.Kind
= "FullComment";
380 Top
.Children
.emplace_back(std::make_unique
<CommentInfo
>());
381 CommentInfo
*BlankLine
= Top
.Children
.back().get();
382 BlankLine
->Kind
= "ParagraphComment";
383 BlankLine
->Children
.emplace_back(std::make_unique
<CommentInfo
>());
384 BlankLine
->Children
.back()->Kind
= "TextComment";
386 Top
.Children
.emplace_back(std::make_unique
<CommentInfo
>());
387 CommentInfo
*Brief
= Top
.Children
.back().get();
388 Brief
->Kind
= "ParagraphComment";
389 Brief
->Children
.emplace_back(std::make_unique
<CommentInfo
>());
390 Brief
->Children
.back()->Kind
= "TextComment";
391 Brief
->Children
.back()->Name
= "ParagraphComment";
392 Brief
->Children
.back()->Text
= " Brief description.";
394 Top
.Children
.emplace_back(std::make_unique
<CommentInfo
>());
395 CommentInfo
*Extended
= Top
.Children
.back().get();
396 Extended
->Kind
= "ParagraphComment";
397 Extended
->Children
.emplace_back(std::make_unique
<CommentInfo
>());
398 Extended
->Children
.back()->Kind
= "TextComment";
399 Extended
->Children
.back()->Text
= " Extended description that";
400 Extended
->Children
.emplace_back(std::make_unique
<CommentInfo
>());
401 Extended
->Children
.back()->Kind
= "TextComment";
402 Extended
->Children
.back()->Text
= " continues onto the next line.";
404 Top
.Children
.emplace_back(std::make_unique
<CommentInfo
>());
405 CommentInfo
*HTML
= Top
.Children
.back().get();
406 HTML
->Kind
= "ParagraphComment";
407 HTML
->Children
.emplace_back(std::make_unique
<CommentInfo
>());
408 HTML
->Children
.back()->Kind
= "TextComment";
409 HTML
->Children
.emplace_back(std::make_unique
<CommentInfo
>());
410 HTML
->Children
.back()->Kind
= "HTMLStartTagComment";
411 HTML
->Children
.back()->Name
= "ul";
412 HTML
->Children
.back()->AttrKeys
.emplace_back("class");
413 HTML
->Children
.back()->AttrValues
.emplace_back("test");
414 HTML
->Children
.emplace_back(std::make_unique
<CommentInfo
>());
415 HTML
->Children
.back()->Kind
= "HTMLStartTagComment";
416 HTML
->Children
.back()->Name
= "li";
417 HTML
->Children
.emplace_back(std::make_unique
<CommentInfo
>());
418 HTML
->Children
.back()->Kind
= "TextComment";
419 HTML
->Children
.back()->Text
= " Testing.";
420 HTML
->Children
.emplace_back(std::make_unique
<CommentInfo
>());
421 HTML
->Children
.back()->Kind
= "HTMLEndTagComment";
422 HTML
->Children
.back()->Name
= "ul";
423 HTML
->Children
.back()->SelfClosing
= true;
425 Top
.Children
.emplace_back(std::make_unique
<CommentInfo
>());
426 CommentInfo
*Verbatim
= Top
.Children
.back().get();
427 Verbatim
->Kind
= "VerbatimBlockComment";
428 Verbatim
->Name
= "verbatim";
429 Verbatim
->CloseName
= "endverbatim";
430 Verbatim
->Children
.emplace_back(std::make_unique
<CommentInfo
>());
431 Verbatim
->Children
.back()->Kind
= "VerbatimBlockLineComment";
432 Verbatim
->Children
.back()->Text
= " The description continues.";
434 Top
.Children
.emplace_back(std::make_unique
<CommentInfo
>());
435 CommentInfo
*ParamOut
= Top
.Children
.back().get();
436 ParamOut
->Kind
= "ParamCommandComment";
437 ParamOut
->Direction
= "[out]";
438 ParamOut
->ParamName
= "I";
439 ParamOut
->Explicit
= true;
440 ParamOut
->Children
.emplace_back(std::make_unique
<CommentInfo
>());
441 ParamOut
->Children
.back()->Kind
= "ParagraphComment";
442 ParamOut
->Children
.back()->Children
.emplace_back(
443 std::make_unique
<CommentInfo
>());
444 ParamOut
->Children
.back()->Children
.back()->Kind
= "TextComment";
445 ParamOut
->Children
.back()->Children
.emplace_back(
446 std::make_unique
<CommentInfo
>());
447 ParamOut
->Children
.back()->Children
.back()->Kind
= "TextComment";
448 ParamOut
->Children
.back()->Children
.back()->Text
= " is a parameter.";
450 Top
.Children
.emplace_back(std::make_unique
<CommentInfo
>());
451 CommentInfo
*ParamIn
= Top
.Children
.back().get();
452 ParamIn
->Kind
= "ParamCommandComment";
453 ParamIn
->Direction
= "[in]";
454 ParamIn
->ParamName
= "J";
455 ParamIn
->Children
.emplace_back(std::make_unique
<CommentInfo
>());
456 ParamIn
->Children
.back()->Kind
= "ParagraphComment";
457 ParamIn
->Children
.back()->Children
.emplace_back(
458 std::make_unique
<CommentInfo
>());
459 ParamIn
->Children
.back()->Children
.back()->Kind
= "TextComment";
460 ParamIn
->Children
.back()->Children
.back()->Text
= " is a parameter.";
461 ParamIn
->Children
.back()->Children
.emplace_back(
462 std::make_unique
<CommentInfo
>());
463 ParamIn
->Children
.back()->Children
.back()->Kind
= "TextComment";
465 Top
.Children
.emplace_back(std::make_unique
<CommentInfo
>());
466 CommentInfo
*Return
= Top
.Children
.back().get();
467 Return
->Kind
= "BlockCommandComment";
468 Return
->Name
= "return";
469 Return
->Explicit
= true;
470 Return
->Children
.emplace_back(std::make_unique
<CommentInfo
>());
471 Return
->Children
.back()->Kind
= "ParagraphComment";
472 Return
->Children
.back()->Children
.emplace_back(
473 std::make_unique
<CommentInfo
>());
474 Return
->Children
.back()->Children
.back()->Kind
= "TextComment";
475 Return
->Children
.back()->Children
.back()->Text
= "void";
477 I
.Description
.emplace_back(std::move(Top
));
479 auto G
= getYAMLGenerator();
482 llvm::raw_string_ostream
Actual(Buffer
);
483 auto Err
= G
->generateDocForInfo(&I
, Actual
, ClangDocContext());
485 std::string Expected
=
487 USR: '0000000000000000000000000000000000000000'
490 - Kind: 'FullComment'
492 - Kind: 'ParagraphComment'
494 - Kind: 'TextComment'
495 - Kind: 'ParagraphComment'
497 - Kind: 'TextComment'
498 Text: ' Brief description.'
499 Name: 'ParagraphComment'
500 - Kind: 'ParagraphComment'
502 - Kind: 'TextComment'
503 Text: ' Extended description that'
504 - Kind: 'TextComment'
505 Text: ' continues onto the next line.'
506 - Kind: 'ParagraphComment'
508 - Kind: 'TextComment'
509 - Kind: 'HTMLStartTagComment'
515 - Kind: 'HTMLStartTagComment'
517 - Kind: 'TextComment'
519 - Kind: 'HTMLEndTagComment'
522 - Kind: 'VerbatimBlockComment'
524 CloseName: 'endverbatim'
526 - Kind: 'VerbatimBlockLineComment'
527 Text: ' The description continues.'
528 - Kind: 'ParamCommandComment'
533 - Kind: 'ParagraphComment'
535 - Kind: 'TextComment'
536 - Kind: 'TextComment'
537 Text: ' is a parameter.'
538 - Kind: 'ParamCommandComment'
542 - Kind: 'ParagraphComment'
544 - Kind: 'TextComment'
545 Text: ' is a parameter.'
546 - Kind: 'TextComment'
547 - Kind: 'BlockCommandComment'
551 - Kind: 'ParagraphComment'
553 - Kind: 'TextComment'
574 EXPECT_EQ(Expected
, Actual
.str());