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