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

source code of clang-tools-extra/unittests/clang-doc/MergeTest.cpp