Skip to content

Commit 312ffb4

Browse files
committed
improved testing of --template* options / added TODOs
1 parent 1423b02 commit 312ffb4

2 files changed

Lines changed: 152 additions & 2 deletions

File tree

cli/cmdlineparser.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -881,6 +881,7 @@ bool CmdLineParser::parseFromArgs(int argc, const char* const argv[])
881881
}
882882
}
883883

884+
// TODO: deprecate "--template <template>"
884885
// Output formatter
885886
else if (std::strcmp(argv[i], "--template") == 0 ||
886887
std::strncmp(argv[i], "--template=", 11) == 0) {
@@ -894,6 +895,7 @@ bool CmdLineParser::parseFromArgs(int argc, const char* const argv[])
894895
printError("argument to '--template' is missing.");
895896
return false;
896897
}
898+
// TODO: bail out when no placeholders are found?
897899

898900
if (mSettings->templateFormat == "gcc") {
899901
mSettings->templateFormat = "{bold}{file}:{line}:{column}: {magenta}warning:{default} {message} [{id}]{reset}\\n{code}";
@@ -915,6 +917,7 @@ bool CmdLineParser::parseFromArgs(int argc, const char* const argv[])
915917
}
916918
}
917919

920+
// TODO: deprecate "--template-location <template>"
918921
else if (std::strcmp(argv[i], "--template-location") == 0 ||
919922
std::strncmp(argv[i], "--template-location=", 20) == 0) {
920923
// "--template-location format"
@@ -924,9 +927,10 @@ bool CmdLineParser::parseFromArgs(int argc, const char* const argv[])
924927
++i;
925928
mSettings->templateLocation = argv[i];
926929
} else {
927-
printError("argument to '--template' is missing.");
930+
printError("argument to '--template-location' is missing.");
928931
return false;
929932
}
933+
// TODO: bail out when no placeholders are found?
930934
}
931935

