xref: /llvm-project/clang-tools-extra/unittests/clang-doc/MergeTest.cpp (revision 68266828b170d2f978d0bc2f508de2b3b9e41665)
1 //===-- clang-doc/MergeTest.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 "Representation.h"
11 #include "gtest/gtest.h"
12 
13 namespace clang {
14 namespace doc {
15 
16 TEST(MergeTest, mergeNamespaceInfos) {
17   NamespaceInfo One;
18   One.Name = "Namespace";
19   One.Namespace.emplace_back(EmptySID, "A", InfoType::IT_namespace);
20 
21   One.ChildNamespaces.emplace_back(NonEmptySID, "ChildNamespace",
22                                    InfoType::IT_namespace);
23   One.ChildRecords.emplace_back(NonEmptySID, "ChildStruct",
24                                 InfoType::IT_record);
25   One.ChildFunctions.emplace_back();
26   One.ChildFunctions.back().Name = "OneFunction";
27   One.ChildFunctions.back().USR = NonEmptySID;
28   One.ChildEnums.emplace_back();
29   One.ChildEnums.back().Name = "OneEnum";
30   One.ChildEnums.back().USR = NonEmptySID;
31 
32   NamespaceInfo Two;
33   Two.Name = "Namespace";
34   Two.Namespace.emplace_back(EmptySID, "A", InfoType::IT_namespace);
35 
36   Two.ChildNamespaces.emplace_back(EmptySID, "OtherChildNamespace",
37                                    InfoType::IT_namespace);
38   Two.ChildRecords.emplace_back(EmptySID, "OtherChildStruct",
39                                 InfoType::IT_record);
40   Two.ChildFunctions.emplace_back();
41   Two.ChildFunctions.back().Name = "TwoFunction";
42   Two.ChildEnums.emplace_back();
43   Two.ChildEnums.back().Name = "TwoEnum";
44 
45   std::vector<std::unique_ptr<Info>> Infos;
46   Infos.emplace_back(std::make_unique<NamespaceInfo>(std::move(One)));
47   Infos.emplace_back(std::make_unique<NamespaceInfo>(std::move(Two)));
48 
49   auto Expected = std::make_unique<NamespaceInfo>();
50   Expected->Name = "Namespace";
51   Expected->Namespace.emplace_back(EmptySID, "A", InfoType::IT_namespace);
52 
53   Expected->ChildNamespaces.emplace_back(NonEmptySID, "ChildNamespace",
54                                          InfoType::IT_namespace);
55   Expected->ChildRecords.emplace_back(NonEmptySID, "ChildStruct",
56                                       InfoType::IT_record);
57   Expected->ChildNamespaces.emplace_back(EmptySID, "OtherChildNamespace",
58                                          InfoType::IT_namespace);
59   Expected->ChildRecords.emplace_back(EmptySID, "OtherChildStruct",
60                                       InfoType::IT_record);
61   Expected->ChildFunctions.emplace_back();
62   Expected->ChildFunctions.back().Name = "OneFunction";
63   Expected->ChildFunctions.back().USR = NonEmptySID;
64   Expected->ChildFunctions.emplace_back();
65   Expected->ChildFunctions.back().Name = "TwoFunction";
66   Expected->ChildEnums.emplace_back();
67   Expected->ChildEnums.back().Name = "OneEnum";
68   Expected->ChildEnums.back().USR = NonEmptySID;
69   Expected->ChildEnums.emplace_back();
70   Expected->ChildEnums.back().Name = "TwoEnum";
71 
72   auto Actual = mergeInfos(Infos);
73   assert(Actual);
74   CheckNamespaceInfo(InfoAsNamespace(Expected.get()),
75                      InfoAsNamespace(Actual.get().get()));
76 }
77 
78 TEST(MergeTest, mergeRecordInfos) {
79   RecordInfo One;
80   One.Name = "r";
81   One.IsTypeDef = true;
82   One.Namespace.emplace_back(EmptySID, "A", InfoType::IT_namespace);
83 
84   One.DefLoc = Location(10, llvm::SmallString<16>{"test.cpp"});
85 
86   One.Members.emplace_back("int", "X", AccessSpecifier::AS_private);
87   One.TagType = TagTypeKind::TTK_Class;
88   One.Parents.emplace_back(EmptySID, "F", InfoType::IT_record);
89   One.VirtualParents.emplace_back(EmptySID, "G", InfoType::IT_record);
90 
91   One.Bases.emplace_back(EmptySID, "F", "path/to/F", true,
92                          AccessSpecifier::AS_protected, true);
93   One.ChildRecords.emplace_back(NonEmptySID, "SharedChildStruct",
94                                 InfoType::IT_record);
95   One.ChildFunctions.emplace_back();
96   One.ChildFunctions.back().Name = "OneFunction";
97   One.ChildFunctions.back().USR = NonEmptySID;
98   One.ChildEnums.emplace_back();
99   One.ChildEnums.back().Name = "OneEnum";
100   One.ChildEnums.back().USR = NonEmptySID;
101 
102   RecordInfo Two;
103   Two.Name = "r";
104   Two.Namespace.emplace_back(EmptySID, "A", InfoType::IT_namespace);
105 
106   Two.Loc.emplace_back(12, llvm::SmallString<16>{"test.cpp"});
107 
108   Two.TagType = TagTypeKind::TTK_Class;
109 
110   Two.ChildRecords.emplace_back(NonEmptySID, "SharedChildStruct",
111                                 InfoType::IT_record, "path");
112   Two.ChildFunctions.emplace_back();
113   Two.ChildFunctions.back().Name = "TwoFunction";
114   Two.ChildEnums.emplace_back();
115   Two.ChildEnums.back().Name = "TwoEnum";
116 
117   std::vector<std::unique_ptr<Info>> Infos;
118   Infos.emplace_back(std::make_unique<RecordInfo>(std::move(One)));
119   Infos.emplace_back(std::make_unique<RecordInfo>(std::move(Two)));
120 
121   auto Expected = std::make_unique<RecordInfo>();
122   Expected->Name = "r";
123   Expected->IsTypeDef = true;
124   Expected->Namespace.emplace_back(EmptySID, "A", InfoType::IT_namespace);
125 
126   Expected->DefLoc = Location(10, llvm::SmallString<16>{"test.cpp"});
127   Expected->Loc.emplace_back(12, llvm::SmallString<16>{"test.cpp"});
128 
129   Expected->Members.emplace_back("int", "X", AccessSpecifier::AS_private);
130   Expected->TagType = TagTypeKind::TTK_Class;
131   Expected->Parents.emplace_back(EmptySID, "F", InfoType::IT_record);
132   Expected->VirtualParents.emplace_back(EmptySID, "G", InfoType::IT_record);
133   Expected->Bases.emplace_back(EmptySID, "F", "path/to/F", true,
134                                AccessSpecifier::AS_protected, true);
135 
136   Expected->ChildRecords.emplace_back(NonEmptySID, "SharedChildStruct",
137                                       InfoType::IT_record, "path");
138   Expected->ChildFunctions.emplace_back();
139   Expected->ChildFunctions.back().Name = "OneFunction";
140   Expected->ChildFunctions.back().USR = NonEmptySID;
141   Expected->ChildFunctions.emplace_back();
142   Expected->ChildFunctions.back().Name = "TwoFunction";
143   Expected->ChildEnums.emplace_back();
144   Expected->ChildEnums.back().Name = "OneEnum";
145   Expected->ChildEnums.back().USR = NonEmptySID;
146   Expected->ChildEnums.emplace_back();
147   Expected->ChildEnums.back().Name = "TwoEnum";
148 
149   auto Actual = mergeInfos(Infos);
150   assert(Actual);
151   CheckRecordInfo(InfoAsRecord(Expected.get()),
152                   InfoAsRecord(Actual.get().get()));
153 }
154 
155 TEST(MergeTest, mergeFunctionInfos) {
156   FunctionInfo One;
157   One.Name = "f";
158   One.Namespace.emplace_back(EmptySID, "A", InfoType::IT_namespace);
159 
160   One.DefLoc = Location(10, llvm::SmallString<16>{"test.cpp"});
161   One.Loc.emplace_back(12, llvm::SmallString<16>{"test.cpp"});
162 
163   One.IsMethod = true;
164   One.Parent = Reference(EmptySID, "Parent", InfoType::IT_namespace);
165 
166   One.Description.emplace_back();
167   auto OneFullComment = &One.Description.back();
168   OneFullComment->Kind = "FullComment";
169   auto OneParagraphComment = std::make_unique<CommentInfo>();
170   OneParagraphComment->Kind = "ParagraphComment";
171   auto OneTextComment = std::make_unique<CommentInfo>();
172   OneTextComment->Kind = "TextComment";
173   OneTextComment->Text = "This is a text comment.";
174   OneParagraphComment->Children.push_back(std::move(OneTextComment));
175   OneFullComment->Children.push_back(std::move(OneParagraphComment));
176 
177   FunctionInfo Two;
178   Two.Name = "f";
179   Two.Namespace.emplace_back(EmptySID, "A", InfoType::IT_namespace);
180 
181   Two.Loc.emplace_back(12, llvm::SmallString<16>{"test.cpp"});
182 
183   Two.ReturnType = TypeInfo(EmptySID, "void", InfoType::IT_default);
184   Two.Params.emplace_back("int", "P");
185 
186   Two.Description.emplace_back();
187   auto TwoFullComment = &Two.Description.back();
188   TwoFullComment->Kind = "FullComment";
189   auto TwoParagraphComment = std::make_unique<CommentInfo>();
190   TwoParagraphComment->Kind = "ParagraphComment";
191   auto TwoTextComment = std::make_unique<CommentInfo>();
192   TwoTextComment->Kind = "TextComment";
193   TwoTextComment->Text = "This is a text comment.";
194   TwoParagraphComment->Children.push_back(std::move(TwoTextComment));
195   TwoFullComment->Children.push_back(std::move(TwoParagraphComment));
196 
197   std::vector<std::unique_ptr<Info>> Infos;
198   Infos.emplace_back(std::make_unique<FunctionInfo>(std::move(One)));
199   Infos.emplace_back(std::make_unique<FunctionInfo>(std::move(Two)));
200 
201   auto Expected = std::make_unique<FunctionInfo>();
202   Expected->Name = "f";
203   Expected->Namespace.emplace_back(EmptySID, "A", InfoType::IT_namespace);
204 
205   Expected->DefLoc = Location(10, llvm::SmallString<16>{"test.cpp"});
206   Expected->Loc.emplace_back(12, llvm::SmallString<16>{"test.cpp"});
207 
208   Expected->ReturnType = TypeInfo(EmptySID, "void", InfoType::IT_default);
209   Expected->Params.emplace_back("int", "P");
210   Expected->IsMethod = true;
211   Expected->Parent = Reference(EmptySID, "Parent", InfoType::IT_namespace);
212 
213   Expected->Description.emplace_back();
214   auto ExpectedFullComment = &Expected->Description.back();
215   ExpectedFullComment->Kind = "FullComment";
216   auto ExpectedParagraphComment = std::make_unique<CommentInfo>();
217   ExpectedParagraphComment->Kind = "ParagraphComment";
218   auto ExpectedTextComment = std::make_unique<CommentInfo>();
219   ExpectedTextComment->Kind = "TextComment";
220   ExpectedTextComment->Text = "This is a text comment.";
221   ExpectedParagraphComment->Children.push_back(std::move(ExpectedTextComment));
222   ExpectedFullComment->Children.push_back(std::move(ExpectedParagraphComment));
223 
224   auto Actual = mergeInfos(Infos);
225   assert(Actual);
226   CheckFunctionInfo(InfoAsFunction(Expected.get()),
227                     InfoAsFunction(Actual.get().get()));
228 }
229 
230 TEST(MergeTest, mergeEnumInfos) {
231   EnumInfo One;
232   One.Name = "e";
233   One.Namespace.emplace_back(EmptySID, "A", InfoType::IT_namespace);
234 
235   One.DefLoc = Location(10, llvm::SmallString<16>{"test.cpp"});
236   One.Loc.emplace_back(12, llvm::SmallString<16>{"test.cpp"});
237 
238   One.Scoped = true;
239 
240   EnumInfo Two;
241   Two.Name = "e";
242   Two.Namespace.emplace_back(EmptySID, "A", InfoType::IT_namespace);
243 
244   Two.Loc.emplace_back(20, llvm::SmallString<16>{"test.cpp"});
245 
246   Two.Members.emplace_back("X");
247   Two.Members.emplace_back("Y");
248 
249   std::vector<std::unique_ptr<Info>> Infos;
250   Infos.emplace_back(std::make_unique<EnumInfo>(std::move(One)));
251   Infos.emplace_back(std::make_unique<EnumInfo>(std::move(Two)));
252 
253   auto Expected = std::make_unique<EnumInfo>();
254   Expected->Name = "e";
255   Expected->Namespace.emplace_back(EmptySID, "A", InfoType::IT_namespace);
256 
257   Expected->DefLoc = Location(10, llvm::SmallString<16>{"test.cpp"});
258   Expected->Loc.emplace_back(12, llvm::SmallString<16>{"test.cpp"});
259   Expected->Loc.emplace_back(20, llvm::SmallString<16>{"test.cpp"});
260 
261   Expected->Members.emplace_back("X");
262   Expected->Members.emplace_back("Y");
263   Expected->Scoped = true;
264 
265   auto Actual = mergeInfos(Infos);
266   assert(Actual);
267   CheckEnumInfo(InfoAsEnum(Expected.get()), InfoAsEnum(Actual.get().get()));
268 }
269 
270 } // namespace doc
271 } // namespace clang
272