12 #include "clang/Tooling/CompilationDatabase.h" 13 #include "llvm/Support/ScopedPrinter.h" 14 #include "gmock/gmock.h" 15 #include "gtest/gtest.h" 18 using ::testing::AllOf;
19 using ::testing::Pair;
20 using ::testing::UnorderedElementsAre;
21 using ::testing::UnorderedElementsAreArray;
27 const char *
YAML = R
"( 37 FileURI: file:///path/foo.h 46 Documentation: 'Foo doc' 63 FileURI: file:///path/bar.h 72 CompletionSnippetSuffix: '-snippet' 79 FileURI: file:///path/foo.cc 102 TEST(SerializationTest, NoCrashOnEmptyYAML) {
106 TEST(SerializationTest, YAMLConversions) {
108 EXPECT_TRUE(
bool(In)) << In.takeError();
111 ASSERT_TRUE(
bool(ParsedYAML)) << ParsedYAML.takeError();
112 ASSERT_TRUE(
bool(ParsedYAML->Symbols));
114 *ParsedYAML->Symbols,
115 UnorderedElementsAre(ID(
"057557CEBF6E6B2D"), ID(
"057557CEBF6E6B2E")));
117 auto Sym1 = *ParsedYAML->Symbols->find(
119 auto Sym2 = *ParsedYAML->Symbols->find(
122 EXPECT_THAT(Sym1,
QName(
"clang::Foo1"));
123 EXPECT_EQ(Sym1.Signature,
"");
124 EXPECT_EQ(Sym1.Documentation,
"Foo doc");
125 EXPECT_EQ(Sym1.ReturnType,
"int");
126 EXPECT_EQ(StringRef(Sym1.CanonicalDeclaration.FileURI),
"file:///path/foo.h");
127 EXPECT_EQ(Sym1.Origin, static_cast<SymbolOrigin>(1 << 7));
128 EXPECT_EQ(static_cast<uint8_t>(Sym1.Flags), 129);
131 EXPECT_THAT(Sym1.IncludeHeaders,
132 UnorderedElementsAre(IncludeHeaderWithRef(
"include1", 7u),
133 IncludeHeaderWithRef(
"include2", 3u)));
135 EXPECT_THAT(Sym2,
QName(
"clang::Foo2"));
136 EXPECT_EQ(Sym2.Signature,
"-sig");
137 EXPECT_EQ(Sym2.ReturnType,
"");
138 EXPECT_EQ(llvm::StringRef(Sym2.CanonicalDeclaration.FileURI),
139 "file:///path/bar.h");
143 ASSERT_TRUE(
bool(ParsedYAML->Refs));
147 ::testing::SizeIs(1))));
148 auto Ref1 = ParsedYAML->Refs->begin()->second.front();
150 EXPECT_EQ(StringRef(Ref1.Location.FileURI),
"file:///path/foo.cc");
154 ASSERT_TRUE(
bool(ParsedYAML->Relations));
156 *ParsedYAML->Relations,
160 std::vector<std::string> YAMLFromSymbols(
const SymbolSlab &Slab) {
161 std::vector<std::string> Result;
162 for (
const auto &Sym : Slab)
163 Result.push_back(
toYAML(Sym));
166 std::vector<std::string> YAMLFromRefs(
const RefSlab &Slab) {
167 std::vector<std::string> Result;
168 for (
const auto &
Refs : Slab)
173 std::vector<std::string> YAMLFromRelations(
const RelationSlab &Slab) {
174 std::vector<std::string> Result;
175 for (
const auto &Rel : Slab)
176 Result.push_back(
toYAML(Rel));
180 TEST(SerializationTest, BinaryConversions) {
182 EXPECT_TRUE(
bool(In)) << In.takeError();
185 IndexFileOut Out(*In);
187 std::string Serialized = llvm::to_string(Out);
190 ASSERT_TRUE(
bool(In2)) << In.takeError();
191 ASSERT_TRUE(In2->Symbols);
192 ASSERT_TRUE(In2->Refs);
193 ASSERT_TRUE(In2->Relations);
196 EXPECT_THAT(YAMLFromSymbols(*In2->Symbols),
197 UnorderedElementsAreArray(YAMLFromSymbols(*In->Symbols)));
198 EXPECT_THAT(YAMLFromRefs(*In2->Refs),
199 UnorderedElementsAreArray(YAMLFromRefs(*In->Refs)));
200 EXPECT_THAT(YAMLFromRelations(*In2->Relations),
201 UnorderedElementsAreArray(YAMLFromRelations(*In->Relations)));
204 TEST(SerializationTest, SrcsTest) {
206 EXPECT_TRUE(
bool(In)) << In.takeError();
208 std::string TestContent(
"TestContent");
209 IncludeGraphNode IGN;
210 IGN.Digest =
digest(TestContent);
211 IGN.DirectIncludes = {
"inc1",
"inc2"};
216 Sources[IGN.URI] = IGN;
218 IndexFileOut Out(*In);
220 Out.Sources = &Sources;
222 std::string Serialized = llvm::to_string(Out);
225 ASSERT_TRUE(
bool(In)) << In.takeError();
226 ASSERT_TRUE(In->Symbols);
227 ASSERT_TRUE(In->Refs);
228 ASSERT_TRUE(In->Sources);
229 ASSERT_TRUE(In->Sources->count(IGN.URI));
231 EXPECT_THAT(YAMLFromSymbols(*In->Symbols),
232 UnorderedElementsAreArray(YAMLFromSymbols(*In->Symbols)));
233 EXPECT_THAT(YAMLFromRefs(*In->Refs),
234 UnorderedElementsAreArray(YAMLFromRefs(*In->Refs)));
235 auto IGNDeserialized = In->Sources->lookup(IGN.URI);
236 EXPECT_EQ(IGNDeserialized.Digest, IGN.Digest);
237 EXPECT_EQ(IGNDeserialized.DirectIncludes, IGN.DirectIncludes);
238 EXPECT_EQ(IGNDeserialized.URI, IGN.URI);
239 EXPECT_EQ(IGNDeserialized.Flags, IGN.Flags);
243 TEST(SerializationTest, CmdlTest) {
245 EXPECT_TRUE(
bool(In)) << In.takeError();
247 tooling::CompileCommand Cmd;
248 Cmd.Directory =
"testdir";
249 Cmd.CommandLine.push_back(
"cmd1");
250 Cmd.CommandLine.push_back(
"cmd2");
251 Cmd.Filename =
"ignored";
252 Cmd.Heuristic =
"ignored";
253 Cmd.Output =
"ignored";
255 IndexFileOut Out(*In);
259 std::string Serialized = llvm::to_string(Out);
262 ASSERT_TRUE(
bool(In)) << In.takeError();
263 ASSERT_TRUE(In->Cmd);
265 const tooling::CompileCommand &SerializedCmd = In->Cmd.getValue();
266 EXPECT_EQ(SerializedCmd.CommandLine, Cmd.CommandLine);
267 EXPECT_EQ(SerializedCmd.Directory, Cmd.Directory);
268 EXPECT_NE(SerializedCmd.Filename, Cmd.Filename);
269 EXPECT_NE(SerializedCmd.Heuristic, Cmd.Heuristic);
270 EXPECT_NE(SerializedCmd.Output, Cmd.Output);
static llvm::Expected< SymbolID > fromStr(llvm::StringRef)
llvm::Expected< IndexFileIn > readIndexFile(llvm::StringRef Data)
std::string toYAML(const Symbol &)
llvm::StringMap< IncludeGraphNode > IncludeGraph
Whether or not this symbol is meant to be used for the code completion.
TEST(BackgroundQueueTest, Priority)
static constexpr llvm::StringLiteral Name
FileDigest digest(llvm::StringRef Content)
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
std::string IncludeHeader
Indicates if the symbol is deprecated.
std::unique_ptr< GlobalCompilationDatabase > Base
std::array< uint8_t, 20 > SymbolID