932936
else if (std::strncmp(argv[i], "--template-max-time=", 20) == 0) {

test/testcmdlineparser.cpp

Lines changed: 147 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,16 @@ class TestCmdlineParser : public TestFixture {
160160
TEST_CASE(templatesGcc);
161161
TEST_CASE(templatesVs);
162162
TEST_CASE(templatesEdit);
163+
TEST_CASE(templatesCppcheck1);
164+
TEST_CASE(templatesDaca2);
165+
TEST_CASE(templatesSelfcheck);
166+
TEST_CASE(templatesNoPlaceholder);
167+
TEST_CASE(templateFormatInvalid);
168+
TEST_CASE(templateFormatInvalid2);
169+
TEST_CASE(templateFormatEmpty);
170+
TEST_CASE(templateLocationInvalid);
171+
TEST_CASE(templateLocationInvalid2);
172+
TEST_CASE(templateLocationEmpty);
163173
TEST_CASE(xml);
164174
TEST_CASE(xmlver2);
165175
TEST_CASE(xmlver2both);
@@ -1220,23 +1230,28 @@ class TestCmdlineParser : public TestFixture {
12201230

12211231
void templates() {
12221232
REDIRECT;
1223-
const char * const argv[] = {"cppcheck", "--template", "{file}:{line},{severity},{id},{message}", "file.cpp"};
1233+
const char * const argv[] = {"cppcheck", "--template", "{file}:{line},{severity},{id},{message}", "--template-location={file}:{line}:{column} {info}", "file.cpp"};
12241234
settings.templateFormat.clear();
1235+
settings.templateLocation.clear();
12251236
ASSERT(defParser.parseFromArgs(4, argv));
12261237
ASSERT_EQUALS("{file}:{line},{severity},{id},{message}", settings.templateFormat);
1238+
ASSERT_EQUALS("{file}:{line}:{column} {info}", settings.templateLocation);
12271239
ASSERT_EQUALS("", GET_REDIRECT_OUTPUT);
12281240
}
12291241

12301242
void templatesGcc() {
12311243
REDIRECT;
12321244
const char * const argv[] = {"cppcheck", "--template", "gcc", "file.cpp"};
12331245
settings.templateFormat.clear();
1246+
settings.templateLocation.clear();
12341247
ASSERT(defParser.parseFromArgs(4, argv));
12351248
if (isStdOutATty()) {
12361249
ASSERT_EQUALS("\x1b[1m{file}:{line}:{column}: \x1b[35mwarning:\x1b[39m {message} [{id}]\x1b[0m\n{code}", settings.templateFormat);
1250+
ASSERT_EQUALS("\x1b[1m{file}:{line}:{column}: \x1b[2mnote:\x1b[0m {info}\n{code}", settings.templateLocation);
12371251
}
12381252
else {
12391253
ASSERT_EQUALS("{file}:{line}:{column}: warning: {message} [{id}]\n{code}", settings.templateFormat);
1254+
ASSERT_EQUALS("{file}:{line}:{column}: note: {info}\n{code}", settings.templateLocation);
12401255
}
12411256
ASSERT_EQUALS("", GET_REDIRECT_OUTPUT);
12421257
}
@@ -1245,17 +1260,148 @@ class TestCmdlineParser : public TestFixture {
12451260
REDIRECT;
12461261
const char * const argv[] = {"cppcheck", "--template", "vs", "file.cpp"};
12471262
settings.templateFormat.clear();
1263+
settings.templateLocation.clear();
12481264
ASSERT(defParser.parseFromArgs(4, argv));
12491265
ASSERT_EQUALS("{file}({line}): {severity}: {message}", settings.templateFormat);
1266+
ASSERT_EQUALS("", settings.templateLocation);
12501267
ASSERT_EQUALS("", GET_REDIRECT_OUTPUT);
12511268
}
12521269

12531270
void templatesEdit() {
12541271
REDIRECT;
12551272
const char * const argv[] = {"cppcheck", "--template", "edit", "file.cpp"};
12561273
settings.templateFormat.clear();
1274+
settings.templateLocation.clear();
12571275
ASSERT(defParser.parseFromArgs(4, argv));
12581276
ASSERT_EQUALS("{file} +{line}: {severity}: {message}", settings.templateFormat);
1277+
ASSERT_EQUALS("", settings.templateLocation);
1278+
ASSERT_EQUALS("", GET_REDIRECT_OUTPUT);
1279+
}
1280+
1281+
void templatesCppcheck1() {
1282+
REDIRECT;
1283+
const char * const argv[] = {"cppcheck", "--template=cppcheck1", "file.cpp"};
1284+
settings.templateFormat.clear();
1285+
settings.templateLocation.clear();
1286+
ASSERT(defParser.parseFromArgs(3, argv));
1287+
ASSERT_EQUALS("{callstack}: ({severity}{inconclusive:, inconclusive}) {message}", settings.templateFormat);
1288+
ASSERT_EQUALS("", settings.templateLocation);
1289+
ASSERT_EQUALS("", GET_REDIRECT_OUTPUT);
1290+
}
1291+
1292+
void templatesDaca2() {
1293+
REDIRECT;
1294+
const char * const argv[] = {"cppcheck", "--template=daca2", "file.cpp"};
1295+
settings.templateFormat.clear();
1296+
settings.templateLocation.clear();
1297+
ASSERT(defParser.parseFromArgs(3, argv));
1298+
ASSERT_EQUALS("{file}:{line}:{column}: {severity}:{inconclusive:inconclusive:} {message} [{id}]", settings.templateFormat);
1299+
ASSERT_EQUALS("{file}:{line}:{column}: note: {info}", settings.templateLocation);
1300+
ASSERT_EQUALS(true, settings.daca);
1301+
ASSERT_EQUALS("", GET_REDIRECT_OUTPUT);
1302+
}
1303+
1304+
void templatesSelfcheck() {
1305+
REDIRECT;
1306+
const char * const argv[] = {"cppcheck", "--template=selfcheck", "file.cpp"};
1307+
settings.templateFormat.clear();
1308+
settings.templateLocation.clear();
1309+
ASSERT(defParser.parseFromArgs(3, argv));
1310+
ASSERT_EQUALS("{file}:{line}:{column}: {severity}:{inconclusive:inconclusive:} {message} [{id}]\n{code}", settings.templateFormat);
1311+
ASSERT_EQUALS("{file}:{line}:{column}: note: {info}\n{code}", settings.templateLocation);
1312+
ASSERT_EQUALS("", GET_REDIRECT_OUTPUT);
1313+
}
1314+
1315+
// TODO: we should bail out on this
1316+
void templatesNoPlaceholder() {
1317+
REDIRECT;
1318+
const char * const argv[] = {"cppcheck", "--template=selfchek", "file.cpp"};
1319+
settings.templateFormat.clear();
1320+
settings.templateLocation.clear();
1321+
TODO_ASSERT(!defParser.parseFromArgs(3, argv));
1322+
ASSERT_EQUALS("selfchek", settings.templateFormat);
1323+
ASSERT_EQUALS("", settings.templateLocation);
1324+
ASSERT_EQUALS("", GET_REDIRECT_OUTPUT);
1325+
}
1326+
1327+
void templateFormatInvalid() {
1328+
REDIRECT;
1329+
const char* const argv[] = { "cppcheck", "--template", "--template-location={file}", "file.cpp" };
1330+
ASSERT(!defParser.parseFromArgs(4, argv));
1331+
ASSERT_EQUALS("cppcheck: error: argument to '--template' is missing.\n", GET_REDIRECT_OUTPUT);
1332+
}
1333+
1334+
// TODO: will not error out as he next option does not start with a "-"
1335+
void templateFormatInvalid2() {
1336+
REDIRECT;
1337+
settings.templateFormat.clear();
1338+
settings.templateLocation.clear();
1339+
const char* const argv[] = { "cppcheck", "--template", "file.cpp" };
1340+
TODO_ASSERT(!defParser.parseFromArgs(3, argv));
1341+
ASSERT_EQUALS("file.cpp", settings.templateFormat);
1342+
ASSERT_EQUALS("", settings.templateLocation);
1343+
ASSERT_EQUALS("", GET_REDIRECT_OUTPUT);
1344+
}
1345+
1346+
// will use the default
1347+
// TODO: bail out on empty?
1348+
void templateFormatEmpty() {
1349+
REDIRECT;
1350+
settings.templateFormat.clear();
1351+
settings.templateLocation.clear();
1352+
const char* const argv[] = { "cppcheck", "--template=", "file.cpp" };
1353+
ASSERT(defParser.parseFromArgs(3, argv));
1354+
if (isStdOutATty()) {
1355+
ASSERT_EQUALS("\x1b[1m{file}:{line}:{column}: \x1b[31m{inconclusive:\x1b[35m}{severity}:{inconclusive: inconclusive:}\x1b[39m {message} [{id}]\x1b[0m\n{code}", settings.templateFormat);
1356+
ASSERT_EQUALS("\x1b[1m{file}:{line}:{column}: \x1b[2mnote:\x1b[0m {info}\n{code}", settings.templateLocation);
1357+
}
1358+
else {
1359+
ASSERT_EQUALS("{file}:{line}:{column}: {inconclusive:}{severity}:{inconclusive: inconclusive:} {message} [{id}]\n{code}", settings.templateFormat);
1360+
ASSERT_EQUALS("{file}:{line}:{column}: note: {info}\n{code}", settings.templateLocation);
1361+
}
1362+
ASSERT_EQUALS("", GET_REDIRECT_OUTPUT);
1363+
}
1364+
1365+
void templateLocationInvalid() {
1366+
REDIRECT;
1367+
const char* const argv[] = { "cppcheck", "--template-location", "--template={file}", "file.cpp" };
1368+
ASSERT(!defParser.parseFromArgs(4, argv));
1369+
ASSERT_EQUALS("cppcheck: error: argument to '--template-location' is missing.\n", GET_REDIRECT_OUTPUT);
1370+
}
1371+
1372+
// TODO: will not error out as he next option does not start with a "-"
1373+
void templateLocationInvalid2() {
1374+
REDIRECT;
1375+
settings.templateFormat.clear();
1376+
settings.templateLocation.clear();
1377+
const char* const argv[] = { "cppcheck", "--template-location", "file.cpp" };
1378+
TODO_ASSERT(!defParser.parseFromArgs(3, argv));
1379+
if (isStdOutATty()) {
1380+
ASSERT_EQUALS("\x1b[1m{file}:{line}:{column}: \x1b[31m{inconclusive:\x1b[35m}{severity}:{inconclusive: inconclusive:}\x1b[39m {message} [{id}]\x1b[0m\n{code}", settings.templateFormat);
1381+
}
1382+
else {
1383+
ASSERT_EQUALS("{file}:{line}:{column}: {inconclusive:}{severity}:{inconclusive: inconclusive:} {message} [{id}]\n{code}", settings.templateFormat);
1384+
}
1385+
ASSERT_EQUALS("file.cpp", settings.templateLocation);
1386+
ASSERT_EQUALS("", GET_REDIRECT_OUTPUT);
1387+
}
1388+
1389+
// will use the default
1390+
// TODO: bail out on empty?
1391+
void templateLocationEmpty() {
1392+
REDIRECT;
1393+
settings.templateFormat.clear();
1394+
settings.templateLocation.clear();
1395+
const char* const argv[] = { "cppcheck", "--template-location=", "file.cpp" };
1396+
ASSERT(defParser.parseFromArgs(3, argv));
1397+
if (isStdOutATty()) {
1398+
ASSERT_EQUALS("\x1b[1m{file}:{line}:{column}: \x1b[31m{inconclusive:\x1b[35m}{severity}:{inconclusive: inconclusive:}\x1b[39m {message} [{id}]\x1b[0m\n{code}", settings.templateFormat);
1399+
ASSERT_EQUALS("\x1b[1m{file}:{line}:{column}: \x1b[2mnote:\x1b[0m {info}\n{code}", settings.templateLocation);
1400+
}
1401+
else {
1402+
ASSERT_EQUALS("{file}:{line}:{column}: {inconclusive:}{severity}:{inconclusive: inconclusive:} {message} [{id}]\n{code}", settings.templateFormat);
1403+
ASSERT_EQUALS("{file}:{line}:{column}: note: {info}\n{code}", settings.templateLocation);
1404+
}
12591405
ASSERT_EQUALS("", GET_REDIRECT_OUTPUT);
12601406
}
12611407

0 commit comments

Comments
 (0)