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