1//===-- clang-doc/ClangDocTest.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 "clang/AST/RecursiveASTVisitor.h"
12#include "gtest/gtest.h"
13
14namespace clang {
15namespace doc {
16
17NamespaceInfo *InfoAsNamespace(Info *I) {
18 assert(I->IT == InfoType::IT_namespace);
19 return static_cast<NamespaceInfo *>(I);
20}
21
22RecordInfo *InfoAsRecord(Info *I) {
23 assert(I->IT == InfoType::IT_record);
24 return static_cast<RecordInfo *>(I);
25}
26
27FunctionInfo *InfoAsFunction(Info *I) {
28 assert(I->IT == InfoType::IT_function);
29 return static_cast<FunctionInfo *>(I);
30}
31
32EnumInfo *InfoAsEnum(Info *I) {
33 assert(I->IT == InfoType::IT_enum);
34 return static_cast<EnumInfo *>(I);
35}
36
37TypedefInfo *InfoAsTypedef(Info *I) {
38 assert(I->IT == InfoType::IT_typedef);
39 return static_cast<TypedefInfo *>(I);
40}
41
42void CheckCommentInfo(const std::vector<CommentInfo> &Expected,
43 const std::vector<CommentInfo> &Actual);
44void CheckCommentInfo(const std::vector<std::unique_ptr<CommentInfo>> &Expected,
45 const std::vector<std::unique_ptr<CommentInfo>> &Actual);
46
47void CheckCommentInfo(const CommentInfo &Expected, const CommentInfo &Actual) {
48 EXPECT_EQ(Expected.Kind, Actual.Kind);
49 EXPECT_EQ(Expected.Text, Actual.Text);
50 EXPECT_EQ(Expected.Name, Actual.Name);
51 EXPECT_EQ(Expected.Direction, Actual.Direction);
52 EXPECT_EQ(Expected.ParamName, Actual.ParamName);
53 EXPECT_EQ(Expected.CloseName, Actual.CloseName);
54 EXPECT_EQ(Expected.SelfClosing, Actual.SelfClosing);
55 EXPECT_EQ(Expected.Explicit, Actual.Explicit);
56
57 ASSERT_EQ(Expected.AttrKeys.size(), Actual.AttrKeys.size());
58 for (size_t Idx = 0; Idx < Actual.AttrKeys.size(); ++Idx)
59 EXPECT_EQ(Expected.AttrKeys[Idx], Actual.AttrKeys[Idx]);
60
61 ASSERT_EQ(Expected.AttrValues.size(), Actual.AttrValues.size());
62 for (size_t Idx = 0; Idx < Actual.AttrValues.size(); ++Idx)
63 EXPECT_EQ(Expected.AttrValues[Idx], Actual.AttrValues[Idx]);
64
65 ASSERT_EQ(Expected.Args.size(), Actual.Args.size());
66 for (size_t Idx = 0; Idx < Actual.Args.size(); ++Idx)
67 EXPECT_EQ(Expected.Args[Idx], Actual.Args[Idx]);
68
69 CheckCommentInfo(Expected: Expected.Children, Actual: Actual.Children);
70}
71
72void CheckCommentInfo(const std::vector<CommentInfo> &Expected,
73 const std::vector<CommentInfo> &Actual) {
74 ASSERT_EQ(Expected.size(), Actual.size());
75 for (size_t Idx = 0; Idx < Actual.size(); ++Idx)
76 CheckCommentInfo(Expected: Expected[Idx], Actual: Actual[Idx]);
77}
78
79void CheckCommentInfo(const std::vector<std::unique_ptr<CommentInfo>> &Expected,
80 const std::vector<std::unique_ptr<CommentInfo>> &Actual) {
81 ASSERT_EQ(Expected.size(), Actual.size());
82 for (size_t Idx = 0; Idx < Actual.size(); ++Idx)
83 CheckCommentInfo(Expected: *Expected[Idx], Actual: *Actual[Idx]);
84}
85
86void CheckReference(Reference &Expected, Reference &Actual) {
87 EXPECT_EQ(Expected.Name, Actual.Name);
88 EXPECT_EQ(Expected.RefType, Actual.RefType);
89 EXPECT_EQ(Expected.Path, Actual.Path);
90}
91
92void CheckTypeInfo(TypeInfo *Expected, TypeInfo *Actual) {
93 CheckReference(Expected&: Expected->Type, Actual&: Actual->Type);
94}
95
96void CheckFieldTypeInfo(FieldTypeInfo *Expected, FieldTypeInfo *Actual) {
97 CheckTypeInfo(Expected, Actual);
98 EXPECT_EQ(Expected->Name, Actual->Name);
99}
100
101void CheckMemberTypeInfo(MemberTypeInfo *Expected, MemberTypeInfo *Actual) {
102 CheckFieldTypeInfo(Expected, Actual);
103 EXPECT_EQ(Expected->Access, Actual->Access);
104 CheckCommentInfo(Expected: Expected->Description, Actual: Actual->Description);
105}
106
107void CheckBaseInfo(Info *Expected, Info *Actual) {
108 EXPECT_EQ(size_t(20), Actual->USR.size());
109 EXPECT_EQ(Expected->Name, Actual->Name);
110 EXPECT_EQ(Expected->Path, Actual->Path);
111 ASSERT_EQ(Expected->Namespace.size(), Actual->Namespace.size());
112 for (size_t Idx = 0; Idx < Actual->Namespace.size(); ++Idx)
113 CheckReference(Expected&: Expected->Namespace[Idx], Actual&: Actual->Namespace[Idx]);
114 CheckCommentInfo(Expected: Expected->Description, Actual: Actual->Description);
115}
116
117void CheckSymbolInfo(SymbolInfo *Expected, SymbolInfo *Actual) {
118 CheckBaseInfo(Expected, Actual);
119 EXPECT_EQ(Expected->DefLoc.has_value(), Actual->DefLoc.has_value());
120 if (Expected->DefLoc && Actual->DefLoc.has_value()) {
121 EXPECT_EQ(Expected->DefLoc->LineNumber, Actual->DefLoc->LineNumber);
122 EXPECT_EQ(Expected->DefLoc->Filename, Actual->DefLoc->Filename);
123 }
124 ASSERT_EQ(Expected->Loc.size(), Actual->Loc.size());
125 for (size_t Idx = 0; Idx < Actual->Loc.size(); ++Idx)
126 EXPECT_EQ(Expected->Loc[Idx], Actual->Loc[Idx]);
127}
128
129void CheckFunctionInfo(FunctionInfo *Expected, FunctionInfo *Actual) {
130 CheckSymbolInfo(Expected, Actual);
131
132 EXPECT_EQ(Expected->IsMethod, Actual->IsMethod);
133 CheckReference(Expected&: Expected->Parent, Actual&: Actual->Parent);
134 CheckTypeInfo(Expected: &Expected->ReturnType, Actual: &Actual->ReturnType);
135
136 ASSERT_EQ(Expected->Params.size(), Actual->Params.size());
137 for (size_t Idx = 0; Idx < Actual->Params.size(); ++Idx)
138 EXPECT_EQ(Expected->Params[Idx], Actual->Params[Idx]);
139
140 EXPECT_EQ(Expected->Access, Actual->Access);
141}
142
143void CheckEnumInfo(EnumInfo *Expected, EnumInfo *Actual) {
144 CheckSymbolInfo(Expected, Actual);
145
146 EXPECT_EQ(Expected->Scoped, Actual->Scoped);
147 ASSERT_EQ(Expected->Members.size(), Actual->Members.size());
148 for (size_t Idx = 0; Idx < Actual->Members.size(); ++Idx)
149 EXPECT_EQ(Expected->Members[Idx], Actual->Members[Idx]);
150}
151
152void CheckTypedefInfo(TypedefInfo *Expected, TypedefInfo *Actual) {
153 CheckSymbolInfo(Expected, Actual);
154 EXPECT_EQ(Expected->IsUsing, Actual->IsUsing);
155 CheckTypeInfo(Expected: &Expected->Underlying, Actual: &Actual->Underlying);
156}
157
158void CheckNamespaceInfo(NamespaceInfo *Expected, NamespaceInfo *Actual) {
159 CheckBaseInfo(Expected, Actual);
160
161 ASSERT_EQ(Expected->Children.Namespaces.size(),
162 Actual->Children.Namespaces.size());
163 for (size_t Idx = 0; Idx < Actual->Children.Namespaces.size(); ++Idx)
164 CheckReference(Expected&: Expected->Children.Namespaces[Idx],
165 Actual&: Actual->Children.Namespaces[Idx]);
166
167 ASSERT_EQ(Expected->Children.Records.size(), Actual->Children.Records.size());
168 for (size_t Idx = 0; Idx < Actual->Children.Records.size(); ++Idx)
169 CheckReference(Expected&: Expected->Children.Records[Idx],
170 Actual&: Actual->Children.Records[Idx]);
171
172 ASSERT_EQ(Expected->Children.Functions.size(),
173 Actual->Children.Functions.size());
174 for (size_t Idx = 0; Idx < Actual->Children.Functions.size(); ++Idx)
175 CheckFunctionInfo(Expected: &Expected->Children.Functions[Idx],
176 Actual: &Actual->Children.Functions[Idx]);
177
178 ASSERT_EQ(Expected->Children.Enums.size(), Actual->Children.Enums.size());
179 for (size_t Idx = 0; Idx < Actual->Children.Enums.size(); ++Idx)
180 CheckEnumInfo(Expected: &Expected->Children.Enums[Idx], Actual: &Actual->Children.Enums[Idx]);
181}
182
183void CheckRecordInfo(RecordInfo *Expected, RecordInfo *Actual) {
184 CheckSymbolInfo(Expected, Actual);
185
186 EXPECT_EQ(Expected->TagType, Actual->TagType);
187
188 EXPECT_EQ(Expected->IsTypeDef, Actual->IsTypeDef);
189
190 ASSERT_EQ(Expected->Members.size(), Actual->Members.size());
191 for (size_t Idx = 0; Idx < Actual->Members.size(); ++Idx)
192 EXPECT_EQ(Expected->Members[Idx], Actual->Members[Idx]);
193
194 ASSERT_EQ(Expected->Parents.size(), Actual->Parents.size());
195 for (size_t Idx = 0; Idx < Actual->Parents.size(); ++Idx)
196 CheckReference(Expected&: Expected->Parents[Idx], Actual&: Actual->Parents[Idx]);
197
198 ASSERT_EQ(Expected->VirtualParents.size(), Actual->VirtualParents.size());
199 for (size_t Idx = 0; Idx < Actual->VirtualParents.size(); ++Idx)
200 CheckReference(Expected&: Expected->VirtualParents[Idx], Actual&: Actual->VirtualParents[Idx]);
201
202 ASSERT_EQ(Expected->Bases.size(), Actual->Bases.size());
203 for (size_t Idx = 0; Idx < Actual->Bases.size(); ++Idx)
204 CheckBaseRecordInfo(Expected: &Expected->Bases[Idx], Actual: &Actual->Bases[Idx]);
205
206 ASSERT_EQ(Expected->Children.Records.size(), Actual->Children.Records.size());
207 for (size_t Idx = 0; Idx < Actual->Children.Records.size(); ++Idx)
208 CheckReference(Expected&: Expected->Children.Records[Idx],
209 Actual&: Actual->Children.Records[Idx]);
210
211 ASSERT_EQ(Expected->Children.Functions.size(),
212 Actual->Children.Functions.size());
213 for (size_t Idx = 0; Idx < Actual->Children.Functions.size(); ++Idx)
214 CheckFunctionInfo(Expected: &Expected->Children.Functions[Idx],
215 Actual: &Actual->Children.Functions[Idx]);
216
217 ASSERT_EQ(Expected->Children.Enums.size(), Actual->Children.Enums.size());
218 for (size_t Idx = 0; Idx < Actual->Children.Enums.size(); ++Idx)
219 CheckEnumInfo(Expected: &Expected->Children.Enums[Idx], Actual: &Actual->Children.Enums[Idx]);
220}
221
222void CheckBaseRecordInfo(BaseRecordInfo *Expected, BaseRecordInfo *Actual) {
223 CheckRecordInfo(Expected, Actual);
224
225 EXPECT_EQ(Expected->IsVirtual, Actual->IsVirtual);
226 EXPECT_EQ(Expected->Access, Actual->Access);
227 EXPECT_EQ(Expected->IsParent, Actual->IsParent);
228}
229
230void CheckIndex(Index &Expected, Index &Actual) {
231 CheckReference(Expected, Actual);
232 ASSERT_EQ(Expected.Children.size(), Actual.Children.size());
233 for (size_t Idx = 0; Idx < Actual.Children.size(); ++Idx)
234 CheckIndex(Expected&: Expected.Children[Idx], Actual&: Actual.Children[Idx]);
235}
236
237} // namespace doc
238} // namespace clang
239

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