Debian Codegraph @

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

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