diff options
Diffstat (limited to 'unittests/Tooling/ToolingTest.cpp')
-rw-r--r-- | unittests/Tooling/ToolingTest.cpp | 84 |
1 files changed, 84 insertions, 0 deletions
diff --git a/unittests/Tooling/ToolingTest.cpp b/unittests/Tooling/ToolingTest.cpp index da89c0ba10..6e5bc6b613 100644 --- a/unittests/Tooling/ToolingTest.cpp +++ b/unittests/Tooling/ToolingTest.cpp @@ -7,6 +7,7 @@ // //===----------------------------------------------------------------------===// +#include "llvm/ADT/Twine.h" #include "clang/AST/ASTConsumer.h" #include "clang/AST/DeclCXX.h" #include "clang/AST/DeclGroup.h" @@ -86,6 +87,89 @@ TEST(RunSyntaxOnlyToolOnCode, FindsClassDecl) { EXPECT_FALSE(FoundClassDeclX); } +TEST(FindCompileArgsInJsonDatabase, FindsNothingIfEmpty) { + std::string ErrorMessage; + CompileCommand NotFound = FindCompileArgsInJsonDatabase( + "a-file.cpp", "", ErrorMessage); + EXPECT_TRUE(NotFound.CommandLine.empty()) << ErrorMessage; + EXPECT_TRUE(NotFound.Directory.empty()) << ErrorMessage; +} + +TEST(FindCompileArgsInJsonDatabase, ReadsSingleEntry) { + llvm::StringRef Directory("/some/directory"); + llvm::StringRef FileName("/path/to/a-file.cpp"); + llvm::StringRef Command("/path/to/compiler and some arguments"); + std::string ErrorMessage; + CompileCommand FoundCommand = FindCompileArgsInJsonDatabase( + FileName, + (llvm::Twine("[{\"directory\":\"") + Directory + "\"," + + "\"command\":\"" + Command + "\"," + "\"file\":\"" + FileName + "\"}]").str(), ErrorMessage); + EXPECT_EQ(Directory, FoundCommand.Directory) << ErrorMessage; + ASSERT_EQ(4u, FoundCommand.CommandLine.size()) << ErrorMessage; + EXPECT_EQ("/path/to/compiler", FoundCommand.CommandLine[0]) << ErrorMessage; + EXPECT_EQ("and", FoundCommand.CommandLine[1]) << ErrorMessage; + EXPECT_EQ("some", FoundCommand.CommandLine[2]) << ErrorMessage; + EXPECT_EQ("arguments", FoundCommand.CommandLine[3]) << ErrorMessage; + + CompileCommand NotFound = FindCompileArgsInJsonDatabase( + "a-file.cpp", + (llvm::Twine("[{\"directory\":\"") + Directory + "\"," + + "\"command\":\"" + Command + "\"," + "\"file\":\"" + FileName + "\"}]").str(), ErrorMessage); + EXPECT_TRUE(NotFound.Directory.empty()) << ErrorMessage; + EXPECT_TRUE(NotFound.CommandLine.empty()) << ErrorMessage; +} + +TEST(FindCompileArgsInJsonDatabase, ReadsCompileCommandLinesWithSpaces) { + llvm::StringRef Directory("/some/directory"); + llvm::StringRef FileName("/path/to/a-file.cpp"); + llvm::StringRef Command("\\\"/path to compiler\\\" \\\"and an argument\\\""); + std::string ErrorMessage; + CompileCommand FoundCommand = FindCompileArgsInJsonDatabase( + FileName, + (llvm::Twine("[{\"directory\":\"") + Directory + "\"," + + "\"command\":\"" + Command + "\"," + "\"file\":\"" + FileName + "\"}]").str(), ErrorMessage); + ASSERT_EQ(2u, FoundCommand.CommandLine.size()); + EXPECT_EQ("/path to compiler", FoundCommand.CommandLine[0]) << ErrorMessage; + EXPECT_EQ("and an argument", FoundCommand.CommandLine[1]) << ErrorMessage; +} + +TEST(FindCompileArgsInJsonDatabase, ReadsDirectoryWithSpaces) { + llvm::StringRef Directory("/some directory / with spaces"); + llvm::StringRef FileName("/path/to/a-file.cpp"); + llvm::StringRef Command("a command"); + std::string ErrorMessage; + CompileCommand FoundCommand = FindCompileArgsInJsonDatabase( + FileName, + (llvm::Twine("[{\"directory\":\"") + Directory + "\"," + + "\"command\":\"" + Command + "\"," + "\"file\":\"" + FileName + "\"}]").str(), ErrorMessage); + EXPECT_EQ(Directory, FoundCommand.Directory) << ErrorMessage; +} + +TEST(FindCompileArgsInJsonDatabase, FindsEntry) { + llvm::StringRef Directory("directory"); + llvm::StringRef FileName("file"); + llvm::StringRef Command("command"); + std::string JsonDatabase = "["; + for (int I = 0; I < 10; ++I) { + if (I > 0) JsonDatabase += ","; + JsonDatabase += (llvm::Twine( + "{\"directory\":\"") + Directory + llvm::Twine(I) + "\"," + + "\"command\":\"" + Command + llvm::Twine(I) + "\"," + "\"file\":\"" + FileName + llvm::Twine(I) + "\"}").str(); + } + JsonDatabase += "]"; + std::string ErrorMessage; + CompileCommand FoundCommand = FindCompileArgsInJsonDatabase( + "file4", JsonDatabase, ErrorMessage); + EXPECT_EQ("directory4", FoundCommand.Directory) << ErrorMessage; + ASSERT_EQ(1u, FoundCommand.CommandLine.size()) << ErrorMessage; + EXPECT_EQ("command4", FoundCommand.CommandLine[0]) << ErrorMessage; +} + } // end namespace tooling } // end namespace clang |