Debian Codegraph @

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

raw
1 #include "ClangDocTest.h"
2 #include "Generators.h"
3 #include "Representation.h"
4 #include "gtest/gtest.h"
5
6 namespace clang {
7 namespace doc {
8
9 static std::unique_ptr<Generator> getJSONGenerator() {
10 auto G = doc::findGeneratorByName("json");
11 if (!G)
12 return nullptr;
13 return std::move(G.get());
14 }
15
16 class JSONGeneratorTest : public ClangDocContextTest {};
17
18 TEST_F(JSONGeneratorTest, emitRecordJSON) {
19 RecordInfo I;
20 I.Name = "Foo";
21 I.IsTypeDef = false;
22 I.Namespace.emplace_back(EmptySID, "GlobalNamespace", InfoType::IT_namespace);
23 I.Path = "GlobalNamespace";
24 I.DefLoc = Location(1, 1, "main.cpp");
25 I.TagType = TagTypeKind::Class;
26
27 I.Template = TemplateInfo();
28 I.Template->Params.emplace_back("class T");
29
30 I.Children.Enums.emplace_back();
31 I.Children.Enums.back().Name = "Color";
32 I.Children.Enums.back().Scoped = false;
33 I.Children.Enums.back().Members.emplace_back();
34 I.Children.Enums.back().Members.back().Name = "RED";
35 I.Children.Enums.back().Members.back().Value = "0";
36
37 I.Members.emplace_back(TypeInfo("int"), "X", AccessSpecifier::AS_protected);
38
39 I.Bases.emplace_back(EmptySID, "F", "path/to/F", true,
40 AccessSpecifier::AS_public, true);
41 I.Bases.back().Children.Functions.emplace_back();
42 I.Bases.back().Children.Functions.back().Name = "InheritedFunctionOne";
43 I.Bases.back().Members.emplace_back(TypeInfo("int"), "N",
44 AccessSpecifier::AS_public);
45
46 // F is in the global namespace
47 I.Parents.emplace_back(EmptySID, "F", InfoType::IT_record, "");
48 I.VirtualParents.emplace_back(EmptySID, "G", InfoType::IT_record,
49 "path::to::G::G", "path/to/G");
50
51 I.Children.Records.emplace_back(EmptySID, "ChildStruct", InfoType::IT_record,
52 "path::to::A::r::ChildStruct", "path/to/A/r");
53 I.Children.Functions.emplace_back();
54 I.Children.Functions.back().Name = "OneFunction";
55
56 auto G = getJSONGenerator();
57 assert(G);
58 std::string Buffer;
59 llvm::raw_string_ostream Actual(Buffer);
60 auto Err = G->generateDocForInfo(&I, Actual, getClangDocContext());
61 assert(!Err);
62 std::string Expected = R"raw({
63 "Bases": [
64 {
65 "Access": "public",
66 "End": true,
67 "HasPublicMembers": true,
68 "HasPublicMethods": true,
69 "InfoType": "record",
70 "IsParent": true,
71 "IsTypedef": false,
72 "IsVirtual": true,
73 "MangledName": "",
74 "Name": "F",
75 "Path": "path/to/F",
76 "PublicMembers": [
77 {
78 "IsStatic": false,
79 "Name": "N",
80 "Type": "int"
81 }
82 ],
83 "PublicMethods": [
84 {
85 "InfoType": "function",
86 "IsStatic": false,
87 "Name": "InheritedFunctionOne",
88 "ReturnType": {
89 "IsBuiltIn": false,
90 "IsTemplate": false,
91 "Name": "",
92 "QualName": "",
93 "USR": "0000000000000000000000000000000000000000"
94 },
95 "USR": "0000000000000000000000000000000000000000"
96 }
97 ],
98 "TagType": "struct",
99 "USR": "0000000000000000000000000000000000000000"
100 }
101 ],
102 "Enums": [
103 {
104 "End": true,
105 "InfoType": "enum",
106 "Members": [
107 {
108 "End": true,
109 "Name": "RED",
110 "Value": "0"
111 }
112 ],
113 "Name": "Color",
114 "Scoped": false,
115 "USR": "0000000000000000000000000000000000000000"
116 }
117 ],
118 "HasEnums": true,
119 "HasParents": true,
120 "HasProtectedMembers": true,
121 "HasPublicMethods": true,
122 "HasRecords": true,
123 "HasVirtualParents": true,
124 "InfoType": "record",
125 "IsTypedef": false,
126 "Location": {
127 "Filename": "main.cpp",
128 "LineNumber": 1
129 },
130 "MangledName": "",
131 "Name": "Foo",
132 "Namespace": [
133 "GlobalNamespace"
134 ],
135 "Parents": [
136 {
137 "End": true,
138 "Name": "F",
139 "QualName": "",
140 "USR": "0000000000000000000000000000000000000000"
141 }
142 ],
143 "Path": "GlobalNamespace",
144 "ProtectedMembers": [
145 {
146 "IsStatic": false,
147 "Name": "X",
148 "Type": "int"
149 }
150 ],
151 "PublicMethods": [
152 {
153 "InfoType": "function",
154 "IsStatic": false,
155 "Name": "OneFunction",
156 "ReturnType": {
157 "IsBuiltIn": false,
158 "IsTemplate": false,
159 "Name": "",
160 "QualName": "",
161 "USR": "0000000000000000000000000000000000000000"
162 },
163 "USR": "0000000000000000000000000000000000000000"
164 }
165 ],
166 "Records": [
167 {
168 "End": true,
169 "Name": "ChildStruct",
170 "Path": "path/to/A/r",
171 "QualName": "path::to::A::r::ChildStruct",
172 "USR": "0000000000000000000000000000000000000000"
173 }
174 ],
175 "TagType": "class",
176 "Template": {
177 "Parameters": [
178 {
179 "End": true,
180 "Param": "class T"
181 }
182 ]
183 },
184 "USR": "0000000000000000000000000000000000000000",
185 "VirtualParents": [
186 {
187 "End": true,
188 "Name": "G",
189 "Path": "path/to/G",
190 "QualName": "path::to::G::G",
191 "USR": "0000000000000000000000000000000000000000"
192 }
193 ]
194 })raw";
195 EXPECT_EQ(Expected, Actual.str());
196 }
197
198 TEST_F(JSONGeneratorTest, emitNamespaceJSON) {
199 NamespaceInfo I;
200 I.Name = "Namespace";
201 I.Path = "path/to/A";
202 I.Namespace.emplace_back(EmptySID, "A", InfoType::IT_namespace);
203
204 I.Children.Namespaces.emplace_back(
205 EmptySID, "ChildNamespace", InfoType::IT_namespace,
206 "path::to::A::Namespace::ChildNamespace", "path/to/A/Namespace");
207 I.Children.Records.emplace_back(EmptySID, "ChildStruct", InfoType::IT_record,
208 "path::to::A::Namespace::ChildStruct",
209 "path/to/A/Namespace");
210 I.Children.Functions.emplace_back();
211 I.Children.Functions.back().Name = "OneFunction";
212 I.Children.Functions.back().Access = AccessSpecifier::AS_none;
213 I.Children.Enums.emplace_back();
214 I.Children.Enums.back().Name = "OneEnum";
215
216 auto G = getJSONGenerator();
217 assert(G);
218 std::string Buffer;
219 llvm::raw_string_ostream Actual(Buffer);
220 auto Err = G->generateDocForInfo(&I, Actual, getClangDocContext());
221 assert(!Err);
222 std::string Expected = R"raw({
223 "Enums": [
224 {
225 "End": true,
226 "InfoType": "enum",
227 "Name": "OneEnum",
228 "Scoped": false,
229 "USR": "0000000000000000000000000000000000000000"
230 }
231 ],
232 "Functions": [
233 {
234 "End": true,
235 "InfoType": "function",
236 "IsStatic": false,
237 "Name": "OneFunction",
238 "ReturnType": {
239 "IsBuiltIn": false,
240 "IsTemplate": false,
241 "Name": "",
242 "QualName": "",
243 "USR": "0000000000000000000000000000000000000000"
244 },
245 "USR": "0000000000000000000000000000000000000000"
246 }
247 ],
248 "HasEnums": true,
249 "HasFunctions": true,
250 "HasNamespaces": true,
251 "HasRecords": true,
252 "InfoType": "namespace",
253 "Name": "Global Namespace",
254 "Namespace": [
255 "A"
256 ],
257 "Namespaces": [
258 {
259 "End": true,
260 "Name": "ChildNamespace",
261 "Path": "path/to/A/Namespace",
262 "QualName": "path::to::A::Namespace::ChildNamespace",
263 "USR": "0000000000000000000000000000000000000000"
264 }
265 ],
266 "Path": "path/to/A",
267 "Records": [
268 {
269 "End": true,
270 "Name": "ChildStruct",
271 "Path": "path/to/A/Namespace",
272 "QualName": "path::to::A::Namespace::ChildStruct",
273 "USR": "0000000000000000000000000000000000000000"
274 }
275 ],
276 "USR": "0000000000000000000000000000000000000000"
277 })raw";
278 EXPECT_EQ(Expected, Actual.str());
279 }
280 } // namespace doc
281 } // namespace clang