xref: /llvm-project/clang-tools-extra/unittests/clang-doc/MergeTest.cpp (revision 93a290fdc974007ed3eeb7be11b527f66f43a01d)
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(llvm::make_unique<NamespaceInfo>(std::move(One)));
47   Infos.emplace_back(llvm::make_unique<NamespaceInfo>(std::move(Two)));
48 
49   auto Expected = llvm::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.Namespace.emplace_back(EmptySID, "A", InfoType::IT_namespace);
82 
83   One.DefLoc = Location(10, llvm::SmallString<16>{"test.cpp"});
84 
85   One.Members.emplace_back("int", "X", AccessSpecifier::AS_private);
86   One.TagType = TagTypeKind::TTK_Class;
87   One.Parents.emplace_back(EmptySID, "F", InfoType::IT_record);
88   One.VirtualParents.emplace_back(EmptySID, "G", InfoType::IT_record);
89 
90   One.ChildRecords.emplace_back(NonEmptySID, "ChildStruct",
91                                 InfoType::IT_record);
92   One.ChildFunctions.emplace_back();
93   One.ChildFunctions.back().Name = "OneFunction";
94   One.ChildFunctions.back().USR = NonEmptySID;
95   One.ChildEnums.emplace_back();
96   One.ChildEnums.back().Name = "OneEnum";
97   One.ChildEnums.back().USR = NonEmptySID;
98 
99   RecordInfo Two;
100   Two.Name = "r";
101   Two.Namespace.emplace_back(EmptySID, "A", InfoType::IT_namespace);
102 
103   Two.Loc.emplace_back(12, llvm::SmallString<16>{"test.cpp"});
104 
105   Two.TagType = TagTypeKind::TTK_Class;
106 
107   Two.ChildRecords.emplace_back(EmptySID, "OtherChildStruct",
108                                 InfoType::IT_record);
109   Two.ChildFunctions.emplace_back();
110   Two.ChildFunctions.back().Name = "TwoFunction";
111   Two.ChildEnums.emplace_back();
112   Two.ChildEnums.back().Name = "TwoEnum";
113 
114   std::vector<std::unique_ptr<Info>> Infos;
115   Infos.emplace_back(llvm::make_unique<RecordInfo>(std::move(One)));
116   Infos.emplace_back(llvm::make_unique<RecordInfo>(std::move(Two)));
117 
118   auto Expected = llvm::make_unique<RecordInfo>();
119   Expected->Name = "r";
120   Expected->Namespace.emplace_back(EmptySID, "A", InfoType::IT_namespace);
121 
122   Expected->DefLoc = Location(10, llvm::SmallString<16>{"test.cpp"});
123   Expected->Loc.emplace_back(12, llvm::SmallString<16>{"test.cpp"});
124 
125   Expected->Members.emplace_back("int", "X", AccessSpecifier::AS_private);
126   Expected->TagType = TagTypeKind::TTK_Class;
127   Expected->Parents.emplace_back(EmptySID, "F", InfoType::IT_record);
128   Expected->VirtualParents.emplace_back(EmptySID, "G", InfoType::IT_record);
129 
130   Expected->ChildRecords.emplace_back(NonEmptySID, "ChildStruct",
131                                       InfoType::IT_record);
132   Expected->ChildRecords.emplace_back(EmptySID, "OtherChildStruct",
133                                       InfoType::IT_record);
134   Expected->ChildFunctions.emplace_back();
135   Expected->ChildFunctions.back().Name = "OneFunction";
136   Expected->ChildFunctions.back().USR = NonEmptySID;
137   Expected->ChildFunctions.emplace_back();
138   Expected->ChildFunctions.back().Name = "TwoFunction";
139   Expected->ChildEnums.emplace_back();
140   Expected->ChildEnums.back().Name = "OneEnum";
141   Expected->ChildEnums.back().USR = NonEmptySID;
142   Expected->ChildEnums.emplace_back();
143   Expected->ChildEnums.back().Name = "TwoEnum";
144 
145   auto Actual = mergeInfos(Infos);
146   assert(Actual);
147   CheckRecordInfo(InfoAsRecord(Expected.get()),
148                   InfoAsRecord(Actual.get().get()));
149 }
150 
151 TEST(MergeTest, mergeFunctionInfos) {
152   FunctionInfo One;
153   One.Name = "f";
154   One.Namespace.emplace_back(EmptySID, "A", InfoType::IT_namespace);
155 
156   One.DefLoc = Location(10, llvm::SmallString<16>{"test.cpp"});
157   One.Loc.emplace_back(12, llvm::SmallString<16>{"test.cpp"});
158 
159   One.IsMethod = true;
160   One.Parent = Reference(EmptySID, "Parent", InfoType::IT_namespace);
161 
162   One.Description.emplace_back();
163   auto OneFullComment = &One.Description.back();
164   OneFullComment->Kind = "FullComment";
165   auto OneParagraphComment = llvm::make_unique<CommentInfo>();
166   OneParagraphComment->Kind = "ParagraphComment";
167   auto OneTextComment = llvm::make_unique<CommentInfo>();
168   OneTextComment->Kind = "TextComment";
169   OneTextComment->Text = "This is a text comment.";
170   OneParagraphComment->Children.push_back(std::move(OneTextComment));
171   OneFullComment->Children.push_back(std::move(OneParagraphComment));
172 
173   FunctionInfo Two;
174   Two.Name = "f";
175   Two.Namespace.emplace_back(EmptySID, "A", InfoType::IT_namespace);
176 
177   Two.Loc.emplace_back(12, llvm::SmallString<16>{"test.cpp"});
178 
179   Two.ReturnType = TypeInfo(EmptySID, "void", InfoType::IT_default);
180   Two.Params.emplace_back("int", "P");
181 
182   Two.Description.emplace_back();
183   auto TwoFullComment = &Two.Description.back();
184   TwoFullComment->Kind = "FullComment";
185   auto TwoParagraphComment = llvm::make_unique<CommentInfo>();
186   TwoParagraphComment->Kind = "ParagraphComment";
187   auto TwoTextComment = llvm::make_unique<CommentInfo>();
188   TwoTextComment->Kind = "TextComment";
189   TwoTextComment->Text = "This is a text comment.";
190   TwoParagraphComment->Children.push_back(std::move(TwoTextComment));
191   TwoFullComment->Children.push_back(std::move(TwoParagraphComment));
192 
193   std::vector<std::unique_ptr<Info>> Infos;
194   Infos.emplace_back(llvm::make_unique<FunctionInfo>(std::move(One)));
195   Infos.emplace_back(llvm::make_unique<FunctionInfo>(std::move(Two)));
196 
197   auto Expected = llvm::make_unique<FunctionInfo>();
198   Expected->Name = "f";
199   Expected->Namespace.emplace_back(EmptySID, "A", InfoType::IT_namespace);
200 
201   Expected->DefLoc = Location(10, llvm::SmallString<16>{"test.cpp"});
202   Expected->Loc.emplace_back(12, llvm::SmallString<16>{"test.cpp"});
203 
204   Expected->ReturnType = TypeInfo(EmptySID, "void", InfoType::IT_default);
205   Expected->Params.emplace_back("int", "P");
206   Expected->IsMethod = true;
207   Expected->Parent = Reference(EmptySID, "Parent", InfoType::IT_namespace);
208 
209   Expected->Description.emplace_back();
210   auto ExpectedFullComment = &Expected->Description.back();
211   ExpectedFullComment->Kind = "FullComment";
212   auto ExpectedParagraphComment = llvm::make_unique<CommentInfo>();
213   ExpectedParagraphComment->Kind = "ParagraphComment";
214   auto ExpectedTextComment = llvm::make_unique<CommentInfo>();
215   ExpectedTextComment->Kind = "TextComment";
216   ExpectedTextComment->Text = "This is a text comment.";
217   ExpectedParagraphComment->Children.push_back(std::move(ExpectedTextComment));
218   ExpectedFullComment->Children.push_back(std::move(ExpectedParagraphComment));
219 
220   auto Actual = mergeInfos(Infos);
221   assert(Actual);
222   CheckFunctionInfo(InfoAsFunction(Expected.get()),
223                     InfoAsFunction(Actual.get().get()));
224 }
225 
226 TEST(MergeTest, mergeEnumInfos) {
227   EnumInfo One;
228   One.Name = "e";
229   One.Namespace.emplace_back(EmptySID, "A", InfoType::IT_namespace);
230 
231   One.DefLoc = Location(10, llvm::SmallString<16>{"test.cpp"});
232   One.Loc.emplace_back(12, llvm::SmallString<16>{"test.cpp"});
233 
234   One.Scoped = true;
235 
236   EnumInfo Two;
237   Two.Name = "e";
238   Two.Namespace.emplace_back(EmptySID, "A", InfoType::IT_namespace);
239 
240   Two.Loc.emplace_back(20, llvm::SmallString<16>{"test.cpp"});
241 
242   Two.Members.emplace_back("X");
243   Two.Members.emplace_back("Y");
244 
245   std::vector<std::unique_ptr<Info>> Infos;
246   Infos.emplace_back(llvm::make_unique<EnumInfo>(std::move(One)));
247   Infos.emplace_back(llvm::make_unique<EnumInfo>(std::move(Two)));
248 
249   auto Expected = llvm::make_unique<EnumInfo>();
250   Expected->Name = "e";
251   Expected->Namespace.emplace_back(EmptySID, "A", InfoType::IT_namespace);
252 
253   Expected->DefLoc = Location(10, llvm::SmallString<16>{"test.cpp"});
254   Expected->Loc.emplace_back(12, llvm::SmallString<16>{"test.cpp"});
255   Expected->Loc.emplace_back(20, llvm::SmallString<16>{"test.cpp"});
256 
257   Expected->Members.emplace_back("X");
258   Expected->Members.emplace_back("Y");
259   Expected->Scoped = true;
260 
261   auto Actual = mergeInfos(Infos);
262   assert(Actual);
263   CheckEnumInfo(InfoAsEnum(Expected.get()), InfoAsEnum(Actual.get().get()));
264 }
265 
266 } // namespace doc
267 } // namespace clang
268