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