1#include "ClangDocTest.h"
2#include "Generators.h"
3#include "Representation.h"
4#include "gtest/gtest.h"
5
6namespace clang {
7namespace doc {
8
9static std::unique_ptr<Generator> getJSONGenerator() {
10 auto G = doc::findGeneratorByName(Format: "json");
11 if (!G)
12 return nullptr;
13 return std::move(G.get());
14}
15
16TEST(JSONGeneratorTest, emitRecordJSON) {
17 RecordInfo I;
18 I.Name = "Foo";
19 // FIXME: FullName is not emitted correctly.
20 I.FullName = "";
21 I.IsTypeDef = false;
22 I.Namespace.emplace_back(Args: EmptySID, Args: "GlobalNamespace", Args: 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(args: "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(Args: TypeInfo("int"), Args: "X", Args: AccessSpecifier::AS_protected);
38
39 I.Bases.emplace_back(args: EmptySID, args: "F", args: "path/to/F", args: true,
40 args: AccessSpecifier::AS_public, args: 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(Args: TypeInfo("int"), Args: "N",
44 Args: AccessSpecifier::AS_public);
45
46 // F is in the global namespace
47 I.Parents.emplace_back(Args: EmptySID, Args: "F", Args: InfoType::IT_record, Args: "");
48 I.VirtualParents.emplace_back(Args: EmptySID, Args: "G", Args: InfoType::IT_record,
49 Args: "path::to::G::G", Args: "path/to/G");
50
51 I.Children.Records.emplace_back(args: EmptySID, args: "ChildStruct", args: InfoType::IT_record,
52 args: "path::to::A::r::ChildStruct", args: "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: &I, OS&: Actual, CDCtx: ClangDocContext());
61 assert(!Err);
62 std::string Expected = R"raw({
63 "Bases": [
64 {
65 "Access": "public",
66 "FullName": "",
67 "IsParent": true,
68 "IsTypedef": false,
69 "IsVirtual": true,
70 "MangledName": "",
71 "Name": "F",
72 "Path": "path/to/F",
73 "PublicFunctions": [
74 {
75 "IsStatic": false,
76 "Name": "InheritedFunctionOne",
77 "ReturnType": {
78 "IsBuiltIn": false,
79 "IsTemplate": false,
80 "Name": "",
81 "QualName": "",
82 "USR": "0000000000000000000000000000000000000000"
83 },
84 "USR": "0000000000000000000000000000000000000000"
85 }
86 ],
87 "PublicMembers": [
88 {
89 "Name": "N",
90 "Type": "int"
91 }
92 ],
93 "TagType": "struct",
94 "USR": "0000000000000000000000000000000000000000"
95 }
96 ],
97 "Enums": [
98 {
99 "Members": [
100 {
101 "Name": "RED",
102 "Value": "0"
103 }
104 ],
105 "Name": "Color",
106 "Scoped": false,
107 "USR": "0000000000000000000000000000000000000000"
108 }
109 ],
110 "FullName": "",
111 "IsTypedef": false,
112 "Location": {
113 "Filename": "main.cpp",
114 "LineNumber": 1
115 },
116 "MangledName": "",
117 "Name": "Foo",
118 "Namespace": [
119 "GlobalNamespace"
120 ],
121 "Parents": [
122 {
123 "Name": "F",
124 "Path": "",
125 "QualName": "",
126 "USR": "0000000000000000000000000000000000000000"
127 }
128 ],
129 "Path": "GlobalNamespace",
130 "ProtectedMembers": [
131 {
132 "Name": "X",
133 "Type": "int"
134 }
135 ],
136 "PublicFunctions": [
137 {
138 "IsStatic": false,
139 "Name": "OneFunction",
140 "ReturnType": {
141 "IsBuiltIn": false,
142 "IsTemplate": false,
143 "Name": "",
144 "QualName": "",
145 "USR": "0000000000000000000000000000000000000000"
146 },
147 "USR": "0000000000000000000000000000000000000000"
148 }
149 ],
150 "Records": [
151 {
152 "Name": "ChildStruct",
153 "Path": "path/to/A/r",
154 "QualName": "path::to::A::r::ChildStruct",
155 "USR": "0000000000000000000000000000000000000000"
156 }
157 ],
158 "TagType": "class",
159 "Template": {
160 "Parameters": [
161 "class T"
162 ]
163 },
164 "USR": "0000000000000000000000000000000000000000",
165 "VirtualParents": [
166 {
167 "Name": "G",
168 "Path": "path/to/G",
169 "QualName": "path::to::G::G",
170 "USR": "0000000000000000000000000000000000000000"
171 }
172 ]
173})raw";
174 EXPECT_EQ(Expected, Actual.str());
175}
176
177TEST(JSONGeneratorTest, emitNamespaceJSON) {
178 NamespaceInfo I;
179 I.Name = "Namespace";
180 I.Path = "path/to/A";
181 I.Namespace.emplace_back(Args: EmptySID, Args: "A", Args: InfoType::IT_namespace);
182
183 I.Children.Namespaces.emplace_back(
184 args: EmptySID, args: "ChildNamespace", args: InfoType::IT_namespace,
185 args: "path::to::A::Namespace::ChildNamespace", args: "path/to/A/Namespace");
186 I.Children.Records.emplace_back(args: EmptySID, args: "ChildStruct", args: InfoType::IT_record,
187 args: "path::to::A::Namespace::ChildStruct",
188 args: "path/to/A/Namespace");
189 I.Children.Functions.emplace_back();
190 I.Children.Functions.back().Name = "OneFunction";
191 I.Children.Functions.back().Access = AccessSpecifier::AS_none;
192 I.Children.Enums.emplace_back();
193 I.Children.Enums.back().Name = "OneEnum";
194
195 auto G = getJSONGenerator();
196 assert(G);
197 std::string Buffer;
198 llvm::raw_string_ostream Actual(Buffer);
199 auto Err = G->generateDocForInfo(I: &I, OS&: Actual, CDCtx: ClangDocContext());
200 assert(!Err);
201 std::string Expected = R"raw({
202 "Enums": [
203 {
204 "Name": "OneEnum",
205 "Scoped": false,
206 "USR": "0000000000000000000000000000000000000000"
207 }
208 ],
209 "Functions": [
210 {
211 "IsStatic": false,
212 "Name": "OneFunction",
213 "ReturnType": {
214 "IsBuiltIn": false,
215 "IsTemplate": false,
216 "Name": "",
217 "QualName": "",
218 "USR": "0000000000000000000000000000000000000000"
219 },
220 "USR": "0000000000000000000000000000000000000000"
221 }
222 ],
223 "Name": "Namespace",
224 "Namespace": [
225 "A"
226 ],
227 "Namespaces": [
228 {
229 "Name": "ChildNamespace",
230 "Path": "path/to/A/Namespace",
231 "QualName": "path::to::A::Namespace::ChildNamespace",
232 "USR": "0000000000000000000000000000000000000000"
233 }
234 ],
235 "Path": "path/to/A",
236 "Records": [
237 {
238 "Name": "ChildStruct",
239 "Path": "path/to/A/Namespace",
240 "QualName": "path::to::A::Namespace::ChildStruct",
241 "USR": "0000000000000000000000000000000000000000"
242 }
243 ],
244 "USR": "0000000000000000000000000000000000000000"
245})raw";
246 EXPECT_EQ(Expected, Actual.str());
247}
248} // namespace doc
249} // namespace clang
250

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