Skip to content

Commit 32238aa

Browse files
committed
Add testcases
1 parent 9ee5bf8 commit 32238aa

File tree

1 file changed

+188
-0
lines changed

1 file changed

+188
-0
lines changed

clang/unittests/Tooling/ToolingTest.cpp

Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "clang/Testing/CommandLineArgs.h"
2121
#include "clang/Tooling/ArgumentsAdjusters.h"
2222
#include "clang/Tooling/CompilationDatabase.h"
23+
#include "clang/Tooling/JSONCompilationDatabase.h"
2324
#include "llvm/ADT/STLExtras.h"
2425
#include "llvm/ADT/StringRef.h"
2526
#include "llvm/Support/Path.h"
@@ -1034,5 +1035,192 @@ TEST(runToolOnCode, TestResetDiagnostics) {
10341035
"void func() { long x; Foo f(x); }"));
10351036
}
10361037

1038+
TEST(ClangToolTest, ProgressReportSingleFile) {
1039+
SmallString<32> BaseDir;
1040+
llvm::sys::path::system_temp_directory(false, BaseDir);
1041+
llvm::sys::path::native(BaseDir, llvm::sys::path::Style::posix);
1042+
std::string BaseDirStr(BaseDir);
1043+
1044+
std::string File = BaseDirStr + "/test.cpp";
1045+
1046+
std::string ErrorMessage;
1047+
std::string JSONDatabase = R"json([
1048+
{
1049+
"directory": "%DIR%",
1050+
"command": "clang++ -c test.cpp",
1051+
"file": "test.cpp"
1052+
}])json";
1053+
1054+
{
1055+
size_t Pos = JSONDatabase.find("%DIR%");
1056+
ASSERT_NE(Pos, std::string::npos);
1057+
JSONDatabase.replace(Pos, 5, BaseDirStr);
1058+
}
1059+
1060+
std::unique_ptr<CompilationDatabase> Database(
1061+
JSONCompilationDatabase::loadFromBuffer(JSONDatabase, ErrorMessage,
1062+
JSONCommandLineSyntax::Gnu));
1063+
ASSERT_TRUE(Database);
1064+
1065+
ClangTool Tool(*Database, {File});
1066+
Tool.mapVirtualFile(File, "int x;");
1067+
1068+
testing::internal::CaptureStderr();
1069+
Tool.run(newFrontendActionFactory<SyntaxOnlyAction>().get());
1070+
std::string Output = testing::internal::GetCapturedStderr();
1071+
1072+
EXPECT_TRUE(Output.empty());
1073+
}
1074+
1075+
TEST(ClangToolTest, ProgressReportMultipleFiles) {
1076+
SmallString<32> BaseDir;
1077+
llvm::sys::path::system_temp_directory(false, BaseDir);
1078+
llvm::sys::path::native(BaseDir, llvm::sys::path::Style::posix);
1079+
std::string BaseDirStr(BaseDir);
1080+
1081+
std::string File1 = BaseDirStr + "/test1.cpp";
1082+
std::string File2 = BaseDirStr + "/test2.cpp";
1083+
1084+
std::string ErrorMessage;
1085+
std::string JSONDatabase = R"json([
1086+
{
1087+
"directory": "%DIR%",
1088+
"command": "clang++ -c test1.cpp",
1089+
"file": "test1.cpp"
1090+
},
1091+
{
1092+
"directory": "%DIR%",
1093+
"command": "clang++ -c test2.cpp",
1094+
"file": "test2.cpp"
1095+
}])json";
1096+
1097+
for (size_t Pos = 0;
1098+
(Pos = JSONDatabase.find("%DIR%", Pos)) != std::string::npos;) {
1099+
JSONDatabase.replace(Pos, 5, BaseDirStr);
1100+
Pos += BaseDirStr.size();
1101+
}
1102+
1103+
std::unique_ptr<CompilationDatabase> Database(
1104+
JSONCompilationDatabase::loadFromBuffer(JSONDatabase, ErrorMessage,
1105+
JSONCommandLineSyntax::Gnu));
1106+
ASSERT_TRUE(Database);
1107+
1108+
ClangTool Tool(*Database, {File1, File2});
1109+
Tool.mapVirtualFile(File1, "int x;");
1110+
Tool.mapVirtualFile(File2, "int y;");
1111+
1112+
testing::internal::CaptureStderr();
1113+
Tool.run(newFrontendActionFactory<SyntaxOnlyAction>().get());
1114+
std::string Output = testing::internal::GetCapturedStderr();
1115+
1116+
std::string Expected = "[1/2] Processing file " + File1 + ".\n" +
1117+
"[2/2] Processing file " + File2 + ".\n";
1118+
EXPECT_EQ(Output, Expected);
1119+
}
1120+
1121+
TEST(ClangToolTest, ProgressReportMultipleCommands) {
1122+
SmallString<32> BaseDir;
1123+
llvm::sys::path::system_temp_directory(false, BaseDir);
1124+
llvm::sys::path::native(BaseDir, llvm::sys::path::Style::posix);
1125+
std::string BaseDirStr(BaseDir);
1126+
1127+
std::string File = BaseDirStr + "/test.cpp";
1128+
1129+
std::string ErrorMessage;
1130+
std::string JSONDatabase = R"json([
1131+
{
1132+
"directory": "%DIR%",
1133+
"command": "clang++ -c test.cpp -DCMD1",
1134+
"file": "test.cpp"
1135+
},
1136+
{
1137+
"directory": "%DIR%",
1138+
"command": "clang++ -c test.cpp -DCMD2",
1139+
"file": "test.cpp"
1140+
}])json";
1141+
1142+
for (size_t Pos = 0;
1143+
(Pos = JSONDatabase.find("%DIR%", Pos)) != std::string::npos;) {
1144+
JSONDatabase.replace(Pos, 5, BaseDirStr);
1145+
Pos += BaseDirStr.size();
1146+
}
1147+
1148+
std::unique_ptr<CompilationDatabase> Database(
1149+
JSONCompilationDatabase::loadFromBuffer(JSONDatabase, ErrorMessage,
1150+
JSONCommandLineSyntax::Gnu));
1151+
ASSERT_TRUE(Database);
1152+
1153+
ClangTool Tool(*Database, {File});
1154+
Tool.mapVirtualFile(File, "int x;");
1155+
1156+
testing::internal::CaptureStderr();
1157+
Tool.run(newFrontendActionFactory<SyntaxOnlyAction>().get());
1158+
std::string Output = testing::internal::GetCapturedStderr();
1159+
1160+
std::string Expected = "[1/1] (1/2) Processing file " + File + ".\n" +
1161+
"[1/1] (2/2) Processing file " + File + ".\n";
1162+
EXPECT_EQ(Output, Expected);
1163+
}
1164+
1165+
TEST(ClangToolTest, ProgressReportMixed) {
1166+
SmallString<32> BaseDir;
1167+
llvm::sys::path::system_temp_directory(false, BaseDir);
1168+
llvm::sys::path::native(BaseDir, llvm::sys::path::Style::posix);
1169+
std::string BaseDirStr(BaseDir);
1170+
1171+
std::string File1 = BaseDirStr + "/test1.cpp";
1172+
std::string File2 = BaseDirStr + "/test2.cpp";
1173+
std::string File3 = BaseDirStr + "/test3.cpp";
1174+
1175+
std::string ErrorMessage;
1176+
std::string JSONDatabase = R"json([
1177+
{
1178+
"directory": "%DIR%",
1179+
"command": "clang++ -c test1.cpp",
1180+
"file": "test1.cpp"
1181+
},
1182+
{
1183+
"directory": "%DIR%",
1184+
"command": "clang++ -c test2.cpp -DCMD1",
1185+
"file": "test2.cpp"
1186+
},
1187+
{
1188+
"directory": "%DIR%",
1189+
"command": "clang++ -c test2.cpp -DCMD2",
1190+
"file": "test2.cpp"
1191+
},
1192+
{
1193+
"directory": "%DIR%",
1194+
"command": "clang++ -c test3.cpp",
1195+
"file": "test3.cpp"
1196+
}])json";
1197+
1198+
for (size_t Pos = 0;
1199+
(Pos = JSONDatabase.find("%DIR%", Pos)) != std::string::npos;) {
1200+
JSONDatabase.replace(Pos, 5, BaseDirStr);
1201+
Pos += BaseDirStr.size();
1202+
}
1203+
1204+
std::unique_ptr<CompilationDatabase> Database(
1205+
JSONCompilationDatabase::loadFromBuffer(JSONDatabase, ErrorMessage,
1206+
JSONCommandLineSyntax::Gnu));
1207+
ASSERT_TRUE(Database);
1208+
1209+
ClangTool Tool(*Database, {File1, File2, File3});
1210+
Tool.mapVirtualFile(File1, "int x;");
1211+
Tool.mapVirtualFile(File2, "int y;");
1212+
Tool.mapVirtualFile(File3, "int z;");
1213+
1214+
testing::internal::CaptureStderr();
1215+
Tool.run(newFrontendActionFactory<SyntaxOnlyAction>().get());
1216+
std::string Output = testing::internal::GetCapturedStderr();
1217+
1218+
std::string Expected = "[1/3] Processing file " + File1 + ".\n" +
1219+
"[2/3] (1/2) Processing file " + File2 + ".\n" +
1220+
"[2/3] (2/2) Processing file " + File2 + ".\n" +
1221+
"[3/3] Processing file " + File3 + ".\n";
1222+
EXPECT_EQ(Output, Expected);
1223+
}
1224+
10371225
} // end namespace tooling
10381226
} // end namespace clang

0 commit comments

Comments
 (0)